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