/
TestSun.cpp
147 lines (115 loc) · 2.96 KB
/
TestSun.cpp
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
#include <stdio.h>
#include <windows.h>
#include <gl/GL.h>
#include <gl/glut.h>
#include "common.h"
#include "GlutApp.h"
#ifdef TEST_SUN
static int year = 0, day = 0;
class TestSun : public GlutApp
{
virtual void onTimer()
{
//static int count = 0;
//printf("Alarm %d!\n", count++);
year = (year+1) % 360;
glutPostRedisplay();
}
virtual void onInit()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
GLfloat mat_specular[] = { 1, 1, 1, 1 };
GLfloat mat_shininess[] = {50};
GLfloat light_position[] = {1, 1, 1, 0};
// GLfloat light_position[] = {1, 1, 1, 1};
GLfloat white_light[] = {1, 1, 1, 1};
GLfloat model_ambint[] = {.5, .5, .5, 1};
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambint);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // ¸ü¸Ä¶à±ßÐλæÖÆÐÎ
}
virtual void onDisplay()
{
GLfloat light_position[] = {0, 0, 0, 1};
//glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1, 0, 0);
glPushMatrix();
glutSolidSphere(1.0, 20, 16); // draw sun.
glRotatef((GLfloat)year, 0.0, 1.0, 0.0);
glTranslatef(2.0, 0.0, 0.0);
glRotatef((GLfloat) day, 0.0, 1.0, 0.0);
// glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glColor3f(1.0, 1.0, 1.0);
glutSolidSphere(0.2, 10, 8); // draw planet.
glPopMatrix();
GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
printf("onDisplay() ERROR: %s\n", gluErrorString(err));
}
glutSwapBuffers();
}
void onResize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
0.0, 0.0, 5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
printf("onResize() ERROR: %s\n", gluErrorString(err));
}
}
void onKey(unsigned char key, int x, int y)
{
switch (key)
{
case 'd':
day = (day+10) % 360;
glutPostRedisplay();
break;
case 'D':
day = (day-10) % 360;
glutPostRedisplay();
break;
case 'y':
year = (year+5) % 360;
glutPostRedisplay();
break;
case 'Y':
year = (year-5) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
};
int main(int argc, char** argv)
{
GlutApp* app = new TestSun();
app->initGlut(argc, argv);
app->setTitle("Timer test");
app->setWindowsSize(640, 480);
app->setTimer(1000, 20);
app->run();
return 0;
}
#endif // TEST_4FACES