예제 #1
0
파일: ge_context.c 프로젝트: lmorel3/libge
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;
}
예제 #2
0
파일: gedraw3d.c 프로젝트: drewet/libge
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;
}