Sign in to follow this  

Striped not checkered?

This topic is 4748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Howdy, I am confused as to why the following function is creating a plane that is striped and not checkered. The function creates a floor centered on 0,0,0 by drawing 1x1 squares. The color of the squares should alternate. Inside of the loops it checks the value of mat_color and changes it then sets the material values. I know am missing something insanely simple but I need another set of eyes to help me out. Thanks! Here's the code:
void draw_floor(int tiles) 
{
  GLfloat mat_amb0[] = {0.5, 0.25, 0.25, 1.0};	
  GLfloat mat_dif0[] = {0.5, 0.25, 0.25, 1.0};	
  GLfloat mat_spec0[] = {0.5,0.5,0.5,1};		
  GLfloat mat_shininess0 = 50.0f;				
  GLfloat mat_amb1[] = {0.25, 0.5, 0.75, 1.0};	
  GLfloat mat_dif1[] = {0.25, 0.5, 0.75, 1.0};	
  GLfloat mat_spec1[] = {0.5,0.5,0.5,1};		
  GLfloat mat_shininess1 = 50.0f;				
  int mat_color = 0;

  if(tiles%2 != 0) 
    tiles--;

  int start = -1 * int(tiles/2);
  int cur_pos_z = start;
  int cur_pos_x = start;

  glBegin(GL_QUADS);
  for(int c=0; c<tiles; c++) 
  {
    for(int d=0; d<tiles; d++) 
    {
      if(mat_color) 
      {
        mat_color = 0;
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_spec0);
        glMaterialfv(GL_FRONT, GL_AMBIENT, mat_amb0);
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_dif0);
        glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess0);
      }
      else 
      {
	mat_color = 1;
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_spec1);
        glMaterialfv(GL_FRONT, GL_AMBIENT, mat_amb1);
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_dif1);
        glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess1);
      }
      //draw square
      glVertex3f(cur_pos_x,0,cur_pos_z); 
      glVertex3f(cur_pos_x+1,0,cur_pos_z); 
      glVertex3f(cur_pos_x+1,0,cur_pos_z+1); 
      glVertex3f(cur_pos_x,0,cur_pos_z+1);

      cur_pos_x++;
    }
    cur_pos_x = start; //reset x coord
    cur_pos_z++;
  }
	
  glEnd();
}


Fruny - there I fixed it for you. [Edited by - Fruny on December 16, 2004 3:26:37 PM]

Share this post


Link to post
Share on other sites
... and about your specifiq problem: tiles is a multiple of 2, therefore mat_color is always 1 when d is 0.


if(tiles%2 != 0)
tiles--;

Should be:

if(tiles && (tiles%2 == 0))
tiles--;

Although i'm not sure it make sense in your case.
If you want a checkered board, you'd better use the expression (((c&1)+(d&1))&1) for example instead your mat_color
HTH,

Share this post


Link to post
Share on other sites
Formatting fixed.

You should negate mat_color in between each row, otherwise the squares will be aligned, forming stripes instead of a checkerboard.

Emmanuel: mat_color = (c^d)&1

Share this post


Link to post
Share on other sites

This topic is 4748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this