void CL_GL1FrameBufferProvider::sync_texture()
{
	if (pbuffer_changed && (!pbuffer.is_null()))
	{
		set_active();

		CL_Size surface_size = selected_texture_provider->get_surface_size();

		CLint old_viewport[4], old_matrix_mode;
		CLfloat old_matrix_projection[16], old_matrix_modelview[16];
		cl1GetIntegerv(CL_VIEWPORT, old_viewport);
		cl1GetIntegerv(CL_MATRIX_MODE, &old_matrix_mode);
		cl1GetFloatv(CL_PROJECTION_MATRIX, old_matrix_projection);
		cl1GetFloatv(CL_MODELVIEW_MATRIX, old_matrix_modelview);
		CLboolean blending = cl1IsEnabled(CL_BLEND);
		cl1Disable(CL_BLEND);

		cl1Viewport(0, 0, surface_size.width, surface_size.height);
		cl1MatrixMode(CL_PROJECTION);
		cl1LoadIdentity();
		cl1MultMatrixf(CL_Mat4f::ortho_2d(0.0f, (float)surface_size.width, 0.0f, (float)surface_size.height));
		cl1MatrixMode(CL_MODELVIEW);
		cl1LoadIdentity();

		if (cl1ActiveTexture != 0)
			cl1ActiveTexture( CL_TEXTURE0 );

		cl1PixelZoom(1.0f, 1.0f);

		// Copy pbuffer to texture:
		cl1Enable(CL_TEXTURE_2D);
		cl1BindTexture(CL_TEXTURE_2D,  selected_texture_provider->get_handle());
		cl1CopyTexSubImage2D(
			CL_TEXTURE_2D,
			0,
			0, 0,
			0, 0,
			surface_size.width, surface_size.height);

		cl1Disable(CL_TEXTURE_2D);
		cl1BindTexture(CL_TEXTURE_2D, 0);

		if (blending)
			cl1Enable(CL_BLEND);
		cl1Viewport(old_viewport[0], old_viewport[1], old_viewport[2], old_viewport[3]);
		cl1MatrixMode(CL_PROJECTION);
		cl1LoadMatrixf(old_matrix_projection);
		cl1MatrixMode(CL_MODELVIEW);
		cl1LoadMatrixf(old_matrix_modelview);
		cl1MatrixMode(old_matrix_mode);

#ifdef __APPLE__
		cl1Flush();
#endif
	}
	pbuffer_changed = false;
}
void CL_GL1FrameBufferProvider::sync_pbuffer()
{
	set_active();

	CL_Size surface_size = selected_texture_provider->get_surface_size();
	CL_Size texture_size = selected_texture_provider->get_texture_size();

	CLint old_viewport[4], old_matrix_mode;
	CLfloat old_matrix_projection[16], old_matrix_modelview[16];
	cl1GetIntegerv(CL_VIEWPORT, old_viewport);
	cl1GetIntegerv(CL_MATRIX_MODE, &old_matrix_mode);
	cl1GetFloatv(CL_PROJECTION_MATRIX, old_matrix_projection);
	cl1GetFloatv(CL_MODELVIEW_MATRIX, old_matrix_modelview);
	CLboolean blending = cl1IsEnabled(CL_BLEND);
	cl1Disable(CL_BLEND);

	cl1Viewport(0, 0, surface_size.width, surface_size.height);
	cl1MatrixMode(CL_PROJECTION);
	cl1LoadIdentity();
	cl1MultMatrixf(CL_Mat4f::ortho_2d(0.0f, (float)surface_size.width, 0.0f, (float)surface_size.height));
	cl1MatrixMode(CL_MODELVIEW);
	cl1LoadIdentity();

	// Copy texture to pbuffer:

	cl1ActiveTexture( CL_TEXTURE0 + 0 );
	cl1Enable(CL_TEXTURE_2D);
	cl1BindTexture(CL_TEXTURE_2D, selected_texture_provider->get_handle());

	if (cl1ActiveTexture != 0)
			cl1ActiveTexture( CL_TEXTURE0 + 0 );

	cl1DisableClientState(CL_TEXTURE_COORD_ARRAY);

	cl1MatrixMode(CL_TEXTURE);
	CLfloat scale_matrix[16];
	memset(scale_matrix, 0, sizeof(CLfloat)*16);
	scale_matrix[0] = 1.0f/float(texture_size.width);
	scale_matrix[5] = 1.0f/float(texture_size.height);
	scale_matrix[10] = 1.0f;
	scale_matrix[15] = 1.0f;
	cl1LoadMatrixf(scale_matrix);
	cl1MatrixMode(CL_MODELVIEW);

	cl1Begin(CL_QUADS);
	cl1Color3f(1.0f, 1.0f, 1.0f);
	cl1TexCoord2f(float(surface_size.width), 0.0f);
	cl1Vertex2f(float(surface_size.width), 0.0f);
	cl1TexCoord2f(float(surface_size.width), float(surface_size.height));
	cl1Vertex2f(float(surface_size.width), float(surface_size.height));
	cl1TexCoord2f(0.0f, float(surface_size.height));
	cl1Vertex2f(0.0f, float(surface_size.height));
	cl1TexCoord2f(0.0f, 0.0f);
	cl1Vertex2f(0.0f, 0.0f);
	cl1End();
	cl1Disable(CL_TEXTURE_2D);
	cl1BindTexture(CL_TEXTURE_2D, 0);

	cl1MatrixMode(CL_TEXTURE);
	cl1LoadIdentity();
	cl1MatrixMode(CL_MODELVIEW);

	if (blending)
		cl1Enable(CL_BLEND);
	cl1Viewport(old_viewport[0], old_viewport[1], old_viewport[2], old_viewport[3]);
	cl1MatrixMode(CL_PROJECTION);
	cl1LoadMatrixf(old_matrix_projection);
	cl1MatrixMode(CL_MODELVIEW);
	cl1LoadMatrixf(old_matrix_modelview);
	cl1MatrixMode(old_matrix_mode);

#ifdef __APPLE__
	cl1Flush();
#endif

	pbuffer_changed = true;
}
예제 #3
0
// The start of the Application
int App::start(const std::vector<CL_String> &args)
{
	quit = false;
    CL_GL1WindowDescription desc;

	desc.set_title("ClanLib Object 3D Example");
	desc.set_size(CL_Size(640, 480), true);
	desc.set_multisampling(4);
	desc.set_depth_size(16);

	CL_DisplayWindow window(desc);

#ifdef _DEBUG
	//struct aiLogStream stream;
	//stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
	//aiAttachLogStream(&stream);
	//stream = aiGetPredefinedLogStream(aiDefaultLogStream_FILE,"assimp_log.txt");
	//aiAttachLogStream(&stream);
#endif

	// Connect the Window close event
	CL_Slot slot_quit = window.sig_window_close().connect(this, &App::on_window_close);

	// Connect a keyboard handler to on_key_up()
	CL_Slot slot_input_up = (window.get_ic().get_keyboard()).sig_key_up().connect(this, &App::on_input_up);

	// Get the graphic context
	CL_GraphicContext gc = window.get_gc();

#ifdef USE_OPENGL_1
    CL_GraphicContext_GL1 gc_gl1 = gc;
#endif

	// Prepare the display
	gc.set_map_mode(cl_user_projection);

	CL_PolygonRasterizer polygon_rasterizer;
	polygon_rasterizer.set_culled(true);
	polygon_rasterizer.set_face_cull_mode(cl_cull_back);
	polygon_rasterizer.set_front_face(cl_face_side_clockwise);
	gc.set_polygon_rasterizer(polygon_rasterizer);

	CL_BufferControl buffer_control;
	buffer_control.enable_depth_test(true);
	buffer_control.set_depth_compare_function(cl_comparefunc_lequal);
	buffer_control.enable_depth_write(true);
	gc.set_buffer_control(buffer_control);

#ifdef USE_OPENGL_1
	// Set the lights
	CL_LightModel_GL1 light_model;
	light_model.enable_lighting(true);
	light_model.set_flat_shading(false);
	light_model.set_scene_ambient_light(CL_Colorf(0.2f, 0.2f, 0.2f, 1.0f));
	gc_gl1.set_light_model(light_model);

	CL_LightSource_GL1 light_distant;
	light_distant.set_spot_cutoff(180.0f);
	light_distant.set_diffuse_intensity(CL_Colorf(1.0f, 1.0f, 1.0f, 1.0f));
	light_distant.set_position(CL_Vec4f(0.0f, -2.0f, 30.0f, 0.0f).normalize3());
	gc_gl1.set_light(0, light_distant);

	cl1Enable(GL_NORMALIZE);
#endif

#ifdef USE_OPENGL_2
    Shader shader(gc);
#endif

	// Create the objects

	aiSetImportPropertyFloat(AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE,89.53f);

	const struct aiScene* scene_teapot = aiImportFile("../Clan3D/Resources/teapot.dae",aiProcessPreset_TargetRealtime_MaxQuality);
	if (!scene_teapot)
		throw CL_Exception("Cannot load the teapot model");

	const struct aiScene* scene_clanlib = aiImportFile("../Clan3D/Resources/clanlib.dae",aiProcessPreset_TargetRealtime_MaxQuality);
	if (!scene_clanlib)
		throw CL_Exception("Cannot load the clanlib model");

	const struct aiScene* scene_tuxball = aiImportFile("../Clan3D/Resources/tux_ball.dae",aiProcessPreset_TargetRealtime_MaxQuality | aiProcess_FlipUVs);
	if (!scene_tuxball)
		throw CL_Exception("Cannot load the tux ball model");

	// Load the texture
	CL_Texture tux(gc, "../Clan3D/Resources/tux.png");

	float angle = 0.0f;
	// Run until someone presses escape
	while (!quit)
	{

		CL_Mat4f perp = CL_Mat4f::perspective(45.0f, ((float) gc.get_width()) / ((float) gc.get_height()), 0.1f, 1000.0f);
		gc.set_projection(perp);

		gc.clear(CL_Colorf::black);
		gc.clear_depth(1.0f);

		angle += 1.0f;
		if (angle >= 360.0f)
			angle -= 360.0f;


#ifdef USE_OPENGL_2
        shader.Set(gc);
        shader.Use(gc);
#else
        gc.set_program_object(cl_program_color_only);
#endif

		CL_PrimitivesArray prim_array(gc);

		gc.set_modelview(CL_Mat4f::identity());
		gc.mult_scale(1.0f,1.0f, -1.0f);	// So +'ve Z goes into the screen
		gc.mult_translate(0.0f, 0.0f, 2.0f);
		gc.mult_rotate(CL_Angle(angle, cl_degrees), 0.0f, 1.0f, 0.0f, false);

		gc.push_modelview();
		recursive_render(gc, scene_teapot, scene_teapot->mRootNode, false);
		gc.pop_modelview();

		gc.push_modelview();
		gc.mult_scale(0.5f, 0.5f, 0.5f);
		gc.mult_translate(0.0f, -0.5f, 0.0f);
		recursive_render(gc, scene_clanlib, scene_clanlib->mRootNode, false);
		gc.pop_modelview();

#ifdef USE_OPENGL_2
        shader.Set(gc, 0);
        shader.Use(gc);
#else
        gc.set_program_object(cl_program_single_texture);
#endif

		gc.set_texture(0, tux);
 		gc.set_modelview(CL_Mat4f::identity());
		gc.mult_scale(1.0f,1.0f, -1.0f);	// So +'ve Z goes into the screen
		gc.mult_translate(0.7f, 0.5f, 2.0f);
		gc.mult_scale(0.05f, 0.05f, 0.05f);
		gc.mult_rotate(CL_Angle(angle * 4.0f, cl_degrees), 0.0f, 1.0f, 0.0f, false);
		recursive_render(gc, scene_tuxball, scene_tuxball->mRootNode, true);
		gc.reset_texture(0);

		gc.reset_program_object();
		
		// Flip the display, showing on the screen what we have drawed
		// since last call to flip()
		window.flip(1);

		// This call processes user input and other events
		CL_KeepAlive::process();
	}

	aiReleaseImport(scene_tuxball);
	aiReleaseImport(scene_clanlib);
	aiReleaseImport(scene_teapot);
	aiDetachAllLogStreams();

	return 0;
}