Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // OpenGLApplication.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "glut.h"
- #include <gl/gl.h>
- int screen_width=640;
- int screen_height=480;
- float z = 280.0, d = 10.0;
- int r = 1;
- int count = 0;
- void initOpenGL()
- {
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glShadeModel(GL_SMOOTH);
- glViewport(0, 0, screen_width, screen_height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0f, (GLfloat)screen_width/(GLfloat)screen_height, 1.0f, 1000.0f);
- glEnable(GL_DEPTH_TEST);
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- glMatrixMode(GL_MODELVIEW);
- }
- void renderScene(void)
- {
- count++;
- if(count % 30 == 0)
- r++;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- gluLookAt(0.0, 0.0, z, 0.0, 0.0, -10.0, 0.0, 1.0, 0.0);
- glColor4f(1,0.8,0.2,0.9);
- glPushMatrix();
- glRotatef(r/5,0,1,0);
- glutSolidSphere(30,16,16);
- glPopMatrix();
- glRotatef(r,0,1,0);
- glTranslatef(70,0,0);
- glColor4f(0,0.3,1,0.9);
- glPushMatrix();
- glRotatef(r,0,1,0);
- glutSolidSphere(15,16,16);
- glPopMatrix();
- glPushMatrix();
- glRotatef(r,0,1,0);
- glRotatef(r*5,0,1,0);
- glTranslatef(20,0,0);
- glColor4f(0.3,0,0.3,0.9);
- glutSolidSphere(8,16,16);
- glPopMatrix();
- glutSwapBuffers();
- }
- void changeSize(int w, int h)
- {
- screen_width=w;
- screen_height=h;
- if(h == 0)
- h = 1;
- float ratio = 1.0*w/h;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glViewport(0, 0, w, h);
- gluPerspective(45.0f, ratio, 1.0f, 1000.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0.0f, 0.0f, 50.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
- }
- void processNormalKeys(unsigned char key, int x, int y)
- {
- switch(key)
- {
- case 't':
- //process
- glutPostRedisplay();
- break;
- case 's':
- z=z*2;
- break;
- case 'w':
- z = (int)z/2;
- break;
- case 'r':
- r++;
- break;
- case 'y':
- r--;
- break;
- case 'd':
- d=d++;
- break;
- case 'a':
- d=(int)d--;
- break;
- }
- }
- int main(int argc, char* argv[])
- {
- //Initialize the GLUT library
- glutInit(&argc, argv);
- //Set the display mode
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- //Set the initial position and dimensions of the window
- glutInitWindowPosition(100, 100);
- glutInitWindowSize(screen_width, screen_height);
- //creates the window
- glutCreateWindow("First OpenGL Application");
- //Specifies the function to call when the window needs to be redisplayed
- glutDisplayFunc(renderScene);
- //Sets the idle callback function
- glutIdleFunc(renderScene);
- //Sets the reshape callback function
- glutReshapeFunc(changeSize);
- //Keyboard callback function
- glutKeyboardFunc(processNormalKeys);
- //Initialize some OpenGL parameters
- initOpenGL();
- //Starts the GLUT infinite loop
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement