1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
float ratio;
#include <iostream.h>
static void setup_opengl( int width, int height )
{
ratio = (float) width / (float) height;
glShadeModel( GL_SMOOTH );
glPolygonMode(GL_FRONT,GL_FILL);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
gluPerspective( 60.0, ratio, 1, 10000.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
void renderScene(void)
{glReadBuffer(GL_FRONT);
glClearColor( 0.0f, 0.0f, 0.0f, 1 );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.,0.,50,0.,0.,0.,0.,1.,0.);
glTranslatef(-10.,0.,0.);
glutSolidCube(10);
unsigned char *pixel=new unsigned char[18];
*pixel=20;
*(pixel+1)=20;
*(pixel+2)=20;
*(pixel+3)=20;
glutSwapBuffers();
}
void MouseFunc(int button, int state,int x, int y){
glClearColor( 1.0f, 0.0f, 0.0f, 1 );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.,0.,50,0.,0.,0.,0.,1.,0.);
glTranslatef(-10.,0.,0.);
glutSolidCube(10);
cerr<<"SELECTION\n";
unsigned char *pixel=new unsigned char[18];
*pixel=20;
*(pixel+1)=20;
*(pixel+2)=20;
*(pixel+3)=20;
glFinish();
glReadBuffer(GL_BACK);
glReadPixels(x,480-y,2,2,GL_RGB,GL_UNSIGNED_BYTE,pixel);
glFinish();
cerr<<x<<" "<<480-y<<" pix: "<<(int)(*(pixel++))<<" "<<(int)(*(pixel++))<<" "<<(int)(*(pixel++))<<endl;
}
int main (int argc, char * * argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,350);
glutInitWindowSize(640,480);
glutCreateWindow("COUCOU");
setup_opengl(640,480);
glutDisplayFunc(renderScene);
glutMouseFunc(MouseFunc);
glutMainLoop();
return 0;
} |
Partager