Skip to main content
GameDev.net gamedev.net
🔒 Locked

Cannot align objects

Started by f1restar Jul 1, 2004 at 4:18 AM 0 replies 400+ views
Original Post
f1restar
f1restar
Hi, I've be learning opengl for a while, so forgive my noobness ;) I'm trying to create a grid of cubes. I've written a function that allows me to position a cube on a specific x,y,z The problem is that the z axis is wrong, some object are more in front then others. I just want a flat grid of cubes, that will allow me to eventually rotate the cubes. This is my cube code:

void glCube(float x, float y, float z, float scale){
  glPushMatrix();
  glTranslatef(x, y, z);              // move 5 units into the screen.  
  // Rotate around the x- and y-axis.
  //glRotatef(angle,s,c,0.0f);
  
  for (int i=0;i<4;i++) {
    // Push the current matrix stack (there is a stack of matrices for each of the matrix modes).
    glPushMatrix();

    // Rotate around the y-axis to create the front, back, left and right faces.
    glRotatef(i*90.0f,0.0f,1.0f,0.0f);
    // Move along the z-axis (towards the camera).
    glTranslatef(0.0f,0.0f,1.0f);

    // Use a named texture.
    glBindTexture(GL_TEXTURE_2D,texture_list);
    // Define the front face.
    glBegin(GL_QUADS);
      glTexCoord2f(1.0f,0.0f);
      glVertex3f(+1.0f,+1.0f,0.0f);  // Upper right vertex.
      glTexCoord2f(0.0f,0.0f);
      glVertex3f(-1.0f,+1.0f,0.0f);  // Upper left vertex.
      glTexCoord2f(0.0f,1.0f);
      glVertex3f(-1.0f,-1.0f,0.0f);  // Lower left vertex.
      glTexCoord2f(1.0f,1.0f);
      glVertex3f(+1.0f,-1.0f,0.0f);  // Lower right vertex.
    glEnd();

    // Pop the current matrix stack.
    glPopMatrix();
  }

  // Push the current matrix stack (there is a stack of matrices for each of the matrix modes).
  glPushMatrix();
  // Rotate around the x-axis to create the bottom face.
  glRotatef(90.0f,1.0f,0.0f,0.0f);
  // Move along the z-axis (towards the camera).
  glTranslatef(0.0f,0.0f,1.0f);
  // Use a named texture.
  glBindTexture(GL_TEXTURE_2D,texture_list[4]);
  // Define the front face.
  glBegin(GL_QUADS);
    glTexCoord2f(1.0f,0.0f);
    glVertex3f(+1.0f,+1.0f,0.0f);  // Upper right vertex.
    glTexCoord2f(0.0f,0.0f);
    glVertex3f(-1.0f,+1.0f,0.0f);  // Upper left vertex.
    glTexCoord2f(0.0f,1.0f);
    glVertex3f(-1.0f,-1.0f,0.0f);  // Lower left vertex.
    glTexCoord2f(1.0f,1.0f);
    glVertex3f(+1.0f,-1.0f,0.0f);  // Lower right vertex.
  glEnd();
  // Pop the current matrix stack.
  glPopMatrix();

  // Rotate around the x-axis to create the top face.
  glRotatef(-90.0f,1.0f,0.0f,0.0f);
  // Move along the z-axis (towards the camera).
  glTranslatef(0.0f,0.0f,1.0f);
  // Use a named texture.
  glBindTexture(GL_TEXTURE_2D,texture_list[5]);
  // Define the front face.
  glBegin(GL_QUADS);
    glTexCoord2f(1.0f,0.0f);
    glVertex3f(+1.0f,+1.0f,0.0f);  // Upper right vertex.
    glTexCoord2f(0.0f,0.0f);
    glVertex3f(-1.0f,+1.0f,0.0f);  // Upper left vertex.
    glTexCoord2f(0.0f,1.0f);
    glVertex3f(-1.0f,-1.0f,0.0f);  // Lower left vertex.
    glTexCoord2f(1.0f,1.0f);
    glVertex3f(+1.0f,-1.0f,0.0f);  // Lower right vertex.
  glEnd();
  glPopMatrix(); 
}
Code to make the grid of cubes:

  float xcount, ycount;
  //glTranslatef(0.0f, 0.0f, ((unsigned int)angle)/100.0f);
  cubecount=0;
  for (xcount=-1*split; xcount<=1*split; xcount+=2){
	for (ycount=-1*split; ycount<=1*split; ycount+=2){
			cubecount++;
			glCube(xcount, ycount, -timer, 1.0f);
			fprintf(stderr, "x: %5f y: %5f z: %5f\n", xcount, ycount, -timer);
	}
  }
Does anybody know whats wrong with my code? Or does anybody have a better way of doing this? Btw I have to be able to give every side of the cube another texture. TIA.
Hamster
Hamster
Where do you get the timer variable from? If it is just the time since your last frame then the z positions of all objects will shift back and forth as each frame will be taking a slightly different amount of time to draw. This would produce a really odd effect, but shouldn't really result in your cubes not being in line with each other during a single frame. Another thing I notice is that you enclose all your Y-axis rotations in their own glPushMatrix, glPopMatrix pair, but don't do this when you're drawing the top face. It's been a while since I used glRotatef and glTranslatef, so I'm probably missing something fairly obvious. Feel free to ridicule :-).

From the SGI OpenGL man pages:
Quote:

glPushMatrix pushes the current matrix stack down by one, duplicating the current matrix. That is, after a glPushMatrix call, the matrix on top of the stack is identical to the one below it.

glPopMatrix pops the current matrix stack, replacing the current matrix with the one below it on the stack.


This would suggest that you probably ought to use glLoadIdentity here and there after a push if you want a fresh matrix not affected by what has gone before. Good Luck.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.