Advertisement
NithinBiliya

Untitled

Apr 21st, 2012
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. // OpenGL problem
  2.  
  3. #ifdef __APPLE__
  4. # include <GLUT/glut.h>
  5. #else
  6. # include <GL/glut.h>
  7. #endif
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. void passive(int,int);
  13. void reshape(int,int);
  14. void init(void);
  15. void display(void);
  16. void camera(void);
  17. void GetOGLPos(int,int);
  18.  
  19. int cursorX,cursorY,width,height,centerX=-4,centerY=-3;
  20.  
  21. GLint viewport[4];
  22. GLdouble modelview[16];
  23. GLdouble projection[16];
  24. GLfloat winX, winY, winZ;
  25. GLdouble posX, posY, posZ;
  26.  
  27. int main (int argc,char **argv) {
  28. glutInit (&argc,argv);
  29. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
  30. glutInitWindowSize(1364,689);
  31. glutInitWindowPosition(0,0);
  32. glutCreateWindow("Sample");
  33. init();
  34. glutDisplayFunc(display);
  35. glutIdleFunc(display);
  36. glutPassiveMotionFunc(passive);
  37. glutReshapeFunc(reshape);
  38. glutMainLoop();
  39. return 0;
  40. }
  41.  
  42. void display() {
  43. glClearColor (0.0,0.0,0.0,1.0);
  44. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  45. glLoadIdentity();
  46. camera();
  47.  
  48. glColor3f(1,0,0);
  49. glPushMatrix();
  50. glTranslatef(centerX,centerY,0);
  51. glutSolidSphere(5,50,50);
  52. glPopMatrix();
  53.  
  54. GetOGLPos(cursorX,cursorY);
  55.  
  56. glColor3f(1,1,1);
  57. glBegin(GL_LINES);
  58. glVertex3f(centerX,centerY,0);
  59. // glTranslatef(0,0,80);
  60. glVertex3f(posX,posY,posZ);
  61. // glTranslatef(0,0,-80);
  62. glEnd();
  63.  
  64. cout<<posX<<" "<<posY<<" "<<posZ<<"\n";
  65.  
  66. glutSwapBuffers();
  67. }
  68.  
  69. void GetOGLPos(int x, int y)
  70. {
  71. glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
  72. glGetDoublev( GL_PROJECTION_MATRIX, projection );
  73. glGetIntegerv( GL_VIEWPORT, viewport );
  74.  
  75. winX = (float)x;
  76. winY = (float)viewport[3] - (float)y;
  77. glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
  78.  
  79. gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
  80. }
  81.  
  82. void camera(void) {
  83. glRotatef(0.0,1.0,0.0,0.0);
  84. glRotatef(0.0,0.0,1.0,0.0);
  85. glTranslated(0,0,-20);
  86. }
  87.  
  88. void init(void) {
  89. glEnable (GL_DEPTH_TEST);
  90. glEnable (GL_BLEND);
  91. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  92. glEnable(GL_COLOR_MATERIAL);
  93. }
  94.  
  95. void reshape(int w, int h) {
  96. width=w; height=h;
  97. glViewport(0,0,(GLsizei)w,(GLsizei)h); //set the viewport to the current window specifications
  98. glMatrixMode(GL_PROJECTION); //set the matrix to projection
  99. glLoadIdentity();
  100. gluPerspective(60,(GLfloat)w/(GLfloat)h,1.0,100.0); //set the perspective (angle of sight, width, height, , depth)
  101. glMatrixMode(GL_MODELVIEW); //set the matrix back to model
  102. }
  103.  
  104. void passive(int x1,int y1) {
  105. cursorX=x1; cursorY=y1;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement