Mercurial > hg > Game > Cerium
annotate Renderer/Engine/viewerGL.cc @ 1379:13065ad17328 draft
collada moved but only my mac.
author | e095732 <e095732@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 26 Jan 2012 21:56:32 +0900 |
parents | ab9b7d21b32b |
children |
rev | line source |
---|---|
922 | 1 #include "viewerGL.h" |
1128 | 2 #include "polygon_pack.h" |
922 | 3 |
4 | |
5 static void | |
6 ApplyMatrix(float *v, float *m) | |
7 { | |
944 | 8 float t[4]; |
922 | 9 |
944 | 10 t[0] = v[0]; |
11 t[1] = v[1]; | |
12 t[2] = v[2]; | |
13 t[3] = v[3]; | |
922 | 14 |
944 | 15 for (int i = 0; i < 4; i++) { |
16 v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8] + t[3]*m[i+12]; | |
17 } | |
922 | 18 } |
1042
d0bb27bf985b
AllExecute speparation.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1031
diff
changeset
|
19 #if 0 |
922 | 20 static void |
21 ApplyNormalMatrix(float *v, float *m) | |
22 { | |
944 | 23 float t[4]; |
922 | 24 |
944 | 25 t[0] = v[0]; |
26 t[1] = v[1]; | |
27 t[2] = v[2]; | |
922 | 28 |
944 | 29 for (int i = 0; i < 3; i++) { |
30 v[i] = t[0]*m[i] + t[1]*m[i+4] + t[2]*m[i+8]; | |
31 } | |
922 | 32 } |
1042
d0bb27bf985b
AllExecute speparation.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
1031
diff
changeset
|
33 #endif |
922 | 34 |
944 | 35 ViewerGL::ViewerGL(TaskManager *m, int b, int w, int h, int _num) |
922 | 36 { |
944 | 37 spe_num = _num; |
38 manager = m; | |
922 | 39 |
944 | 40 quit_flag = false; |
41 start_time = 0; | |
42 this_time = 0; | |
43 frames = 0; | |
927
fada580e4604
remove garbage codes from viewerGL
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
922
diff
changeset
|
44 |
922 | 45 video_init(b, w, h); |
46 } | |
47 | |
48 void | |
944 | 49 ViewerGL::video_init(int bpp, int width, int height) |
922 | 50 { |
944 | 51 SDL_Surface *screen; |
52 int rgb_size[3]; | |
53 int value = 1; | |
54 Uint32 sdl_flag = SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK; | |
55 Uint32 video_flag = SDL_OPENGL; | |
56 | |
57 if (SDL_Init(sdl_flag) < 0) { | |
58 fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError()); | |
59 exit(1); | |
922 | 60 } |
944 | 61 |
62 /* See if we should detect the display depth */ | |
63 if ( bpp == 0 ) | |
64 { | |
65 if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8 ) | |
66 { | |
67 bpp = 8; | |
68 } | |
69 else | |
70 { | |
71 bpp = 32; | |
72 } | |
73 } | |
74 | |
75 /* Initialize the bpp */ | |
76 switch (bpp) | |
77 { | |
78 case 8: | |
79 rgb_size[0] = 3; | |
80 rgb_size[1] = 3; | |
81 rgb_size[2] = 2; | |
82 break; | |
83 case 15: | |
84 case 16: | |
85 rgb_size[0] = 5; | |
86 rgb_size[1] = 5; | |
87 rgb_size[2] = 5; | |
88 break; | |
89 default: | |
90 rgb_size[0] = 8; | |
91 rgb_size[1] = 8; | |
92 rgb_size[2] = 8; | |
93 break; | |
94 } | |
95 | |
96 screen = SDL_SetVideoMode(width, height, bpp, video_flag); | |
97 if (screen == NULL) { | |
98 fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError()); | |
99 SDL_Quit(); | |
100 exit(1); | |
101 } | |
102 this->width = screen->w; | |
103 this->height = screen->h; | |
104 this->bpp = screen->format->BitsPerPixel; | |
105 | |
106 //各パラメータがちゃんと取れているか確認 | |
107 printf("Screen BPP: %d\n", SDL_GetVideoSurface()->format->BitsPerPixel); | |
108 printf("\n"); | |
109 printf( "Vendor : %s\n", glGetString( GL_VENDOR ) ); | |
110 printf( "Renderer : %s\n", glGetString( GL_RENDERER ) ); | |
111 printf( "Version : %s\n", glGetString( GL_VERSION ) ); | |
112 printf( "Extensions : %s\n", glGetString( GL_EXTENSIONS ) ); | |
113 printf("\n"); | |
114 | |
115 SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value ); | |
116 printf( "SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0],value); | |
117 SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value ); | |
118 printf( "SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1],value); | |
119 SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value ); | |
120 printf( "SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2],value); | |
121 SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value ); | |
122 printf( "SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value ); | |
123 SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value ); | |
124 printf( "SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value ); | |
125 | |
126 //OpenGLの設定 | |
127 glViewport( 0, 0, width, height ); | |
128 glMatrixMode( GL_PROJECTION ); | |
129 glLoadIdentity( ); | |
130 | |
131 //正射影 | |
1031 | 132 //glOrtho( 0.0, width, height, 0.0, OPENGL_PARAM::near, OPENGL_PARAM::far ); |
133 glOrtho( 0.0, width, height, 0.0, OPENGL_PARAM::far, OPENGL_PARAM::near ); | |
944 | 134 |
135 glMatrixMode( GL_MODELVIEW ); | |
136 glLoadIdentity( ); | |
137 | |
138 //アルファブレンディング | |
139 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
140 | |
141 //光源 | |
142 glLightfv(GL_LIGHT0, GL_AMBIENT, OPENGL_PARAM::lightAmbient); | |
143 glLightfv(GL_LIGHT0, GL_DIFFUSE, OPENGL_PARAM::lightDiffuse); | |
144 glLightfv(GL_LIGHT0, GL_SPECULAR, OPENGL_PARAM::lightSpecular); | |
145 glLightfv(GL_LIGHT0, GL_POSITION, OPENGL_PARAM::lightPosition); | |
146 | |
960
418939c6837d
success alpha blending neatly
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
959
diff
changeset
|
147 glEnable(GL_DEPTH_TEST); |
418939c6837d
success alpha blending neatly
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
959
diff
changeset
|
148 glEnable(GL_ALPHA_TEST); |
418939c6837d
success alpha blending neatly
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
959
diff
changeset
|
149 glAlphaFunc(GL_GREATER, 0); |
944 | 150 glDepthFunc(GL_LESS); |
1031 | 151 //glShadeModel(GL_SMOOTH); |
922 | 152 } |
153 | |
154 void | |
944 | 155 ViewerGL::mainLoop() |
922 | 156 { |
944 | 157 glEnable(GL_LIGHTING); |
158 glEnable(GL_LIGHT0); | |
159 glEnable(GL_BLEND); | |
160 | |
161 while(!quit_flag) { | |
162 run_loop(); | |
163 } | |
922 | 164 } |
165 | |
166 void | |
944 | 167 ViewerGL::run_loop() |
922 | 168 { |
944 | 169 clear_screen(); |
922 | 170 |
944 | 171 quit_flag = quit_check(); |
172 if (quit_flag == true) { | |
173 this_time = get_ticks(); | |
174 run_finish(); | |
175 return; | |
176 } | |
177 | |
178 clean_pixels(); | |
179 | |
180 sgroot->updateControllerState(); | |
181 sgroot->allExecute(width, height); | |
182 light_xyz_stock = sgroot->getLightVector(); | |
183 light_switch_stock = sgroot->getLightSwitch(); | |
184 light_sysswitch_stock = sgroot->getLightSysSwitch(); | |
185 pickup_vertex(); | |
186 | |
187 psx_sync_n(); | |
188 frames++; | |
922 | 189 } |
190 | |
191 void | |
944 | 192 ViewerGL::pickup_vertex() |
922 | 193 { |
944 | 194 float xyz1[4], xyz2[4], xyz3[4]; |
195 float tex_xy1[2], tex_xy2[2], tex_xy3[2]; | |
196 float normal1[4],normal2[4],normal3[4]; | |
197 GLuint texture; | |
198 | |
199 SceneGraphPtr sg_top = sgroot->getDrawSceneGraph(); | |
200 SceneGraphPtr sg = sg_top; | |
201 | |
202 while (sg) { | |
203 if (sg->flag_drawable) { | |
1130 | 204 if (!sg->texture_info->gl_tex) { |
205 sg->texture_info->gl_tex = SDL_GL_LoadTexture(sg->texture_info->texture_image); | |
944 | 206 } |
1302
ab9b7d21b32b
removed real_matrix. sparated screen matrix from camera matrix.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1130
diff
changeset
|
207 |
ab9b7d21b32b
removed real_matrix. sparated screen matrix from camera matrix.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1130
diff
changeset
|
208 float normal_matrix[16]; |
ab9b7d21b32b
removed real_matrix. sparated screen matrix from camera matrix.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1130
diff
changeset
|
209 |
ab9b7d21b32b
removed real_matrix. sparated screen matrix from camera matrix.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1130
diff
changeset
|
210 for (int i = 0; i<16; i++) normal_matrix[i]=sg->matrix[i]; |
ab9b7d21b32b
removed real_matrix. sparated screen matrix from camera matrix.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1130
diff
changeset
|
211 normal_matrix[4*0+3]=normal_matrix[4*1+3]=normal_matrix[4*2+3]=0; |
ab9b7d21b32b
removed real_matrix. sparated screen matrix from camera matrix.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1130
diff
changeset
|
212 |
1130 | 213 texture = sg->texture_info->gl_tex; |
944 | 214 glBindTexture(GL_TEXTURE_2D, texture); |
922 | 215 |
944 | 216 glEnable(GL_TEXTURE_2D); |
217 glBegin( GL_TRIANGLES); | |
1128 | 218 |
219 for (int i = 0; i < sg->pp_num; i++) { | |
220 for (int j = 0; j < sg->pp[i].info.size; j++) { | |
221 TrianglePack tri = sg->pp[i].tri[j]; | |
222 | |
223 xyz1[0] = tri.ver1.x; | |
224 xyz1[1] = tri.ver1.y; | |
225 xyz1[2] = tri.ver1.z * -1.0f; | |
944 | 226 xyz1[3] = 1.0f; |
227 | |
1128 | 228 xyz2[0] = tri.ver2.x; |
229 xyz2[1] = tri.ver2.y; | |
230 xyz2[2] = tri.ver2.z * -1.0f; | |
944 | 231 xyz2[3] = 1.0f; |
232 | |
1128 | 233 xyz3[0] = tri.ver3.x; |
234 xyz3[1] = tri.ver3.y; | |
235 xyz3[2] = tri.ver3.z * -1.0f; | |
944 | 236 xyz3[3] = 1.0f; |
237 | |
238 // sg->matrix = 回転行列*透視変換行列 | |
239 ApplyMatrix(xyz1, sg->matrix); | |
240 ApplyMatrix(xyz2, sg->matrix); | |
241 ApplyMatrix(xyz3, sg->matrix); | |
242 | |
1031 | 243 |
944 | 244 xyz1[0] /= xyz1[2]; |
245 xyz1[1] /= xyz1[2]; | |
246 xyz2[0] /= xyz2[2]; | |
247 xyz2[1] /= xyz2[2]; | |
248 xyz3[0] /= xyz3[2]; | |
249 xyz3[1] /= xyz3[2]; | |
1031 | 250 |
1128 | 251 tex_xy1[0] = tri.ver1.tex_x; |
252 tex_xy1[1] = tri.ver1.tex_y; | |
253 tex_xy2[0] = tri.ver2.tex_x; | |
254 tex_xy2[1] = tri.ver2.tex_y; | |
255 tex_xy3[0] = tri.ver3.tex_x; | |
256 tex_xy3[1] = tri.ver3.tex_y; | |
1031 | 257 |
1128 | 258 normal1[0] = tri.normal1.x; |
259 normal1[1] = tri.normal1.y; | |
260 normal1[2] = tri.normal1.z * -1.0f; | |
261 | |
944 | 262 normal1[3] = 0.0f; |
263 | |
1128 | 264 normal2[0] = tri.normal2.x; |
265 normal2[1] = tri.normal2.y; | |
266 normal2[2] = tri.normal2.z * -1.0f; | |
267 | |
944 | 268 normal2[3] = 0.0f; |
269 | |
1128 | 270 normal3[0] = tri.normal3.x; |
271 normal3[1] = tri.normal3.y; | |
272 normal3[2] = tri.normal3.z * -1.0f; | |
273 | |
944 | 274 normal3[3] = 0.0f; |
275 | |
1302
ab9b7d21b32b
removed real_matrix. sparated screen matrix from camera matrix.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1130
diff
changeset
|
276 ApplyMatrix(normal1, normal_matrix); |
ab9b7d21b32b
removed real_matrix. sparated screen matrix from camera matrix.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1130
diff
changeset
|
277 ApplyMatrix(normal2, normal_matrix); |
ab9b7d21b32b
removed real_matrix. sparated screen matrix from camera matrix.
Yutaka_Kinjyo <yutaka@cr.ie.u-ryukyu.ac.jp>
parents:
1130
diff
changeset
|
278 ApplyMatrix(normal3, normal_matrix); |
944 | 279 |
280 obj_draw(xyz1, tex_xy1, normal1); | |
281 obj_draw(xyz2, tex_xy2, normal2); | |
282 obj_draw(xyz3, tex_xy3, normal3); | |
1128 | 283 } |
944 | 284 } |
285 glEnd( ); | |
286 glDisable(GL_TEXTURE_2D); | |
922 | 287 } |
944 | 288 |
289 if (sg->children != NULL) { | |
290 sg = sg->children; | |
291 } else if (sg->brother != NULL) { | |
292 sg = sg->brother; | |
293 } else { | |
294 while (sg) { | |
295 if (sg->brother != NULL) { | |
296 sg = sg->brother; | |
922 | 297 break; |
298 } else { | |
944 | 299 if (sg->parent == NULL) { |
300 sg = NULL; | |
301 break; | |
302 } else { | |
303 sg = sg->parent; | |
304 } | |
922 | 305 } |
306 } | |
307 } | |
308 } | |
309 } | |
310 | |
311 void | |
944 | 312 ViewerGL::obj_draw(float *xyz, float *tex_xyz, float *normal_xyz) |
922 | 313 { |
1031 | 314 |
315 | |
944 | 316 glTexCoord2f(tex_xyz[0], tex_xyz[1]); |
317 glVertex3f(xyz[0], xyz[1], xyz[2]); | |
318 glNormal3f(normal_xyz[0], normal_xyz[1], normal_xyz[2]); | |
922 | 319 } |
320 | |
321 void | |
944 | 322 ViewerGL::clean_pixels() |
922 | 323 { |
944 | 324 glClearColor( 0.0, 0.0, 0.0, 1.0 ); |
325 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
922 | 326 } |
327 | |
328 void | |
944 | 329 ViewerGL::clear_screen() |
922 | 330 { |
944 | 331 GLenum gl_error; |
332 char* sdl_error; | |
333 | |
334 SDL_GL_SwapBuffers( ); | |
335 | |
336 /* Check for error conditions. */ | |
337 gl_error = glGetError( ); | |
338 | |
339 if( gl_error != GL_NO_ERROR ) | |
340 { | |
341 fprintf( stderr, "OpenGL error: %d\n", gl_error ); | |
342 } | |
343 | |
344 sdl_error = SDL_GetError( ); | |
345 | |
346 if( sdl_error[0] != '\0' ) | |
347 { | |
348 fprintf(stderr, "SDL error '%s'\n", sdl_error); | |
349 SDL_ClearError(); | |
350 } | |
922 | 351 } |
352 | |
353 void | |
944 | 354 ViewerGL::run_finish() |
922 | 355 { |
960
418939c6837d
success alpha blending neatly
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
959
diff
changeset
|
356 glDisable(GL_ALPHA_TEST); |
418939c6837d
success alpha blending neatly
koba <koba@cr.ie.u-ryukyu.ac.jp>
parents:
959
diff
changeset
|
357 glDisable(GL_DEPTH_TEST); |
944 | 358 glDisable(GL_BLEND); |
359 glDisable(GL_LIGHT0); | |
360 glDisable(GL_LIGHTING); | |
361 | |
362 if (this_time != start_time) { | |
363 printf("%f FPS\n", (((float)frames)/(this_time-start_time))*1000.0); | |
364 } | |
365 | |
366 delete sgroot; | |
367 quit(); | |
922 | 368 } |
369 | |
370 /* end */ |