Advertisement
NithinBiliya

OpenGL

Apr 16th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 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.  
  9. void passive(int,int);
  10. void reshape(int,int);
  11. void init(void);
  12. void display(void);
  13. void camera(void);
  14.  
  15. int cursorX,cursorY,width,height;
  16. double centerX,centerY,centerZ;
  17. //GLfloat modelviewMatrix[16],projectionMatrix[16];
  18. GLdouble modelviewMatrix[16],projectionMatrix[16];
  19. GLint viewport[4];
  20.  
  21. int main (int argc,char **argv) {
  22. glutInit (&argc,argv);
  23. glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
  24. glutInitWindowSize(1364,689);
  25. glutInitWindowPosition(0,0);
  26. glutCreateWindow("Sample");
  27. init();
  28. glutDisplayFunc(display);
  29. glutIdleFunc(display);
  30. glutPassiveMotionFunc(passive);
  31. glutReshapeFunc(reshape);
  32. glutMainLoop();
  33. return 0;
  34. }
  35.  
  36. void display() {
  37. glClearColor (0.0,0.0,0.0,1.0);
  38. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  39.  
  40. // Render 3D content
  41. glMatrixMode(GL_PROJECTION);
  42. glLoadIdentity();
  43. gluPerspective(60,(GLfloat)width/(GLfloat)height,1.0,100.0); // create 3D perspective projection matrix
  44. glMatrixMode(GL_MODELVIEW);
  45. glPushMatrix();
  46. camera();
  47. glTranslatef(-6,-2,0);
  48. glColor3f(1,0,0);
  49. glutSolidSphere(5,50,50);
  50. glPopMatrix();
  51.  
  52. glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix);
  53. glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
  54. glGetIntegerv(GL_VIEWPORT, viewport);
  55. // get 3D coordinates based on window coordinates
  56. gluProject(-6, -2, 0, modelviewMatrix, projectionMatrix, viewport, &centerX, &centerY, &centerZ);
  57.  
  58. // Render 2D content
  59. glMatrixMode(GL_PROJECTION);
  60. glLoadIdentity();
  61. gluOrtho2D(0, width,height, 0); // create 2D orthographic projection matrix
  62. glMatrixMode(GL_MODELVIEW);
  63. glColor3f(1,1,1);
  64. glBegin(GL_LINES);
  65. glVertex2f( centerX,centerY ); // coordinate of center of the sphere in orthographic projection
  66. glVertex2f( cursorX,cursorY );
  67. glEnd();
  68.  
  69. glutSwapBuffers();
  70. }
  71.  
  72. void camera(void) {
  73. glRotatef(0.0,1.0,0.0,0.0);
  74. glRotatef(0.0,0.0,1.0,0.0);
  75. glTranslated(0,0,-20);
  76. }
  77.  
  78. void init(void) {
  79. glEnable (GL_DEPTH_TEST);
  80. glEnable (GL_BLEND);
  81. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  82. glEnable(GL_COLOR_MATERIAL);
  83. }
  84.  
  85. void reshape(int w, int h) {
  86. width=w; height=h;
  87. }
  88.  
  89. void passive(int x1,int y1) {
  90. cursorX=x1; cursorY=y1;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement