Advertisement
Guest User

OpenGL Reflection Render Issue

a guest
Jul 18th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. #include <windows.h>
  2. #include <SFML/Graphics.hpp>
  3. #include <math.h>
  4.  
  5. #include <glew.h>
  6. #include <gl/gl.h>
  7. #include <gl/glu.h>
  8. #include <glut.h>
  9.  
  10. using namespace std;
  11.  
  12. class Scene {
  13. public:
  14. void resize( int w, int h ) {
  15. glViewport( 0, 0, w, h );
  16. glMatrixMode( GL_PROJECTION );
  17. glLoadIdentity();
  18. gluPerspective( 50, (GLdouble)w/(GLdouble)h, 1, 5000.0 );
  19. glMatrixMode( GL_MODELVIEW );
  20. }
  21. };
  22. void DrawFloor()
  23. {
  24. glBegin(GL_QUADS);
  25. glNormal3f(0, 1, 0);
  26. glVertex3f(-2, 0, 2);
  27. glVertex3f(-2, 0, -2);
  28. glVertex3f(2, 0, -2);
  29. glVertex3f(2, 0, 2);
  30. glEnd();
  31. }
  32. void DrawObject()
  33. {
  34. glutSolidSphere(1,32,16);
  35. }
  36. int main() {
  37. ///Setup the scene, materials, lighting
  38. sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test");
  39. Scene scene;
  40. scene.resize(window.getSize().x,window.getSize().y);
  41. glewInit();
  42.  
  43. GLfloat zoom = -7.0f; // Depth Into The Screen
  44. GLfloat height = 2.0f; // Height Of Ball From Floor
  45.  
  46. static GLfloat LightAmb[] = {0.7f, 0.7f, 0.7f, 1.0f};
  47. static GLfloat LightDif[] = {1.0f, 1.0f, 1.0f, 1.0f};
  48. static GLfloat LightPos[] = {4.0f, 4.0f, 6.0f, 1.0f};
  49.  
  50. glShadeModel(GL_SMOOTH);
  51. glClearColor(0.2f, 0.5f, 1.0f, 1.0f);
  52. glClearDepth(1.0f);
  53. glClearStencil(0);
  54. glEnable(GL_DEPTH_TEST);
  55. glDepthFunc(GL_LEQUAL);
  56. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  57. glEnable(GL_TEXTURE_2D);
  58.  
  59. glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmb);
  60. glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDif);
  61. glLightfv(GL_LIGHT0, GL_POSITION, LightPos);
  62.  
  63. glEnable(GL_LIGHT0);
  64. glEnable(GL_LIGHTING);
  65.  
  66. double XP = 5, YP = 5, ZP = 5;
  67.  
  68. ///Start loop
  69. while( window.isOpen() ) {
  70. sf::Event event;
  71. while( window.pollEvent( event ) ) {
  72. if(event.type == sf::Event::Closed) window.close();
  73. }
  74.  
  75. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  76. glMatrixMode(GL_PROJECTION);
  77. glLoadIdentity();
  78. scene.resize(window.getSize().x,window.getSize().y);
  79. glMatrixMode(GL_MODELVIEW);
  80. glLoadIdentity();
  81. glClearColor(0.0,0.0,0.0,0.0);
  82. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  83. gluLookAt(XP, YP, ZP, 0, 0, 0, 0, 1, 0);
  84.  
  85. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) XP += 0.1;
  86. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) XP -= 0.1;
  87. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) ZP += 0.1;
  88. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) ZP -= 0.1;
  89. if(sf::Keyboard::isKeyPressed(sf::Keyboard::T)) YP += 0.1;
  90. if(sf::Keyboard::isKeyPressed(sf::Keyboard::G)) YP -= 0.1;
  91.  
  92. // Clip Plane Equations
  93. double eqr[] = {0.0f,-1.0f, 0.0f, 0.0f}; // Plane Equation To Use For The Reflected Objects
  94.  
  95. glColorMask(0,0,0,0); // Set Color Mask
  96. glEnable(GL_STENCIL_TEST); // Enable Stencil Buffer For "marking" The Floor
  97. glStencilFunc(GL_ALWAYS, 1, 1); // Always Passes, 1 Bit Plane, 1 As Mask
  98. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); // We Set The Stencil Buffer To 1 Where We Draw Any Polygon
  99. // Keep If Test Fails, Keep If Test Passes But Buffer Test Fails
  100. // Replace If Test Passes
  101. glDisable(GL_DEPTH_TEST); // Disable Depth Testing
  102. DrawFloor(); // Draw The Floor (Draws To The Stencil Buffer)
  103. // We Only Want To Mark It In The Stencil Buffer
  104. glEnable(GL_DEPTH_TEST); // Enable Depth Testing
  105. glColorMask(1,1,1,1); // Set Color Mask to TRUE, TRUE, TRUE, TRUE
  106. glStencilFunc(GL_EQUAL, 1, 1); // We Draw Only Where The Stencil Is 1
  107. // (I.E. Where The Floor Was Drawn)
  108. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // Don't Change The Stencil Buffer
  109. glEnable(GL_CLIP_PLANE0); // Enable Clip Plane For Removing Artifacts
  110. // (When The Object Crosses The Floor)
  111. glClipPlane(GL_CLIP_PLANE0, eqr); // Equation For Reflected Objects
  112. glPushMatrix(); // Push The Matrix Onto The Stack
  113. glScalef(1.0f, -1.0f, 1.0f); // Mirror Y Axis
  114. glLightfv(GL_LIGHT0, GL_POSITION, LightPos); // Set Up Light0
  115. glTranslatef(0.0f, height, 0.0f); // Position The Object
  116. DrawObject(); // Draw The Sphere (Reflection)
  117. glPopMatrix(); // Pop The Matrix Off The Stack
  118. glDisable(GL_CLIP_PLANE0); // Disable Clip Plane For Drawing The Floor
  119. glDisable(GL_STENCIL_TEST); // We Don't Need The Stencil Buffer Any More (Disable)
  120.  
  121. glLightfv(GL_LIGHT0, GL_POSITION, LightPos); // Set Up Light0 Position
  122. glEnable(GL_BLEND); // Enable Blending (Otherwise The Reflected Object Wont Show)
  123. glDisable(GL_LIGHTING); // Since We Use Blending, We Disable Lighting
  124. glColor4f(1.0f, 1.0f, 1.0f, 0.8f); // Set Color To White With 80% Alpha
  125. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Blending Based On Source Alpha And 1 Minus Dest Alpha
  126. DrawFloor(); // Draw The Floor To The Screen
  127. glEnable(GL_LIGHTING); // Enable Lighting
  128. glDisable(GL_BLEND); // Disable Blending
  129. glTranslatef(0.0f, height, 0.0f); // Position The Ball At Proper Height
  130. DrawObject();
  131.  
  132. window.display();
  133. }
  134. return 1;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement