void GFX_multiply_matrix( mat4 *m ) { switch( gfx.matrix_mode ) { case MODELVIEW_MATRIX: { mat4_multiply_mat4( GFX_get_modelview_matrix(), GFX_get_modelview_matrix(), m ); break; } case PROJECTION_MATRIX: { mat4_multiply_mat4( GFX_get_projection_matrix(), GFX_get_projection_matrix(), m ); break; } case TEXTURE_MATRIX: { mat4_multiply_mat4( GFX_get_texture_matrix(), GFX_get_texture_matrix(), m ); break; } } }
void mat4_ortho( mat4 *dst, float left, float right, float bottom, float top, float clip_start, float clip_end ) { mat4 mat; mat.m[ 0 ].x = 2.0f / ( right - left ); mat.m[ 1 ].x = 0.0f; mat.m[ 2 ].x = 0.0f; mat.m[ 3 ].x = -( right + left ) / ( right - left ); mat.m[ 0 ].y = 0.0f; mat.m[ 1 ].y = 2.0f / ( top - bottom ); mat.m[ 2 ].y = 0.0f; mat.m[ 3 ].y = -( top + bottom ) / ( top - bottom ); mat.m[ 0 ].z = 0.0f; mat.m[ 1 ].z = 0.0f; mat.m[ 2 ].z = -2.0f / ( clip_end - clip_start ); mat.m[ 3 ].z = -( clip_end + clip_start ) / ( clip_end - clip_start ); mat.m[ 0 ].w = mat.m[ 1 ].w = mat.m[ 2 ].w = 0.0f; mat.m[ 3 ].w = 1.0f; mat4_multiply_mat4( dst, dst, &mat ); }
mat4 *GFX_get_modelview_projection_matrix( void ) { mat4_multiply_mat4( &gfx.modelview_projection_matrix, GFX_get_projection_matrix(), GFX_get_modelview_matrix() ); return &gfx.modelview_projection_matrix; }
void mat4_rotate( mat4 *dst, mat4 *m, vec4 *v ) { float s = sinf( v->w * DEG_TO_RAD ), c = cosf( v->w * DEG_TO_RAD ), xx, yy, zz, xy, yz, zx, xs, ys, zs, c1; mat4 mat; vec3 t = { v->x, v->y, v->z }; mat4_identity( &mat ); if( !v->w || !vec3_normalize( &t, &t ) ) return; xx = t.x * t.x; yy = t.y * t.y; zz = t.z * t.z; xy = t.x * t.y; yz = t.y * t.z; zx = t.z * t.x; xs = t.x * s; ys = t.y * s; zs = t.z * s; c1 = 1.0f - c; mat.m[ 0 ].x = ( c1 * xx ) + c; mat.m[ 1 ].x = ( c1 * xy ) - zs; mat.m[ 2 ].x = ( c1 * zx ) + ys; mat.m[ 0 ].y = ( c1 * xy ) + zs; mat.m[ 1 ].y = ( c1 * yy ) + c; mat.m[ 2 ].y = ( c1 * yz ) - xs; mat.m[ 0 ].z = ( c1 * zx ) - ys; mat.m[ 1 ].z = ( c1 * yz ) + xs; mat.m[ 2 ].z = ( c1 * zz ) + c; mat4_multiply_mat4( m, m, &mat ); }
void draw_scene_from_projector(void) { GFX_set_matrix_mode(PROJECTION_MATRIX); GFX_load_identity(); GFX_set_perspective(light->spot_fov, (float)viewport_matrix[2] / (float)viewport_matrix[3], 1.0f, 20.0f, -90.0f); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); GFX_look_at((vec3 *) & light->position, ¢er, &up_axis); projector_matrix.m[0].x = 0.5f; projector_matrix.m[0].y = 0.0f; projector_matrix.m[0].z = 0.0f; projector_matrix.m[0].w = 0.0f; projector_matrix.m[1].x = 0.0f; projector_matrix.m[1].y = 0.5f; projector_matrix.m[1].z = 0.0f; projector_matrix.m[1].w = 0.0f; projector_matrix.m[2].x = 0.0f; projector_matrix.m[2].y = 0.0f; projector_matrix.m[2].z = 0.5f; projector_matrix.m[2].w = 0.0f; projector_matrix.m[3].x = 0.5f; projector_matrix.m[3].y = 0.5f; projector_matrix.m[3].z = 0.5f; projector_matrix.m[3].w = 1.0f; mat4_multiply_mat4(&projector_matrix, &projector_matrix, GFX_get_modelview_projection_matrix()); glBindFramebuffer(GL_FRAMEBUFFER, shadowmap_buffer); glViewport(0, 0, shadowmap_width, shadowmap_height); glClear(GL_DEPTH_BUFFER_BIT); glCullFace(GL_FRONT); unsigned int i = 0; PROGRAM *program = OBJ_get_program(obj, "writedepth", 0); while (i != obj->n_objmaterial) { obj->objmaterial[i].program = program; ++i; } i = 0; while (i != obj->n_objmesh) { objmesh = &obj->objmesh[i]; GFX_push_matrix(); GFX_translate(objmesh->location.x, objmesh->location.y, objmesh->location.z); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); ++i; } glCullFace(GL_BACK); }
void draw_scene_from_projector( void ) { GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_perspective( light->spot_fov, ( float )viewport_matrix[ 2 ] / ( float )viewport_matrix[ 3 ], 1.0f, 20.0f, -90.0f ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); GFX_look_at( ( vec3 * )&light->position, ¢er, &up_axis ); projector_matrix.m[ 0 ].x = 0.5f; projector_matrix.m[ 0 ].y = 0.0f; projector_matrix.m[ 0 ].z = 0.0f; projector_matrix.m[ 0 ].w = 0.0f; projector_matrix.m[ 1 ].x = 0.0f; projector_matrix.m[ 1 ].y = 0.5f; projector_matrix.m[ 1 ].z = 0.0f; projector_matrix.m[ 1 ].w = 0.0f; projector_matrix.m[ 2 ].x = 0.0f; projector_matrix.m[ 2 ].y = 0.0f; projector_matrix.m[ 2 ].z = 0.5f; projector_matrix.m[ 2 ].w = 0.0f; projector_matrix.m[ 3 ].x = 0.5f; projector_matrix.m[ 3 ].y = 0.5f; projector_matrix.m[ 3 ].z = 0.5f; projector_matrix.m[ 3 ].w = 1.0f; mat4_multiply_mat4( &projector_matrix, &projector_matrix, GFX_get_modelview_projection_matrix() ); }
void mat4_rotate_fast( mat4 *m, vec4 *v ) { float s = sinf( v->w * DEG_TO_RAD ), c = cosf( v->w * DEG_TO_RAD ); mat4 mat; mat4_identity( &mat ); if( !v->x ) { if( !v->y ) { if( v->z ) { mat.m[ 0 ].x = c; mat.m[ 1 ].y = c; if( v->z < 0.0f ) { mat.m[ 1 ].x = s; mat.m[ 0 ].y = -s; } else { mat.m[ 1 ].x = -s; mat.m[ 0 ].y = s; } } } else if( !v->z ) { mat.m[ 0 ].x = c; mat.m[ 2 ].z = c; if( v->y < 0.0f ) { mat.m[ 2 ].x = -s; mat.m[ 0 ].z = s; } else { mat.m[ 2 ].x = s; mat.m[ 0 ].z = -s; } } } else if( !v->y ) { if( !v->z ) { mat.m[ 1 ].y = c; mat.m[ 2 ].z = c; if( v->x < 0.0f ) { mat.m[ 2 ].y = s; mat.m[ 1 ].z = -s; } else { mat.m[ 2 ].y = -s; mat.m[ 1 ].z = s; } } } mat4_multiply_mat4( m, m, &mat ); }