So I’ve looked at some tutorials… copied some code and came up with a small c program to display a set of rotating cubes.
it compiles and runs exactly the way i expect on both linux and windows machines, my desktop pc dual boots ubuntu and windowsXp and the code works on both, however i moved the code to my laptop running ubuntu and now i get a seg fault. i narrowed the problem down to a single line of code that when commented out runs perfectly. my question is why do i need to comment out this line on my laptop but not any other system?
the line that gives the seg fault is : glEnable(GL_DEPTH_TEST)
my code is :
/*
* Copyright (c) 1993-1997, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
* OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
*/
/*
* Test.c
* This is a simple, introductory OpenGL program.
*/
#include <GL/glut.h>
#include <stdio.h>
#include <unistd.h>
typedef int BOOL;
#define TRUE 1
#define FALSE 0
static GLfloat angle = 0.0;
static BOOL Button1Down = FALSE;
static GLfloat lx=0;
static GLfloat ly=0;
static int xclick=0;
static int yclick=0;
static int frame=0;
static int time=0;
static int timebase=0;
static GLfloat s=0;
void MouseButton(int button, int state, int x, int y)
{
/* Respond to mouse button presses.
If button1 pressed, mark this state so we know in motion function.*/
if (button == GLUT_LEFT_BUTTON)
{
Button1Down = (state == GLUT_DOWN) ? TRUE : FALSE;
xclick = x;
yclick = y;
}
}
void MouseMotion(int x, int y)
{
/* If button1 pressed....*/
if (Button1Down)
{
if (x>xclick){
ly=ly+(xclick-x)*.5;
xclick=x;}
if (x<xclick){
ly=ly+(xclick-x)*.5;
xclick=x;}
if (y>yclick){
lx=lx+(yclick-y)*.5;
yclick=y;}
if (y<yclick){
lx=lx+(yclick-y)*.5;
yclick=y;}
glutPostRedisplay();
}
}
void drawscene(){
glBegin(GL_POLYGON);
glVertex3f (-1, -1, 0);
glVertex3f (1, -1, 0);
glVertex3f (1, -.9, 0);
glVertex3f (-1, -.9, 0);
glEnd();
}
void display(void)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 0, 0, 0, -1, 0, 1, 0);
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor3f(0,0,1);
glRotated(lx,1,0,0);
glRotated(ly,0,1,0);
glRotated(angle,0,0,1);
glRotated((angle+5),1,1,0);
glTranslated(0,0,0);
glutSolidCube(.5);
glColor3f(0,1,0);
glutWireCube(.5);
glTranslated(.5,0,0);
glColor3f(0,0,1);
glutSolidCube(.5);
glColor3f(0,1,0);
glutWireCube(.5);
glTranslated(-1,0,0);
glColor3f(0,0,1);
glutSolidCube(.5);
glColor3f(0,1,0);
glutWireCube(.5);
glTranslated(.5,.5,0);
glColor3f(0,0,1);
glutSolidCube(.5);
glColor3f(0,1,0);
glutWireCube(.5);
glTranslated(0,-.5,.5);
glColor3f(0,0,1);
glutSolidCube(.5);
glColor3f(0,1,0);
glutWireCube(.5);
glPopMatrix();
glPushMatrix();
glColor3f(1,1,1);
glTranslated(0,0,0);
drawscene();
glPopMatrix();
glEnd();
/* don't wait!
* start processing buffered OpenGL routines
*/
glFlush();
glutSwapBuffers();
}
void animate (void)
{
angle+=0.1;
if (angle>360.00f){
angle = 0.0;}
frame++;
time=glutGet(GLUT_ELAPSED_TIME);
if (time - timebase > 1000) {
s=(frame*1000.0/(time-timebase));
timebase = time;
frame = 0;
}
printf("FPS %f", s);
printf("%s", " ");
printf("angle %f
", angle);
glutPostRedisplay();
}
void init (void)
{
/* select clearing color */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DOUBLE);
/* glEnable(GL_DEPTH_TEST); --- this line works on all my machines except my laptop! WTF!!!*/
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Tims 3D - TEST");
init ();
glutDisplayFunc(display);
glutMouseFunc (MouseButton);
glutMotionFunc (MouseMotion);
glutIdleFunc (animate);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}