Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp
- #include "main.h"
- GLfloat angle = 0.0; //the rotation value
- void cube (void) {
- glRotatef(angle, 1.0, 0.0, 0.0); //rotate on the x axis
- glRotatef(angle, 0.0, 1.0, 0.0); //rotate on the y axis
- glRotatef(angle, 0.0, 0.0, 1.0); //rotate on the z axis
- glColor3f(1.0, 0.0, 0.0);
- glutWireCube(2);
- }
- void display (void) {
- keyOperations();
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to ?
- glClear (GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
- glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations
- glTranslatef(0.0f, 0.0f, -5.0f); // Push eveything 5 units back into the scene,
- //otherwise we won't see the primitive
- cube();
- glFlush(); // Flush the OpenGL buffers to the window
- angle++; //update the angle of rotation
- }
- void reshape (int width, int height) {
- glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
- glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we
- // can manipulate how our scene is viewed
- glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we
- // don't get any artifacts (cleaning up)
- gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle
- // (in degrees), the aspect ratio of our window, and the new and far planes
- glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we
- // can start drawing shapes correctly
- }
- int main (int argc, char **argv) {
- glutInit(&argc, argv); // Initialize GLUT
- glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
- glutInitWindowSize (500, 500); // Set the width and height of the window
- glutInitWindowPosition (100, 100); // Set the position of the window
- glutCreateWindow ("Hello, World!"); // Set the title for the window
- glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering
- glutIdleFunc (display); //change any idle values accordingly
- glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for rendering
- glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses
- glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events
- glutSpecialFunc(keySpecial); // Tell GLUT to use the method "keySpecial" for special key presses
- glutSpecialUpFunc(keySpecialUp); // Tell GLUT to use the method "keySpecialUp" for special up key events
- glutMainLoop();
- }
- main.h
- #ifndef MAIN_H
- #define MAIN_H
- #include "GL/glew.h"
- #include "GL/freeglut.h"
- #include "keyOps.h"
- #endif
- keyOps.cpp
- #include "keyOps.h"
- bool* keyStates = new bool[256]; // Create an array of boolean values of length 256 (0-255)
- bool* keySpecialStates = new bool[246]; // Create an array of boolean values of length 256 (0-255)
- void keyOperations () {
- if (keyStates['a']) { // If the 'a' key has been pressed
- //cout << "Test!" << endl;
- }
- }
- void keySpecialOperations() {
- if (keySpecialStates[GLUT_KEY_LEFT]) { // If the left arrow key has been pressed
- //cout << "Test!" << endl;
- }
- }
- void keyPressed (unsigned char key, int x, int y) {
- keyStates[key] = true; // Set the state of the current key to pressed
- }
- void keyUp (unsigned char key, int x, int y) {
- keyStates[key] = false; // Set the state of the current key to not pressed
- }
- void keySpecial (int key, int x, int y) {
- keySpecialStates[key] = true; // Set the state of the current key to pressed
- }
- void keySpecialUp (int key, int x, int y) {
- keySpecialStates[key] = false; // Set the state of the current key to not pressed
- }
- keyOps.h
- #ifndef KEYOPS_H
- #define KEYOPS_H
- #include "GL/glew.h"
- #include "GL/freeglut.h"
- #include <iostream>
- using namespace std;
- //PROTOTYPES
- void keyOperations ();
- void keySpecialOperations();
- void keyPressed (unsigned char key, int x, int y);
- void keyUp (unsigned char key, int x, int y);
- void keySpecial (int key, int x, int y);
- void keySpecialUp (int key, int x, int y);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement