PDA

View Full Version : Getting blank screen as output for this program



achint.akv
04-09-2011, 01:27 PM
I am an OpenGl newbie. Can someone please help.
Can someone find an error in this code to draw a grid in the xz plane in 3D
I am always getting a blank window.
My other programs work perfectly fine(even those involving 3D)



#include<GL/glut.h>
#define N 20
#define dx 50;
#define dz 50;
GLint x[N],z[N];
GLint i,j;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
x[0]=0;
z[0]=0;
for(i=1;i<N;i++)
x[i]=x[0]+dx;
for(i=1;i<N;i++)
z[i]=z[0]+dz;
glColor3f(0.0,0.0,1.0);
for(i=0;i<N;i++)
{

glBegin(GL_LINE_STRIP);
for(j=0;j<N;j++)
glVertex3f(x[i],x[i],z[j]);
glEnd();
}
glColor3f(0.0,0.0,1.0);
for(i=0;i<N;i++)
{

glBegin(GL_LINE_STRIP);
for(j=0;j<N;j++)
glVertex3f(x[j],x[i],z[i]);
glEnd();
}
glFlush();
}
void myinit()
{
glOrtho(0,400,0,400,0,400);
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
}

void main(int argc,char** argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPT H);
//glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(200,200);
glutCreateWindow("grid");
glutDisplayFunc(display);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-100,100,-100,100,100,2000);
glMatrixMode(GL_MODELVIEW);
glTranslatef(-240,-150,220);
glRotatef(-30,1,0,0);
myinit();
glutMainLoop();
}

Mukund
04-09-2011, 02:05 PM
What is that you want to do? Please elaborate more. Also, why are you specifying a perpective as well as an orthogonal view?

1) glOrtho(0,400,0,400,0,400); in myinit()

2) glFrustum(-100,100,-100,100,100,2000); in main().

Moreover, its not a good idea to have the viewing specified in main(). Its generally not the practice. having it in a display callback, or a reshape callback would be more appropriate.

achint.akv
04-10-2011, 01:23 AM
Want to draw a grid in the x-z plane. The view of the grid is such that the plane is converging towards the horizon of the screen that gives a perception of depth.

Mukund
04-10-2011, 11:58 AM
Ok, then set a frustum view.(perspective projection i mean). Also, enclose code within [ code ] .. [ / code ] for better readability. Also, ensure that the grid is within your viewing volume. Make these changes and paste the code.

achint.akv
04-11-2011, 02:08 PM
Will post the code soon.Thanks in advance.Hope it works now.

achint.akv
04-14-2011, 01:38 AM
i'm still getting a blank white window as output.Please give some suggestions.Thanking you.



#include<GL/glut.h>
#define N 20
#define dx 50;
#define dz 50;
GLint x[N],z[N];
GLint i,j;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);//|GL_DEPTH_BUFFER_BIT);
x[0]=0;
z[0]=0;
for(i=1;i<N;i++)
x[i]=x[0]+dx;
for(i=1;i<N;i++)
z[i]=z[0]+dz;
glColor3f(0.0,0.0,1.0);
for(i=0;i<N;i++)
{

glBegin(GL_LINE_STRIP);
for(j=0;j<N;j++)
glVertex3f(x[i],x[i],z[j]);
glEnd();
}
glColor3f(0.0,0.0,1.0);
for(i=0;i<N;i++)
{

glBegin(GL_LINE_STRIP);
for(j=0;j<N;j++)
glVertex3f(x[j],x[i],z[i]);
glEnd();
}
glFlush();
}
void myinit()
{
glClearColor(1,1,1,1);
//glEnable(GL_DEPTH_TEST);
}
void reshape(int w,int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glFrustum(0,400,0,400,100,200);
glMatrixMode(GL_MODELVIEW);
}

void main(int argc,char** argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE);//|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(200,200);
glutCreateWindow("grid");
myinit();
glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();
}

BionicBytes
04-14-2011, 02:02 AM
There are many, many issues with this code.....


You are getting a white window because you told GL to clear the window to white.

glClearColor(1,1,1,1);

You are calling your GLUT methods in the wrong order (small point however)


glutDisplayFunc(display);
glutReshapeFunc(reshape);

...as you can't draw something before you have setup your projection and modelview matricies.




x[0]=0;
z[0]=0;
for(i=1;i<N;i++)
x[i]=x[0]+dx;
for(i=1;i<N;i++)
z[i]=z[0]+dz;

will produce virtually nothing as the way you have it all lines are on top of each other.
Perhaps you ment to have the following:


x[i]=x[i-1]+dx;
z[i]=z[i-1]+dz;





glColor3f(0.0,0.0,1.0);

...all your lines are black. Just an observation. perhaps you should set horizontal and vertical ines to some different colour. Looks prettier too.



void reshape(int w,int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glFrustum(0,400,0,400,100,200);
glMatrixMode(GL_MODELVIEW);
}

There is no glLoadIdentity in any of these matrix setups. Therefore you can't be sure what the modelview (camera) will contain.



glFrustum(0,400,0,400,100,200);

You have set the near plane to 100 and far to only 200.
YET, the code producing the grid of lines starts at z=0 and will endup at 1000!
I suggest you change this to glFrustum(0,400,0,400,0.5,1500);



...I think that's enough to be getting on with.

mbentrup
04-14-2011, 02:03 AM
x[0]=0;
z[0]=0;
for(i=1;i<N;i++)
x[i]=x[0]+dx;
for(i=1;i<N;i++)
z[i]=z[0]+dz;




All z coordinates (except z[0]) have the same value 50, so I guess all vertexes are simply clipped by the near plane at z=100.