-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel_view.c
282 lines (214 loc) · 5.4 KB
/
model_view.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "config.h"
#include "SDL.h"
#include "glew.h"
#include "model.h"
#include "utils.h"
#include <stdbool.h>
#include <SDL.h>
#include <glew.h>
#include <math.h>
GLuint texture;
Box model={
0,
{-4, -2, -6},
{8, 4, 12},
{16, 16},
5,
{
{{4, 2, 12},&(Box){1,{-4,-4,0},{8,8,8},{0,0}}},
{{-2, 2, 10},&(Box){2,{-2,-2,-10},{4,4,12},{40,16}}},
{{10, 2, 10},&(Box){3,{-2,-2,-10},{4,4,12},{40,16}}},
{{2, 2, 0},&(Box){4,{-2,-2,-12},{4,4,12},{0,16}}},
{{6, 2, 0},&(Box){5,{-2,-2,-12},{4,4,12},{0,16}}},
}
};
Vec4f rotationA[] = {
{0.0,0.0,8.0},
{8.0,0.0,0.0},
{30.0,0.0,0.0},
{-30.0,0.0,0.0},
{30.0,0.0,0.0},
{-30.0,0.0,0.0},
};
Vec4f rotationB[] = {
{0.0,0.0,-8.0},
{-8.0,0.0,0.0},
{-30.0,0.0,0.0},
{30.0,0.0,0.0},
{-30.0,0.0,0.0},
{30.0,0.0,0.0},
};
static float f(float x)
{
if(x<0.5)
return pow(x*x,1);
else
return pow(1-(x-2)*(x-1),1);
}
void draw()
{
static int mode=1;
static float phase=0.0;
phase+=(mode-0.5)*0.05;
if(phase>=1.0 || phase<0.0)
{
phase=clampf(phase,-1,1);
mode=1-mode;
}
Vec4f rot[6];
Vec4f p=(Vec4f){f(phase),f(phase),f(phase),f(phase)};
Vec4f q=(Vec4f){1-f(phase),1-f(phase),1-f(phase),1-f(phase)};
printf("%f %f %f %f\n",phase, p[0],q[0],p[0]+q[0]);
for(int i=0;i<lengthof(rotationA);i++)
{
rot[i]=rotationA[i]*p+rotationB[i]*q;
}
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D,texture);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glScalef(0.1,0.1,0.1);
drawBox(&model,rot);
glPopMatrix();
glBindTexture(GL_TEXTURE_2D,0);
}
void init()
{
texture=loadTexture("data/mob/char.png");
}
static SDL_Surface* screen;
static bool fullscreen=false;
static SDL_Rect window_rect={0,0,720,420};
static SDL_Rect fullscreen_rect;
static bool grab_mouse=false;
static float rotX=0.0;
static float rotY=1.0;
static float zoom=2.0;
static void grabMouse()
{
SDL_ShowCursor(!grab_mouse);
SDL_WM_GrabInput(grab_mouse?SDL_GRAB_ON:SDL_GRAB_OFF);
}
static void initVideo()
{
if(fullscreen)
screen=SDL_SetVideoMode(fullscreen_rect.w,fullscreen_rect.h,0,SDL_OPENGL|SDL_FULLSCREEN);
else
screen=SDL_SetVideoMode(window_rect.w,window_rect.h,0,SDL_OPENGL|SDL_RESIZABLE);
if(screen==NULL)
panic("unable to set video mode");
grabMouse();
glViewport(0,0,screen->w,screen->h);
float aspect=(float)screen->w/(float)screen->h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float d=0.1;
glFrustum(-aspect*d,aspect*d,1*d,-1*d,0.1,300);
}
__attribute__((noreturn)) static void quit()
{
SDL_Quit();
exit(0);
}
static bool handleEvent(const SDL_Event* event)
{
switch(event->type)
{
case SDL_QUIT:
quit();
case SDL_KEYDOWN:
switch(event->key.keysym.sym)
{
case SDLK_ESCAPE:
grab_mouse=!grab_mouse;
fullscreen&=grab_mouse;
initVideo();
return false;
case SDLK_F10:
quit();
case SDLK_F11:
fullscreen=!fullscreen;
grab_mouse|=fullscreen;
initVideo();
return false;
default:
return true;
}
break;
case SDL_VIDEORESIZE:
window_rect=(SDL_Rect){0,0,event->resize.w,event->resize.h};
initVideo();
return false;
case SDL_MOUSEBUTTONDOWN:
if(event->button.button==4)
{
zoom*=0.9;
}
else if(event->button.button==5)
{
zoom/=0.9;
}
return false;
case SDL_MOUSEMOTION:
if(grab_mouse||event->motion.state)
{
rotX-=event->motion.xrel/100.0;
rotY-=event->motion.yrel/100.0;
}
return false;
default:
return true;
}
}
static void drawScene()
{
glClearColor(0.5,0.5,0.5,0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
//glRotatef(0,0,1,rotX);
//glRotatef(0,1,0,rotY);
glTranslatef(0,0,-zoom);
glRotatef(rotY*180/M_PI,1,0,0);
glRotatef(rotX*180/M_PI,0,0,1);
glColor3f(0.75,0.75,0.75);
glBegin(GL_LINES);
for(int i=-10;i<11;i++)
{
glVertex3f( -1.0,i/10.0,0.0);
glVertex3f( 1.0,i/10.0,0.0);
glVertex3f(i/10.0, -1.0,0.0);
glVertex3f(i/10.0, 1.0,0.0);
}
glEnd();
glColor3f(1,1,1);
draw();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
const SDL_VideoInfo* video_info=SDL_GetVideoInfo();
fullscreen_rect=(SDL_Rect){0,0,video_info->current_w,video_info->current_h};
initVideo();
if(glewInit()!=GLEW_OK)
panic("glew error");
init();
while(true)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
handleEvent(&event);
}
int t=SDL_GetTicks();
drawScene();
assert(glGetError()==0);
SDL_GL_SwapBuffers();
const int delay=30-SDL_GetTicks()+t;
if(delay>0)
SDL_Delay(delay);
}
}