Results 1 to 8 of 8

Thread: Apply normals

  1. #1
    Member Contributor
    Join Date
    Apr 2011
    Location
    Barcelona, Catalunya
    Posts
    55

    Apply normals

    Hi, can someone tell me if is this the correct method to enable normal vectors of an object?

    Step 1: Do the arrays of vertex and normals
    Step 2: Create buffers:
    Code :
        GLuint vertexs,normals;
        glGenBuffers(1,&vertexs);
        glBindBuffer(GL_ARRAY_BUFFER,vertexs);
        glBufferData(GL_ARRAY_BUFFER,sizeVertex*sizeof(GLfloat),vertexArray,GL_DYNAMIC_DRAW);
        glVertexPointer(3,GL_FLOAT,3*sizeof(GL_FLOAT),0);
     
        glGenBuffers(1,&normals);
        glBindBuffer(GL_ARRAY_BUFFER,normals);
        glBufferData(GL_ARRAY_BUFFER,sizeNormal*sizeof(GLfloat),normalArray,GL_DYNAMIC_DRAW);
        glNormalPointer(GL_FLOAT,3*sizeof(GL_FLOAT),0);

    Step 3: Draw:
    Code :
            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_NORMAL_ARRAY);
            glDrawArrays(GL_TRIANGLES,0,sizeVertex);
            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_NORMAL_ARRAY);

    EDIT: I put this on the forum because it doesn't work but I cannot find the reason ^^.

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jan 2011
    Location
    Paris, France
    Posts
    281

    Re: Apply normals

    Have test with glEnable(GL_LIGHTING) / glEnable(GL_LIGHT0) and others glLightfv() calls ?
    @+
    Yannoo

  3. #3
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Apply normals

    Your glVertexPointer and glNormalPointer calls are in the wrong place. They should be with the draw code and use a null pointer since you have opted to use buffer objects as the source of the vertex attributes.

  4. #4
    Member Contributor
    Join Date
    Apr 2011
    Location
    Barcelona, Catalunya
    Posts
    55

    Re: Apply normals

    Quote Originally Posted by BionicBytes
    Your glVertexPointer and glNormalPointer calls are in the wrong place. They should be with the draw code and use a null pointer since you have opted to use buffer objects as the source of the vertex attributes.
    You're saying to do this:
    Code :
    glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_NORMAL_ARRAY);
            glVertexPointer(3,GL_FLOAT,3*sizeof(GL_FLOAT),NULL);
            glNormalPointer(GL_FLOAT,3*sizeof(GL_FLOAT),NULL);
            glDrawArrays(GL_TRIANGLES,0,sizeVertex);
            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_NORMAL_ARRAY);

    It doesn't work too. In fact it's worst because the object doesn't apear.

  5. #5
    Senior Member Regular Contributor
    Join Date
    Oct 2009
    Posts
    463

    Re: Apply normals

    You are storing "sizeVertex*sizeof(GLfloat)" bytes in your buffer object for positions + "sizeNormal*sizeof(GLfloat)" bytes in the buffer object for normals, what do sizeVertex + sizeNormal represent?

    You appear to be using sizeVertex for the vertex count in glDrawArrays, in which case you'd probably want to be using sizeVertex*3*sizeof(GLfloat) bytes in both glBufferData calls.

    ps. You are using "sizeof(GL_FLOAT)" elsewhere, which may work but doesn't make much sense, it should be "sizeof(GLfloat)".

  6. #6
    Member Contributor
    Join Date
    Apr 2011
    Location
    Barcelona, Catalunya
    Posts
    55

    Re: Apply normals

    sizeNormal/sizeVertex represent the size of the respective arrays of normals and vertices.
    For each 3 vertices coords there's one normal vector.

  7. #7
    Senior Member Regular Contributor
    Join Date
    Oct 2009
    Posts
    463

    Re: Apply normals

    You need 1 normal for each vertex, so will need to replicate the normal data so you have an equal number of each.

    If sizeVertex is the number of vertices (size of array doesn't really help, vertexArray/normalArray could be an array of floats, or be using an array of some vector type - which will give different lengths), then you will need sizeVertex*3*sizeof(GLfloat) bytes to store the position information in the buffer object.

  8. #8
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Apply normals

    Originally Posted By: BionicBytes
    Your glVertexPointer and glNormalPointer calls are in the wrong place. They should be with the draw code and use a null pointer since you have opted to use buffer objects as the source of the vertex attributes.


    You're saying to do this:
    Code:
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glVertexPointer(3,GL_FLOAT,3*sizeof(GL_FLOAT),NULL );
    glNormalPointer(GL_FLOAT,3*sizeof(GL_FLOAT),NULL);
    glDrawArrays(GL_TRIANGLES,0,sizeVertex);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);


    It doesn't work too. In fact it's worst because the object doesn't apear.
    But you have not bound each buffer object before the vertex attribute pointers.

Similar Threads

  1. Replies: 6
    Last Post: 04-14-2004, 10:07 PM
  2. face normals or vertex normals
    By infinitecmdz in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 02-21-2004, 07:29 AM
  3. vertex normals or face normals
    By infinitecmdz in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-20-2004, 09:35 AM
  4. is it possible to have vertex normals or even face normals using triangle strips nt
    By wildeyedboyfromfreecloud in forum OpenGL: Advanced Coding
    Replies: 63
    Last Post: 03-19-2002, 11:42 PM
  5. Face Normals vs. Vertex Normals
    By detzel in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 03-05-2001, 10:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean