void renLightPosition(S_Renderer *pRenderer, const S_Coords *pPosition) { IZG_ASSERT(pRenderer && pPosition); pRenderer->light_position = *pPosition; }
void renMatSpecular(S_Renderer *pRenderer, const S_Material *pParams) { IZG_ASSERT(pRenderer && pParams); pRenderer->mat_specular = *pParams; }
void renMatDiffuse(S_Renderer *pRenderer, const S_Material *pParams) { IZG_ASSERT(pRenderer && pParams); pRenderer->mat_diffuse = *pParams; }
void renMatAmbient(S_Renderer *pRenderer, const S_Material *pParams) { IZG_ASSERT(pRenderer && pParams); pRenderer->mat_ambient = *pParams; }
void onKeyboard(SDL_KeyboardEvent *key) { int width, height; /* test existence rendereru */ IZG_ASSERT(renderer); /* vetveni podle stisknute klavesy */ switch( key->keysym.sym ) { /* ukonceni programu - klavesa Esc nebo q */ case SDLK_ESCAPE: case SDLK_q: quit = 1; break; /* prepnuti do celoobrazovkoveho modu */ case SDLK_f: SDL_WM_ToggleFullScreen(screen); break; /* prepnuti na studentsky renderer */ case SDLK_p: /* zapamatovani aktualni velikosti frame bufferu */ width = renderer->frame_w; height = renderer->frame_h; /* zruseni a znovu vytvoreni rendereru */ renderer->releaseFunc(&renderer); renderer = studrenCreate(); renderer->createBuffersFunc(renderer, width, height); /* zmena titulku okna */ SDL_WM_SetCaption(PROGRAM_TITLE_STUDENT, 0); break; /* prepnuti na originalni renderer */ case SDLK_o: width = renderer->frame_w; height = renderer->frame_h; renderer->releaseFunc(&renderer); renderer = renCreate(); renderer->createBuffersFunc(renderer, width, height); SDL_WM_SetCaption(PROGRAM_TITLE, 0); break; /* posun kamery pomoci klaves */ case SDLK_DOWN: renSceneMoveXY(renderer, 0, 1); break; case SDLK_UP: renSceneMoveXY(renderer, 0, -1); break; case SDLK_LEFT: renSceneMoveXY(renderer, -1, 0); break; case SDLK_RIGHT: renSceneMoveXY(renderer, 1, 0); break; default: break; } }
S_RGBA studrenPhongReflectance(S_Renderer *pRenderer, const S_Coords *point, const S_Coords *normal) { S_Coords lvec, eyevec, reflect; double diffuse, specular, reflect_scalar; double r, g, b; /* If = Ia + Id + Is */ S_RGBA color; IZG_ASSERT(pRenderer && point && normal); /* vektor ke zdroji svetla */ lvec = makeCoords(pRenderer->light_position.x - point->x, pRenderer->light_position.y - point->y, pRenderer->light_position.z - point->z); coordsNormalize(&lvec); /* vektor ke kamere */ eyevec = makeCoords(-point->x, -point->y, -pRenderer->camera_dist - point->z); coordsNormalize(&eyevec); /* ambientni cast -- Ia = (Al * Am) + (As * Am) */ /* As je barva sceny, muzeme zanedbat */ r = pRenderer->light_ambient.red * pRenderer->mat_ambient.red; g = pRenderer->light_ambient.green * pRenderer->mat_ambient.green; b = pRenderer->light_ambient.blue * pRenderer->mat_ambient.blue; /* difuzni cast -- Id = Dl * Dm * LambertTerm */ /* LambertTerm = dot(N, L) */ diffuse = lvec.x * normal->x + lvec.y * normal->y + lvec.z * normal->z; if( diffuse > 0 ) { r += diffuse * pRenderer->light_diffuse.red * pRenderer->mat_diffuse.red; g += diffuse * pRenderer->light_diffuse.green * pRenderer->mat_diffuse.green; b += diffuse * pRenderer->light_diffuse.blue * pRenderer->mat_diffuse.blue; } /* odraziva cast -- Is = Sm * Sl * pow( max( dot(R, E), 0.0), f ) */ /* R = reflect(-L, N) = 2 * dot(N, L) * N - L */ reflect_scalar = 2 * (normal->x * lvec.x + normal->y * lvec.y + normal->z * lvec.z); reflect.x = reflect_scalar * normal->x - lvec.x; reflect.y = reflect_scalar * normal->y - lvec.y; reflect.z = reflect_scalar * normal->z - lvec.z; specular = pow( MAX(reflect.x * eyevec.x + reflect.y * eyevec.y + reflect.z * eyevec.z, 0.0f), ((S_StudentRenderer*)pRenderer)->mat_shininess); r += specular * pRenderer->light_specular.red * pRenderer->mat_specular.red; g += specular * pRenderer->light_specular.green * pRenderer->mat_specular.green; b += specular * pRenderer->light_specular.blue * pRenderer->mat_specular.blue; /* saturace osvetleni*/ r = MIN(1, r); g = MIN(1, g); b = MIN(1, b); /* kreslici barva */ color.red = ROUND2BYTE(255 * r); color.green = ROUND2BYTE(255 * g); color.blue = ROUND2BYTE(255 * b); return color; }