Jump to content
  • Advertisement
Sign in to follow this  
yaazz

OpenGL trying to get depth test to show my cube correctly

This topic is 3705 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

Hey there y'all, I fixed my problem with picking, but now i have encountered an entirely new issue. My cube (Which is actually made up of 6 3d planes of 8*8 squares) isnt going through GL depth test correctly. Instead of drawing the front pieces and throwing away the back, its drawing the back and throwing away the front. I figured I might be able to solve this problem by changing glDepthFunc to GL_GREATER, but then nothing gets drawn at all. Youtube video of my program here:
EDIT: A,d move square horizontally, W,s move square vertical I know it must be something simple but I can't seem to figure it out, can you help me?
//
// minesweeper.c Author:**********************
// a game of minesweeper written in openGL

#include <GL/glut.h>
#include <stdio.h> 
#include <stdlib.h>
#include <time.h>
#include <math.h> 

#define NUMSQUARES 64
#define WIDTH 10 
#define HEIGHT 10 
#define wWIDTH 320
#define wHEIGHT 240 
#define PI 3.14152 



void list_hits(GLint hits, GLuint *names);
void mousedw(int x, int y, int but);
void list_hits(GLint hits, GLuint *names);

void gl_selall(GLint hits, GLuint *buff);
int bounceX=0,bounceY=0,bounceZ=0;
double angle=0,vAngle=0; //angle and vertical angle
//Strcut square and board represent the minesweeper field 
struct square 
{
  int x;
  int y; 	     // X and Y position of square		
  int vertex[2];
  char letter; //state of square, i.e clicked, exploded, flagged, etc
  int r;
  int g;
  int b;
  int bombsAround;
  int isBomb; //is this a bomb square
  int mouseON; //is mouse on square
  unsigned char uniqueColor[3];
}; 

struct coord
{
  int worldX; //world coords from mouse
  int worldY; 
};
struct board
{
  int boardState; //state of board
  int width; //width of squares
  int height; //height of squares 
  struct square grid[NUMSQUARES]; //amount of squares
};
int R=100,G=100,B=100,elapsedTime=0; //RGB and timer values
int windowH=480,windowW=640; 
struct board gameGrid; 
struct coord C;

void createGrid(struct board* inGrid); 
void keyboard(unsigned char key, int x, int y); 
void game(int value);
void mydisplay(); 
void mouseClick(int button, int state, int x, int y); 
void mousedw(int x, int y, int but);
int gl_select(int x, int y); 
void gl_selall(GLint hits, GLuint *buff); 
void list_hits(GLint hits, GLuint *names); 
void myReshape(int w, int h); 
int checkCollision(struct board* b,int mx, int my);
int main(int argc, char** argv); 
void getOGLPos(struct coord* c, int x, int y);
void updateGrid(struct board* grid);
void myPick();
void motionFunc(int x, int y);
void setBombs(struct board* b);
void checkEnd(struct board* b);
void checkAdjacentBombs(int i);
double degToRad(double degree);
double radToDeg(double radian);

void checkAdjacentBombs(int i)
{
  if (gameGrid.grid[i-1].isBomb==1)
    gameGrid.grid.bombsAround++;

  if (gameGrid.grid[i+1].isBomb==1)
    gameGrid.grid.bombsAround++;

  if (gameGrid.grid[i-7].isBomb==1)
    gameGrid.grid.bombsAround++;

  if (gameGrid.grid[i-8].isBomb==1)
    gameGrid.grid.bombsAround++;

  if (gameGrid.grid[i-9].isBomb==1)
    gameGrid.grid.bombsAround++;

  if (gameGrid.grid[i+7].isBomb==1)
    gameGrid.grid.bombsAround++;

  if (gameGrid.grid[i+8].isBomb==1)
    gameGrid.grid.bombsAround++;

  if (gameGrid.grid[i+9].isBomb==1)
    gameGrid.grid.bombsAround++;

  printf("%d Ajacent bombs\n",gameGrid.grid.bombsAround);
}


//check for the end of the game!
//letters stand for 
//c=clicked
//b=bomb
//f=flagged
//h=nothing there... dunno why i picked h
void checkEnd(struct board* b)
{
  int i=0;
  printf("Checking end....");
  int gameover=1; //0 if not over
  for(i=0;i<64;i++)
    {
     
      if (b->grid.isBomb==0)
	{
	  if(b->grid.letter!='c') //c= clicked
	  gameover=0;
	}
      else if(b->grid.letter=='b')//b= bomb
	{
	  printf("Bomb found at %d, game over",i);
	  gameover=2;
	  gameGrid.boardState=1;
	  break;
	}
    }

  if(gameover==1)
    {
      //game is over you win draw win 
      printf("GAME OVER U WIN SCORE=%d \n",elapsedTime);
      gameGrid.boardState=1;
     
      }


  printf("\n");
}

//reset game and randomly place bombs
void setBombs(struct board* b)
{
  int i,rndnum,ok=0;
  for(i=0;i<64;i++)
    {
      b->grid.isBomb=0;
      b->grid.letter='h';
    }
  for(i=0;i<10;i++)
    {
      ok=0;
      
      while(ok!=1)
      {
	  rndnum=(rand() %64);
	  if(b->grid[rndnum].isBomb==0)
	  {
	    printf("bomb at %d \n",rndnum);
	      b->grid[rndnum].isBomb=1;
	      ok=1;
	        }
	      }
      
    }
}

//keep track of motion
void motionFunc(int x, int y)
{
  if(gameGrid.boardState==0)
    {
      getOGLPos(&C,wWIDTH-x,y);
      int i,index;
      for(i=0;i<64;i++)
	{
	  gameGrid.grid.mouseON=0;
	}
      index=gl_select(x,y); 
      if(index != -1)
	{
	  gameGrid.grid[index].mouseON=1;
	}
      updateGrid(&gameGrid);
      glutPostRedisplay();
    }
}

//update the state of the game
void updateGrid(struct board* b)
{
  int i=0;
  
    
      for (i=0;i<64;i++)
	{
	   if(b->grid.mouseON==0)
		{
	      if(b->grid.letter=='c')
		{
		  if(b->grid.bombsAround==0)
		    {
		      b->grid.r=100;
		      b->grid.g=100;
		      b->grid.b=100;
		    }
		  //yellow for 1
		  else if(b->grid.bombsAround==1)
		    {
		      b->grid.r=255;
		      b->grid.g=255;
		      b->grid.b=0;
		    }
		  //pink for 2
		  else if(b->grid.bombsAround==2)
		    {
		      b->grid.r=255;
		      b->grid.g=20;
		      b->grid.b=147;
		    }
		  //purple for 3
		  else if(b->grid.bombsAround==3)
		    {
		      b->grid.r=186;
		      b->grid.g=85;
		      b->grid.b=211;
		    }
		  //dark green for 4
		  else if(b->grid.bombsAround==4)
		    {
		      b->grid.r=34;
		      b->grid.g=139;
		      b->grid.b=34;
		    }
		  //dark blue for 5
		  else if(b->grid.bombsAround==5)
		    {
		      b->grid.r=25;
		      b->grid.g=25;
		      b->grid.b=12;
		    }
		  //aquamarine for 6
		  else if(b->grid.bombsAround==6)
		    {
		      b->grid.r=102;
		      b->grid.g=205;
		      b->grid.b=170;
		    }
		  //chocolate for 7
		  else if(b->grid.bombsAround==7)
		    {
		      b->grid.r=210;
		      b->grid.g=105;
		      b->grid.b=30;
		    }
		  //black for 8
		  else if(b->grid.bombsAround==8)
		    {
		      b->grid.r=0;
		      b->grid.g=0;
		      b->grid.b=0;
		    }
		}  
	      else if(b->grid.letter=='f')
		{
		  gameGrid.grid.r=0;
		  gameGrid.grid.g=0;
		  gameGrid.grid.b=255;
		}
	      else if(b->grid.letter=='b')
		{
		  gameGrid.grid.r=255;
		  gameGrid.grid.g=0;
		  gameGrid.grid.b=0;
		}
	  else if(b->grid.isBomb==1)
	    {
	      gameGrid.grid.r=240;
	      gameGrid.grid.g=240;
	      gameGrid.grid.b=240;
	    }
	    else
		{
		  gameGrid.grid.r=255;
		  gameGrid.grid.g=255;
		  gameGrid.grid.b=255;
		}
	    }
		else
		{
			 b->grid.r=0;
		      b->grid.g=255;
		      b->grid.b=100;
		}


	  
	}
      
 glutPostRedisplay(); 
    
}

