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 <windows.h>
- #include <GL/glut.h>
- const int WIDTH = 600;
- const int HEIGHT = 480;
- /* Prototypes */
- void init();
- void display();
- GLvoid ReSizeGLScene(GLsizei width, GLsizei height);
- /* 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 TRUE; // Initialization Went OK
- }
- GLvoid IdleFunc( )
- {
- glutPostRedisplay();
- }
- 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
- }
- /* Initializes the OpenGL state */
- void init() {
- InitGL();
- }
- /* Displays a black clear screen */
- void display() {
- // Clear Screen And Depth Buffer
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- draw_cube();
- glutSwapBuffers(); /* Double buffering */
- }
- /* The main function */
- int main( int argc, char *argv[] ) {
- /* Glut setup function calls */
- glutInit( &argc, argv );
- glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); /* Use double buffering and RGB colors */
- glutInitWindowPosition( 100, 100 );
- glutInitWindowSize( WIDTH, HEIGHT );
- glutCreateWindow( argv[0] );
- init();
- glutDisplayFunc( display ); /* Call back display function */
- glutReshapeFunc( ReSizeGLScene );
- glutIdleFunc( IdleFunc );
- glutMainLoop(); /* Continue drawing the scene */
- return 0;
- }
Add Comment
Please, Sign In to add comment