forked from DaisukeMiyamoto/test-glut-texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_glut.c
262 lines (209 loc) · 5.49 KB
/
test_glut.c
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <stdio.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include "test_glut.h"
struct {
int pos[2];
int button;
} MouseState;
struct {
float distance;
float twist;
float elevation;
float azimuth;
float znear;
float zfar;
float fovy;
} CameraState;
struct {
unsigned int show_fps:1;
} FLAGS;
// GLUT functions
void display_func(void);
void keyboard_func(unsigned char key, int x, int y);
void mouse_func(int button ,int state, int x, int y);
void motion_func(int x, int y);
void fpstimer(int value);
// local functions
static void setup_texture(Texture *tex);
static void reset_view(void);
static void polarview(void);
static void init (char *progname);
// Texture
Texture _tex;
// FPS counter
unsigned int nframe = 0;
void display_func(void)
{
const float texcolor[] = { 1.0, 1.0, 1.0, 1.0 };
nframe++;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, texcolor);
glEnable(GL_TEXTURE_2D);
glPushMatrix ();
{
polarview();
glEnable( GL_DEPTH_TEST );
glNormal3f(0.0,1.0,0.0);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_QUADS);
{
glTexCoord2f(0.0,0.0); glVertex3f(1.0,0.0,1.0);
glTexCoord2f(0.0,5.0); glVertex3f(1.0,0.0,-1.0);
glTexCoord2f(5.0,5.0); glVertex3f(-1.0,0.0,-1.0);
glTexCoord2f(5.0,0.0); glVertex3f(-1.0,0.0,1.0);
}
glEnd();
glDisable(GL_DEPTH_TEST);
}
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
void keyboard_func(unsigned char key, int x, int y)
{
switch(key) {
case 'R':
reset_view();
break;
case 'T':
//reset_texture();
break;
case 'F':
FLAGS.show_fps = ~FLAGS.show_fps;
break;
case KEY_ESC:
exit( 0 );
default :
break;
}
glutPostRedisplay();
}
void mouse_func(int button ,int state, int x, int y)
{
if (state == GLUT_DOWN) {
MouseState.pos[0] = x;
MouseState.pos[1] = y;
MouseState.button = button;
}
}
void motion_func(int x, int y)
{
int x_move, y_move;
x_move = x - MouseState.pos[0];
y_move = y - MouseState.pos[1];
switch(MouseState.button){
case GLUT_LEFT_BUTTON:
CameraState.azimuth += (float) x_move/2.0;
CameraState.elevation -= (float) y_move/2.0;
break;
case GLUT_MIDDLE_BUTTON:
CameraState.twist = fmod (CameraState.twist + x_move/3.0, 360.0);
break;
case GLUT_RIGHT_BUTTON:
CameraState.distance -= (float) y_move/60.0;
break;
}
MouseState.pos[0] = x;
MouseState.pos[1] = y;
glutPostRedisplay();
}
void reshape_func(int width, int height)
{
float aspect;
aspect = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(CameraState.fovy, aspect, CameraState.znear, CameraState.zfar);
glMatrixMode(GL_MODELVIEW);
}
void fpstimer(int value)
{
static unsigned int nframe_prev=0;
if(FLAGS.show_fps == 1){
printf("FPS : %d (%d, %d)\n", nframe - nframe_prev, nframe, nframe_prev);
}
nframe_prev = nframe;
glutTimerFunc(1000, fpstimer, 0);
}
static void setup_texture(Texture *tex)
{
const int TEX_BOX_SIZE = 32;
int i, j;
tex->width = 128;
tex->height = 128;
tex->img = (unsigned char *)malloc(tex->width * tex->height * 4 * sizeof(unsigned char));
for(i=0; i<tex->height; i++){
for(j=0; j<tex->width; j++){
if( (i / TEX_BOX_SIZE)&0x1 && (j / TEX_BOX_SIZE)&0x1 ){
tex->img[4 * (i*tex->width + j) + 0] = 255;
tex->img[4 * (i*tex->width + j) + 1] = 0;
tex->img[4 * (i*tex->width + j) + 2] = 0;
tex->img[4 * (i*tex->width + j) + 3] = 0;
}else{
tex->img[4 * (i*tex->width + j) + 0] = 0;
tex->img[4 * (i*tex->width + j) + 1] = 0;
tex->img[4 * (i*tex->width + j) + 2] = 0;
tex->img[4 * (i*tex->width + j) + 3] = 0;
}
}
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 4, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex->img);
}
static void reset_view(void)
{
CameraState.znear = 1.0;
CameraState.zfar = 80.0;
CameraState.fovy = 40.0;
CameraState.distance = 4.0;
CameraState.twist = 0.0;
CameraState.elevation = 90.0;
CameraState.azimuth = 0.0;
}
static void polarview(void)
{
glTranslatef( 0.0, 0.0, -CameraState.distance);
glRotatef( -CameraState.twist, 0.0, 0.0, 1.0);
glRotatef( -CameraState.elevation, 1.0, 0.0, 0.0);
glRotatef( -CameraState.azimuth, 0.0, 1.0, 0.0);
}
static void init (char *progname)
{
int width = 800, height = 800;
// initialize valiables
reset_view();
FLAGS.show_fps = 0;
// setup window
glutInitWindowPosition(0, 0);
glutInitWindowSize( width, height);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutCreateWindow(progname);
glClearColor (0.0, 0.0, 0.0, 1.0);
// setup glut callback funcs
glutKeyboardFunc(keyboard_func);
glutMouseFunc(mouse_func);
glutMotionFunc(motion_func);
glutReshapeFunc (reshape_func);
glutDisplayFunc(display_func);
glutTimerFunc(1000, fpstimer, 0);
// setup OpenGL environment
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
setup_texture(&_tex);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
init(argv[0]);
glutMainLoop();
return(0);
}