//change mouse coords into world coords
void getOGLPos(struct coord* c,int x, int y)
{
  GLint viewport[4];
  GLdouble modelview[16];
  GLdouble projection[16];
  GLfloat winX, winY, winZ; 
  GLdouble posX, posY, posZ; 

  glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
  glGetDoublev(GL_PROJECTION_MATRIX, projection);
  glGetIntegerv(GL_VIEWPORT, viewport); 

  winX= (float)x;
  winY= (float)y; 
  glReadPixels(x,(int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ); 
  gluUnProject( winX, winY,winZ,modelview,projection,viewport, &posX, &posY, &posZ); 
  c->worldX=(int)-posX; 
  c->worldY=(int)-posY;
  //printf("%d %d",c->worldX, c->worldY);
  
}





//function to fill the Board struct with proper coordinates
void createGrid(struct board* inGrid) 
{
  inGrid->boardState=0;
  int i=0,tempx=0,tempy=0,tempR=1,tempB=0,tempG=0,verX=-40,verY=-40;
	 inGrid->grid[0].y=0;
	inGrid->grid[0].x=0; 
	inGrid->grid[0].letter='h'; 	  
	inGrid->grid[0].r=255;
        inGrid->grid[0].g=255;
        inGrid->grid[0].b=255;
		inGrid->grid[0].vertex[0]=-40;
		inGrid->grid[0].vertex[1]=-40;
	//printf("grid 0 x=%d y=%d letter=%c \n",inGrid->grid[0].x,inGrid->grid[0].y);
	for(i=1;i<64;i++)
	{
	  inGrid->grid.r=255;
	  inGrid->grid.g=255;
	  inGrid->grid.b=255;
		if(i%8==0) 
		{
			tempy+=inGrid->height;
			tempx=0-inGrid->width;
		    verY+=HEIGHT;
			verX=-50;	
		}
		tempx+=inGrid->width; 
		verX+=WIDTH;
		inGrid->grid.x=tempx; 
		inGrid->grid.y=tempy;
 		inGrid->grid.vertex[0]=verX; 
		inGrid->grid.vertex[1]=verY;
		inGrid->grid.letter='h';
		
		   
			tempR++;
           if(tempR > 254)
           {
  				tempR=0;
				tempB++;
                if(tempB > 254)
                {
					tempB=0;
					 tempG++;
                 
                }
           }
		   
		inGrid->grid.uniqueColor[0]=tempR;
		inGrid->grid.uniqueColor[1]=tempB;
		inGrid->grid.uniqueColor[2]=tempG;


	}
}

//print off the grid
void printGrid(struct board* inGrid) 
{
	
  int i;

	for(i=1;i<64;i++)
	{

		printf("grid %d x=%d y=%d unique color %d %d %d Vertex %d %d \n",i,inGrid->grid.x,inGrid->grid.y,inGrid->grid.uniqueColor[0],inGrid->grid.uniqueColor[1],inGrid->grid.uniqueColor[2],inGrid->grid.vertex[0],inGrid->grid.vertex[1]);
	}
}

//Keyboard input function

void keyboard(unsigned char key, int x, int y)
{
	
	//if r is pressed get a random number for each color variable	
	if (key==112)
	{
		
		if(gameGrid.boardState==0)
		  {
		    printf("paused\n");
		    gameGrid.boardState=2;
		  }
		else if(gameGrid.boardState!=1)
		  {
		    printf("unpaused\n");
		    gameGrid.boardState=0;
		  }
	}
	else if(key==100)//d 
	{
		angle-=10;

		if(angle<0)angle=360;
		glutPostRedisplay();
	}
	else if(key==97)//d 
	{
		angle+=10;

		if(angle>360)angle=0;
		glutPostRedisplay();
	}
	else if(key==115)//d 
	{
		vAngle-=10;

		if(angle<0)angle=360;
		glutPostRedisplay();
	}
	else if(key==119)//d 
	{
		vAngle+=10;

		if(angle<0)angle=360;
		glutPostRedisplay();
	}
	else if(key==110)
	  {
	    printf("new game!\n");
	    elapsedTime=0;
	    setBombs(&gameGrid);
	    updateGrid(&gameGrid);
	    gameGrid.boardState=0;
	  }
	else if(key==113)  //if q is pressed exit program!
	{
		exit(0);
	}
}

void angleTimer(int value)
{
	/*
  if(gameGrid.boardState==0)
    {
		angle+=10;
		bounceZ+=10;
		if(bounceZ>300)bounceZ=0;
		if(angle>360)angle=0;
		glutPostRedisplay();
    }
glutTimerFunc(100,angleTimer,0); //create another timerfunc so we have concurency
    */

}

//game is the timer function. 
void game(int value)
{
  if(gameGrid.boardState==0)
    {
elapsedTime++; //add to elapsed time
 printf("Time: %d\n",elapsedTime);
    }
glutTimerFunc(1000,game,0); //cweranidrreate another timerfunc so we have concurency
    

}

//drawing function
void mydisplay()
{
int i,x=0,y=0;
double tempX,tempY;
tempX=gameGrid.grid[55].x*((cos((angle*PI)/180)));
tempY=gameGrid.grid[55].y*((sin((angle*PI)/180)));
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 

for(i=0;i<64;i++)
{


	glLoadIdentity();
glRotated(vAngle,1.0,0.0,0.0);
glRotated(angle,0.0,1.0,0.0);
	glPushMatrix();
	glTranslated(gameGrid.grid.x,gameGrid.grid.y,0);	

	glColor3ub(gameGrid.grid.r,gameGrid.grid.g,gameGrid.grid.b); //use the timer random colors

	//load the name on to the name stack
	glPushName(i);

	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,0);        
			glVertex3d(5,-5,0);        
			glVertex3d(5,5,0);        
			glVertex3d(-5,5,0);        
	glEnd();
//glColor3ub(0,255,0);
	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,-70);        
			glVertex3d(5,-5,-70);        
			glVertex3d(5,5,-70);        
			glVertex3d(-5,5,-70);        
	glEnd();
glPopMatrix();
//glColor3ub(255,0,0);
glRotated(90,0.0,1.0,0.0);
glPushMatrix();
glTranslated(gameGrid.grid.x,gameGrid.grid.y,0);	

	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,0);        
			glVertex3d(5,-5,0);        
			glVertex3d(5,5,0);        
			glVertex3d(-5,5,0);        
	glEnd();

