1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-23 02:42:39 +01:00

Normals are now generated on-the-fly if they don't already exist

This commit is contained in:
Timmy Sjöstedt 2013-07-04 11:59:00 +02:00
parent db2ff2a024
commit 47117cbd65
2 changed files with 31 additions and 0 deletions

View File

@ -310,8 +310,12 @@ void GTARenderer::renderObject(GTAEngine* engine, const std::unique_ptr<Model>&
glBindBuffer(GL_ARRAY_BUFFER, model->geometries[g].VBO);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 0, (void*)(model->geometries[g].vertices.size() * sizeof(float) * 3));
glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, 0,
(void *) ((model->geometries[g].vertices.size() * sizeof(float) * 3) + (model->geometries[g].texcoords.size() * sizeof(float) * 2))
);
glEnableVertexAttribArray(posAttrib);
glEnableVertexAttribArray(texAttrib);
glEnableVertexAttribArray(normalAttrib);
for(size_t sg = 0; sg < model->geometries[g].subgeom.size(); ++sg)
{

View File

@ -165,6 +165,33 @@ std::unique_ptr<Model> LoaderDFF::loadFromMemory(char *data)
}
}
// Generate normals if they don't exist already
if (geometryStruct.normals.size() == 0) {
geometryStruct.normals.resize(geometry.numverts);
for (auto &subgeom : geometryStruct.subgeom) {
glm::vec3 normal{0, 0, 0};
for (size_t i = 0; i+2 < subgeom.indices.size(); i += 3) {
glm::vec3 p1 = geometryStruct.vertices[subgeom.indices[i]];
glm::vec3 p2 = geometryStruct.vertices[subgeom.indices[i+1]];
glm::vec3 p3 = geometryStruct.vertices[subgeom.indices[i+2]];
glm::vec3 U = p2 - p1;
glm::vec3 V = p3 - p1;
normal.x = (U.y * V.z) - (U.z * V.y);
normal.y = (U.z * V.x) - (U.x * V.z);
normal.z = (U.x * V.y) - (U.y * V.x);
if (glm::length(normal) > 0.0000001)
normal = glm::normalize(normal);
geometryStruct.normals[subgeom.indices[i]] = normal;
geometryStruct.normals[subgeom.indices[i+1]] = normal;
geometryStruct.normals[subgeom.indices[i+2]] = normal;
}
}
}
// OpenGL buffer stuff
glGenBuffers(1, &geometryStruct.VBO);
glGenBuffers(1, &geometryStruct.EBO);