Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef __APPLE__
- #include <OpenGL/OpenGL.h>
- #include <GLUT/glut.h>
- #else
- #include <glut.h>
- #endif
- using namespace std;
- //Called when a key is pressed
- void handleKeypress(unsigned char key, //The key that was pressed
- int x, int y) { //The current mouse coordinates
- switch (key) {
- case 27: //Escape key
- exit(0); //Exit the program
- }
- }
- a
- //Initializes 3D rendering
- void initRendering() {
- //Makes 3D drawing work when something is in front of something else
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_COLOR_MATERIAL);
- glClearColor(0.7f, 0.9f, 1.0f, 1.0f);
- }
- //Called when the window is resized
- void handleResize(int w, int h) {
- //Tell OpenGL how to convert from coordinates to pixel values
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
- glLoadIdentity(); //Reset the camera
- gluPerspective(45.0, //The camera angle
- (double)w / (double)h, //The width-to-height ratio
- 1.0, //The near z clipping coordinate
- 200.0); //The far z clipping coordinate
- }
- float _angle = 30.0f;
- float _cameraAngle = 0.0f;
- //FIS Size Variables
- //Should I just use an array with all of the FIS size and action variables instead of these? If the list gets too long, then I will
- float _radius = 0.5f;
- float _height = 2.0f;
- //FIS Action Variables
- float walk_speed = 0.0f;
- float max_speed = 1.0f;
- float stance = 2.0f; //2 is Standing, 1 is crouching, .5 is prone (Prone may not be included)
- bool can_jump = false;
- //Character FIS Spacial Variables
- float _origin_x = 1.0f;
- float _origin_y = 0.0f;
- float _origin_z = 0.0f;
- float _xangle = 0.0f;
- float _yangle = 0.0f;
- float _zangle = 0.0f;
- //Character FIS Spacial and Phsyics Arrays (instead of individual variadbles)
- //Going to use thses arrays to phase out the individual variables. Tidier and faster.
- float loc[3] = {1.0f, 0.0f, 0.0f};
- float rot[3] = {0.0f, 0.0f, 0.0f};
- float loc_speed[3] = {0.0f, 0.0f, 0.0f};//Added to loc array in update.
- float rot_speed[3] = {0.0f, 0.0f, 0.0f};//Added to rot array in update.
- //FIS Physics Variables
- float _xspeed = 0.0f;//speeds are added to origins in update every frame: [_origin_x = _origin_x + _xspeed] so on and so forth.
- float _yspeed = 0.0f;
- float _zspeed = 0.0f;
- float _xrotspeed = 0.0f;//Similar to speeds, these are added to respective angles in update.
- float _yrotspeed = 0.0f;
- float _zrotspeed = 0.0f;
- //Draws the 3D scene
- void drawScene() {
- //Clear information from last draw
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
- glLoadIdentity(); //Reset the drawing perspective
- glRotatef(-_cameraAngle, 0.0f, 0.0f, 0.0f);
- glTranslatef(0.0f, 0.0f, -5.0f);
- glPushMatrix();
- glBegin(GL_QUADS);
- // These placeholders will eventually be replaced with an array that automatically draws all called quads based upon their parameters.
- //Draw a 1x1 Placeholder Facing Image Shape (FIS)
- glVertex3f(-1.5f, 0.0f, 0.0f);
- glVertex3f(-0.5f, 0.0f, 0.0f);
- glVertex3f(-0.5f, 1.0f, 0.0f);
- glVertex3f(-1.5f, 1.0f, 0.0f);
- //Draw a 1x2 Placeholder FIS
- glVertex3f(loc[0] - _radius, loc[1], loc[2]);
- glVertex3f(loc[0] + _radius, loc[1], loc[2]);
- glVertex3f(loc[0] + _radius, loc[1] + _height, loc[2]);
- glVertex3f(loc[0] - _radius, loc[1] + _height, loc[2]);
- glEnd(); //End quadrilateral coordinates
- glPopMatrix();
- /*
- glPushMatrix();
- glTranslatef(1.0f, 1.0f, 0.0f);
- glTranslatef(0.5f, 0.0f, 0.0f);
- glRotatef(_angle, 0.0f, 1.0f, 0.0f);
- glScalef(0.7f, 0.7f, 0.7f);
- glBegin(GL_TRIANGLES); //Begin triangle coordinates
- //Pentagon
- glVertex3f(-0.5f, -0.5f, 0.0f);
- glVertex3f(0.5f, -0.5f, 0.0f);
- glVertex3f(-0.5f, 0.0f, 0.0f);
- glVertex3f(-0.5f, 0.0f, 0.0f);
- glVertex3f(0.5f, -0.5f, 0.0f);
- glVertex3f(0.5f, 0.0f, 0.0f);
- glVertex3f(-0.5f, 0.0f, 0.0f);
- glVertex3f(0.5f, 0.0f, 0.0f);
- glVertex3f(0.0f, 0.5f, 0.0f);
- glEnd();
- glPopMatrix();
- glTranslatef(-1.0f, 1.0f, 0.0f);
- glTranslatef(0.5f, 0.0f, 0.0f);
- glRotatef(_angle, 1.0f, 2.0f, 3.0f);
- glBegin(GL_TRIANGLES);
- //Triangle
- glColor3f(1.0f, 0.7f, 0.0f);
- glVertex3f(0.5f, -0.5f, 0.0f);
- glVertex3f(0.0f, 0.5f, 0.0f);
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(-0.5f, -0.5f, 0.0f);
- glEnd(); //End triangle coordinates
- */
- glutSwapBuffers(); //Send the 3D scene to the screen
- }
- void update(int value) {
- if (loc[0] != loc[0] + loc_speed[0])
- loc[0] = loc[0] + loc_speed[0];
- if (loc[1] != loc[1] + loc_speed[1])
- loc[1] = loc[1] + loc_speed[1];
- if (loc[2] != loc[2] + loc_speed[2])
- loc[2] = loc[2] + loc_speed[2];
- if (can_jump && loc_speed[2] != 0.0f)
- loc_speed[2] = 0.0f;
- glutPostRedisplay();
- glutTimerFunc(25, update, 0);
- }
- int main(int argc, char** argv) {
- //Initialize GLUT
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(400, 400); //Set the window size
- //Create the window
- glutCreateWindow("Basic Shapes - videotutorialsrock.com");
- initRendering(); //Initialize rendering
- //Set handler functions for drawing, keypresses, and window resizes
- glutDisplayFunc(drawScene);
- glutKeyboardFunc(handleKeypress);
- glutReshapeFunc(handleResize);
- glutTimerFunc(25, update, 0);
- glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
- return 0; //This line is never reached
- }
Add Comment
Please, Sign In to add comment