Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Strange problem when drawing wavefront OBJ

  1. #11
    Intern Contributor
    Join Date
    Jul 2014
    Location
    Italy, Lissone
    Posts
    80
    thanks a lot

  2. #12
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    874
    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).

  3. #13
    Intern Newbie
    Join Date
    Apr 2014
    Posts
    47
    Quote Originally Posted by carsten neumann View Post
    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:
    Code :
    struct vertex {
      GLfloat position[3];
      GLfloat normal[3];
      GLfloat texCoord[2];
    };
    Later on, build your info into an array of vertex and then you can conveniently use:
    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);

  4. #14
    Intern Contributor
    Join Date
    Jul 2014
    Location
    Italy, Lissone
    Posts
    80
    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.

  5. #15
    Intern Newbie
    Join Date
    Apr 2014
    Posts
    47
    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •