PDA

View Full Version : Unreliable window



obrien
08-05-2009, 06:14 AM
Hi everyone, this is my first post, I am new to OpenGL. I am working on Ubuntu so I am using GLUT for windowing. I am experimenting with a simple program. I am compiling and running the program from terminal. My problem is that sometimes when I run the program the window is partially obscured by the terminal window on opening. Also, when I hit Escape the window should close but this only happens sometimes. Any help would be most appreciated.

#include <iostream>
#include <stdlib.h> //Needed for "exit" function
#include <GL/glut.h> //OpenGL header files

using namespace std;


//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) //The key that was pressed & the current mouse coordinates
{
switch (key)
{
case 27: //Escape key

exit(0); //Exit the program
}
}


//Initializes 3D rendering
void initRendering()
{

glDisable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
}


//Called when the window is resized
void handleResize(int w, int h)
{
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective

//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //The near z clipping coordinate
200.0); //The far z clipping coordinate
}


//Draws the 3D scene
void drawScene()
{
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT);

const int XSize = 1024, YSize = 768; //These five lines set up a 2D projection as opposed to 3D, makes (0, 0) top-left corner
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, XSize, YSize, 0, 0, 1);
glMatrixMode (GL_MODELVIEW);


float num1 = 20.0, num2 = 620.0, num3 = 460.0;


glBegin(GL_POLYGON); //Begin polygon coordinates
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(num1, num1);
glVertex2f(num2, num1);
glVertex2f(num2, num3);
glVertex2f(num1, num3);
glEnd(); //End polygon coordinates

glBegin(GL_POLYGON); //Begin polygon coordinates
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(20.0f, 460.0f);
glVertex2f(20.0f, 380.0f);
glVertex2f(100.0f, 380.0f);
glVertex2f(100.0f, 460.0f);
glEnd(); //End polygon coordinates


glutSwapBuffers(); //Send the 3D scene to the screen
}




int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1024, 768); //Set the window size

//Create the window
glutCreateWindow("Field");
initRendering(); //Initialize rendering

//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}

Dark Photon
08-10-2009, 04:39 PM
...using GLUT for windowing. ...My problem is that sometimes when I run the program the window is partially obscured by the terminal window on opening. Also, when I hit Escape the window should close but this only happens sometimes. Any help would be most appreciated.
Rock solid repeatable behavior here on NVidia drivers. No problems, and no memory errors when I run through valgrind.

What video card are you using and what driver version (see below)? If not the latest, try an upgrade. Also, ensure that you're running hardware OpenGL by running glxinfo from that same terminal and verify that you see something like this in there ("not" Mesa):

> glxinfo | grep ^OpenGL
OpenGL vendor string: NVIDIA Corporation
OpenGL renderer string: GeForce 8400 GS/PCI/SSE2
OpenGL version string: 3.0.0 NVIDIA 185.18.14
OpenGL extensions: