rendering issue

trying to draw a flat plane, using it as a first step to a terrain generator, i just need this one square drawn then i can just multiply them, but i get a blank screen. this is for OSX

void initLights()
{
// set up light colors (ambient, diffuse, specular)
GLfloat lightKa[] = {.2f, .2f, .2f, 1.0f}; // ambient light
GLfloat lightKd[] = {.7f, .7f, .7f, 1.0f}; // diffuse light
GLfloat lightKs[] = {1, 1, 1, 1}; // specular light
glLightfv(GL_LIGHT0, GL_AMBIENT, lightKa);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightKd);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightKs);

// position the light
float lightPos[4] = {0, 0, 20, 1}; // positional light
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

glEnable(GL_LIGHT0);                        // MUST enable each light source after configuration

}

void setCamera(void)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 0, 0, 0, 1, 0, 1, 0); // eye(x,y,z), focal(x,y,z), up(x,y,z)
}

void InitOpenGL(void)
{
glShadeModel(GL_SMOOTH);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE);

glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);

glClearColor(0,0,0,0);
glClearStencil(0);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
initLights();
setCamera();
Draw();

}

void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();

glBegin(GL_QUADS);
glColor3f(1,1,1);
glVertex3f(-1,-0.25,1);
glColor3f(1,1,0);
glVertex3f(1,-0.25,1);
glColor3f(1,0,0);
glVertex3f(1,-0.25,-1);
glColor3f(1,0,1);
glVertex3f(-1,-0.25,-1);
glEnd();

glPopMatrix();
glutSwapBuffers();

}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800,600);
glutCreateWindow(“Core Engine Test”);
glViewport(0,0,800,600);
glutDisplayFunc(Draw);
glutIdleFunc(Draw);
InitOpenGL();
glutMainLoop();
return 0;
}

Hi,
You should include your code in the [ code ][/ code] tags.

  1. You are not setting the projection matrix. Usually we setup a resize handler to do it in this way,

void Resize(int nw, int nh) {
	glViewport(0,0,nw,nh);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(60,float(nw)/nh,1,100);
	glMatrixMode(GL_MODELVIEW);
}

//in main add the callback like this
glutReshapeFunc(Resize);

  1. To make it work first remove the stencil buffer and the depth function modifications.

  2. You are setting the modelview matrix to identity each frame so the setcamera call is useless. For glutLookAt, the first 3 params are the cam position, the next three are the cam target and the final three are the look up vector. So you need to modify the setcamera to something like this,


void setCamera(void)
{
gluLookAt(2, 2, 2, 0, 0, 0, 0, 1, 0); // eye(x,y,z), focal(x,y,z), up(x,y,z)
}

So to sumup here is the modified code see if this works.


#include <gl/glut.h> 

void Draw();
void initLights()
{
	float lightPos[4] = {0, 0, 20, 1}; // positional light

	// set up light colors (ambient, diffuse, specular)
	GLfloat lightKa[] = {.2f, .2f, .2f, 1.0f}; // ambient light
	GLfloat lightKd[] = {.7f, .7f, .7f, 1.0f}; // diffuse light
	GLfloat lightKs[] = {1, 1, 1, 1}; // specular light
	glLightfv(GL_LIGHT0, GL_AMBIENT, lightKa);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightKd);
	glLightfv(GL_LIGHT0, GL_SPECULAR, lightKs);
	// position the light
	
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
	glEnable(GL_LIGHT0); // MUST enable each light source after configuration
}

void setCamera(void)
{
	gluLookAt(2, 2, 2, 0, 0, 0, 0, 1, 0); // eye(x,y,z), focal(x,y,z), up(x,y,z)
}

void Resize(int nw, int nh) {
	glViewport(0,0,nw,nh);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(60,float(nw)/nh,1,100);
	glMatrixMode(GL_MODELVIEW);
}

void InitOpenGL(void)
{
	glShadeModel(GL_SMOOTH);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
	glEnable(GL_COLOR_MATERIAL);

	glClearColor(0,0,0,0);
	 
	initLights(); 
}

void Draw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	setCamera();
	 
	glBegin(GL_QUADS);
		glColor3f(1,1,1);glVertex3f(-1,-0.25,1);
		glColor3f(1,1,0);glVertex3f(1,-0.25,1);
		glColor3f(1,0,0);glVertex3f(1,-0.25,-1);
		glColor3f(1,0,1);glVertex3f(-1,-0.25,-1);
	glEnd();

	glutSwapBuffers();
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
	glutInitWindowSize(800,600); 
	glutCreateWindow("Core Engine Test");

	glutDisplayFunc(Draw);
	glutIdleFunc(Draw);
	glutReshapeFunc(Resize);
	InitOpenGL();
	glutMainLoop();
	return 0;
}

yes it worked thank you, i tried to add a second quad and it only draws one, now the odd thing is even if i flip the order in which the quads are in the Draw function the larger one gets drawn everytime even if the smaller one is above it and put first or second in the drawing order.


void Draw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	setCamera();
	

	
	glBegin(GL_QUADS);
	glColor3f(1,0,0);glVertex3f(-2,-0.05,2);
	glColor3f(0,1,0);glVertex3f(2,-0.05,2);
	glColor3f(0,0,1);glVertex3f(2,-0.05,-2);
	glColor3f(0,1,1);glVertex3f(-2,-0.05,-2);
	glEnd();
	
	glBegin(GL_QUADS);
	glColor3f(0,1,0);glVertex3f(0,0.5,0);
	glColor3f(0,0,1);glVertex3f(2,0.5,0);
	glColor3f(1,0,0);glVertex3f(2,0.5,2);
	glColor3f(0,1,1);glVertex3f(0,0.5,2);
	glEnd();
	
	glutSwapBuffers();
}

even if i flip the order in which the quads are in the Draw function the larger one gets drawn everytime even if the smaller one is above it

Well the order is decided by the result of the depth test. If u want to render in the order u r giving without depth order then simply disable the depth test.

it turns out it was rendering both quads, the issue was the smaller quad which is above the larger quad on the y axis is only visible on the underside while the larger quad is correct and visible on the top side. how do i get both sides or the correct side to show

got it i disabled CULL_FACE

new problem. since i got two quads on im trying to use a loop to draw a strip, then from that i plan to draw a larger square then add a variable y value and make a primitive terrain generator. now when i use a for loop it wont draw the quads but when i replace “x == 0.9” and “z == 0.9” with “x == 0.1” and “z == 0.1” it draws the quad.


	for (float x = 0; x == 0.9; x += 0.1)
	{
		for (float z = 0; z == 0.9; z += 0.1) 
		{
			glBegin(GL_QUADS);
			glColor3f(1,0,0);glVertex3f(x,0,z);
			glColor3f(0,1,0);glVertex3f(x + 0.1,0,z);
			glColor3f(0,0,1);glVertex3f(x + 0.1,0,z + 0.1);
			glColor3f(0,1,1);glVertex3f(x,0,z + 0.1);
			glEnd();
		}
	}