void Scene::directionLightPass(RenderTarget *target) { ShaderProgram * shader =ShaderPool::getInstance ()->get("dir_light_pass"); shader->use (); int texture_offset = 5; for(int i = 0 ;i<4;i++) { directionLight.getCSM_FBO (i)->BindForReading(GL_TEXTURE0+i+texture_offset); char GLSL_shadowMap_name [30]; sprintf(GLSL_shadowMap_name,"g_shadow_map[%d]",i); directionLight.getCSM_FBO (i)->applyShadowMapTexture (shader,i+texture_offset,GLSL_shadowMap_name); } QMatrix4x4 m; m.setToIdentity (); m_quad->setShaderProgram (shader); shader->setUniformMat4v ("g_MVP_matrix",m.data ()); shader->setUniform2Float ("g_screen_size",1024,768); shader->setUniformInteger ("g_color_map",0); shader->setUniformInteger ("g_position_map",1); shader->setUniformInteger ("g_normal_map",2); shader->setUniformInteger ("g_depth_map",4);//for depth auto camera = target->camera (); if(camera) { shader->setUniform3Float ("g_eye_position", camera->pos ().x(), camera->pos ().y(), camera->pos ().z()); } QMatrix4x4 lightView; lightView.setToIdentity(); QVector3D lightDir = directionLight.getDirection (); QVector3D pos = QVector3D(0,0,0); lightView.lookAt(pos,lightDir,QVector3D(0,1,0)); for(int i =0 ;i <4 ;i++) { if(!camera) break; auto split_frustum_aabb = camera->getSplitFrustumAABB (i); split_frustum_aabb.transForm (target->camera()->getModelTrans ()); split_frustum_aabb.transForm (lightView); auto matrix = getCropMatrix (split_frustum_aabb); QMatrix4x4 light_vp; light_vp = matrix * lightView ; char GLSL_light_VP_name [30]; sprintf(GLSL_light_VP_name,"g_light_vp_matrix[%d]",i); shader->setUniformMat4v (GLSL_light_VP_name,light_vp.data ()); } this->directionLight.apply(shader); this->ambientLight.apply(shader); m_quad->draw (true); }
void Scene::shadowPassDirectional(RenderTarget * target) { auto light = this->getDirectionalLight (); for(int j =0;j<4;j++) { // Clear color and depth buffer glDepthMask(GL_TRUE); glEnable(GL_DEPTH_TEST); light->getCSM_FBO ( j )->BindForWriting (); glClear(GL_DEPTH_BUFFER_BIT); ShaderProgram * shadow_shader = ShaderPool::getInstance ()->get("shadow"); PipeLine p; for (std::vector<Entity *>::iterator i =m_entityList.begin();i!=m_entityList.end();i++) { shadow_shader->use (); Entity * entity = (* i); if(!entity->isEnableShadow()) continue; if(target->isIgnoreEntity (entity))continue; p.setModelMatrix(target->auxMatrix ()*entity->getModelTrans()); QMatrix4x4 lightView; lightView.setToIdentity(); auto lightDir = directionLight.getDirection (); QVector3D pos = QVector3D(0,0,0); lightView.lookAt(pos,lightDir,QVector3D(0,1,0)); auto aabb = camera ()->getSplitFrustumAABB ( j ); aabb.transForm (camera()->getModelTrans ()); aabb.transForm (lightView); auto matrix = getCropMatrix (aabb); p.setProjectionMatrix(matrix); p.setViewMatrix(lightView); ShaderProgram * tmp_shader = entity->getShaderProgram (); entity->setShaderProgram (shadow_shader); setEntityBoneTransform(entity); p.apply(shadow_shader); entity->draw(); entity->setShaderProgram (tmp_shader); } glDepthMask(GL_FALSE); glDisable(GL_DEPTH_TEST); glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);//switch the frame buffer back. } }
void perspective(TrformStr *TrPtr, pPrefs *prefs) { double v[3]; // auxilliary vector double points_per_degree; double mt[3][3]; double alpha, beta, gammar; // Angles in rad double x_off, y_off, d; double a; uint32_t destwidth, destheight; fDesc fD; void *params[4]; // Parameters for perspective control functions params[0] = (void*)mt; params[1] = (void*)&d; params[2] = (void*)&x_off; params[3] = (void*)&y_off; // Set destination image parameters destwidth = prefs->width; destheight = prefs->height; if( destwidth <= 0 || destheight <= 0 ) { TrPtr->success = 0; PrintError( "Zero Destination Image Size" ); return; } if( SetDestImage(TrPtr, destwidth, destheight) != 0 ) { TrPtr->success = 0; PrintError( "Not enough Memory."); return; } // Set parameters for perspective transformation a = DEG_TO_RAD( prefs->hfov ); alpha = DEG_TO_RAD( prefs->y_beta ); beta = DEG_TO_RAD( prefs->x_alpha ); gammar = DEG_TO_RAD( prefs->gamma ); fD.func =(trfn)NULL; switch(prefs->format) { case _rectilinear: if( a >= PI ) { TrPtr->success = 0; PrintError("HFOV must be smaller than 180 degrees"); return; } d = TrPtr->src->width / ( 2.0 * tan( a/2.0 ) ); if( prefs->unit_is_cart ) { alpha = atan( (prefs->y_beta - TrPtr->src->height/2.0 ) / d ); beta = - atan( (prefs->x_alpha - TrPtr->src->width/2.0 ) / d ); } fD.func = persp_rect; break; case _spherical_tp: d = TrPtr->src->width / a; if( prefs->unit_is_cart ) { points_per_degree = ((double) TrPtr->src->width) / (a * 360 / (2 * PI)) ; alpha = ((TrPtr->src->height/2.0 - prefs->y_beta)/ points_per_degree) * 2 * PI / 360; beta = -((TrPtr->src->width/2.0 - prefs->x_alpha)/ points_per_degree) * 2 * PI / 360; } fD.func = persp_sphere; break; } SetMatrix( alpha, beta, gammar , mt, 1 ); // Offset v[0] = 0.0; v[1] = 0.0; v[2] = d; matrix_mult( mt, v ); x_off = v[0]*d/v[2]; y_off = v[1]*d/v[2]; // Do transformation if( fD.func != NULL) { fD.param = params; //transFormEx( TrPtr, &fD, &fD, 0, 1 ); transForm( TrPtr, &fD, 0 ); } else TrPtr->success = 0; if( TrPtr->success == 0 && ! (TrPtr->mode & _destSupplied)) // Moved here myfree( (void**)TrPtr->dest->data ); }