Add vertex color array

This commit is contained in:
2019-03-18 11:48:11 +01:00
parent 86f339ef56
commit 29defbaba9
10 changed files with 82 additions and 49 deletions

View File

@@ -132,7 +132,7 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
plainNode->setTexture(&t_plain_diff, &t_plain_normal, &t_perlin);
plainNode->setMesh(&plain);
plainNode->position = {0, 0, 0};
plainNode->shinyness = 20;
plainNode->shininess = 20;
plainNode->displacementCoefficient = 40;
rootNode->children.push_back(plainNode);
@@ -142,7 +142,7 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
boxNode->position = {500, 500, 40};
boxNode->referencePoint = {25, 25, 25};
boxNode->scale *= 2;
boxNode->shinyness = 20;
boxNode->shininess = 20;
boxNode->displacementCoefficient = 40;
rootNode->children.push_back(boxNode);
@@ -258,7 +258,7 @@ void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight) {
// update positions of nodes (like the car)
plainNode->uvOffset.x += timeDelta*0.5;
plainNode->uvOffset.y -= timeDelta*0.5;
boxNode->rotation.z += timeDelta;
if (boxNode) boxNode->rotation.z += timeDelta;
lightNode[1]->rotation.z -= timeDelta;
}
@@ -312,9 +312,11 @@ void renderNode(SceneNode* node, Gloom::Shader* parent_shader = default_shader)
glUniformMatrix4fv(s->location("MV") , 1, GL_FALSE, glm::value_ptr(node->MV));
glUniformMatrix4fv(s->location("MVnormal"), 1, GL_FALSE, glm::value_ptr(node->MVnormal));
glUniform2fv(s->location("uvOffset") , 1, glm::value_ptr(node->uvOffset));
glUniform1f( s->location("shinyness"), node->shinyness);
glUniform4fv(s->location("basecolor") , 1, glm::value_ptr(node->basecolor));
glUniform1f( s->location("shininess"), node->shininess);
glUniform1f( s->location("displacementCoefficient"), node->displacementCoefficient);
glUniform1ui(s->location("isTextured"), node->isTextured);
glUniform1ui(s->location("isColorMapped"), node->isColorMapped);
glUniform1ui(s->location("isNormalMapped"), node->isNormalMapped);
glUniform1ui(s->location("isDisplacementMapped"), node->isDisplacementMapped);
glUniform1ui(s->location("isIlluminated"), node->isIlluminated);
@@ -330,7 +332,7 @@ void renderNode(SceneNode* node, Gloom::Shader* parent_shader = default_shader)
case SPOT_LIGHT:
case POINT_LIGHT: {
uint id = node->lightID;
lights[id].position = vec3(node->MV * vec4(1.0));
lights[id].position = vec3(node->MV * vec4(vec3(0.0), 1.0));
lights[id].is_spot = node->nodeType == SPOT_LIGHT;
lights[id].spot_target = node->rotation; // already MV space, todo: change this
lights[id].spot_cuttof_angle = glm::sin(node->spot_cuttof_angle);

View File

@@ -18,6 +18,7 @@
using glm::vec2;
using glm::vec3;
using glm::vec4;
using glm::mat4;
using std::map;
using std::vector;
@@ -34,17 +35,18 @@ struct SceneNode {
nodeType = type;
}
void setMesh(Mesh* mesh) {
static map<Mesh*, int> cache;
void setMesh(const Mesh* mesh) {
static map<const Mesh*, int> cache;
if (cache.find(mesh) == cache.end())
cache[mesh] = generateBuffer(*mesh, isNormalMapped || isDisplacementMapped);
vertexArrayObjectID = cache[mesh];
VAOIndexCount = mesh->indices.size();
isColorMapped = ! mesh->colors.empty();
}
void setTexture(PNGImage* diffuse, PNGImage* normal=nullptr, PNGImage* displacement=nullptr) {
static map<PNGImage*, int> cache;
void setTexture(const PNGImage* diffuse, const PNGImage* normal=nullptr, const PNGImage* displacement=nullptr) {
static map<const PNGImage*, int> cache;
assert(vertexArrayObjectID==-1);
isTextured = false;
isNormalMapped = false;
@@ -96,7 +98,8 @@ struct SceneNode {
uint VAOIndexCount = 0;
// textures
float shinyness = 10.0; // specular power
float shininess = 10.0; // specular power
vec4 basecolor = vec4(1.0);
vec2 uvOffset = vec2(0.0, 0.0); // specular power
uint diffuseTextureID;
uint normalTextureID;
@@ -105,6 +108,7 @@ struct SceneNode {
// shader flags
bool isTextured = false;
bool isColorMapped = false;
bool isNormalMapped = false;
bool isDisplacementMapped = false;
bool isIlluminated = true;

View File

@@ -4,11 +4,12 @@
#include "glutils.h"
using std::vector;
using glm::vec4;
using glm::vec3;
using glm::vec2;
typedef unsigned int uint;
uint generateBuffer(Mesh &mesh, bool isNormalMapped) {
uint generateBuffer(const Mesh &mesh, bool doAddTangents) {
uint vaoID;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
@@ -32,21 +33,31 @@ uint generateBuffer(Mesh &mesh, bool isNormalMapped) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.indices.size() * sizeof(uint), mesh.indices.data(), GL_STATIC_DRAW);
if (mesh.textureCoordinates.empty()) return vaoID;
if (!mesh.textureCoordinates.empty()) {
uint textureBufferID;
glGenBuffers(1, &textureBufferID);
glBindBuffer(GL_ARRAY_BUFFER, textureBufferID);
glBufferData(GL_ARRAY_BUFFER, mesh.textureCoordinates.size() * sizeof(vec2), mesh.textureCoordinates.data(), GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);
glEnableVertexAttribArray(2);
}
if (!mesh.colors.empty()) {
uint colorBufferID;
glGenBuffers(1, &colorBufferID);
glBindBuffer(GL_ARRAY_BUFFER, colorBufferID);
glBufferData(GL_ARRAY_BUFFER, mesh.colors.size() * sizeof(vec4), mesh.colors.data(), GL_STATIC_DRAW);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glEnableVertexAttribArray(3);
}
if (doAddTangents && !mesh.textureCoordinates.empty())
addTangents(vaoID, mesh);
uint textureBufferID;
glGenBuffers(1, &textureBufferID);
glBindBuffer(GL_ARRAY_BUFFER, textureBufferID);
glBufferData(GL_ARRAY_BUFFER, mesh.textureCoordinates.size() * sizeof(vec2), mesh.textureCoordinates.data(), GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);
glEnableVertexAttribArray(2);
if (isNormalMapped) addTangents(vaoID, mesh);
return vaoID;
}
void addTangents(uint vaoID, Mesh& mesh) {
void addTangents(uint vaoID, const Mesh& mesh) {
vector<vec3> tangents(mesh.vertices.size());
vector<vec3> bitangents(mesh.vertices.size());
@@ -95,15 +106,15 @@ void addTangents(uint vaoID, Mesh& mesh) {
glGenBuffers(1, &tangentBufferID);
glBindBuffer(GL_ARRAY_BUFFER, tangentBufferID);
glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(vec3), tangents.data(), GL_STATIC_DRAW);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(3);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(4);
uint bitangentBufferID;
glGenBuffers(1, &bitangentBufferID);
glBindBuffer(GL_ARRAY_BUFFER, bitangentBufferID);
glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(vec3), bitangents.data(), GL_STATIC_DRAW);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(4);
glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(5);
}
uint generateTexture(const PNGImage& texture) {

View File

@@ -4,8 +4,8 @@
#include "imageLoader.hpp"
unsigned int generateBuffer(Mesh &mesh, bool isNormalMapped = false);
unsigned int generateBuffer(const Mesh &mesh, bool doAddTangents=false);
void addTangents(unsigned int vaoID, Mesh& mesh);
void addTangents(unsigned int vaoID, const Mesh& mesh);
unsigned int generateTexture(const PNGImage& texture);

View File

@@ -7,6 +7,7 @@ struct Mesh {
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> normals;
std::vector<glm::vec2> textureCoordinates;
std::vector<glm::vec4> colors;
std::vector<unsigned int> indices;
};