Advertisement
Guest User

Untitled

a guest
Sep 17th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1.  
  2.  
  3. /*
  4.  [Please Read]:
  5.     Skipping around? That's fine, just make sure you read the "How to read the comments" section
  6.         at some point so as to make your life easier.
  7.  
  8.  
  9.  ***** OpenGL Tutorial Set: Tutorial 1 - Setting Up OpenGL
  10.  ***** Written by Tyler Camp
  11.  
  12.  [Description]:
  13.     Gives a very small example of rendering 3D using OpenGL.
  14.  
  15.  
  16.  [What the application does]:
  17.     Creates a window using SFML and renders a color-changing triangle with
  18.         a blue background.
  19.  
  20.  
  21.  [How to read the comments]:
  22.     A comment will generally consist of "accurate" and "lay-man's terms" descriptions. The accurate
  23.         description is really what's going on, but the lay-man's description is a more easily-
  24.         understood description that still works for what's being commented. I personally suggest
  25.         reading the lay-man's comment first and then reading the accurate comment, so that you
  26.         can get some perspective on what's going on.
  27.  
  28.     In the case that a comment does have two descriptions, the accurate description will be first
  29.         and the lay-man's description will be second, contained within parentheses.
  30.  
  31.     Not all comments can have two descriptions. In the case that a comment only has one description,
  32.         you'll just have to trust me on it. I may have not provided the accurate definition so as
  33.         not to confuse the reader, or the accurate definition on its own might really just be simple
  34.         enough.
  35.    
  36.     Some tutorials may have additional information at the end of the code, look out for these segments
  37.         where I cover information that couldn't normally be covered in-code.
  38.  */
  39.  
  40.  
  41.  
  42.  
  43. //  We're using SFML to set up our window for us, it sets it up with OpenGL capabilities automatically
  44. #include <Windows.h>
  45. #include <SFML\Graphics.hpp>
  46.  
  47. //  Add the libs so that we can use SFML (SFML libs use static MSVCLIB linking, the projects
  48. //      have been altered accordingly)
  49. #ifdef _DEBUG
  50. #pragma comment (lib, "sfml-system-s-d.lib")
  51. #pragma comment (lib, "sfml-window-s-d.lib")
  52. #pragma comment (lib, "sfml-graphics-s-d.lib")
  53. #else
  54. #pragma comment (lib, "sfml-system-s.lib")
  55. #pragma comment (lib, "sfml-window-s.lib")
  56. #pragma comment (lib, "sfml-graphics-s.lib")
  57. #endif
  58.  
  59. //  OpenGL headers  (Note: SFML generally includes these headers anyways, I'm just
  60. //      pointing out that these headers ARE actually included somewhere)
  61. #include <gl\GL.h>
  62. #include <gl\GLU.h>
  63.  
  64. //  Grab the GLU.h libs (GLU = GL Utilities, it's pretty much a part of OpenGL)
  65. #pragma comment (lib, "glu32.lib")
  66.  
  67. int main ()
  68. {
  69.     //  Make our window
  70.     sf::RenderWindow App (sf::VideoMode (800, 600), "Setting Up OpenGL");
  71.     App.setFramerateLimit (30);
  72.    
  73.     //  Enable the depth buffer
  74.     glEnable (GL_DEPTH_TEST);
  75.     glDepthMask (GL_TRUE);
  76.  
  77.     //  Set our default values
  78.     glClearDepth (1.0f);
  79.     glClearColor (00.0f/255.0f, 162.0f/255.0f, 232.0f/255.0f, 1.0f);
  80.  
  81.     //  Determine the Z-ordering
  82.     glDepthFunc (GL_LEQUAL);
  83.  
  84.     //  Set up our viewing method
  85.     glMatrixMode (GL_PROJECTION);
  86.     glLoadIdentity ();
  87.     gluPerspective (45.0, float (App.getSize().x) / App.getSize().y, 0.1f, 1000.0f);
  88.  
  89.     //  Just for the effect, not essential to the tutorial
  90.     float sinVal = 0;
  91.  
  92.     sf::Event Event;
  93.  
  94.     while (App.isOpen ())
  95.     {
  96.         //  Handle input events from SFML, just so we can close out
  97.         while (App.pollEvent(Event))
  98.          {
  99.              // Close window : exit
  100.              if (Event.type == sf::Event::Closed)
  101.                  App.close();
  102.              if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
  103.                  App.close();
  104.          }
  105.  
  106.         //  Just for the effect, not essential to the tutorial
  107.         sinVal += 0.01f;
  108.         float clearVal = sinf (sinVal)/2.0f + 0.5f;
  109.  
  110.         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  111.  
  112.         //  Tell OpenGL that we're going to be giving it vertices, colors, etc. that should
  113.         //      be used to make the primitive that we tell it to.
  114.         glBegin (GL_TRIANGLES);
  115.  
  116.             //  glColor changes OpenGL's vertex-color state, which is the color that the
  117.             //      vertices should be.
  118.             glColor3f (clearVal, clearVal, clearVal);
  119.  
  120.             //  Top-left vertex   (Note: Coordinates in 3D OpenGL are the same as in math class,
  121.             //      positive Y is up.)
  122.             glVertex3f (-2.0f,  2.0f, -10.0f);          //  The color is applied here
  123.  
  124.             //  Bottom-left vertex
  125.             glVertex3f ( 2.0f,  2.0f, -10.0f);          //  and here
  126.  
  127.  
  128.             glColor3f  (0.5f, 0.5f, 0.0f);              //  But not here, because we specify a different color
  129.             glVertex3f (0.0f, -2.0f, -10.0f);           //  which is applied here
  130.  
  131.  
  132.  
  133.  
  134.             //  Note: -Z is farther away, +Z is closer.
  135.  
  136.         glEnd ();
  137.  
  138.         //  Tell SFML to update the window with the new stuff that OpenGL just drew for us.
  139.         App.display ();
  140.     }
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement