thanks a lot
thanks a lot
Code :glBufferData(GL_ARRAY_BUFFER, sizeof(data), &data, GL_STATIC_DRAW);
This uploads 3 * sizeof(void*), so 24 bytes on a typical 64 bit host, for a usual implementation of std::vector. You want 3 * sizeof(GLfloat) * data.size() and (as already mentioned) data.data() (if using C++11) or &data.front() (for older compilers/standard library implementations).
Great catch with the data length parameter!!!
You might want to use a struct to store your data. It provides a convenient method so you don't need to remember how many float values you have per vertex and it lets you more easily modify arrays of data. For example:
Later on, build your info into an array of vertex and then you can conveniently use:Code :struct vertex { GLfloat position[3]; GLfloat normal[3]; GLfloat texCoord[2]; };
Code :std::vector< vertex > data = // ... glBufferData(GL_ARRAY_BUFFER, data.size()*sizeof(vertex), data.data(), GL_STATIC_DRAW); // sets vao vertex pointers vertex * v = NULL; glVertexAttribPointer( positionAttribLoc, 3, GL_FLOAT, GL_FALSE, sizeof(v), v->position); glVertexAttribPointer( normalAttribLocation, 3, GL_FLOAT, GL_FALSE, sizeof(v), v->normal); glVertexAttribPointer( uvAttribLocation, 2, GL_FLOAT, GL_FALSE, sizeof(v), v->uv);
thanks to all for replys,
after a few days of work i solve my problem and now i can import only the position and the indices but hey, it works.
I want to ask you only one thing about this argument before decleare it solved.
is it better to import the normal from the mesh if there are or is it better to calculate normals every time i load a model?
PS: sorry for my bad english.
Let's first talk about loading vertices from an OBJ file. The wavefront format (.OBJ) does not associate a vertex with a specific normal--it specifies a vertex and normal for each vertex in the face. This means that a single vertex position can be associated with multiple normals over the entire model.
Think of the corners of a cube. A .OBJ file can specify each cube face using a set of 6 normals and 8 vertices, but each face can pick whatever normal is in the direction of the face for a specific vertex of a face. So, the same vertex position could be associated with three different normals, one for each of the three faces that share that vertex.
Lets now talk about calculating normals for faces. You can calculate a normal to the plane each triangle is in but there are disadvantages to this.
1. Computation time for the normals can significantly increase your load time, since calculating the normal will require computing a cross product which is an expensive operation (6 multiplications and three subtractions per normal) following by normalizing the normal (square root).
2. It can make your model look faceted, since in some cases you specifically don't want your normals perpendicular to each triangle. A simple example would be a sphere--you want every normal to point out from the center since geometrically that is what should be true. If you calculated normals for a sphere using each triangle, you sphere will shade in such a way that it looks faceted, a fact you probably want to hide since it makes it look really artificial (perhaps unless you are creating a disco ball).
3. Loading normals from models allows artists to be able to adjust normals in their own way, and they come essentially for free (minus read cost).