Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <SFML/Graphics.hpp>
- #include <math.h>
- #include <glew.h>
- #include <gl/gl.h>
- #include <gl/glu.h>
- #include <glut.h>
- using namespace std;
- class Scene {
- public:
- void resize( int w, int h ) {
- glViewport( 0, 0, w, h );
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 50, (GLdouble)w/(GLdouble)h, 1, 5000.0 );
- glMatrixMode( GL_MODELVIEW );
- }
- };
- void DrawFloor()
- {
- glBegin(GL_QUADS);
- glNormal3f(0, 1, 0);
- glVertex3f(-2, 0, 2);
- glVertex3f(-2, 0, -2);
- glVertex3f(2, 0, -2);
- glVertex3f(2, 0, 2);
- glEnd();
- }
- void DrawObject()
- {
- glutSolidSphere(1,32,16);
- }
- int main() {
- ///Setup the scene, materials, lighting
- sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test");
- Scene scene;
- scene.resize(window.getSize().x,window.getSize().y);
- glewInit();
- GLfloat zoom = -7.0f; // Depth Into The Screen
- GLfloat height = 2.0f; // Height Of Ball From Floor
- static GLfloat LightAmb[] = {0.7f, 0.7f, 0.7f, 1.0f};
- static GLfloat LightDif[] = {1.0f, 1.0f, 1.0f, 1.0f};
- static GLfloat LightPos[] = {4.0f, 4.0f, 6.0f, 1.0f};
- glShadeModel(GL_SMOOTH);
- glClearColor(0.2f, 0.5f, 1.0f, 1.0f);
- glClearDepth(1.0f);
- glClearStencil(0);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- glEnable(GL_TEXTURE_2D);
- glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmb);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDif);
- glLightfv(GL_LIGHT0, GL_POSITION, LightPos);
- glEnable(GL_LIGHT0);
- glEnable(GL_LIGHTING);
- double XP = 5, YP = 5, ZP = 5;
- ///Start loop
- while( window.isOpen() ) {
- sf::Event event;
- while( window.pollEvent( event ) ) {
- if(event.type == sf::Event::Closed) window.close();
- }
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- scene.resize(window.getSize().x,window.getSize().y);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glClearColor(0.0,0.0,0.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
- gluLookAt(XP, YP, ZP, 0, 0, 0, 0, 1, 0);
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) XP += 0.1;
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) XP -= 0.1;
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) ZP += 0.1;
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) ZP -= 0.1;
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::T)) YP += 0.1;
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::G)) YP -= 0.1;
- // Clip Plane Equations
- double eqr[] = {0.0f,-1.0f, 0.0f, 0.0f}; // Plane Equation To Use For The Reflected Objects
- glColorMask(0,0,0,0); // Set Color Mask
- glEnable(GL_STENCIL_TEST); // Enable Stencil Buffer For "marking" The Floor
- glStencilFunc(GL_ALWAYS, 1, 1); // Always Passes, 1 Bit Plane, 1 As Mask
- glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); // We Set The Stencil Buffer To 1 Where We Draw Any Polygon
- // Keep If Test Fails, Keep If Test Passes But Buffer Test Fails
- // Replace If Test Passes
- glDisable(GL_DEPTH_TEST); // Disable Depth Testing
- DrawFloor(); // Draw The Floor (Draws To The Stencil Buffer)
- // We Only Want To Mark It In The Stencil Buffer
- glEnable(GL_DEPTH_TEST); // Enable Depth Testing
- glColorMask(1,1,1,1); // Set Color Mask to TRUE, TRUE, TRUE, TRUE
- glStencilFunc(GL_EQUAL, 1, 1); // We Draw Only Where The Stencil Is 1
- // (I.E. Where The Floor Was Drawn)
- glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // Don't Change The Stencil Buffer
- glEnable(GL_CLIP_PLANE0); // Enable Clip Plane For Removing Artifacts
- // (When The Object Crosses The Floor)
- glClipPlane(GL_CLIP_PLANE0, eqr); // Equation For Reflected Objects
- glPushMatrix(); // Push The Matrix Onto The Stack
- glScalef(1.0f, -1.0f, 1.0f); // Mirror Y Axis
- glLightfv(GL_LIGHT0, GL_POSITION, LightPos); // Set Up Light0
- glTranslatef(0.0f, height, 0.0f); // Position The Object
- DrawObject(); // Draw The Sphere (Reflection)
- glPopMatrix(); // Pop The Matrix Off The Stack
- glDisable(GL_CLIP_PLANE0); // Disable Clip Plane For Drawing The Floor
- glDisable(GL_STENCIL_TEST); // We Don't Need The Stencil Buffer Any More (Disable)
- glLightfv(GL_LIGHT0, GL_POSITION, LightPos); // Set Up Light0 Position
- glEnable(GL_BLEND); // Enable Blending (Otherwise The Reflected Object Wont Show)
- glDisable(GL_LIGHTING); // Since We Use Blending, We Disable Lighting
- glColor4f(1.0f, 1.0f, 1.0f, 0.8f); // Set Color To White With 80% Alpha
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Blending Based On Source Alpha And 1 Minus Dest Alpha
- DrawFloor(); // Draw The Floor To The Screen
- glEnable(GL_LIGHTING); // Enable Lighting
- glDisable(GL_BLEND); // Disable Blending
- glTranslatef(0.0f, height, 0.0f); // Position The Ball At Proper Height
- DrawObject();
- window.display();
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement