Hey Guys. I’ve been searching the internet for a while now trying to find an answer. The following program runs fine on my laptop I take to school, and all the commands and graphics work. However when I am on my tower, it breaks, doing what the attached screenshot shows. Moving over the options occasionally flickers what is missing, and clicking sometimes flickers what should be on the screen.
When working correctly, the default shape is a circle, which can be drawn to the screen (default color is white) centered on where you click, and can also be dragged. Changing shape or color should not clear the screen, but the clear screen menu option should. F1 exits, as well as the menu exit, and UP and DOWN change the size. These still work, but the drawing to the screen doesn’t.
Tower is running an integrated ATI Radeon HD 3300 card, on Ubuntu 10.4. It is running the ATI driver (downloaded latest as of last night).
Laptop is running a NVIDIA GeForce 8400M GS , on Ubuntu 10.4. The driver is listed as current.
/*
* simple.c
* This program draws a white rectangle on a black background.
*/
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h> /* glut.h includes gl.h and glu.h*/
#endif
#include <math.h>
#include <time.h>
#include <stdlib.h>
GLsizei wh = 500, ww = 500;
GLfloat size = 15.0;
int rainbow = 0;
GLfloat mult = 0.01;
void (*fnPtr)(int x, int y) = NULL;
void drawCircle(int x, int y)
{
y = wh - y;
glBegin(GL_POLYGON);
int theta = 0;
while(theta <= 360)
{
if(rainbow > 0)
{
glColor3f(mult * (rand() % 100), mult * (rand() % 100), mult * (rand() % 100));
}
float radian = theta * (M_PI / 180.0);
glVertex2f(size * cos(radian) + x, size * sin(radian) + y);
theta += 12;
}
glEnd();
glFlush();
}
void drawBox(int x, int y)
{
/* clear window */
/* glClear(GL_COLOR_BUFFER_BIT); */
/* draw unit square polygon */
y = wh - y;
glBegin(GL_POLYGON);
if(rainbow > 0)
{
glColor3f(mult * (rand() % 100), mult * (rand() % 100), mult * (rand() % 100));
}
glVertex2f(x-size,y-size);
if(rainbow > 0)
{
glColor3f(mult * (rand() % 100), mult * (rand() % 100), mult * (rand() % 100));
}
glVertex2f(x-size,y+size);
if(rainbow > 0)
{
glColor3f(mult * (rand() % 100), mult * (rand() % 100), mult * (rand() % 100));
}
glVertex2f(x+size,y+size);
if(rainbow > 0)
{
glColor3f(mult * (rand() % 100), mult * (rand() % 100), mult * (rand() % 100));
}
glVertex2f(x+size,y-size);
glEnd();
/* flush GL buffers */
glFlush();
}
void init()
{
srand((unsigned)time(0));
glViewport(0,0,ww,wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,(GLdouble)ww,0.0,(GLdouble)wh,-1.0,1.0);
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
/* set clear color to black */
/* glClearColor (0.0, 0.0, 0.0, 0.0); */
/* set fill color to white */
/* glColor3f(1.0, 1.0, 1.0); */
/* set up standard orthogonal view with clipping */
/* box as cube of side 2 centered at origin */
/* This is default view and these statement could be removed */
/* glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); */
}
void display(void)
{
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
/* draw unit square polygon */
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
/* flush GL buffers */
glFlush();
}
void myReshape(GLsizei w, GLsizei h)
{
/* adjust clipping box */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* adjust viewport and clear */
glViewport(0,0,w,h);
glClearColor(0.0,0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
/* set global size for use by drawing routine */
ww = w;
wh = h;
}
void paint(int x, int y)
{
(*fnPtr)(x, y);
}
void mouse(int btn, int state, int x, int y)
{
/* if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) exit(0);*/
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if(fnPtr == NULL)
{
fnPtr = &drawCircle;
}
paint(x,y);
}
}
void special(int key, int x, int y)
{
if(key == GLUT_KEY_F1) exit(0);
if(key == GLUT_KEY_UP)
{
if(size > 400)
{
size = 400;
}
else
{
size++;
}
}
if(key == GLUT_KEY_DOWN)
{
if(size < 1)
{
size = 1;
}
else
{
size--;
}
}
}
void colorMenu(int value)
{
switch(value)
{
case 1:
/* Red */
glColor3f(1.0,0.0,0.0);
break;
case 2:
/* Green */
glColor3f(0.0,1.0,0.0);
break;
case 3:
/* Blue */
glColor3f(0.0,0.0,1.0);
break;
case 4:
/* Orange */
glColor3f(1.0,0.5,0.0);
break;
case 5:
/* Purple */
glColor3f(0.5,0.0,0.5);
break;
case 6:
/* Yellow */
glColor3f(1.0,1.0,0.0);
break;
case 7:
/* Cyan */
glColor3f(0.0,1.0,1.0);
break;
case 8:
/* Magenta */
glColor3f(1.0,0.0,1.0);
break;
case 9:
/* Gray */
glColor3f(0.5,0.5,0.5);
break;
case 10:
/* Rainbow */
rainbow = 1;
break;
default:
/* White */
glColor3f(1.0,1.0,1.0);
break;
}
}
void mymenu(int value)
{
switch(value)
{
case 1: /* Change Color */
break;
case 2:
fnPtr = &drawCircle;
break;
case 3:
fnPtr = &drawBox;
break;
case 4:
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
break;
case 5:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
/* Initialize mode and open a window in upper left corner of screen */
/* Window title is name of program (arg[0]) */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("simple");
/*glutDisplayFunc(display);*/
glutReshapeFunc(myReshape);
glutMouseFunc(mouse);
glutSpecialFunc(special);
init();
int sid = glutCreateMenu(colorMenu);
glutAddMenuEntry("Red", 1);
glutAddMenuEntry("Green", 2);
glutAddMenuEntry("Blue", 3);
glutAddMenuEntry("Orange", 4);
glutAddMenuEntry("Purple", 5);
glutAddMenuEntry("Yellow", 6);
glutAddMenuEntry("Cyan", 7);
glutAddMenuEntry("Magenta", 8);
glutAddMenuEntry("Gray", 9);
glutAddMenuEntry("Rainbow", 10);
glutAddMenuEntry("White", 9000);
int id = glutCreateMenu(mymenu);
glutAddSubMenu("Change Color", sid);
glutAddMenuEntry("Draw Circle", 2);
glutAddMenuEntry("Draw Box", 3);
glutAddMenuEntry("Clear Screen", 4);
glutAddMenuEntry("Exit", 5);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMotionFunc(paint);
glutMainLoop();
}