void geMatrixMult(float* m){ // geMatrix44Mult(ge_current_matrix, ge_current_matrix, m); float tmp[16]; geMatrix44Mult(tmp, ge_current_matrix, m); memcpy(ge_current_matrix, tmp, sizeof(float)*16); ge_current_matrix_update = true; }
void ge_Translate(float* m, float x, float y, float z){ float t[16]; ge_LoadIdentity(t); t[12] = x; t[13] = y; t[14] = z; geMatrix44Mult(m, m, t); }
void geRotate(float x, float y, float z){ float tx[16], ty[16], tz[16], tb[16]; if(x != 0.0f){ ge_LoadIdentity(tx); float cx = cosf(x); float sx = sinf(x); tx[1*4+1] = cx; tx[1*4+2] = sx; tx[2*4+1] = -sx; tx[2*4+2] = cx; memcpy(tb, ge_current_matrix, sizeof(float)*16); geMatrix44Mult(ge_current_matrix, tb, tx); } if(y != 0.0f){ ge_LoadIdentity(ty); float cy = cosf(y); float sy = sinf(y); ty[0*4+0] = cy; ty[0*4+2] = -sy; ty[2*4+0] = sy; ty[2*4+2] = cy; float tb[16]; memcpy(tb, ge_current_matrix, sizeof(float)*16); geMatrix44Mult(ge_current_matrix, tb, ty); } if(z != 0.0f){ ge_LoadIdentity(tz); float cz = cosf(z); float sz = sinf(z); tz[0*4+0] = cz; tz[0*4+1] = sz; tz[1*4+0] = -sz; tz[1*4+1] = cz; float tb[16]; memcpy(tb, ge_current_matrix, sizeof(float)*16); geMatrix44Mult(ge_current_matrix, tb, tz); } ge_current_matrix_update = true; }
void ge_Scale(float* m, float x, float y, float z){ float t[16]; ge_LoadIdentity(t); t[0] = x; t[5] = y; t[10] = z; geMatrix44Mult(m, m, t); }
void geTranslate(float x, float y, float z){ float t[16]; ge_LoadIdentity(t); t[12] = x; t[13] = y; t[14] = z; geMatrix44Mult(ge_current_matrix, ge_current_matrix, t); ge_current_matrix_update = true; }
void SendModelMatrices(){ geSendMatrix(GE_MATRIX_MODELVIEW, (float*)&ge_matrices[GE_MATRIX_MODELVIEW]); geSendMatrix(GE_MATRIX_NORMAL, (float*)&ge_matrices[GE_MATRIX_NORMAL]); float temp[16]; //9 geMatrix44Mult(temp, ge_matrices[GE_MATRIX_PROJECTION], ge_matrices[GE_MATRIX_MODELVIEW]); glUniformMatrix4fv(loc_mvp, 1, GL_FALSE, temp); // PrintMatrix("MatrixModelView", (float*)&ge_matrices[GE_MATRIX_MODELVIEW], 4); // PrintMatrix("MatrixNormal", (float*)&ge_matrices[GE_MATRIX_NORMAL], 4); }
void geScale(float x, float y, float z){ float t[16]; ge_LoadIdentity(t); t[0] = x; t[5] = y; t[10] = z; geMatrix44Mult(ge_current_matrix, ge_current_matrix, t); ge_current_matrix_update = true; }
void geLookAt(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ){ float m[16]; float f[4]; float u[3]; float s[4]; f[0] = centerX - eyeX; f[1] = centerY - eyeY; f[2] = centerZ - eyeZ; f[3] = 0.0; geNormalize(f); geCrossProduct(s, f, u_vec); s[3] = 0.0; geNormalize(s); geCrossProduct(u, s, f); m[0] = s[0]; m[4] = s[1]; m[8] = s[2]; m[12]= 0.0; m[1] = u[0]; m[5] = u[1]; m[9] = u[2]; m[13]= 0.0; m[2] = -f[0]; m[6] = -f[1]; m[10]= -f[2]; m[14]= 0.0; m[3] = 0.0; m[7] = 0.0; m[11]= 0.0; m[15]= 1.0; float result[16] = { 0.0 }; geMatrix44Mult(result, ge_current_matrix, m); memcpy(ge_current_matrix, result, sizeof(float)*16); geTranslate(-eyeX, -eyeY, -eyeZ); ge_current_matrix_update = true; }
void CalculateModelMatrices(){ float ModelView3[9]; float temp[16]; //9 geMatrix44Mult(ge_matrices[GE_MATRIX_MODELVIEW], ge_matrices[GE_MATRIX_VIEW], ge_matrices[GE_MATRIX_MODEL]); ModelView3[0]=ge_matrices[GE_MATRIX_MODELVIEW][0]; ModelView3[1]=ge_matrices[GE_MATRIX_MODELVIEW][1]; ModelView3[2]=ge_matrices[GE_MATRIX_MODELVIEW][2]; ModelView3[3]=ge_matrices[GE_MATRIX_MODELVIEW][4]; ModelView3[4]=ge_matrices[GE_MATRIX_MODELVIEW][5]; ModelView3[5]=ge_matrices[GE_MATRIX_MODELVIEW][6]; ModelView3[6]=ge_matrices[GE_MATRIX_MODELVIEW][8]; ModelView3[7]=ge_matrices[GE_MATRIX_MODELVIEW][9]; ModelView3[8]=ge_matrices[GE_MATRIX_MODELVIEW][10]; geMatrixInverse(temp, ModelView3, 3); geMatrixTranspose(ge_matrices[GE_MATRIX_NORMAL], temp, 3); /* float temp[16]; //9 geMatrixInverse(temp, ge_matrices[GE_MATRIX_MODELVIEW], 4); geMatrixTranspose(temp2, temp, 4); ge_matrices[GE_MATRIX_NORMAL][0]=temp2[0]; ge_matrices[GE_MATRIX_NORMAL][1]=temp2[1]; ge_matrices[GE_MATRIX_NORMAL][2]=temp2[2]; ge_matrices[GE_MATRIX_NORMAL][3]=temp2[4]; ge_matrices[GE_MATRIX_NORMAL][4]=temp2[5]; ge_matrices[GE_MATRIX_NORMAL][5]=temp2[6]; ge_matrices[GE_MATRIX_NORMAL][6]=temp2[8]; ge_matrices[GE_MATRIX_NORMAL][7]=temp2[9]; ge_matrices[GE_MATRIX_NORMAL][8]=temp2[10]; */ }
void geLightComputeShadow(ge_Light* light, ge_Camera* cam, void (*render)(void*), void* udata){ geFramebufferUse(light->shadow_fbo); geViewport(0, 0, light->shadow_fbo->depth->width, light->shadow_fbo->depth->height); glDisable(GL_SCISSOR_TEST); float save_proj[16]; memcpy(save_proj, geGetMatrix(GE_MATRIX_PROJECTION), sizeof(float) * 16); u32 save_clear_color = libge_context->clear_color; geClearColor(0xFFFFFFFF); // glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); //glDisable(GL_CULL_FACE); //glEnable(GL_CULL_FACE); //glCullFace(GL_BACK); glFrontFace(GL_CCW); geForceShader(light->shadow_shader); geMatrixMode(GE_MATRIX_VIEW); geLoadIdentity(); float l_pos[3] = { light->position.x, light->position.y, light->position.z }; geNormalize(l_pos); l_pos[0] *= 10000.0; l_pos[1] *= 10000.0; l_pos[2] *= 10000.0; float cCam[3] = { cam->cX - cam->x, cam->cY - cam->y, cam->cZ - cam->z }; geNormalize(cCam); cCam[0] = cCam[0] * 10.0 + cam->x; cCam[1] = cCam[1] * 10.0 + cam->y; cCam[2] = cCam[2] * 10.0 + cam->z; l_pos[0] += cCam[0]; l_pos[1] += cCam[1]; l_pos[2] += cCam[2]; geLookAt(l_pos[0], l_pos[1], l_pos[2], light->target.x + cCam[0], light->target.y + cCam[1], light->target.z + cCam[2]); static ge_Camera fake_cam; fake_cam.x = l_pos[0]; fake_cam.y = l_pos[1]; fake_cam.z = l_pos[2]; ge_Camera* cam_save = ge_current_camera; ge_current_camera = &fake_cam; /* float cCam[3] = { cam->cX - cam->x, cam->cY - cam->y, cam->cZ - cam->z }; geNormalize(cCam); cCam[0] = cCam[0] * 20.0 + cam->x; cCam[1] = cCam[1] * 20.0 + cam->y; cCam[2] = cCam[2] * 20.0 + cam->z; l_pos[0] += cCam[0]; l_pos[1] += cCam[1]; l_pos[2] += cCam[2]; geLookAt(l_pos[0], l_pos[1], l_pos[2], light->target.x + cCam[0], light->target.y + cCam[1], light->target.z + cCam[2]); */ // geLookAt(light->position.x, light->position.y, light->position.z, light->position.x + light->target.x * 10.0, light->position.y + light->target.y * 10.0, light->position.z + light->target.z * 10.0); int i, j; light->iShadow = max(1, (light->iShadow + 1) % light->shadow_depth); // for(j=0; j<3; j++){ for(j=0; j<2 && j<light->shadow_depth; j++){ if(j == 0){ i = 0; }else{ i = max(1, (light->iShadow + j) % light->shadow_depth); } glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, light->shadow->id, 0, i); //glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, light->shadow->id, 0, i); //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, light->shadow_fbo->depth->id, 0); geClearScreen(); geMatrixMode(GE_MATRIX_PROJECTION); geLoadIdentity(); if(light->type == GE_LIGHT_TYPE_SPOT){ gePerspective(90.0, 1.0, 0.001, 500.0); //light->spot_cutoff }else{ //float range = 10.0 * expf((float)i * 1.0); float range = 10.0 * powf(4.0, (float)i * light->shadow_factor); //float range = 10.0 * powf(2.0, (float)i * 1.0); // geOrthogonal(-range, range, -range, range, 1.0, 5000.0); geOrthogonal(-range, range, -range, range, 1.0, 50000.0); } float bias[16] = { 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0 }; float* m_projection = geGetMatrix(GE_MATRIX_PROJECTION); float* m_view = geGetMatrix(GE_MATRIX_VIEW); float m_1[16] = { 0.0 }; geMatrix44Mult(m_1, m_projection, m_view); geMatrixMode(GE_MATRIX_TEXTURE7 - i); geLoadMatrix(bias); geMatrixMult(m_1); //geForceCap(GL_DEPTH_TEST, false); render(udata); //geForceCap(GL_DEPTH_TEST, -1); } //glEnable(GL_CULL_FACE); //glCullFace(GL_FRONT); glFrontFace(GL_CW); geForceShader(NULL); geFramebufferUse(NULL); // geClearMode(last_clear_mode); glEnable(GL_SCISSOR_TEST); // glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); geViewport(0, 0, geGetContext()->width, geGetContext()->height); geClearColor(save_clear_color); memcpy(libge_context->projection_matrix, save_proj, sizeof(float)*16); memcpy(geGetMatrix(GE_MATRIX_PROJECTION), save_proj, sizeof(float)*16); ge_current_camera = cam_save; }