Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // OpenGL problem
- #ifdef __APPLE__
- # include <GLUT/glut.h>
- #else
- # include <GL/glut.h>
- #endif
- #include <iostream>
- using namespace std;
- void passive(int,int);
- void reshape(int,int);
- void init(void);
- void display(void);
- void camera(void);
- void GetOGLPos(int,int);
- int cursorX,cursorY,width,height,centerX=-4,centerY=-3;
- GLint viewport[4];
- GLdouble modelview[16];
- GLdouble projection[16];
- GLfloat winX, winY, winZ;
- GLdouble posX, posY, posZ;
- int main (int argc,char **argv) {
- glutInit (&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
- glutInitWindowSize(1364,689);
- glutInitWindowPosition(0,0);
- glutCreateWindow("Sample");
- init();
- glutDisplayFunc(display);
- glutIdleFunc(display);
- glutPassiveMotionFunc(passive);
- glutReshapeFunc(reshape);
- glutMainLoop();
- return 0;
- }
- void display() {
- glClearColor (0.0,0.0,0.0,1.0);
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- camera();
- glColor3f(1,0,0);
- glPushMatrix();
- glTranslatef(centerX,centerY,0);
- glutSolidSphere(5,50,50);
- glPopMatrix();
- GetOGLPos(cursorX,cursorY);
- glColor3f(1,1,1);
- glBegin(GL_LINES);
- glVertex3f(centerX,centerY,0);
- // glTranslatef(0,0,80);
- glVertex3f(posX,posY,posZ);
- // glTranslatef(0,0,-80);
- glEnd();
- cout<<posX<<" "<<posY<<" "<<posZ<<"\n";
- glutSwapBuffers();
- }
- void GetOGLPos(int x, int y)
- {
- glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
- glGetDoublev( GL_PROJECTION_MATRIX, projection );
- glGetIntegerv( GL_VIEWPORT, viewport );
- winX = (float)x;
- winY = (float)viewport[3] - (float)y;
- glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
- gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
- }
- void camera(void) {
- glRotatef(0.0,1.0,0.0,0.0);
- glRotatef(0.0,0.0,1.0,0.0);
- glTranslated(0,0,-20);
- }
- void init(void) {
- glEnable (GL_DEPTH_TEST);
- glEnable (GL_BLEND);
- glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glEnable(GL_COLOR_MATERIAL);
- }
- void reshape(int w, int h) {
- width=w; height=h;
- glViewport(0,0,(GLsizei)w,(GLsizei)h); //set the viewport to the current window specifications
- glMatrixMode(GL_PROJECTION); //set the matrix to projection
- glLoadIdentity();
- gluPerspective(60,(GLfloat)w/(GLfloat)h,1.0,100.0); //set the perspective (angle of sight, width, height, , depth)
- glMatrixMode(GL_MODELVIEW); //set the matrix back to model
- }
- void passive(int x1,int y1) {
- cursorX=x1; cursorY=y1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement