PDA

View Full Version : Clicking out of window



tigermain
10-23-2002, 12:44 AM
Hey peeps,

Im new to all this open gl stuff, and have to do it for my degree. I have prgrammed a simple application which draws dots lines etc etc in a window.

If I click out of the window (to the console) and back I loose the contents (as expected).

So the question is. How do I keep the contents without having to write the whole lot of points to arrays and redrawing them that way ???

Cheers. Im sure this has been mentioned before but I couldnt find the threads through search.

Gavin
10-23-2002, 12:57 AM
Why is it expected that you lose the contents pf the window!? When the window is damaged and redrawn it should redraw you contents, as it did the first time.

tigermain
10-23-2002, 03:18 AM
well it doesnt, I will post code

tigermain
10-23-2002, 03:20 AM
#include <windows.h>
#include <GL/gl.h>
#include <gl/glut.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int WindowWidth = 640; // declares width of window
int WindowHeight = 480; // declares height of window
int WindowLeft = 100; // sets the left position of window
int WindowTop = 150; // sets the top location of window
int MaxNoOfLines = 100; // max no of lines
int MaxNoOfPolygons = 7; //max no of polygons
int MaxNoOfVertices = 7; //max no of polygon vertices must be > 3
int MaxNoOfDots = 100; //max no of dots
int RotationPosition = 0; //defines which shape to draw

float RandomColour(void){ //randomises a no between 0-0.99 for colours
float RndColour;
RndColour = rand()%99;
RndColour = RndColour / 100;
return (RndColour);
}

void DrawDots(void){ //draw dot function
glBegin(GL_POINTS);
int i, NoOfDots;
NoOfDots = (rand()%MaxNoOfDots); // randomises no of dots to display
for (i=1; i<NoOfDots+1; i++) { // loops through random no of dots
glColor3f(RandomColour(),RandomColour(),RandomColo ur()); // randomises dot colour
glVertex2i((rand()%WindowWidth),(rand()%WindowHeig ht)); // draws dot random location between screen boundries
}
glEnd();
}

void DrawLines(void){
glBegin(GL_LINES);
int i, NoOfLines;
NoOfLines = (rand()%MaxNoOfLines);
for (i=1; i<NoOfLines+1; i++) {
glColor3f(RandomColour(),RandomColour(),RandomColo ur());
glVertex2i((rand()%WindowWidth),(rand()%WindowHeig ht));
glVertex2i((rand()%WindowWidth),(rand()%WindowHeig ht));
}
glEnd();
}

void DrawPolygons(void){
int i,h, NoOfPolygons, NoOfVertices;
NoOfPolygons = (rand()%MaxNoOfPolygons); // randomised no of polygons to draw
for (i=1; i<MaxNoOfPolygons+1; i++) {
glBegin(GL_POLYGON);
NoOfVertices = (3+rand()%(MaxNoOfVertices-3))
; // randomises no of vertices for each polygon
glColor3f(RandomColour(),RandomColour(),RandomColo ur());
for (h=1; h<MaxNoOfVertices+1; h++) {
glVertex2i((rand()%WindowWidth),(rand()%WindowHeig ht));
}
glEnd();
}

}

void myInit(void){
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}

void RotateShapes(void){
switch(RotationPosition)
{
case 0:
DrawDots(); //draw dots
break;
case 1:
DrawLines(); //draw lines
break;
case 2:
DrawPolygons(); //draw polygons
break;
default:
break; //do nothing
}
}

void myKeyboard(unsigned char key, int x, int y){
switch(key)
{
case 100: //check if key = d for dots
DrawDots();
break;
case 108: //check if key = l for lines
DrawLines();
break;
case 112: //check if key = p for polygons
DrawPolygons();
break;
case 113: //check if key = q to quit
exit(-1);
break;
case 32: //check if key = space
RotateShapes();
break;
default:
break; //do nothing
}
}

void myMouse(int button, int state, int x, int y){
//if (state==GLUT_DOWN) TakeKeyInput(1, 1 ,button);
if (state==GLUT_DOWN && button==GLUT_LEFT_BUTTON) RotateShapes();

if (state==GLUT_DOWN && button==GLUT_RIGHT_BUTTON) {
glClear(GL_COLOR_BUFFER_BIT);
if (RotationPosition<2) {
RotationPosition++;
} else {
RotationPosition = 0;
}
}
}

void myDisplay(void){
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}

void main(int argc, char** argv){
srand ( time(NULL) );
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(WindowWidth,WindowHeight);
glutInitWindowPosition(WindowLeft,WindowTop);
glutCreateWindow("Random Stuffage");
printf("[d] for dots\n[l] for lines\n[p] for polygons\n[leftmouse/space] for shape rotation\n[rightmouse] for clear and next shape\n\n");
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouse);
glutKeyboardFunc(myKeyboard);
myInit();
glutMainLoop();
}

coredump
10-23-2002, 03:28 AM
easy one...you have nothing in your display function. each time through your rendering loop you clear the buffer, the do a glFlush. you need to put all your rendering code into your display func. in the func with the switch, just set some boolen flags that you can check in your display func, then draw what you need.

b

[This message has been edited by coredump (edited 10-23-2002).]

tigermain
10-23-2002, 03:50 AM
but wont it make a difference with all the points being random ?

nexusone
10-23-2002, 04:24 AM
Hi, peep!

No not really, it is the nature of how openGL is rendered to the screen. The best and easy way would be to use arrays to store the values.

You may could do work around by, removing the glClear in myDisplay. Maybe have it called ony from a key press when you want to clear the buffer.


Originally posted by tigermain:
Hey peeps,

Im new to all this open gl stuff, and have to do it for my degree. I have prgrammed a simple application which draws dots lines etc etc in a window.

If I click out of the window (to the console) and back I loose the contents (as expected).

So the question is. How do I keep the contents without having to write the whole lot of points to arrays and redrawing them that way ???

Cheers. Im sure this has been mentioned before but I couldnt find the threads through search.