Open LooksForFuture opened 2 years ago
I find the use of vector
The way the orignal code worked you can do it with this: glBufferData(GL_ARRAY_BUFFER, vertices.size() sizeof(Vertex), NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() sizeof(Vertex), &vertices[0]);
Maybe you could try a single line: glBufferSubData(GL_ARRAY_BUFFER, 0, data.size(), &data[0]);
Three line version: glBufferSubData(GL_ARRAY_BUFFER, 0, vertSize, &(this->vertices)[0]); glBufferSubData(GL_ARRAY_BUFFER, vertSize, vertSize + normSize, &(this->normals)[0]); glBufferSubData(GL_ARRAY_BUFFER, vertSize + normSize, vertSize + normSize + texSize, &(this->texCoords)[0]);
Thank you for your solution, but it still doesn't work.
Aww. - _ Hmmmm.... O_o
Wait! I see a problem! The stride is wrong in the glVertexAttribPointer. The parameter is: (Index, size, type, normalized, stride, data) Originally the stride is sizeof(Vertex) Vertex is a struct that have all of the data member types. You put in a size of vec3 and 2. but a matter of fact, it should be the size of vertice + tex + normal I don't know the full purpose of having a temporary object data here. You can actually do better without. Remember that it goes out of scope and the information could no longer exist. It was stored in Vertex struct. You can use that instead.
Here is my code that worked for me. (Make sure your shader is right too.) glBufferData(GL_ARRAY_BUFFER, vertices.size() sizeof(Vertex), NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() sizeof(Vertex), &vertices[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
// set the vertex attribute pointers
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));
glEnableVertexAttribArray(5);
glVertexAttribIPointer(5, 4, GL_INT, sizeof(Vertex), (void*)offsetof(Vertex, m_BoneIDs));
glEnableVertexAttribArray(6);
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, m_Weights));
glBindVertexArray(0);
If that doesn't fix it. It could be something outside of the code too. Unless you observed it to happen here. Hope that work for you bud!
Thanks. I suddenly deleted the folder of my custom mesh class and I don't have a backup. So I use the premade class for the rest of tutorial. BTW, do you know why texture of the first loaded object is assigned to all?
make sure you set the parameter before render. shader.setInt("Texture", i); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureid); Draw.... glBindTexture(GL_TEXTURE_2D, 0); // good practice!
It render your sample based on what is currently set in the texture slots. Your shader sample2D will read from a slot it's set to use. Hence, shader.setInt("Texture", i);
i wanted to rewrite the mesh class to can change attributes more easily with glBufferSubData and this is the output I get:
But it should be:
I tracked all the variables and found out the vertices data are being passed to shader, but normals and texture coordinations don't.
This is the costructor of my Mesh class:
But when I store all the attributes in a vector of float named data and change "glBufferData(GL_ARRAY_BUFFER, vertSize + normSize + texSize, NULL, GL_STATIC_DRAW);" to "glBufferData(GL_ARRAY_BUFFER, vertSize + normSize + texSize, data.data(), GL_STATIC_DRAW);" and comment glBufferSubData it works.