Skip to content

Commit 145dc42

Browse files
committed
rotation translation order
1 parent ecf8149 commit 145dc42

File tree

3 files changed

+167
-8
lines changed

3 files changed

+167
-8
lines changed

Tutorial-02/cube.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,34 @@ void keyboard(unsigned char key, int x, int y)
3838
{
3939
switch (key)
4040
{
41+
case '0':
42+
translationx = 0.0;
43+
rotation = 0.0;
44+
rotateFirst = 0;
45+
glutPostRedisplay();
46+
break;
4147
case '1':
42-
translationx += 0.01;
48+
translationx += 0.05;
4349
glutPostRedisplay();
4450
break;
45-
case '2':
46-
scale *= 1.1;
51+
case '!':
52+
translationx -= 0.05;
4753
glutPostRedisplay();
4854
break;
49-
case '3':
55+
case '2':
5056
rotation += 5;
5157
glutPostRedisplay();
5258
break;
53-
case '4':
54-
if (rotateFirst == 0)
59+
case '@':
60+
rotation -= 5;
61+
glutPostRedisplay();
62+
break;
63+
case '3':
5564
rotateFirst = 1;
56-
else
65+
break;
66+
case '#':
5767
rotateFirst = 0;
68+
break;
5869
}
5970
}
6071
void init()
@@ -67,7 +78,7 @@ void init()
6778
glEnable(GL_DEPTH_TEST);
6879

6980
glMatrixMode(GL_PROJECTION);
70-
gluPerspective(100.0, 1.0, 1.0, 10.0);
81+
gluPerspective(50.0, 1.0, 1.0, 10.0);
7182
gluLookAt(eye[0], eye[1], eye[2],
7283
center[0], center[1], center[2],
7384
up[0], up[1], up[2]);

Tutorial-02/rotate_then_translate.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <GL/glut.h>
2+
3+
float light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. */
4+
float light_position[] = {-1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */
5+
float eye[] = {0.0, 0.0, 5.0};
6+
float center[] = {0.0, 0.0, 0.0};
7+
float up[] = {0.0, 1.0, 0.0};
8+
float translationx = 0.0;
9+
float scale = 1;
10+
float rotation = 45.0;
11+
int rotateFirst = 0;
12+
13+
void display()
14+
{
15+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
16+
17+
glLoadIdentity();
18+
glTranslatef(1.0, 0.0, 0.0);
19+
glRotatef(135.0, 0.0, 0.0, 1.0);
20+
glutSolidCube(scale);
21+
glutSwapBuffers();
22+
}
23+
24+
void keyboard(unsigned char key, int x, int y)
25+
{
26+
switch (key)
27+
{
28+
case '1':
29+
translationx += 0.01;
30+
glutPostRedisplay();
31+
break;
32+
case '2':
33+
scale *= 1.1;
34+
glutPostRedisplay();
35+
break;
36+
case '3':
37+
rotation += 5;
38+
glutPostRedisplay();
39+
break;
40+
case '4':
41+
if (rotateFirst == 0)
42+
rotateFirst = 1;
43+
else
44+
rotateFirst = 0;
45+
}
46+
}
47+
void init()
48+
{
49+
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
50+
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
51+
glEnable(GL_LIGHT0);
52+
glEnable(GL_LIGHTING);
53+
54+
glEnable(GL_DEPTH_TEST);
55+
56+
glMatrixMode(GL_PROJECTION);
57+
gluPerspective(50.0, 1.0, 1.0, 10.0);
58+
gluLookAt(eye[0], eye[1], eye[2],
59+
center[0], center[1], center[2],
60+
up[0], up[1], up[2]);
61+
glMatrixMode(GL_MODELVIEW);
62+
}
63+
64+
int main(int argc, char **argv)
65+
{
66+
glutInit(&argc, argv);
67+
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
68+
glutCreateWindow("red 3D lighted cube");
69+
glutDisplayFunc(display);
70+
glutKeyboardFunc(keyboard);
71+
init();
72+
glutMainLoop();
73+
return 0;
74+
}

Tutorial-02/translate_then_rotate.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <GL/glut.h>
2+
3+
float light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. */
4+
float light_position[] = {-1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */
5+
float eye[] = {0.0, 0.0, 5.0};
6+
float center[] = {0.0, 0.0, 0.0};
7+
float up[] = {0.0, 1.0, 0.0};
8+
float translationx = 0.0;
9+
float scale = 1;
10+
float rotation = 45.0;
11+
int rotateFirst = 0;
12+
13+
void display()
14+
{
15+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
16+
17+
glLoadIdentity();
18+
glRotatef(135.0, 0.0, 0.0, 1.0);
19+
glTranslatef(1.0, 0.0, 0.0);
20+
glutSolidCube(scale);
21+
glutSwapBuffers();
22+
}
23+
24+
void keyboard(unsigned char key, int x, int y)
25+
{
26+
switch (key)
27+
{
28+
case '1':
29+
translationx += 0.01;
30+
glutPostRedisplay();
31+
break;
32+
case '2':
33+
scale *= 1.1;
34+
glutPostRedisplay();
35+
break;
36+
case '3':
37+
rotation += 5;
38+
glutPostRedisplay();
39+
break;
40+
case '4':
41+
if (rotateFirst == 0)
42+
rotateFirst = 1;
43+
else
44+
rotateFirst = 0;
45+
}
46+
}
47+
void init()
48+
{
49+
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
50+
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
51+
glEnable(GL_LIGHT0);
52+
glEnable(GL_LIGHTING);
53+
54+
glEnable(GL_DEPTH_TEST);
55+
56+
glMatrixMode(GL_PROJECTION);
57+
gluPerspective(50.0, 1.0, 1.0, 10.0);
58+
gluLookAt(eye[0], eye[1], eye[2],
59+
center[0], center[1], center[2],
60+
up[0], up[1], up[2]);
61+
glMatrixMode(GL_MODELVIEW);
62+
}
63+
64+
int main(int argc, char **argv)
65+
{
66+
glutInit(&argc, argv);
67+
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
68+
glutCreateWindow("red 3D lighted cube");
69+
glutDisplayFunc(display);
70+
glutKeyboardFunc(keyboard);
71+
init();
72+
glutMainLoop();
73+
return 0;
74+
}

0 commit comments

Comments
 (0)