Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Demo.cpp
- * A simple OpenGL program using GLUT. It creates a blank black
- * window which can be used as a starting point for OpenGL programming.
- *
- * Link in opengl32, glu32, and glut32 libraries and make sure to include
- * windows.h if you are compiling with Eclipse in Windows.
- *
- * Author: Paul Solt 3-4-07
- * Based on examples from the Red book OpenGL Programming Guide
- */
- #include <GL/glew.h>
- #include <GL/glfw.h>
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- /*
- #ifndef GLEW_STATIC
- #define GLEW_STATIC
- #endif
- */
- const int WIDTH = 600;
- const int HEIGHT = 480;
- /* Definitions */
- /////////////////////////////////////////////////////////////////////////////////////////////////
- // THE OPENGL INIT
- /////////////////////////////////////////////////////////////////////////////////////////////////
- int InitGL( ) // All Setup For OpenGL Goes Here
- {
- glShadeModel(GL_SMOOTH); // Enable Smooth Shading
- glClearColor(0.0f, 0.0f, 0.1f, 0.5f); // Black Background
- glClearDepth(1.0f); // Depth Buffer Setup
- glEnable(GL_DEPTH_TEST); // Enables Depth Testing
- glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
- return 1; // Initialization Went OK
- }
- void draw_triangle(){
- glTranslatef(0.0f, 0.0f, -6.0f); // Translate Into The Screen
- glBegin(GL_TRIANGLES); // Start Drawing A Triangle
- glColor3f( 1.0f,0.0f,0.0f); // Red Color
- glVertex3f(0.0f, 1.0f, 0.0f); // Top Color
- glColor3f(0.0f,1.0f,0.0f); // Green
- glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left Vertex
- glColor3f(0.0f,0.0f,1.0f); // Blue Color
- glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right Vertex
- glEnd(); // Finished Drawing The Triangle
- }
- float rotqube=0;
- void draw_cube(){
- // Reset The Current Modelview Matrix
- glLoadIdentity();
- //NEW//////////////////NEW//////////////////NEW//////////////////NEW/////////////
- glTranslatef(0.0f, 0.0f,-7.0f); // Translate Into The Screen 7.0 Units
- glRotatef(rotqube,0.0f,1.0f,0.0f); // Rotate The cube around the Y axis
- glRotatef(rotqube,1.0f,1.0f,1.0f);
- glBegin(GL_QUADS); // Draw The Cube Using quads
- glColor3f(0.0f,1.0f,0.0f); // Color Blue
- glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
- glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
- glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
- glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
- glColor3f(1.0f,0.5f,0.0f); // Color Orange
- glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
- glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
- glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
- glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
- glColor3f(1.0f,0.0f,0.0f); // Color Red
- glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
- glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
- glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
- glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
- glColor3f(1.0f,1.0f,0.0f); // Color Yellow
- glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
- glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
- glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
- glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
- glColor3f(0.0f,0.0f,1.0f); // Color Blue
- glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
- glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
- glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
- glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
- glColor3f(1.0f,0.0f,1.0f); // Color Violet
- glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
- glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
- glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
- glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
- glEnd(); // End Drawing The Cube
- rotqube +=0.9f; // Increase Angle
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////
- // THE RESIZE GL SCENE
- /////////////////////////////////////////////////////////////////////////////////////////////////
- GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
- {
- if (height==0) // Prevent A Divide By Zero By
- {
- height=1; // Making Height Equal One
- }
- glViewport(0,0,width,height); // Reset The Current Viewport
- glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
- glLoadIdentity(); // Reset The Projection Matrix
- // Calculate The Aspect Ratio Of The Window
- gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
- glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
- glLoadIdentity(); // Reset The Modelview Matrix
- }
- /* The main function */
- int main( int argc, char *argv[] ) {
- int running = GL_TRUE;
- // Initialize GLEW.
- GLenum err = glewInit();
- if (GLEW_OK != err)
- {
- /* Problem: glewInit failed, something is seriously wrong. */
- cout << "Error: " << glewGetErrorString(err) << endl;
- }
- cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << endl;
- // Initialize GLFW
- if( !glfwInit() )
- {
- exit( EXIT_FAILURE );
- }
- // Open an OpenGL window
- if( !glfwOpenWindow( WIDTH,HEIGHT, 0,0,0,0,0,0, GLFW_WINDOW ) )
- {
- glfwTerminate();
- exit( EXIT_FAILURE );
- }
- // Initialize OpenGL.
- InitGL();
- // Set the callback for when the window is resized.
- glfwSetWindowSizeCallback( ReSizeGLScene );
- // Main loop
- while( running )
- {
- // OpenGL rendering goes here...
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- draw_cube();
- //draw_triangle();
- // Swap front and back rendering buffers
- glfwSwapBuffers();
- // Check if ESC key was pressed or window was closed
- running = !glfwGetKey( GLFW_KEY_ESC ) &&
- glfwGetWindowParam( GLFW_OPENED );
- }
- // Close window and terminate GLFW
- glfwTerminate();
- // Exit program
- exit( EXIT_SUCCESS );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement