Results 1 to 3 of 3

Thread: Normals to use for a viewer facing rectangle

  1. #1
    Member Newbie
    Join Date
    Apr 2011

    Normals to use for a viewer facing rectangle

    Hello. I'm attempting to render a very simple normal map scene. I'm making a 2d program for now so every object drawn will always be on a quad facing in the same direction, as I didn't want to get into too complex of 3d to start.
    I would however like to use normal mapping and lighting for some shading effect and am having difficulty understanding how to set up the normals for this.

    Here is the relevant code which works for lighting, but the moving the light around just increases intensity and doesn't change the direction so I'm guessing I have my normals wrong?

    Code :
    GLfloat vertices[] = {1,1,1,  -1,1,1,  -1,-1,1,  1,-1,1};
    GLubyte indices[] = {0,1,2,3};
    GLfloat textureCoords [] = {1.0f,0.0f,  0.0f,0.0f,  0.0f,1.0f,  1.0f,1.0f};
    GLfloat normals[] = {0,0,1,  0,0,1,  0,0,1,  0,0,1};
        glTexCoordPointer(2, GL_FLOAT, 0,textureCoords);
        glTexCoordPointer(2, GL_FLOAT, 0,textureCoords);
        glNormalPointer(GL_FLOAT, 0, normals);
        glVertexPointer(3, GL_FLOAT, 0, vertices);
        glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices);

    The result of the normal map and my texture shows up, so I think I have that correct, just the light direction is always the same. Could someone let me know the correct normals to use on a simple quad that will always be facing the user? Thanks.

  2. #2
    Member Newbie
    Join Date
    Apr 2011

    Re: Normals to use for a viewer facing rectangle

    I think I've made some progress but still not working. I think I've figured out in the normal array the first 2 coordinates are for each vertex and the last is the normal direction, so setting the first ones to each vertex and the last to 1 should be facing the user correct? I've changed it to this:

    GLfloat normals[] = {1,1,1, -1,1,1, -1,-1,1, 1,-1,1};

    Now some part of the image gets shaded light or dark, however the normal map I have still gets rendered as if the light were coming from the same direction no matter where I move the light.

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

    Re: Normals to use for a viewer facing rectangle

    Firstly, using the fixed function pipeline to render with lighting a simple cube is never going to look very good - it's not tesselated anywhere near enough for proper lighting.

    Secondly, each and every vertex should have a normal - usually pointing perpendicular to the plane on which the vertex is part of. All 3 elements of the normal (x,y,z components) define the normal's direction - not just the last one.
    If you imagine a peice of paper on the floor, then at each corner (your vertex) you would have a normal defined as (0,1,0) so that the normal is pointing upward (Y-axis). Tesselate this grid sufficiently and then the lighting will approach something half decent. Repeat this for all 6 faces of your cube, ensuring your normals are poining towards the opposite face of the cube.

Similar Threads

  1. Drawing many user facing objects
    By jas511 in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 09-23-2011, 11:25 AM
  2. Several Rectangle or One rectangle with cuts
    By Mickey PT in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 08-03-2011, 04:39 AM
  3. What will be the facing of co-linear triangle?
    By Samme in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 11-17-2008, 07:53 AM
  4. a disk constantly facing the user
    By jyoung77 in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 04-20-2005, 01:08 PM
  5. Viewer facing particles
    By fortikur in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 08-27-2004, 06:04 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