void PolyMesh::updateNormals (SmoothMetric::Enum metric) { vertexNormals.clear(); for (PolyMesh::FaceIter f(this); !f.end(); ++f) updateFaceNormal( *f ); switch (metric) { case SmoothMetric::None: for (PolyMesh::VertIter v(this); !v.end(); ++v) updateVertNormalFlat( *v ); break; case SmoothMetric::All: for (PolyMesh::VertIter v(this); !v.end(); ++v) updateVertNormalSmooth( *v ); break; case SmoothMetric::Face: for (PolyMesh::VertIter v(this); !v.end(); ++v) updateVertNormalGroups( *v ); break; case SmoothMetric::Edge: for (PolyMesh::VertIter v(this); !v.end(); ++v) updateVertNormalEdges( *v ); break; } }
void PolyMesh::updateTangents (TexMesh *texMesh) { PolyMesh::VertIter v; PolyMesh::FaceIter f; PolyMesh::FaceVertIter fv; TexMesh::FaceIter tf; TexMesh::FaceVertIter tfv; //Store tex vertex pointers into poly vertex tags for (f.begin(this), tf.begin(texMesh); !f.end(); ++f, ++tf) for (fv.begin(*f), tfv.begin(*tf); !fv.end(); ++fv, ++tfv) fv->tag.ptr = *tfv; //Calculate tangents vertexTangents.clear(); for (f.begin(this); !f.end() && !tf.end(); ++f, ++tf) updateFaceNormal( *f ); for (v.begin(this); !v.end(); ++v) updateVertTangent( *v, (TexMesh::Vertex*) v->tag.ptr ); }
void cloth::render(Shader* shader) { glPolygonMode(GL_FRONT_AND_BACK, (draw_wire ? GL_LINE : GL_FILL)); // reset normals (which where written to last frame) std::vector<Particle>::iterator particle; for(particle = particles.begin(); particle != particles.end(); particle++) { (*particle).resetNormal(); } updateFaceNormal(); static GLuint vertexArrayObject = 0; static GLuint vertexBuffer = 0; static GLuint texture; static int elementSize; if (vertexArrayObject == 0){ glGenVertexArrays(1, &vertexArrayObject); glBindVertexArray(vertexArrayObject); glGenBuffers(1, &vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); GLuint positionAttributeLocation = glGetAttribLocation(shader->ProgramID, "position"); GLuint uvAttributeLocation = glGetAttribLocation(shader->ProgramID, "uv"); GLuint normalAttributeLocation = glGetAttribLocation(shader->ProgramID, "normal"); glEnableVertexAttribArray(positionAttributeLocation); glEnableVertexAttribArray(uvAttributeLocation); glEnableVertexAttribArray(normalAttributeLocation); glVertexAttribPointer(positionAttributeLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *)0); glVertexAttribPointer(uvAttributeLocation, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *)sizeof(vec3)); glVertexAttribPointer(normalAttributeLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *)(sizeof(vec3)+sizeof(vec2))); std::vector<int> indices; for (int j = 0; j < num_particles_height-1; j++) { int index; if (j > 0) { indices.push_back(j * num_particles_width); // make degenerate } for (int i = 0; i <= num_particles_width-1; i++) { index = j * num_particles_width + i; indices.push_back(index); indices.push_back(index + num_particles_width); } if (j + 1 < num_particles_height-1) { indices.push_back(index + num_particles_width); // make degenerate } } elementSize = indices.size(); GLuint elementArrayBuffer; glGenBuffers(1, &elementArrayBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArrayBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementSize * sizeof(int), &(indices[0]), GL_STATIC_DRAW); texture = loadClothTexture("clothTexture.jpg"); } std::vector<Vertex> vertexData; for(int y=0; y<num_particles_height; y++) { for(int x = 0; x<num_particles_width; x++) { vec2 uv(x/(num_particles_width - 1.0f),y/(num_particles_height-1.0f)); SaveParticleVertexInfo(getParticle(x, y), uv, vertexData); } } glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(Vertex), value_ptr(vertexData[0].position), GL_STREAM_DRAW); computeMatricesFromInputs(); glm::mat4 ModelMatrix = glm::mat4(1.0f); glm::mat4 ProjectionMatrix = getProjectionMatrix(); glm::mat4 ViewMatrix = getViewMatrix(); mat4 mvp = ProjectionMatrix * ViewMatrix * ModelMatrix; mat4 modelView = ViewMatrix * ModelMatrix; glUniformMatrix4fv(glGetUniformLocation(shader->ProgramID, "mvp"),1,false, value_ptr(mvp)); mat3 normalMatrix = inverse(transpose(mat3(modelView))); glUniformMatrix3fv(glGetUniformLocation(shader->ProgramID, "normalMatrix"),1,false, value_ptr(normalMatrix)); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); glUniform1i(glGetUniformLocation(shader->ProgramID, "mainTexture"), 0); glBindVertexArray(vertexArrayObject); glDrawElements(GL_TRIANGLE_STRIP, elementSize, GL_UNSIGNED_INT, 0); }