Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- [Please Read]:
- Skipping around? That's fine, just make sure you read the "How to read the comments" section
- at some point so as to make your life easier.
- ***** OpenGL Tutorial Set: Tutorial 1 - Setting Up OpenGL
- ***** Written by Tyler Camp
- [Description]:
- Gives a very small example of rendering 3D using OpenGL.
- [What the application does]:
- Creates a window using SFML and renders a color-changing triangle with
- a blue background.
- [How to read the comments]:
- A comment will generally consist of "accurate" and "lay-man's terms" descriptions. The accurate
- description is really what's going on, but the lay-man's description is a more easily-
- understood description that still works for what's being commented. I personally suggest
- reading the lay-man's comment first and then reading the accurate comment, so that you
- can get some perspective on what's going on.
- In the case that a comment does have two descriptions, the accurate description will be first
- and the lay-man's description will be second, contained within parentheses.
- Not all comments can have two descriptions. In the case that a comment only has one description,
- you'll just have to trust me on it. I may have not provided the accurate definition so as
- not to confuse the reader, or the accurate definition on its own might really just be simple
- enough.
- Some tutorials may have additional information at the end of the code, look out for these segments
- where I cover information that couldn't normally be covered in-code.
- */
- // We're using SFML to set up our window for us, it sets it up with OpenGL capabilities automatically
- #include <Windows.h>
- #include <SFML\Graphics.hpp>
- // Add the libs so that we can use SFML (SFML libs use static MSVCLIB linking, the projects
- // have been altered accordingly)
- #ifdef _DEBUG
- #pragma comment (lib, "sfml-system-s-d.lib")
- #pragma comment (lib, "sfml-window-s-d.lib")
- #pragma comment (lib, "sfml-graphics-s-d.lib")
- #else
- #pragma comment (lib, "sfml-system-s.lib")
- #pragma comment (lib, "sfml-window-s.lib")
- #pragma comment (lib, "sfml-graphics-s.lib")
- #endif
- // OpenGL headers (Note: SFML generally includes these headers anyways, I'm just
- // pointing out that these headers ARE actually included somewhere)
- #include <gl\GL.h>
- #include <gl\GLU.h>
- // Grab the GLU.h libs (GLU = GL Utilities, it's pretty much a part of OpenGL)
- #pragma comment (lib, "glu32.lib")
- int main ()
- {
- // Make our window
- sf::RenderWindow App (sf::VideoMode (800, 600), "Setting Up OpenGL");
- App.setFramerateLimit (30);
- // Enable the depth buffer
- glEnable (GL_DEPTH_TEST);
- glDepthMask (GL_TRUE);
- // Set our default values
- glClearDepth (1.0f);
- glClearColor (00.0f/255.0f, 162.0f/255.0f, 232.0f/255.0f, 1.0f);
- // Determine the Z-ordering
- glDepthFunc (GL_LEQUAL);
- // Set up our viewing method
- glMatrixMode (GL_PROJECTION);
- glLoadIdentity ();
- gluPerspective (45.0, float (App.getSize().x) / App.getSize().y, 0.1f, 1000.0f);
- // Just for the effect, not essential to the tutorial
- float sinVal = 0;
- sf::Event Event;
- while (App.isOpen ())
- {
- // Handle input events from SFML, just so we can close out
- while (App.pollEvent(Event))
- {
- // Close window : exit
- if (Event.type == sf::Event::Closed)
- App.close();
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
- App.close();
- }
- // Just for the effect, not essential to the tutorial
- sinVal += 0.01f;
- float clearVal = sinf (sinVal)/2.0f + 0.5f;
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Tell OpenGL that we're going to be giving it vertices, colors, etc. that should
- // be used to make the primitive that we tell it to.
- glBegin (GL_TRIANGLES);
- // glColor changes OpenGL's vertex-color state, which is the color that the
- // vertices should be.
- glColor3f (clearVal, clearVal, clearVal);
- // Top-left vertex (Note: Coordinates in 3D OpenGL are the same as in math class,
- // positive Y is up.)
- glVertex3f (-2.0f, 2.0f, -10.0f); // The color is applied here
- // Bottom-left vertex
- glVertex3f ( 2.0f, 2.0f, -10.0f); // and here
- glColor3f (0.5f, 0.5f, 0.0f); // But not here, because we specify a different color
- glVertex3f (0.0f, -2.0f, -10.0f); // which is applied here
- // Note: -Z is farther away, +Z is closer.
- glEnd ();
- // Tell SFML to update the window with the new stuff that OpenGL just drew for us.
- App.display ();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement