PDA

View Full Version : OpenGL in Windows and Linux



neutrino17
05-25-2008, 03:36 AM
This simple program using GLUT, light and quadric object shows red sphere in Linux and nothing in Windows (MinGW).



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

GLUquadricObj* sphere;
GLdouble radius = 0.5;
GLint slices = 20;
GLint stacks = 20;
GLfloat sphere_coord[3] = {0.0,0.0,0.0};

void init(void)
{
GLfloat mat_specular[] = {1.0,1.0,1.0,1.0};
GLfloat mat_diffuse[] = {0.5,0.0,0.0,0.0};
GLfloat mat_shininess[] = {100.0};
GLfloat light0_position[] = {-1.0,1.0,-3.0,0.0};
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glEnable(GL_DEPTH_TEST);
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}

void drawSphere(float x, float y, float z, GLUquadricObj* sphere,
GLfloat radius, GLint slices, GLint stacks)
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(x, y, z);
gluQuadricDrawStyle(sphere, GLU_FILL);
gluSphere(sphere, radius, slices, stacks);
glPopMatrix();
}

void display(void)
{
int i;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glColor3f(0.3, 0.1, 0.7);
drawSphere(sphere_coord[0], sphere_coord[1], sphere_coord[2], sphere, radius, slices, stacks);
glFlush ();
}

void reshape (int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, 2.0, -1.0);
glTranslatef(0.0, 0.0, -1.0);
}

int main(int argc, char** argv)
{
int i;
sphere = gluNewQuadric();

glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}


In Linux I use gcc 4.1.3 and libglut3 3.7-25 while in Windows gcc 4.3.0 and libglut 3.7.6. Did I miss something?

Zengar
05-25-2008, 06:19 AM
Try doublebuffering (replace GLUT_SINGLE with GLUT_DOUBLE and glFlush with glutSwapBuffers)

-NiCo-
05-25-2008, 06:25 AM
When you're enabling depth testing you may want to add GLUT_DEPTH to the window initialization and clear the depth buffer before rendering.

neutrino17
05-25-2008, 08:30 AM
Yes, it was about depth testing. I added GL_DEPTH_BUFFER_BIT to glClear and it works now. Thanks.