Jump to content

  • Log In with Google      Sign In   
  • Create Account


Indexed primitive looks wonky with lighting


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 bradhe   Members   -  Reputation: 102

Like
0Likes
Like

Posted 26 August 2011 - 10:48 AM

Hey there, not even 100% sure this is the right place for this question, so please be gentle! My first time posting on these forums.

I've got what I'm sure is a simple problem stemming from some deficiency with regard to my understanding of how crap works in OpenGL.

Basically, all I want is 2 "brick" meshes with lighting. In order to support future growth, I want to display them as indexed primitives and I am attempting to compute normals from the faces. Here is the relevant code -- it basically creates to relavent vertices, indexes, and computes the appropriate normals. I don't include the normals here in hopes that the normals were what is messing up my lighting. Note that I also am shading this model GL_FLAT.



void create_brick() {
  Vector vertices[] = { 
    Vector( 1.0f, 0.25f, 1.0f),
    Vector( 1.0f,-0.25f, 1.0f),
    Vector(-1.0f, 0.25f, 1.0f),
    Vector(-1.0f,-0.25f, 1.0f),
    Vector( 1.0f,-0.25f,-1.0f),
    Vector(-1.0f, 0.25f,-1.0f),
    Vector( 1.0f, 0.25f,-1.0f),
    Vector(-1.0f,-0.25f,-1.0f),
  };  

  GLubyte indices[] = { 
    0, 2, 3, 1,
    0, 1, 4, 6,
    0, 6, 5, 2,
    2, 5, 7, 3,
    7, 4, 1, 3,
    4, 7, 5, 6
  };  

  // This took a suprising amount of energy.
  GLfloat * normals = compute_normals(vertices, 8, indices, 24);
  GLfloat * floatVertices = convert_vector_list(vertices, 8); 

  glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, floatVertices);
    glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, indices);
  glDisableClientState(GL_VERTEX_ARRAY);
  
  // Huh? Normals?
  for(int i = 0; i < 24; i += 3) {
    printf("%d: (%f, %f, %f)\n", i, normals[i], normals[i+1], normals[i+2]);
  }

  // Be nice to yourself!
  free(floatVertices);
  free(normals);
}

void render_geometry() {
  glPushMatrix();
  glLoadIdentity();

  glTranslatef(-2.0f, -1.0f, -5.0f);
  create_brick();

  glLoadIdentity();
  glTranslatef(2.0f, -1.0f, -5.0f);
  create_brick();

  glPopMatrix();
}


Pretty sane, I think. Well, apparently not. Here is what is displayed:

http://i.imgur.com/HgPQx.png


What I expected was 2 rectangular primitives. What I got was something...else. Can anyone offer any guidance as to what I'm doing wrong? I can post my lighting code, if necessary!

Edit: Made code a little more relevant and added a little clarification to setup.

Edited by bradhe, 26 August 2011 - 10:52 AM.


Sponsor:

#2 RobTheBloke   Crossbones+   -  Reputation: 2295

Like
0Likes
Like

Posted 26 August 2011 - 11:07 AM

Looks like you don't have depth testing enabled (GL_DEPTH_TEST)

p.s. Shouldn't you be specifying the normals as well before drawing?


  glEnableClientState(GL_VERTEX_ARRAY);

  glEnableClientState(GL_NORMAL_ARRAY);

    glVertexPointer(3, GL_FLOAT, 0, floatVertices);

    glNormalPointer(GL_FLOAT, 0, normals);

    glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, indices);

  glDisableClientState(GL_VERTEX_ARRAY);

  glDisableClientState(GL_NORMAL_ARRAY);



p.p.s. You can rid of that pointless conversion of Vector3's to floats as well....


    glVertexPointer(3, GL_FLOAT, sizeof(Vector), &vertices[0].x);



p.p.p.s. You should probably calculate the normals beforehand (rather than doing it every single time you draw the mesh).

#3 bradhe   Members   -  Reputation: 102

Like
0Likes
Like

Posted 26 August 2011 - 11:08 AM

Looks like you don't have depth testing enabled (GL_DEPTH_TEST)


Awww god. that was it! Thanks. How did you know that? Just experience, or is there a mathematical explanation?

#4 RobTheBloke   Crossbones+   -  Reputation: 2295

Like
0Likes
Like

Posted 26 August 2011 - 11:14 AM

Awww god. that was it! Thanks. How did you know that? Just experience, or is there a mathematical explanation?


Experience ;)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS