Problem with glMAtrixmode

Hi all,

I’m trying to write a code to draw a circle. I’m a beginner to openGL. Often when I write a code to do draw something like this I’m not getting anything on the scree. I generally have problem with glMarixmode(), glIdentity(), glviewport() and simialr functions like this. This is the code that I wrote and I’m simply getting a window with black background.

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <ios>
#include <math.h>

#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>

#define WINDOW_X 1000
#define WINDOW_Y 1000

void doInit()
{
glClearColor(0.0,0.0,0.0,0.0);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}

void dodisplay(void)
{
const float DEG2RAD = 3.14159/180;
int radius = 2;
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(0.2,0.2,0.75);
glMatrixMode (GL_MODELVIEW);
glBegin(GL_LINE_LOOP);
for (int i=0; i< 360; i++)
{
float degInRad = i*DEG2RAD;
glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius);
}
glEnd();
glFlush();

}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(WINDOW_X,WINDOW_Y);
glutInitWindowPosition(0,0);
glutCreateWindow(“Project”);
doInit();
glutDisplayFunc(dodisplay);
glutReshapeFunc (reshape);
glutMainLoop();

return 0;

}

can you guys briefly explain me when should I use all these functions in code and which one to use?

Regards,
Arun

You are drawing a 2D shape calling glVertex2f, so the vertex z coordinate is always set as zero.

If you set a perspective projection with a z-range equals to [1, 100] you won’t see anything, because vertices are not in the view volume.

To see your shape, move back the camera, calling for example:

glTranslatef( 0.f, 0.f, -2.f );

just before rendering it.

Or call glVertex3f with a z coordinate in the view volume, so in the [1 100] range in your case.

@above

I tried both. It didn’t work. Instead I used gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) and It worked. Though I still don’t understand what went wrong and what this fluLookAt did to solve the problem.

can you explain how this gluLookAt solved my problem and how is it different form what you said?

I have one more question. How do you draw a circle having one particular point as center. Whats the center of the circle in the above case? Can you guys explain me?

The code I gave worked, it was just not far enough to see the circle entirely:


void dodisplay(void)
{
const float DEG2RAD = 3.14159/180;
int radius = 2; 
glClearColor(0.0,0.0,0.0,1.0); 
glColor3f(0.2,0.2,0.75);
glMatrixMode (GL_MODELVIEW); 
glTranslatef(0.f, 0.f, -5.f);
glBegin(GL_LINE_LOOP);
for (int i=0; i< 360; i++)
{
float degInRad = i*DEG2RAD;
glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius);
}
glEnd();
glFlush();

}

What you did with gluLookAt here is exactly the same. The principle is the same, moving away to see better.

To move your shape use glTranslate.

Thank you very much. I’m actually trying to create a map environment in openGL. Consider (0,0) as the center and the total map is 50k50k50k. I want to create objects at different locations in the map and show different stuffs. I don’t know how to model the co-ordinates for this kinda environment in openGL. I think its all about setting this viewing co-ordinates and clipping window and matrix model. But I’m having problems in understand it. I’m trying to figure it out. Is it possible for you to explain it in simple words or some good reference will be useful to me.

Thanks,
Arun