PDA

View Full Version : problem



Craz1000
08-15-2011, 03:34 PM
trying to draw a flat plane with a bunch of quads to later make terrain. i can draw one quad but when i changed it to draw many, i get no output.


void ResizeWindow(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}

void keyboard(unsigned char key, int x, int y)
{
extern float camHeight;
switch (key)
{
case 'z':case'Z':
camHeight -= 0.05;
break;
case 'a':case'A':
camHeight += 0.05;
break;
}

}

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)
{
extern float camHeight;
gluLookAt(-1, camHeight, -1, 1, 0, 1, 0, 10, 0); // eye(x,y,z), focal(x,y,z), up(x,y,z)
}

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();

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.0f,z);
glColor3f(0,1,0);glVertex3f(x + 0.1f,0.0f,z);
glColor3f(0,0,1);glVertex3f(x + 0.1f,0.0f,z + 0.1f);
glColor3f(0,1,1);glVertex3f(x,0.0f,z + 0.1f);
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(ResizeWindow);
glutKeyboardFunc(keyboard);
InitOpenGL();
glutMainLoop();
return 0;
}

Avithohol
08-16-2011, 01:00 AM
Hello ! Strange line in the drawing loop:
for (float x = 0; x == 0.9; x += 0.1) ...

I think its a wrong condition, you should have
for (float x = 0; x <= 0.9; x += 0.1) or something like this.


On the other hand, i always suggest not to turn on OpenGL lights in testing phase, unless everything draws perfectly and you want lights :). It gives you more complexity in debug phase.

Craz1000
08-16-2011, 04:04 PM
ok disabled the lighting and did what you suggested and nothing howerver after playing arround with it i made it so the loop only executes once and i saw one quad flash then it got cleared. so i made it that the loop gets executed once unless another variable = true... result


void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glLoadIdentity();
setCamera();
if (renderterrain == true )
{
for (float x = 0.0; x == 0.9; x += 0.1)
{
for (float z = 0.0; z == 0.9; z += 0.1)
{
glBegin(GL_QUADS);
glColor3f(1,0,0);glVertex3f(x,0.0f,z);
glColor3f(0,1,0);glVertex3f(x + 0.1f,0.0f,z);
glColor3f(0,0,1);glVertex3f(x + 0.1f,0.0f,z + 0.1f);
glColor3f(0,1,1);glVertex3f(x,0.0f,z + 0.1f);
glEnd();
}
}

glutSwapBuffers();
}
renderterrain = false;
}


when the function is edited so the loop gets executed once (x == 0.0... z == 0.0) it draws the quad and dosent get cleared, however i get no output again when i change it to the original state that "x == 0.9" even if the whole loop gets executed once now

it seems when i try to draw more than one quad this way it just dosent draw but ive manually drawn a full on cube line by line vertex by vertex, this is the same idea just looped, so why wouldnt it work this way?

BionicBytes
08-17-2011, 01:22 AM
for (float x = 0.0; x == 0.9; x += 0.1)
for (float z = 0.0; z == 0.9; z += 0.1)

What do you expect to happen with these two condition tests (x == 0.9) and (z == 0.9)?

Surely for the loop to process, you need to set the condition to test for x <= 0.9 and z <= 0.9

If the loops don't process each frame then nothing will get drawn. I can't say without debugging through your code what actually happens, but it seems completely wrong to specify the loop the way you are.

charliejay
08-17-2011, 01:32 AM
Not entirely sure what language you're writing in (looks like OOC or C++ to me), but I agree with Avitohol, your loops are suspect.

They're suspect even if it's not the actual cause of your problem, because you're doing an exact comparison with a floating point number in your loop termination.

Try doing this:


for(int x=0;x<10;x++)
{
GLfloat xf=((GLfloat)x)*(GLfloat)0.1;

// put other code here, do the same with the inner loop
}


You may need to declare xf outside the body of the loop...

Craz1000
08-17-2011, 03:48 PM
for (float x = 0.0; x == 0.9; x += 0.1)
for (float z = 0.0; z == 0.9; z += 0.1)

What do you expect to happen with these two condition tests (x == 0.9) and (z == 0.9)?

Surely for the loop to process, you need to set the condition to test for x <= 0.9 and z <= 0.9

If the loops don't process each frame then nothing will get drawn. I can't say without debugging through your code what actually happens, but it seems completely wrong to specify the loop the way you are.


i changed it now i get output. im guessing float variables ie 0.8+0.1 dosent exactly = 0.9? which is why you need the "<=" instead of "=="

and charliejay, its for OSX under Xcode v4.1, C++ at the moment.