//glColor3ub(0,0,255);
	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,70);        
			glVertex3d(5,-5,70);        
			glVertex3d(5,5,70);        
			glVertex3d(-5,5,70);        
	glEnd();
glPopMatrix();

glPopMatrix();
//glColor3ub(255,255,0);
glRotated(90,1.0,0.0,0.0);
glPushMatrix();
glTranslated(gameGrid.grid.x,gameGrid.grid.y,0);	

	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,0);        
			glVertex3d(5,-5,0);        
			glVertex3d(5,5,0);        
			glVertex3d(-5,5,0);        
	glEnd();

//glColor3ub(255,0,255);
	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,-70);        
			glVertex3d(5,-5,-70);        
			glVertex3d(5,5,-70);        
			glVertex3d(-5,5,-70);        
	glEnd();
glPopMatrix();

}
	glFlush();
	glutSwapBuffers();
}

//drawing function
void myPick()
{

int i,x=0,y=0;
double tempX,tempY;
tempX=gameGrid.grid[55].x*((cos((angle*PI)/180)));
tempY=gameGrid.grid[55].y*((sin((angle*PI)/180)));
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 

for(i=0;i<64;i++)
{


	glLoadIdentity();
glRotated(angle,0.0,1.0,0.0);
	glPushMatrix();
	glTranslated(gameGrid.grid.x,gameGrid.grid.y,0);	

	glColor3ub(gameGrid.grid.uniqueColor[0],gameGrid.grid.uniqueColor[1],gameGrid.grid.uniqueColor[2]); //use the timer random colors

	//load the name on to the name stack
	glPushName(i);

	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,0);        
			glVertex3d(5,-5,0);        
			glVertex3d(5,5,0);        
			glVertex3d(-5,5,0);        
	glEnd();
	glColor3ub(gameGrid.grid.uniqueColor[0],gameGrid.grid.uniqueColor[1],gameGrid.grid.uniqueColor[2]); //use the timer random colors
	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,-70);        
			glVertex3d(5,-5,-70);        
			glVertex3d(5,5,-70);        
			glVertex3d(-5,5,-70);        
	glEnd();
glPopMatrix();
	glColor3ub(gameGrid.grid.uniqueColor[0],gameGrid.grid.uniqueColor[1],gameGrid.grid.uniqueColor[2]); //use the timer random colors
glRotated(90,0.0,1.0,0.0);
glPushMatrix();
glTranslated(gameGrid.grid.x,gameGrid.grid.y,0);	

	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,0);        
			glVertex3d(5,-5,0);        
			glVertex3d(5,5,0);        
			glVertex3d(-5,5,0);        
	glEnd();

	glColor3ub(gameGrid.grid.uniqueColor[0],gameGrid.grid.uniqueColor[1],gameGrid.grid.uniqueColor[2]); //use the timer random colors
	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,70);        
			glVertex3d(5,-5,70);        
			glVertex3d(5,5,70);        
			glVertex3d(-5,5,70);        
	glEnd();
glPopMatrix();
	glColor3ub(gameGrid.grid.uniqueColor[0],gameGrid.grid.uniqueColor[1],gameGrid.grid.uniqueColor[2]); //use the timer random colors
glRotated(90,1.0,0.0,0.0);
glPushMatrix();
glTranslated(gameGrid.grid.x,gameGrid.grid.y,0);	

	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,0);        
			glVertex3d(5,-5,0);        
			glVertex3d(5,5,0);        
			glVertex3d(-5,5,0);        
	glEnd();

	glColor3ub(gameGrid.grid.uniqueColor[0],gameGrid.grid.uniqueColor[1],gameGrid.grid.uniqueColor[2]); //use the timer random colors
	glBegin(GL_QUADS);        
			glVertex3d(-5,-5,-70);        
			glVertex3d(5,-5,-70);        
			glVertex3d(5,5,-70);        
			glVertex3d(-5,5,-70);        
	glEnd();
glPopMatrix();


}
	glFlush();

	
}



void mouseClick(int button, int state, int x, int y)
{
  int index=-1;
  if(gameGrid.boardState==0)
    {
      getOGLPos(&C,wWIDTH-x,y);
      if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
	{ 
	  index=gl_select(x,y); 
	  if(index != -1)
	    {
	      if(gameGrid.grid[index].isBomb==0)
		{
		  gameGrid.grid[index].letter='c';
		  checkAdjacentBombs(index);
		}
	      else
		{
		  gameGrid.grid[index].letter='b';
		}
	    }
	  checkEnd(&gameGrid);
	}
      
      if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN))
	{ 
	  index=gl_select(x,y); 
	  if(index != -1)
	    {
	      if(gameGrid.grid[index].letter!='f')
		{
	      gameGrid.grid[index].letter='f';
		}
	      else 
		{
		  gameGrid.grid[index].letter='h';
		}
	    }
	}
      updateGrid(&gameGrid);
    }
  else
    {
      if(button==GLUT_LEFT_BUTTON && state== GLUT_DOWN)
	{
	  if(gameGrid.boardState==1)
	    {
	  elapsedTime=0;
	  setBombs(&gameGrid);
	  printf("resetting game\n");
	  gameGrid.boardState=0;
	    }
	}
    }
}
 
 int gl_select(int x, int y)
 {
		int i, retval=-1;
      // turn off texturing, lighting and fog
      glDisable(GL_TEXTURE_2D);
      glDisable(GL_FOG);
      glDisable(GL_LIGHTING);
  
      // render every object in our scene
      // suppose every object is stored in a list container called SceneObjects
      myPick();
 
  
      // get color information from frame buffer
      unsigned char pixel[3];
       
      GLint viewport[4];
      glGetIntegerv(GL_VIEWPORT, viewport);
       
      glReadPixels(x, viewport[3] - y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
 
      // now our picked screen pixel color is stored in pixel[3]
      // so we search through our object list looking for the object that was selected
		 for(i=0;i<64;i++)
         {
           if(gameGrid.grid.uniqueColor[0] == pixel[0] && gameGrid.grid.uniqueColor[1] == pixel[1] && gameGrid.grid.uniqueColor[2] == pixel[2])
           {
                // flag object as selected
                printf("%d Selected!!!\n",i);
				retval=i;
                break;
           }
         }
		if(retval>=0)
		{
 		return retval;
		}
		else
		{
			return -1;
		}
 }

//reshape function
void myReshape(int w, int h)
{
     glEnable(GL_DEPTH_TEST);
   glClearColor(0.0,0.0,0.0,1.0);
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  //glOrtho(-5,WIDTH*8+1,-5,HEIGHT*8+1,-3,3);
 gluPerspective( 45.0,4.0/3.0,-100.0,10.0 );
gluLookAt((GLdouble)WIDTH*4,(GLdouble)HEIGHT*4,-250.0,(GLdouble)WIDTH*4,(GLdouble)HEIGHT*4,0.0,0.0,1.0,0.0); 

  glMatrixMode(GL_MODELVIEW);
}


//main function
int main(int argc, char** argv){
  //printf("HELLO \n");
  //  srand(time(NULL));
        glutInit(&argc, argv);
	gameGrid.width=WIDTH; 
	gameGrid.height=HEIGHT; 
	createGrid(&gameGrid);
	printGrid(&gameGrid);
	setBombs(&gameGrid);
	void(*foo)(int value);

	
	glutReshapeFunc(myReshape);	
   
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

	glutInitWindowSize (wWIDTH, wHEIGHT);
	glutTimerFunc(1000,game,0);
	glutTimerFunc(100,angleTimer,0); //create another timerfunc so we have concurency
	glutCreateWindow("simple");     
	glutKeyboardFunc (keyboard);
	glutMouseFunc(mouseClick);
	glutPassiveMotionFunc(motionFunc);
	glutDisplayFunc(mydisplay);    
	myReshape(wWIDTH,wHEIGHT);

	glutMainLoop();
	
}

double degToRad(double degree)
{
	return ((PI/180.0))*degree;
}


double radToDeg(double radian)
{
	return (180.0/PI) * radian;
}




Share this post


Link to post
Share on other sites
Advertisement
Looks like your front/back face culling need to be reversed. I haven't read through your code but call glFrontFace with either GL_CW or GL_CCW depending on your current cull mode and be sure to have culling enabled by calling glEnable(GL_CULL_FACE).

Hope that helps you out.

Share this post


Link to post
Share on other sites
Hmm i figured it had something to do with drawing order, but That code didnt seem to fix my problem.
When I add culling, it doesn't show up as the inside of a cube anymore, but everything is distorted as if depth testing isnt happening at all.

[Edited by - yaazz on October 26, 2008 5:34:48 PM]

Share this post


Link to post
Share on other sites
There's a lot of code there and much redundant for this topic, which made me not read it all, but:

you need to choose one and only one drawing order, which is either CCW or CW.
I suggest using CCW and I will use from now on.
Then, you need to enable back face culling, as Asplund told you, which would be:
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);

and then you need to stick to the drawing order of the vertices to define the front faces correctly, so for a triangle given by A, B and C point with coordinates A=(5,2) B=(10,7) C=(2,3) (assuming Y points upwards) you need to draw them in order BCA so that the normal, which defines visible side of the polygon, is oriented in correct direction for the face to be visible. If you drew that triangle in CW order, which would be ACB, it will be as in that youtube clip -> the other way around.

Share this post


Link to post
Share on other sites
Sorry there is a LOT of unneeded code there.
I will post the reshape and drawing functions below for easy access.

If you look at my drawing function I go from
glVertex3d(-5,-5,0);
glVertex3d(5,-5,0);
glVertex3d(5,5,0);
glVertex3d(-5,5,0);
Isn't this clockwise ordering?
The way I am drawing the cube, Is I am taking 6 planes, made up of an 8*8 grid of smaller squares, and drawing it, one small square at a time. I then draw the same square again, except with a different Z position.

Then I rotate my matrix by 90 degrees on the Y axis, draw another square, then make another perpendicular to it. Once the loop has completed drawing every small square, we have a cube with no top and bottom.

Then to make the top and bottom of the cube, I rotate my matrix by 90 degrees on the X axis
draw a square, then do the same thing again in the Perpendicular direction, get what im saying?

This happens for the entire for loop, with each iteration drawing 4 more small squares until we have a cube.

Could all the rotations be the problem? I thought since my vertex are all Clockwise it shouldnt be a problem, but you guys are the experts!

also, if you look at the drawing code, in order to get the smaller squares to correctly for a cube, sometimes I was drawing the "perpendicular" square in front, and sometimes behind. Does this make a difference?

reshape func

void myReshape(int w, int h)
{
glEnable(GL_DEPTH_TEST);
glClearColor(0.0,0.0,0.0,1.0);
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-5,WIDTH*8+1,-5,HEIGHT*8+1,-3,3);
gluPerspective( 45.0,4.0/3.0,-100.0,10.0 );
gluLookAt((GLdouble)WIDTH*4,(GLdouble)HEIGHT*4,-250.0,(GLdouble)WIDTH*4,(GLdouble)HEIGHT*4,0.0,0.0,1.0,0.0);

glMatrixMode(GL_MODELVIEW);
}




Drawing func

//drawing function
void mydisplay()
{
int i,x=0,y=0;
double tempX,tempY;
tempX=gameGrid.grid[55].x*((cos((angle*PI)/180)));
tempY=gameGrid.grid[55].y*((sin((angle*PI)/180)));
//glCullFace(GL_FRONT_AND_BACK);
//glFrontFace(GL_CCW);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glCullFace(GL_FRONT_AND_BACK);
for(i=0;i<64;i++)
{


glLoadIdentity();
glRotated(vAngle,1.0,0.0,0.0);
glRotated(angle,0.0,1.0,0.0);
glPushMatrix();
glTranslated(gameGrid.grid.x,gameGrid.grid.y,0);

glColor3ub(gameGrid.grid.r,gameGrid.grid.g,gameGrid.grid.b); //use the timer random colors

//load the name on to the name stack
glPushName(i);

glBegin(GL_QUADS);
glVertex3d(-5,-5,0);
glVertex3d(5,-5,0);
glVertex3d(5,5,0);
glVertex3d(-5,5,0);
glEnd();
glColor3ub(0,255,0);
glBegin(GL_QUADS);
glVertex3d(-5,-5,-70);
glVertex3d(5,-5,-70);
glVertex3d(5,5,-70);
glVertex3d(-5,5,-70);
glEnd();
glPopMatrix();
glColor3ub(255,0,0);
glRotated(90,0.0,1.0,0.0);
glPushMatrix();
glTranslated(gameGrid.grid.x,gameGrid.grid.y,0);

glBegin(GL_QUADS);
glVertex3d(-5,-5,0);
glVertex3d(5,-5,0);
glVertex3d(5,5,0);
glVertex3d(-5,5,0);
glEnd();

glColor3ub(0,0,255);
glBegin(GL_QUADS);
glVertex3d(-5,-5,70);
glVertex3d(5,-5,70);
glVertex3d(5,5,70);
glVertex3d(-5,5,70);
glEnd();
glPopMatrix();

glPopMatrix();
glColor3ub(255,255,0);
glRotated(90,1.0,0.0,0.0);
glPushMatrix();
glTranslated(gameGrid.grid.x,gameGrid.grid.y,0);

glBegin(GL_QUADS);
glVertex3d(-5,-5,0);
glVertex3d(5,-5,0);
glVertex3d(5,5,0);
glVertex3d(-5,5,0);
glEnd();

glColor3ub(255,0,255);
glBegin(GL_QUADS);
glVertex3d(-5,-5,-70);
glVertex3d(5,-5,-70);
glVertex3d(5,5,-70);
glVertex3d(-5,5,-70);
glEnd();
glPopMatrix();

}
glFlush();
glutSwapBuffers();
}





Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!