1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
#include <iostream>
#include <GL/glew.h> // Include the GLEW header file
#include <GL/freeglut.h> // Include the GLUT header file
using namespace std;
GLfloat angle = 0.0;
GLfloat whiteSpecularMaterial[] = {1.0, 1.0, 1.0}; //setthe material to white
GLfloat whiteSpecularLight[] = {1.0, 1.0, 1.0}; //set thelight specular to white
GLfloat mShininess[] = {128}; //set the shininess of thematerial
GLfloat blankMaterial[] = {0.0, 0.0, 0.0}; //set the diffuselight to white
bool keyStates[256] = {false};
bool keySpecialStates [256] = {false};
bool specular = false;
int x = 0;
int y = 0;
int z = 0;
float xRotationAngle = 0.0f;
float yRotationAngle = 0.0f;
float zRotationAngle = 0.0f;
float speed = 5.0f;
void keyOperations (void) {
if(keyStates['e'] == true)
{
zRotationAngle -= speed;
}
else if(keyStates['q'] == true)
{
zRotationAngle += speed;
}
}
void keyUp (unsigned char key, int x, int y) {
if(keyStates['e'] == true)
{
keyOperations();
keyStates['e'] = {false};
}
if(keyStates['q'] == true)
{
keyOperations();
keyStates['q'] = {false};
}
if (keyStates['s'] == true)
{
if (specular==false)
{
specular = true;
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
whiteSpecularMaterial);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mShininess);
}
else if (specular==true)
{
specular = false;
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blankMaterial);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS,blankMaterial);
}
keyStates['s'] == false;
}
}
void keyPressed (unsigned char key, int x, int y) {
keyStates[key] = true; // Set the state of the current key to pressed
}
void keySpecialOperations(void) {
if (keySpecialStates[GLUT_KEY_LEFT])
{
yRotationAngle -= speed;
}
else if(keySpecialStates[GLUT_KEY_RIGHT])
{
yRotationAngle += speed;
}
if(keySpecialStates[GLUT_KEY_UP])
{
xRotationAngle -= speed;
}
else if(keySpecialStates[GLUT_KEY_DOWN])
{
xRotationAngle += speed;
}
}
void keySpecial (int key, int x, int y) {
keySpecialStates[key] = true;
keySpecialOperations();
}
void keySpecialUp (int key, int x, int y) {
keySpecialStates[key] = false;
}
void cube (void) {
glColor3f(1.0, 0.0, 0.0);
glRotatef(xRotationAngle, 1.0f, 0.0f, 0.0f); // Rotate our object around the x axis
glRotatef(yRotationAngle, 0.0f, 1.0f, 0.0f); // Rotate our object around the y axis
glRotatef(zRotationAngle, 0.0f, 0.0f, 1.0f); // Rotate our object around the z axis
glutSolidCube(2);
}
void init (void) {
glEnable (GL_DEPTH_TEST);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
}
void light (void) {
glLightfv(GL_LIGHT0, GL_SPECULAR, whiteSpecularLight);
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
light();
glTranslatef(0,0,-5);
//glRotatef(xRotationAngle, 1.0f, 0.0f, 0.0f); // Rotate our object around the x axis
// glRotatef(yRotationAngle, 0.0f, 1.0f, 0.0f); // Rotate our object around the y axis
// glRotatef(zRotationAngle, 0.0f, 0.0f, 1.0f); // Rotate our object around the z axis
cube();
glutSwapBuffers(); //<-- double buffer
//angle ++;
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hi");
init ();
glutDisplayFunc (display);
glutIdleFunc (display);
glutKeyboardFunc (keyPressed);
glutKeyboardUpFunc(keyUp);
glutSpecialFunc(keySpecial);
glutSpecialUpFunc(keySpecialUp);
glutReshapeFunc (reshape);
glutMainLoop ();
return 0;
}
|