forked from freeglut/freeglut
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswiftless12.cpp
More file actions
129 lines (113 loc) · 3.55 KB
/
swiftless12.cpp
File metadata and controls
129 lines (113 loc) · 3.55 KB
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
#include <GL/gl.h>
#include <GL/glut.h>
GLfloat angle = 0.0;
GLfloat redDiffuseMaterial[] = {1.0, 0.0, 0.0}; //set the material to red
GLfloat whiteSpecularMaterial[] = {1.0, 1.0, 1.0}; //set the material to white
GLfloat greenEmissiveMaterial[] = {0.0, 1.0, 0.0}; //set the material to green
GLfloat whiteSpecularLight[] = {1.0, 1.0, 1.0}; //set the light specular to white
GLfloat blackAmbientLight[] = {0.0, 0.0, 0.0}; //set the light ambient to black
GLfloat whiteDiffuseLight[] = {1.0, 1.0, 1.0}; //set the diffuse light to white
GLfloat blankMaterial[] = {0.0, 0.0, 0.0}; //set the diffuse light to white
GLfloat mShininess[] = {128}; //set the shininess of the material
bool diffuse = false;
bool emissive = false;
bool specular = false;
void init (void) {
glEnable (GL_DEPTH_TEST);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
}
void light (void) {
glLightfv(GL_LIGHT0, GL_SPECULAR, whiteSpecularLight);
glLightfv(GL_LIGHT0, GL_AMBIENT, blackAmbientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteDiffuseLight);
}
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(angle,1,1,1);
glutSolidTeapot(2);
glutSwapBuffers();
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);
}
void keyboard (unsigned char key, int x, int y) {
if (key=='s')
{
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);
}
}
if (key=='d')
{
if (diffuse==false)
{
diffuse = true;
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,
redDiffuseMaterial);
}
else if (diffuse==true)
{
diffuse = false;
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blankMaterial);
}
}
if (key=='e')
{
if (emissive==false)
{
emissive = true;
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION,
greenEmissiveMaterial);
}
else if (emissive==true)
{
emissive = false;
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, blankMaterial);
}
}
}
void joystick(unsigned int buttons, int axis0, int axis1, int axis2)
{
static unsigned int old = 0;
unsigned int pressed = (buttons ^ old) & buttons;
old = buttons;
if (pressed & 0x1) keyboard('s', 0, 0);
if (pressed & 0x2) keyboard('d', 0, 0);
if (pressed & 0x4) keyboard('e', 0, 0);
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("A basic OpenGL Window");
init ();
glutJoystickFunc (joystick, 10);
glutDisplayFunc (display);
glutIdleFunc (display);
glutKeyboardFunc (keyboard);
glutReshapeFunc (reshape);
glutMainLoop ();
return 0;
}