void geObjectDraw(ge_Object* obj){ /* geMatrixMode(GE_MATRIX_MODEL); geLoadMatrix(obj->matrix); CalculateModelMatrices(); geUpdateMatrix(); if(obj->material.textures[0]){ glBindTexture(GL_TEXTURE_2D, obj->material.textures[0]->id); }else{ glBindTexture(GL_TEXTURE_2D, 0); } glDrawArrays(GL_TRIANGLES, obj->vert_start, obj->nVerts); */ geMatrixMode(GE_MATRIX_MODEL); geLoadIdentity(); if(obj->matrix_used){ geLoadMatrix(obj->matrix); } geUpdateMatrix(); if(obj->material.textures[0]){ glActiveTexture(GL_TEXTURE0); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, obj->material.textures[0]->id); }else{ glActiveTexture(GL_TEXTURE0); glDisable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); } glDrawArrays(GL_TRIANGLES, obj->vert_start, obj->nVerts); }
int geDrawingMode(int mode){ int last = libge_context->drawing_mode; if(mode != libge_context->drawing_mode || mode & 0xF0000000){ if(mode & GE_DRAWING_MODE_2D){ glActiveTexture(GL_TEXTURE0); glEnable(GL_TEXTURE_2D); glDisable(GL_CULL_FACE); glEnable(GL_ALPHA_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); geMatrixMode(GE_MATRIX_PROJECTION); geLoadIdentity(); geOrthogonal(0.0, libge_context->width, libge_context->height, 0.0, -2049.0, 2049.0); geMatrixMode(GE_MATRIX_VIEW); geLoadIdentity(); geMatrixMode(GE_MATRIX_MODEL); geLoadIdentity(); geMatrixMode(GE_MATRIX_MODELVIEW); geLoadIdentity(); geMatrixMode(GE_MATRIX_TEXTURE0); geLoadIdentity(); geUpdateMatrix(); if(mode & GE_DRAWING_2D_DEPTH){ glEnable(GL_DEPTH_TEST); geClearMode(GE_CLEAR_COLOR_BUFFER | GE_CLEAR_DEPTH_BUFFER); }else{ glDisable(GL_DEPTH_TEST); geClearMode(GE_CLEAR_COLOR_BUFFER); } geShaderUse(_ge_GetVideoContext()->shader2d); glActiveTexture(GL_TEXTURE0); } if(mode & GE_DRAWING_MODE_3D){ glEnable(GL_CULL_FACE); glFrontFace(GL_CW); glEnable(GL_DEPTH_TEST); geClearMode(GE_CLEAR_COLOR_BUFFER | GE_CLEAR_DEPTH_BUFFER); geMatrixMode(GE_MATRIX_PROJECTION); geLoadIdentity(); if(libge_context->projection_matrix[0] == (float)0xFFFFFFFF){ gePerspective(60.0, (float)libge_context->width / (float)libge_context->height, 0.1, 100000.0); }else{ geLoadMatrix(libge_context->projection_matrix); } } } libge_context->drawing_mode = mode; return last; }
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; }
void geRenderObjects(ge_Renderer* render){ geRendererUse(render); geMatrixMode(GE_MATRIX_MODEL); geLoadIdentity(); int i=0; int j=0; // int current_tex_id[8] = { 0 }; int default_matrix_ok = false; int update_matrices = true; for(i=0; i<render->nObjs; i++){ ge_Object* obj = render->objs[i]; // gePrintDebug(0x100, "Object %d/%d \"%s\", vert_start: %d, nVerts: %d\n", i+1, render->nObjs, obj->name, obj->vert_start, obj->nVerts); if(obj->vert_start<0)continue; if(obj->matrix_used){ geLoadMatrix(obj->matrix); default_matrix_ok = false; update_matrices = true; }else if(!default_matrix_ok){ geLoadIdentity(); default_matrix_ok = true; update_matrices = true; } if(render->ext_func){ render->ext_func(render, i); } if(update_matrices){ // CalculateModelMatrices(); geUpdateMatrix(); // StaticLightingFunc2(current_scene, render, -1); update_matrices = false; } bool textured = false; for(j=7; j>=0; j--){ if(obj->material.textures[j]){ textured = true; if(obj->material.textures[j]->flags & GE_IMAGE_3D){ glActiveTexture(GL_TEXTURE0+j); glEnable(GL_TEXTURE_3D); glBindTexture(GL_TEXTURE_3D, obj->material.textures[j]->id); }else{ glActiveTexture(GL_TEXTURE0+j); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, obj->material.textures[j]->id); char tmp[32] = "ge_Texture"; if(j)sprintf(tmp, "ge_Texture%d", j); glUniform1i(glGetUniformLocation(render->shader->programId, tmp), j); } }else{ // glActiveTexture(GL_TEXTURE0+j); // glDisable(GL_TEXTURE_2D); // break; } } glUniform4f(render->shader->loc_front_ambient, obj->material.ambient[0], obj->material.ambient[1], obj->material.ambient[2], obj->material.ambient[3]); glUniform4f(render->shader->loc_front_diffuse, obj->material.diffuse[0], obj->material.diffuse[1], obj->material.diffuse[2], obj->material.diffuse[3]); glUniform4f(render->shader->loc_front_specular, obj->material.specular[0], obj->material.specular[1], obj->material.specular[2], 1.0); glUniform1i(render->shader->loc_HasTexture, textured); glDrawArrays(render->draw_mode, obj->vert_start, obj->nVerts); if(ge_line_shader){ geShaderUse(ge_line_shader); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glDrawArrays(render->draw_mode, render->objs[i]->vert_start, render->objs[i]->nVerts); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); geShaderUse(render->shader); } } }
void geSceneDraw(ge_Scene* scene){ current_scene = scene; int i = 0; int j = 0; CalculateModelMatrices(); /* if(scene->sky.animator){ geRendererUse(&scene->sky); geShaderUniform1f(scene->sky.depth_enabled, ((float)geGetTick())/1000.0); geMatrixMode(GE_MATRIX_MODEL); geLoadIdentity(); geUpdateMatrix(); for(i=0; i< scene->sky.nObjs; i++){ glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, scene->sky.objs[i]->material.textures[0]->id); if(scene->sky.objs[i]->material.textures[1]){ glActiveTexture(GL_TEXTURE1); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, scene->sky.objs[i]->material.textures[1]->id); // geShaderUniform1i(geShaderUniformID(scene->sky.shader, "ge_Texture1"), 1); } glDrawArrays(GL_TRIANGLES, scene->sky.objs[i]->vert_start, scene->sky.objs[i]->nVerts); } } */ if(scene->cloudsGenerator){ } float time = ((float)geGetTick()) / 1000.0; for(i=0; i<scene->nRenderers; i++){ if(scene->renderers[i].enabled == false)continue; if(scene->renderers[i].matrix_used){ geMatrixMode(GE_MATRIX_PROJECTION); geLoadMatrix(scene->renderers[i].projection_matrix); }else{ geMatrixMode(GE_MATRIX_PROJECTION); geLoadMatrix(libge_context->projection_matrix); } geShaderUse(scene->renderers[i].shader); glUniform1f(scene->renderers[i].shader->loc_time, time); glUniform1f(scene->renderers[i].shader->loc_ratio, ((float)libge_context->width) / ((float)libge_context->height)); if(scene->fogEnabled){ glUniform1f(scene->renderers[i].shader->loc_fog_density, scene->fog->density); glUniform4f(scene->renderers[i].shader->loc_fog_color, scene->fog->color[0], scene->fog->color[1], scene->fog->color[2], scene->fog->color[3]); glUniform1f(scene->renderers[i].shader->loc_fog_start, scene->fog->start); glUniform1f(scene->renderers[i].shader->loc_fog_end, scene->fog->end); } /* if(scene->renderers[i].depth_enabled){ glEnable(GL_DEPTH_TEST); }else{ glDisable(GL_DEPTH_TEST); } if(scene->renderers[i].blend_enabled){ glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); }else{ glDisable(GL_BLEND); } */ if(scene->renderers[i].ext_func){ scene->renderers[i].ext_func(&scene->renderers[i], -1); } StaticLightingFunc2(scene, &scene->renderers[i], -1); for(j=0; j<scene->nDynamicLights; j++){ glUniform4f(scene->renderers[i].shader->loc_lights[j].loc_position, scene->dynamicLights[j].position.x, scene->dynamicLights[j].position.y, scene->dynamicLights[j].position.z, scene->dynamicLights[j].position.w); glUniform4f(scene->renderers[i].shader->loc_lights[j].loc_target, scene->dynamicLights[j].target.x, scene->dynamicLights[j].target.y, scene->dynamicLights[j].target.z, scene->dynamicLights[j].target.w); glUniform4f(scene->renderers[i].shader->loc_lights[j].loc_ambient, scene->dynamicLights[j].ambient[0], scene->dynamicLights[j].ambient[1], scene->dynamicLights[j].ambient[2], scene->dynamicLights[j].ambient[3]); glUniform4f(scene->renderers[i].shader->loc_lights[j].loc_diffuse, scene->dynamicLights[j].diffuse[0], scene->dynamicLights[j].diffuse[1], scene->dynamicLights[j].diffuse[2], scene->dynamicLights[j].diffuse[3]); glUniform4f(scene->renderers[i].shader->loc_lights[j].loc_specular, scene->dynamicLights[j].specular[0], scene->dynamicLights[j].specular[1], scene->dynamicLights[j].specular[2], scene->dynamicLights[j].specular[3]); glUniform1f(scene->renderers[i].shader->loc_lights[j].loc_attenuation, scene->dynamicLights[j].attenuation); glUniform1f(scene->renderers[i].shader->loc_lights[j].loc_spotCutoff, scene->dynamicLights[j].spot_cutoff); glUniform1f(scene->renderers[i].shader->loc_lights[j].loc_spotCosCutoff, scene->dynamicLights[j].spot_coscutoff); glUniform1f(scene->renderers[i].shader->loc_lights[j].loc_spotExponent, scene->dynamicLights[j].spot_exponent); } for(j=0; j<scene->nLights; j++){ glUniform4f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_position, scene->lights[j].position.x, scene->lights[j].position.y, scene->lights[j].position.z, scene->lights[j].position.w); glUniform4f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_target, scene->lights[j].target.x, scene->lights[j].target.y, scene->lights[j].target.z, scene->lights[j].target.w); glUniform4f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_ambient, scene->lights[j].ambient[0], scene->lights[j].ambient[1], scene->lights[j].ambient[2], scene->lights[j].ambient[3]); glUniform4f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_diffuse, scene->lights[j].diffuse[0], scene->lights[j].diffuse[1], scene->lights[j].diffuse[2], scene->lights[j].diffuse[3]); glUniform4f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_specular, scene->lights[j].specular[0], scene->lights[j].specular[1], scene->lights[j].specular[2], scene->lights[j].specular[3]); if(!scene->lights[j].used){ glUniform1f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_attenuation, -1.0); }else if(scene->lights[j].type != GE_LIGHT_TYPE_SPOT){ glUniform1f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_attenuation, -2.0); }else{ glUniform1f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_attenuation, scene->lights[j].attenuation); } glUniform1f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_spotCutoff, scene->lights[j].spot_cutoff); glUniform1f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_spotCosCutoff, scene->lights[j].spot_coscutoff); glUniform1f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_spotExponent, scene->lights[j].spot_exponent); glUniform3f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_vector, scene->lights[j].vector.x, scene->lights[j].vector.y, scene->lights[j].vector.z); glUniform3f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_targetVector, scene->lights[j].target_vector.x, scene->lights[j].target_vector.y, scene->lights[j].target_vector.z); glUniform1f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_CosInnerMinusOuterAngle, scene->lights[j].CosInnerMinusOuterAngle); glUniform1f(scene->renderers[i].shader->loc_lights[j+8/*j+scene->nDynamicLights*/].loc_CosOuterConeAngle, scene->lights[j].CosOuterConeAngle); } if(ge_current_camera){ glUniform3f(scene->renderers[i].shader->loc_camera, ge_current_camera->x, ge_current_camera->y, ge_current_camera->z); } geRenderObjects(&scene->renderers[i]); if(scene->renderers[i].callback){ scene->renderers[i].callback(&scene->renderers[i], -1); } } }
void geRenderObjects(ge_Renderer* render){ geRendererUse(render); geMatrixMode(GE_MATRIX_MODEL); geLoadIdentity(); int i=0; int j=0; // int current_tex_id[8] = { 0 }; int default_matrix_ok = false; int update_matrices = true; for(i=0; i<render->nObjs; i++){ ge_Object* obj = render->objs[i]; // gePrintDebug(0x100, "Object %d/%d \"%s\", vert_start: %d, nVerts: %d\n", i+1, render->nObjs, obj->name, obj->vert_start, obj->nVerts); if(obj->vert_start<0)continue; if(obj->matrix_used){ geLoadMatrix(obj->matrix); default_matrix_ok = false; update_matrices = true; }else if(!default_matrix_ok){ geLoadIdentity(); default_matrix_ok = true; update_matrices = true; } if(render->ext_func){ render->ext_func(render, i); } if(update_matrices){ // CalculateModelMatrices(); geUpdateMatrix(); // StaticLightingFunc2(current_scene, render, -1); update_matrices = false; } for(j=7; j>=0; j--){ if(obj->material.textures[j]){ if(obj->material.textures[j]->flags & GE_IMAGE_3D){ /* glActiveTexture(GL_TEXTURE0+j); glEnable(GL_TEXTURE_3D); glBindTexture(GL_TEXTURE_3D, obj->material.textures[j]->id); */ }else{ glActiveTexture(GL_TEXTURE0+j); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, obj->material.textures[j]->id); } }else{ glActiveTexture(GL_TEXTURE0+j); glDisable(GL_TEXTURE_2D); // break; } } glDrawArrays(GL_TRIANGLES, render->objs[i]->vert_start, render->objs[i]->nVerts); if(ge_line_shader){ //todo } } }