PDA

View Full Version : How to quit opengl by a key?



IF_EXIST
01-15-2010, 07:34 AM
About close a opengl program.

I just know close it by mouse --> click the [X]

How to close it by a function ? Or what Function can Break
Opengl window ?

A example look like this BashScript
(in ubuntu9.10)



#!/bin/bash
echo press any key to exit.
read -n1 REPLY
exit 0


I want to a opengl program that is "Press any key to exit."

At finally, excuse me , my english grammar is too bad ,
so I'am also hope you can tell me the error of my grammar。
Thanks. ^_^

Dark Photon
01-15-2010, 07:45 AM
I just know close it by mouse --> click the [X]

How to close it by a function ? Or what Function can Break
Opengl window ?
So I gather from your post that you want to hook up a key so that it will exit your running OpenGL program.

You need to hook in an event handler to your GL program to handle key presses. How you do this depends on what you're using for event processing. Tells us what you are using?

If you are using GLUT, then use glutKeyboardFunc to hook in that handler function. An example glutKeyboardFunc handler looks like this:


void keyboardCB( unsigned char key, int x, int y )
{
switch ( key )
{
case 27: // Escape key
glutDestroyWindow ( Win.id );
exit (0);
break;
}
glutPostRedisplay();
}

If you're using raw X11 for event processing, let me know -- I can post a handler for you. If other, I'm sure other folks can help out.

IF_EXIST
01-16-2010, 04:22 AM
Thanks,Dark Photon.
I'am using glut.h,


Now I have use the "glutKeyboardFunc(hitkey);" ,
and have wirted this function -


hitkey( unsigned char key, int x, int y )
{
switch ( key )
{
case 27: // Escape key
glutDestroyWindow ( Win.id );
exit (0);
break;
}
glutPostRedisplay();
}




But the command tells me :
amateur@amateur-desktop:~/c$ g++ -o box -lglut Box.c
Box.c: In function ‘void hitkey(unsigned char, int, int)’:
Box.c:30: error: ‘Win’ was not declared in this scope

How to do now? I think it's not far from destination.

IF_EXIST
01-16-2010, 05:19 AM
I think I have too less knowlege of opengl,
so I decided study some days,
than come here again!

^_^
Thank you for your reference ! See you!

IF_EXIST
01-16-2010, 07:44 AM
This is a initial code,
but it's loop and loop (a bad code),I'm try to finish it.


#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
#define xpix 500

void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
gluOrtho2D(0.0,xpix,0.0,xpix);
}

void display(void)
{
int ya=0,yb=10,i,ii,big,arr[xpix];
int add=1;
big=(sqrt(2*yb/10)-sqrt(2*ya/10))*1000;
float x=200,y=490;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(20);

for (i=0;i<xpix;i++) {
arr[i]=(sqrt(2*(i+1)*10)-sqrt(2*i*10))*500;
}

ii=0;
i=0;
while (1)
{
if (ii<=arr[i]){
ii+=1;
} else {
ii=0;
y-=add;
i+=add;
if (y<10) {add=-1;} else {if (y>490) add=1;}
}

glBegin(GL_POINTS);
glColor3f(0.0,0.0,0.0);
glVertex2f(x,y+10*add);
glColor3f(0.5,0.5,0.0);
glVertex2f(x,y);
glEnd();
glFlush();
}
}

int main(int argc,char *argv[])
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(xpix,xpix);
glutInitWindowPosition(100,100);
glutCreateWindow("haha");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Dark Photon
01-16-2010, 04:55 PM
I'am using glut.h,
...

hitkey( unsigned char key, int x, int y )
{
... glutDestroyWindow ( Win.id );
But...
$ g++ -o box -lglut Box.c
Box.c: In function ‘void hitkey(unsigned char, int, int)’:
Box.c:30: error: ‘Win’ was not declared in this scope

How to do now? I think it's not far from destination.
Subtitute the ID you got from glutCreateWindow() for Win.id. I just happened to store it in a little state structure as Win.id:


Win.id = glutCreateWindow( "GLUT Test Program" );