Advertisement
x89codered89x

Lighting

Mar 13th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. //download - http://www.mediafire.com/?1fl2lt5ctxwtdra
  2.  
  3. ////////////////////////////////////////////////////////////
  4. // Headers
  5. ////////////////////////////////////////////////////////////
  6. #include <SFML/Window.hpp>
  7.  
  8.  
  9. ////////////////////////////////////////////////////////////
  10. /// Entry point of application
  11. ///
  12. /// \return Application exit code
  13. ///
  14. ////////////////////////////////////////////////////////////
  15. int main()
  16. {
  17.     // Create the main window
  18.     sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");
  19.  
  20.     // Create a clock for measuring time elapsed
  21.     sf::Clock Clock;
  22.  
  23.     // Set color and depth clear value
  24.     glClearDepth(1.f);
  25.     glClearColor(0.f, 0.f, 0.f, 0.f);
  26.  
  27.     // Enable Z-buffer read and write
  28.     glEnable(GL_DEPTH_TEST);
  29.     glEnable(GL_COLOR_MATERIAL);
  30.     glEnable(GL_LIGHTING); //Enable lighting
  31.     glEnable(GL_LIGHT0); //Enable light #0
  32.     glEnable(GL_LIGHT1); //Enable light #1
  33.     glEnable(GL_NORMALIZE); //Automatically normalize normals
  34.     //glShadeModel(GL_SMOOTH); //Enable smooth shading
  35.     glDepthMask(GL_TRUE);
  36.  
  37.     // Setup a perspective projection
  38.     glMatrixMode(GL_PROJECTION);
  39.     glLoadIdentity();
  40.     gluPerspective(90, 4.0/3.0, 1, 500);
  41.  
  42.     // Start game loop
  43.     while (App.IsOpened())
  44.     {
  45.         // Process events
  46.         sf::Event Event;
  47.         while (App.GetEvent(Event))
  48.         {
  49.             // Close window : exit
  50.             if (Event.Type == sf::Event::Closed)
  51.                 App.Close();
  52.  
  53.             // Escape key : exit
  54.             if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
  55.                 App.Close();
  56.  
  57.             // Resize event : adjust viewport
  58.             if (Event.Type == sf::Event::Resized)
  59.             {
  60.                 glViewport(0, 0, Event.Size.Width, Event.Size.Height);
  61.                 glMatrixMode(GL_PROJECTION);
  62.                 glLoadIdentity();
  63.                 gluPerspective(90, (double) Event.Size.Width / (double) Event.Size.Height, 1, 500);
  64.             }
  65.         }
  66.  
  67.         // Set the active window before using OpenGL commands
  68.         // It's useless here because active window is always the same,
  69.         // but don't forget it if you use multiple windows or controls
  70.         App.SetActive();
  71.  
  72.         // Clear color and depth buffer
  73.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  74.  
  75.         // Apply some transformations
  76.         glMatrixMode(GL_MODELVIEW);
  77.         glLoadIdentity();
  78.  
  79. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  80.  
  81.     glMatrixMode(GL_MODELVIEW);
  82.     glLoadIdentity();
  83.  
  84.     glTranslatef(0.0f, 0.0f, -5.0f);
  85.  
  86.     //Add ambient light
  87.     GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
  88.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
  89.  
  90.     //Add positioned light
  91.     GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
  92.     GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
  93.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
  94.     glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
  95.  
  96.     //Add directed light
  97.     GLfloat lightColor1[] = {2.0f, 0.0f, 0.0f, 0.0f}; //Color (0.5, 0.2, 0.2)
  98.     //Coming from the direction (-1, 0.5, 0.5)
  99.     GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, -0.0f};
  100.     glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
  101.     glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
  102.  
  103.     glRotatef(Clock.GetElapsedTime() * 100, 1.0f, 0.0f, 0.0f);
  104.     glRotatef(Clock.GetElapsedTime() * 100, 0.0f, 1.0f, 0.0f);
  105.     glRotatef(Clock.GetElapsedTime() * 100, 0.0f, 0.0f, 1.0f);
  106.     glColor3f(1.0f, 1.0f, 1.0f);
  107.     glBegin(GL_QUADS);
  108.  
  109.     //Front
  110.     glNormal3f(0.0f, 0.0f, 1.0f);
  111.     //glNormal3f(-1.0f, 0.0f, 1.0f);
  112.     glVertex3f(-1.5f, -1.0f, 1.5f);
  113.     //glNormal3f(1.0f, 0.0f, 1.0f);
  114.     glVertex3f(1.5f, -1.0f, 1.5f);
  115.     //glNormal3f(1.0f, 0.0f, 1.0f);
  116.     glVertex3f(1.5f, 1.0f, 1.5f);
  117.     //glNormal3f(-1.0f, 0.0f, 1.0f);
  118.     glVertex3f(-1.5f, 1.0f, 1.5f);
  119.  
  120.     //Right
  121.     glNormal3f(1.0f, 0.0f, 0.0f);
  122.     //glNormal3f(1.0f, 0.0f, -1.0f);
  123.     glVertex3f(1.5f, -1.0f, -1.5f);
  124.     //glNormal3f(1.0f, 0.0f, -1.0f);
  125.     glVertex3f(1.5f, 1.0f, -1.5f);
  126.     //glNormal3f(1.0f, 0.0f, 1.0f);
  127.     glVertex3f(1.5f, 1.0f, 1.5f);
  128.     //glNormal3f(1.0f, 0.0f, 1.0f);
  129.     glVertex3f(1.5f, -1.0f, 1.5f);
  130.  
  131.     //Back
  132.     glNormal3f(0.0f, 0.0f, -1.0f);
  133.     //glNormal3f(-1.0f, 0.0f, -1.0f);
  134.     glVertex3f(-1.5f, -1.0f, -1.5f);
  135.     //glNormal3f(-1.0f, 0.0f, -1.0f);
  136.     glVertex3f(-1.5f, 1.0f, -1.5f);
  137.     //glNormal3f(1.0f, 0.0f, -1.0f);
  138.     glVertex3f(1.5f, 1.0f, -1.5f);
  139.     //glNormal3f(1.0f, 0.0f, -1.0f);
  140.     glVertex3f(1.5f, -1.0f, -1.5f);
  141.  
  142.     //Left
  143.     glNormal3f(-1.0f, 0.0f, 0.0f);
  144.     //glNormal3f(-1.0f, 0.0f, -1.0f);
  145.     glVertex3f(-1.5f, -1.0f, -1.5f);
  146.     //glNormal3f(-1.0f, 0.0f, 1.0f);
  147.     glVertex3f(-1.5f, -1.0f, 1.5f);
  148.     //glNormal3f(-1.0f, 0.0f, 1.0f);
  149.     glVertex3f(-1.5f, 1.0f, 1.5f);
  150.     //glNormal3f(-1.0f, 0.0f, -1.0f);
  151.     glVertex3f(-1.5f, 1.0f, -1.5f);
  152.  
  153.     //Top
  154.     glNormal3f(0.0f, 1.0f, 0.0f);
  155.     glVertex3f(1.5f, 1.0f, -1.5f);
  156.     glVertex3f(1.5f, 1.0f, 1.5f);
  157.     glVertex3f(-1.5f, 1.0f, 1.5f);
  158.     glVertex3f(-1.5f, 1.0f, -1.5f);
  159.  
  160.     //Back
  161.     glNormal3f(0.0f, -1.0f, 0.0f);
  162.     glVertex3f(1.5f, -1.0f, -1.5f);
  163.     glVertex3f(1.5f, -1.0f, 1.5f);
  164.     glVertex3f(-1.5f, -1.0f, 1.5f);
  165.     glVertex3f(-1.5f, -1.0f, -1.5f);
  166.  
  167.     glEnd();
  168.  
  169.         // Finally, display rendered frame on screen
  170.         App.Display();
  171.     }
  172.  
  173.     return EXIT_SUCCESS;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement