コード例 #1
0
ファイル: preview.c プロジェクト: ElArtista/PixLight
void preview_render(void* userdata, float interpolation)
{
    (void) interpolation;
    struct preview_context* ctx = userdata;

    /* Clear */
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /* Setup shader */
    struct preview_internal_state* is = ctx->_internal_state;
    glUseProgram(is->shdr);
    glUniform1i(glGetUniformLocation(is->shdr, "tex"), 0);
    glUniform2i(glGetUniformLocation(is->shdr, "gScreenSize"), ctx->bitmap->width, ctx->bitmap->height);

    /* Update GPU texture */
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, is->tex);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ctx->bitmap->width, ctx->bitmap->height, 0, GL_RGB, GL_UNSIGNED_BYTE, ctx->bitmap->data);

    /* Render quad */
    glBindVertexArray(is->quad_vao);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glBindVertexArray(0);

    /* Show rendered contents from the backbuffer */
    window_swap_buffers(ctx->wnd);
}
コード例 #2
0
ファイル: main.c プロジェクト: metredigm/hunter
int main(int argc, char** argv) {
	init_core();
	debug_message("[hunter] initialized core");

	init_graphic();
	debug_message("[hunter] initialized graphics, mode W%d H%d", window_get_width(global_get(global_get_singleton(), GLOBAL_WINDOW)), window_get_height(global_get(global_get_singleton(), GLOBAL_WINDOW)));

	init_sound();
	debug_message("[hunter] initialized sound");

	init_systems();
	debug_message("[hunter] initialized systems");

	window* window_handle = global_get(global_get_singleton(), GLOBAL_WINDOW);
	input* input_handle = global_get(global_get_singleton(), GLOBAL_INPUT);
	syscontainer* syscontainer_handle = global_get(global_get_singleton(), GLOBAL_SYSCONTAINER);
	text* text_handle = global_get(global_get_singleton(), GLOBAL_TEXT);
	hl_render* hl_render_handle = global_get(global_get_singleton(), GLOBAL_HL_RENDER);
	shader* shader_texture_handle = global_get(global_get_singleton(), GLOBAL_SHADER_TEXTURE);
	camera* camera_handle = global_get(global_get_singleton(), GLOBAL_CAMERA);
	debug_draw* debug_draw_handle = global_get(global_get_singleton(), GLOBAL_DEBUG_DRAW);

	float delta_ref_point = time_get_elapsed(window_handle);
	float delta_accumulator = 0.0f;
	float delta_frame_time = 0.0f;

	float fps_accumulator = 0.0f;
	float fps_value = 0.0f;
	float fps_value_last = fps_value;
	unsigned int fps_counter = 0;

	unsigned int game_kill_flag = 0;

	debug_message("[hunter] posting init event to systems");
	syscontainer_post_event(syscontainer_handle, EVENT_INIT);

	while (!game_kill_flag) {
		delta_frame_time = time_get_elapsed(window_handle) - delta_ref_point;
		delta_ref_point = time_get_elapsed(window_handle);

		window_update(window_handle);

		input_state current_input_state;
		input_get_input_state(input_handle, &current_input_state);

		game_kill_flag |= window_get_should_close(window_handle);
		game_kill_flag |= current_input_state.key_dev;

		window_set_clear_color(window_handle, 1.0f, 0.0f, 1.0f, 0.0f);
		debug_draw_clear(debug_draw_handle);
		window_clear(window_handle);

		delta_accumulator += delta_frame_time;

		if (delta_accumulator < 0.0f) {
			delta_accumulator = 0.0f;
		}

		while (delta_accumulator >= 1.0f / DELTA_REFERENCE_FPS) {
			syscontainer_post_event(syscontainer_handle, EVENT_PRE_LOGIC);
			syscontainer_post_event(syscontainer_handle, EVENT_LOGIC);
			syscontainer_post_event(syscontainer_handle, EVENT_POST_LOGIC);

			delta_accumulator -= 1.0f / DELTA_REFERENCE_FPS;
		}

		syscontainer_post_event(syscontainer_handle, EVENT_DRAW);

		window_set_blend_mode(window_handle, WINDOW_BLEND_ALPHA);

		debug_draw_render_all(debug_draw_handle);

		hl_render_draw(hl_render_handle, shader_texture_handle, camera_handle);
		hl_render_clear(hl_render_handle);

		fps_counter++;
		fps_accumulator += delta_frame_time;

		if (fps_accumulator >= 1.0f) {
			fps_value_last = fps_value;
			fps_value = (float) fps_counter / fps_accumulator;
			fps_counter = 0;
			fps_accumulator = 0.0f;
		}

		text_batch fps_batch = {20, 20, 0.2f, 0.8f, 1.0f, 1.0f, "", "monospace", 12};
		sprintf(fps_batch.text_data, "FPS : %3.2f (%3.2f)", fps_value, fps_value - fps_value_last);

		text_submit_batch(text_handle, &fps_batch);

		text_render_all(text_handle);
		text_flush_batch(text_handle);

		window_swap_buffers(window_handle);
	}

	debug_message("[hunter] posting destroy event to systems");
	syscontainer_post_event(syscontainer_handle, EVENT_DESTROY);

	free_systems();
	free_sound();
	free_graphic();
	free_core();

	h_free_all();

	debug_message("[hunter] terminated cleanly");
	return 0;
}
コード例 #3
0
ファイル: gltest.c プロジェクト: jbmikk/gltest
int main(void)
{
	if(!glfwInit()) {
		printf("Could not init GLFW");
		getchar();
		return -1;
	}

	glfwWindowHint(GLFW_SAMPLES, 4);
	//Opengl 2.1
	//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
	//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
	//Opengl 3.3
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // MacOS fix
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	if(window_init(&window, 640, 480, "Test")) {
		fprintf( stderr, "Failed to open window.\n" );
		getchar();
		glfwTerminate();
	};

	window_set_size_callback(&window, window_size_callback);


	glewExperimental = GL_TRUE; // Needed for core profile
	if (glewInit() != GLEW_OK) {
		fprintf(stderr, "Failed to initialize GLEW\n");
		getchar();
		glfwTerminate();
		return -1;
	}
	//#clear errors GLEW may trigger
	printError();
	
	glClearColor(0.0f, 0.0f, 0.4f, 0.0f);

	GLuint VertexArrayID;
	glGenVertexArrays(1, &VertexArrayID);
	glBindVertexArray(VertexArrayID);


	//Load model
	//----------

	Model cube;
	model_init(&cube);
	model_set_data_length(&cube, sizeof(cube_vertex_data));
	model_set_vertices(&cube, cube_vertex_data);
	model_set_colors(&cube, cube_color_data);
	model_set_uv_map(&cube, cube_uv_data);
	model_set_texture(&cube, "./textures/bricks.dds");
	model_bind(&cube);


	//Create shaders
	//--------------
	
	GLuint vertexShader, fragmentShader;

	loadShader(&vertexShader, GL_VERTEX_SHADER, mvpVertexShaderSource);
	loadShader(&fragmentShader, GL_FRAGMENT_SHADER, fragmentShaderSource);


	//Create program
	//--------------

	GLuint program;
	createProgram(&program, vertexShader, fragmentShader);


	// Enable z-buffer
	// ---------------

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);


	// Enable culling
	// --------------

	glEnable(GL_CULL_FACE);


	// Get MVP uniform
	// ---------------

	GLuint mvp_ul = glGetUniformLocation(program, "MVP");


	// Model Matrix
	// ------------

	Mat4 s;
	scale_m4(&s, 0.1f, 0.1f, 0.1f);
	printf("Scale:\n");
	print_m4(&s);

	Mat4 r;
	rotate_m4(&r, 0.0f, 1.0f, 0.0f, 0.0f);
	printf("Rotate:\n");
	print_m4(&r);

	Mat4 t;
	translate_m4(&t, 0.0f, 0.0f, 0.0f);
	printf("Translate:\n");
	print_m4(&t);

	Mat4 rs;
	printf("Rotated*Scaled:\n");
	mul_m4(&rs, &r, &s);
	print_m4(&rs);

	Mat4 model;
	printf("Model:\n");
	mul_m4(&model, &t, &rs);
	print_m4(&model);


	// Camera
	// ------

	Vec3 pos;
	Vec3 center;
	Vec3 up;
	Vec3 direction;
	Vec3 right;

	camera_init(&camera);
	camera_set_fov(&camera, 1.0f);
	camera_set_aspect(&camera, (float)window.width/(float)window.height);
	
	Input input;
	input_init(&input, &window, 0.5f, 0.5f, 0.8f, -5.7f, -2.7f);

	do {
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

		glUseProgram(program);
		
		input_get_data(&input, &pos, &direction, &right);

		add_v3(&center, &pos, &direction);
		cross_v3(&up, &direction, &right);

		camera_set_position(&camera, &pos);
		camera_set_center(&camera, &center);
		camera_set_up(&camera, &up);


		// Mvp Matrix
		// ----------

		Mat4 vp;
		camera_get_matrix(&camera, &vp);

		Mat4 mvp;
		mul_m4(&mvp, &vp, &model);
		printf("Perspective:\n");
		print_m4(&mvp);


		// Set MVP transform
		glUniformMatrix4fv(mvp_ul, 1, GL_TRUE, &mvp[0][0]);

		model_render(&cube);

		window_swap_buffers(&window);
		glfwPollEvents();
	} while(
		glfwGetKey(window.handle, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
		glfwWindowShouldClose(window.handle) == 0
	);

	// Dispose
	// -------

	model_dispose(&cube);
	glDeleteVertexArrays(1, &VertexArrayID);
	glDetachShader(program, fragmentShader);
	glDetachShader(program, vertexShader);
	glDeleteShader(fragmentShader);
	glDeleteShader(vertexShader);
	glDeleteProgram(program);

	glfwTerminate();
	return 0;
}