示例#1
0
void camera_update(camera_t *camera)
{
    float size;

    camera->fovy = 20.;
    if (camera->move_to_target) {
        camera->move_to_target = !vec3_ilerp_const(
                &camera->ofs, vec3_neg(camera->target), camera->dist / 128);
    }
    // Update the camera mats
    camera->view_mat = mat4_identity;
    mat4_itranslate(&camera->view_mat, 0, 0, -camera->dist);
    mat4_imul_quat(&camera->view_mat, camera->rot);
    mat4_itranslate(&camera->view_mat,
           camera->ofs.x, camera->ofs.y, camera->ofs.z);

    if (camera->ortho) {
        size = camera->dist;
        camera->proj_mat = mat4_ortho(
                -size, +size,
                -size / camera->aspect, +size / camera->aspect,
                0, 1000);
    } else {
        camera->proj_mat = mat4_perspective(
                camera->fovy, camera->aspect, 1, 1000);
    }
}
static void window_draw(struct window *window)
{
	struct mat4 matrix, modelview, projection, transform, result;
	GLfloat aspect;

	aspect = window->width / (GLfloat)window->height;

	mat4_perspective(&projection, 35.0f, aspect, 1.0f, 1024.0f);
	mat4_identity(&modelview);

	mat4_rotate_x(&transform, x);
	mat4_multiply(&result, &modelview, &transform);
	mat4_rotate_y(&transform, y);
	mat4_multiply(&modelview, &result, &transform);
	mat4_rotate_z(&transform, z);
	mat4_multiply(&result, &modelview, &transform);
	mat4_translate(&transform, 0.0f, 0.0f, -2.0f);
	mat4_multiply(&modelview, &transform, &result);

	mat4_multiply(&matrix, &projection, &modelview);
	glUniformMatrix4fv(mvp, 1, GL_FALSE, (GLfloat *)&matrix);

	glViewport(0, 0, window->width, window->height);
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glDrawElements(GL_TRIANGLES, ARRAY_SIZE(indices), GL_UNSIGNED_SHORT,
		       indices);

	eglSwapBuffers(window->egl.display, window->egl.surface);

	x += 0.3f;
	y += 0.2f;
	z += 0.4f;
}
示例#3
0
static void init(void)
{
	STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO);

	r3dfb_init();
	LCD_SetColors(0x0000, 0xffff);
	LCD_SetFont(&Font8x8);

	itoa(meshes[mesh].count / 3, info_str, 10);
	strcat(info_str, " tris");

	r3d_viewport(1, 1, R3DFB_PIXEL_WIDTH - 1, R3DFB_PIXEL_HEIGHT - 1);
	r3d_backface_culling = R3D_ENABLE;
	r3d_primitive_winding = windings[mesh];

	projection = mat4_perspective(60.0f, (float)R3DFB_PIXEL_WIDTH / (float)R3DFB_PIXEL_HEIGHT, 0.5f, 5.0f);
	view = mat4_lookat(vec3(0.0f, 0.25f, 1.5f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f));

	L3GD20_InitTypeDef L3GD20_InitStructure;
	L3GD20_InitStructure.Power_Mode = L3GD20_MODE_ACTIVE;
	L3GD20_InitStructure.Output_DataRate = L3GD20_OUTPUT_DATARATE_1;
	L3GD20_InitStructure.Axes_Enable = L3GD20_AXES_ENABLE;
	L3GD20_InitStructure.Band_Width = L3GD20_BANDWIDTH_4;
	L3GD20_InitStructure.BlockData_Update = L3GD20_BlockDataUpdate_Continous;
	L3GD20_InitStructure.Endianness = L3GD20_BLE_LSB;
	L3GD20_InitStructure.Full_Scale = L3GD20_FULLSCALE_250;
	L3GD20_Init(&L3GD20_InitStructure);

	L3GD20_FilterConfigTypeDef L3GD20_FilterStructure;
	L3GD20_FilterStructure.HighPassFilter_Mode_Selection = L3GD20_HPM_NORMAL_MODE_RES;
	L3GD20_FilterStructure.HighPassFilter_CutOff_Frequency = L3GD20_HPFCF_0;
	L3GD20_FilterConfig(&L3GD20_FilterStructure);
	L3GD20_FilterCmd(L3GD20_HIGHPASSFILTER_ENABLE);
}
示例#4
0
void cam_update_perspective(Camera *camera, float fov, float znear, float zfar) {
    camera->fov   = fov;
    camera->znear = znear;
    camera->zfar  = zfar;

    mat4_perspective(camera->p, fov, znear, zfar);
    __cam_update_vp(camera);
}
示例#5
0
void proj_tests(void){
	mat4_t m = mat4_look_at(vec4_new(0, 0, 5, 0),
		vec4_new(0, 0, 0, 0), vec4_new(0, 1, 0, 0));
	printf("Look at matrix:\n");
	mat4_print(m);

	m = mat4_ortho(-1, 1, -1, 1, 1, 100);
	printf("ortho matrix:\n");
	mat4_print(m);

	m = mat4_perspective(90, 1, 1, 100);
	printf("perspective:\n");
	mat4_print(m);
}
示例#6
0
void reshape(int width, int height)
{
    if (height == 0)
    {
        height = 1;
    }

    glViewport(0, 0, width, height);

    mat4_identity(projection);
    projection = mat4_perspective(45.0f, (GLfloat) width / (GLfloat) height, 0.01f, 100000.0f , projection);

    mat4_identity(view);
    view = mat4_lookAt(eye, center, up, view);
}
示例#7
0
int main(int argc, char *argv[])
{
	uint32_t width = 1920, height = 1080;
	LoadOpenGLESWindow(width, height);
    
	// initial opengl state
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_ONE, GL_ONE);
	glDisable(GL_CULL_FACE);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	
	// load textures
	GLfloat max_anisotropy;
	glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &max_anisotropy);
	GLuint textures[TEXTURES_NUM];
	glGenTextures(TEXTURES_NUM, textures);
	int i;
	for (i = 0; i < TEXTURES_NUM; i++)
	{
		int x, y, n;
		unsigned char *data = stbi_load(texture_files[i], &x, &y, &n, 3);
		if (data == NULL)
		{
			fprintf(stderr, "Could not load file: %s", texture_files[i]);
			exit(-1);
		}
		
		glBindTexture(GL_TEXTURE_2D, textures[i]);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, x, y, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
		glGenerateMipmap(GL_TEXTURE_2D);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, max_anisotropy);
		
		stbi_image_free(data);
	}
	
	// load program
	GLuint programObject = LoadProgram(vertex_shader, fragment_shader);
	glUseProgram(programObject);
	glUniform1i(glGetUniformLocation(programObject, "tex"), 0);
	GLint colorUniformLocation = glGetUniformLocation(programObject, "color");
	GLint transformUniformLocation = glGetUniformLocation(programObject, "transform");
	
	// load quad data
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, quad_vertices);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, quad_texcoord);
	glEnableVertexAttribArray(1);

    unsigned int frames = 0;
    while(1)
    {
		glViewport(0, 0, width, height);
		
		// animation params
		double t = (frames++) * 0.02f;
		float a = 0.5f + 0.5f * sinf(t * 13.0f);
		float x = 0.2f * sinf(32.0f * t) * sinf(3.5f * t);
		float y = 0.1f * sinf(23.0f * t) * sinf(3.5f * t);
		
		// camera config
		mat4_t proj = mat4_perspective(50.0f, (float)width / (float)height, 0.1f, 1000.0f);
		mat4_t view = mat4_mul(//mat4_mul(
			mat4_translation(vec3(x, y, -3.0f)), // positioning
			mat4_rotation(-90.0f, vec3(0.0f, 0.0f, 1.0f)));//, // beamer rotation
			//mat4_rotation(180.0f, vec3(0.0f, 1.0f, 0.0f))); // horizontal "flip"
		mat4_t vp = mat4_mul(proj, view);
		
		// color
		glUniform3f(colorUniformLocation, 
			a * (0.5f + 0.5f * sinf(t)),
			a * (0.5f + 0.5f * sinf(1.3f * t)),
			a * (0.5f + 0.5f * sinf(1.7f * t)));
			
		// draw radials
		int i;
		for (i = 0; i < 3; i++)
		{
			mat4_t mvp = mat4_mul(vp, mat4_rotation(20.0f * cos(t * 2.0f), vec3(0.0f, 1.0f, 0.0f)));
			mvp = mat4_mul(mvp, mat4_translation(vec3(0.0f, 0.8f, 0.0f)));
			mvp = mat4_mul(mvp, mat4_rotation(t * 32.0f * (0.5f + (i - 1)), vec3(0.0f, 0.0f, 1.0f)));
			glUniformMatrix4fv(transformUniformLocation, 1, GL_FALSE, mvp.m);
			glBindTexture(GL_TEXTURE_2D, textures[RADIAL0 + i]);
			glDrawArrays(GL_TRIANGLES, 0, 6);
		}

		// draw text
		mat4_t mvp = mat4_mul(vp, mat4_rotation(60.0f * sin(t), vec3(0.0f, 1.0f, 0.0f)));
		mvp = mat4_mul(mvp, mat4_translation(vec3(0.0f, -0.8f, 0.0f)));
		glUniformMatrix4fv(transformUniformLocation, 1, GL_FALSE, mvp.m);
		glBindTexture(GL_TEXTURE_2D, textures[TEXT]);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		
		// present
		eglSwapBuffers(display, surface);
    }
}
示例#8
0
int main(int argc, char *argv[])
{
	struct grate_shader *quad_vs, *quad_fs, *quad_linker;
	struct host1x_pixelbuffer *pixbuf;
	struct host1x_bo *bo;
	float aspect;

	if (chdir( dirname(argv[0]) ) == -1)
		fprintf(stderr, "chdir failed\n");

	if (chdir("../../") == -1)
		fprintf(stderr, "chdir failed\n");

	if (!grate_parse_command_line(&options, argc, argv))
		return 1;

	grate = grate_init(&options);
	if (!grate)
		return 1;

	fb = grate_framebuffer_create(grate, options.width, options.height,
				      PIX_BUF_FMT_RGBA8888,
				      PIX_BUF_LAYOUT_TILED_16x16,
				      GRATE_DOUBLE_BUFFERED);
	if (!fb) {
		fprintf(stderr, "grate_framebuffer_create() failed\n");
		return 1;
	}

	aspect = options.width / (float)options.height;
	mat4_perspective(&projection, 60.0f, aspect, 1.0f, 2048.0f);

	grate_clear_color(grate, 0.0, 0.0, 1.0, 1.0f);
	grate_bind_framebuffer(grate, fb);

	/* Prepare shaders */

	quad_vs = grate_shader_parse_vertex_asm_from_file(
				"tests/grate/asm/filter_quad_vs.txt");
	if (!quad_vs) {
		fprintf(stderr, "filter_quad_vs assembler parse failed\n");
		return 1;
	}

	quad_fs = grate_shader_parse_fragment_asm_from_file(
				"tests/grate/asm/filter_quad_fs.txt");
	if (!quad_fs) {
		fprintf(stderr, "filter_quad_fs assembler parse failed\n");
		return 1;
	}

	quad_linker = grate_shader_parse_linker_asm_from_file(
				"tests/grate/asm/filter_quad_linker.txt");
	if (!quad_linker) {
		fprintf(stderr, "filter_quad_linker assembler parse failed\n");
		return 1;
	}

	quad_program = grate_program_new(grate, quad_vs, quad_fs, quad_linker);
	if (!quad_program) {
		fprintf(stderr, "grate_program_new() failed\n");
		return 1;
	}

	grate_program_link(quad_program);

	/* Load font */

	font = grate_create_font(grate, "data/font.png", "data/font.fnt");
	if (!font) {
		fprintf(stderr, "failed to load font\n");
		return 1;
	}

	font_scale = options.width / 700.0f;

	/* Setup context */

	ctx = grate_3d_alloc_ctx(grate);

	grate_3d_ctx_bind_program(ctx, quad_program);
	grate_3d_ctx_set_depth_range(ctx, 0.0f, 1.0f);
	grate_3d_ctx_set_dither(ctx, 0x779);
	grate_3d_ctx_set_point_params(ctx, 0x1401);
	grate_3d_ctx_set_point_size(ctx, 1.0f);
	grate_3d_ctx_set_line_params(ctx, 0x2);
	grate_3d_ctx_set_line_width(ctx, 1.0f);
	grate_3d_ctx_set_viewport_bias(ctx, 0.0f, 0.0f, 0.5f);
	grate_3d_ctx_set_viewport_scale(ctx, options.width, options.height, 0.5f);
	grate_3d_ctx_use_guardband(ctx, true);
	grate_3d_ctx_set_front_direction_is_cw(ctx, false);
	grate_3d_ctx_set_cull_face(ctx, GRATE_3D_CTX_CULL_FACE_NONE);
	grate_3d_ctx_set_scissor(ctx, 0, options.width, 0, options.height);
	grate_3d_ctx_set_point_coord_range(ctx, 0.0f, 1.0f, 0.0f, 1.0f);
	grate_3d_ctx_set_polygon_offset(ctx, 0.0f, 0.0f);
	grate_3d_ctx_set_provoking_vtx_last(ctx, true);

	/* Setup quad attributes */

	location = grate_get_attribute_location(quad_program, "position");
	bo = grate_create_attrib_bo_from_data(grate, vertices);
	grate_3d_ctx_vertex_attrib_float_pointer(ctx, location, 4, bo);
	grate_3d_ctx_enable_vertex_attrib_array(ctx, location);

	location = grate_get_attribute_location(quad_program, "texcoord");
	bo = grate_create_attrib_bo_from_data(grate, uv);
	grate_3d_ctx_vertex_attrib_float_pointer(ctx, location, 2, bo);
	grate_3d_ctx_enable_vertex_attrib_array(ctx, location);

	/* Setup render target */

	grate_3d_ctx_enable_render_target(ctx, 1);

	/* Setup textures */
	tex[0] = grate_create_texture(grate, 2048, 700,
				      PIX_BUF_FMT_RGBA8888,
				      PIX_BUF_LAYOUT_LINEAR);
	grate_texture_load(grate, tex[0], "data/tegra.png");
	grate_texture_generate_mipmap(grate, tex[0]);

	tex[1] = grate_create_texture(grate, 1024, 1024,
				      PIX_BUF_FMT_RGBA8888,
				      PIX_BUF_LAYOUT_LINEAR);
	grate_texture_load(grate, tex[1], "data/miptest/nomipmap.png");
	grate_texture_load_miplevel(grate, tex[1], 0, "data/miptest/1024x1024.png");
	grate_texture_load_miplevel(grate, tex[1], 1, "data/miptest/512x512.png");
	grate_texture_load_miplevel(grate, tex[1], 2, "data/miptest/256x256.png");
	grate_texture_load_miplevel(grate, tex[1], 3, "data/miptest/128x128.png");
	grate_texture_load_miplevel(grate, tex[1], 4, "data/miptest/64x64.png");
	grate_texture_load_miplevel(grate, tex[1], 5, "data/miptest/32x32.png");
	grate_texture_load_miplevel(grate, tex[1], 6, "data/miptest/16x16.png");
	grate_texture_load_miplevel(grate, tex[1], 7, "data/miptest/8x8.png");
	grate_texture_load_miplevel(grate, tex[1], 8, "data/miptest/4x4.png");
	grate_texture_load_miplevel(grate, tex[1], 9, "data/miptest/2x2.png");
	grate_texture_load_miplevel(grate, tex[1], 10, "data/miptest/1x1.png");

	tex[2] = grate_create_texture(grate, 256, 256,
				      PIX_BUF_FMT_RGBA8888,
				      PIX_BUF_LAYOUT_LINEAR);
	grate_texture_load(grate, tex[2], "data/tegra.png");
	grate_texture_generate_mipmap(grate, tex[2]);

	/* Create indices BO */

	idx_bo = grate_create_attrib_bo_from_data(grate, indices);

	profile = grate_profile_start(grate);

	while (true) {
		grate_clear(grate);

		pixbuf = grate_get_draw_pixbuf(fb);
		grate_3d_ctx_bind_render_target(ctx, 1, pixbuf);

		active_tex = ((int)elapsed / 135) & 1;
		max_lod = 11;

		switch ((int)elapsed % 135) {
		case 0 ... 9:
			test1_lod_bias(GRATE_TEXTURE_NEAREST_MIPMAP_NEAREST,
				       GRATE_TEXTURE_LINEAR);
			break;
		case 10 ... 19:
			test1_lod_bias(GRATE_TEXTURE_LINEAR_MIPMAP_NEAREST,
				       GRATE_TEXTURE_LINEAR);
			break;
		case 20 ... 29:
			test1_lod_bias(GRATE_TEXTURE_NEAREST_MIPMAP_LINEAR,
				       GRATE_TEXTURE_LINEAR);
			break;
		case 30 ... 39:
			test1_lod_bias(GRATE_TEXTURE_LINEAR_MIPMAP_LINEAR,
				       GRATE_TEXTURE_LINEAR);
			break;
		case 40 ... 49:
			test2_scale(GRATE_TEXTURE_NEAREST,
				    GRATE_TEXTURE_LINEAR);
			break;
		case 50 ... 59:
			test2_scale(GRATE_TEXTURE_LINEAR,
				    GRATE_TEXTURE_LINEAR);
			break;
		case 60 ... 64:
			test2_scale(GRATE_TEXTURE_NEAREST_MIPMAP_NEAREST,
				    GRATE_TEXTURE_LINEAR);
			break;
		case 65 ... 69:
			test2_scale(GRATE_TEXTURE_NEAREST_MIPMAP_NEAREST,
				    GRATE_TEXTURE_NEAREST);
			break;
		case 70 ... 74:
			test2_scale(GRATE_TEXTURE_LINEAR_MIPMAP_NEAREST,
				    GRATE_TEXTURE_LINEAR);
			break;
		case 75 ... 79:
			test2_scale(GRATE_TEXTURE_LINEAR_MIPMAP_NEAREST,
				    GRATE_TEXTURE_NEAREST);
			break;
		case 80 ... 84:
			test2_scale(GRATE_TEXTURE_NEAREST_MIPMAP_LINEAR,
				    GRATE_TEXTURE_LINEAR);
			break;
		case 85 ... 89:
			test2_scale(GRATE_TEXTURE_NEAREST_MIPMAP_LINEAR,
				    GRATE_TEXTURE_NEAREST);
			break;
		case 90 ... 94:
			test2_scale(GRATE_TEXTURE_LINEAR_MIPMAP_LINEAR,
				    GRATE_TEXTURE_LINEAR);
			break;
		case 95 ... 99:
			test2_scale(GRATE_TEXTURE_LINEAR_MIPMAP_LINEAR,
				    GRATE_TEXTURE_NEAREST);
			break;
		case 100 ... 106:
			active_tex = 2;
			test3_mag(GRATE_TEXTURE_NEAREST, GRATE_TEXTURE_NEAREST);
			break;
		case 107 ... 113:
			active_tex = 2;
			test3_mag(GRATE_TEXTURE_NEAREST, GRATE_TEXTURE_LINEAR);
			break;
		case 114 ... 119:
			active_tex = 2;
			test3_mag(GRATE_TEXTURE_LINEAR, GRATE_TEXTURE_NEAREST);
			break;
		case 120 ... 134:
			max_lod = (powf(sinf(elapsed * 0.3f), 6.0f)) * 12;
			test4_max_lod(GRATE_TEXTURE_LINEAR_MIPMAP_LINEAR,
				      GRATE_TEXTURE_LINEAR);
			break;
		}

		grate_swap_buffers(grate);

		if (grate_key_pressed(grate))
			break;

		grate_profile_sample(profile);

		elapsed = grate_profile_time_elapsed(profile);
	}

	grate_profile_finish(profile);
	grate_profile_free(profile);

	grate_exit(grate);
	return 0;
}
示例#9
0
mat4 camera_proj_matrix(camera* c, float aspect_ratio) {
  return mat4_perspective(c->fov, c->near_clip, c->far_clip, aspect_ratio);
}
示例#10
0
文件: camera.c 项目: Drooids/Corange
mat4 camera_proj_matrix(camera* c) {
  return mat4_perspective(c->fov, c->near_clip, c->far_clip, graphics_viewport_ratio());
}
示例#11
0
static void window_draw(struct window *window)
{
	static const GLfloat vertices[] = {
		/* front */
		-0.5f, -0.5f,  0.5f, 1.0f,
		 0.5f, -0.5f,  0.5f, 1.0f,
		 0.5f,  0.5f,  0.5f, 1.0f,
		-0.5f,  0.5f,  0.5f, 1.0f,
		/* back */
		-0.5f, -0.5f, -0.5f, 1.0f,
		 0.5f, -0.5f, -0.5f, 1.0f,
		 0.5f,  0.5f, -0.5f, 1.0f,
		-0.5f,  0.5f, -0.5f, 1.0f,
		/* left */
		-0.5f, -0.5f,  0.5f, 1.0f,
		-0.5f,  0.5f,  0.5f, 1.0f,
		-0.5f,  0.5f, -0.5f, 1.0f,
		-0.5f, -0.5f, -0.5f, 1.0f,
		/* right */
		 0.5f, -0.5f,  0.5f, 1.0f,
		 0.5f,  0.5f,  0.5f, 1.0f,
		 0.5f,  0.5f, -0.5f, 1.0f,
		 0.5f, -0.5f, -0.5f, 1.0f,
		/* top */
		-0.5f,  0.5f,  0.5f, 1.0f,
		 0.5f,  0.5f,  0.5f, 1.0f,
		 0.5f,  0.5f, -0.5f, 1.0f,
		-0.5f,  0.5f, -0.5f, 1.0f,
		/* bottom */
		-0.5f, -0.5f,  0.5f, 1.0f,
		 0.5f, -0.5f,  0.5f, 1.0f,
		 0.5f, -0.5f, -0.5f, 1.0f,
		-0.5f, -0.5f, -0.5f, 1.0f,
	};

	static const GLfloat colors[] = {
		/* front */
		1.0f, 0.0f, 0.0f, 1.0f,
		1.0f, 0.0f, 0.0f, 1.0f,
		1.0f, 0.0f, 0.0f, 1.0f,
		1.0f, 0.0f, 0.0f, 1.0f,
		/* back */
		1.0f, 1.0f, 0.0f, 1.0f,
		1.0f, 1.0f, 0.0f, 1.0f,
		1.0f, 1.0f, 0.0f, 1.0f,
		1.0f, 1.0f, 0.0f, 1.0f,
		/* left */
		0.0f, 0.0f, 1.0f, 1.0f,
		0.0f, 0.0f, 1.0f, 1.0f,
		0.0f, 0.0f, 1.0f, 1.0f,
		0.0f, 0.0f, 1.0f, 1.0f,
		/* right */
		1.0f, 0.0f, 1.0f, 1.0f,
		1.0f, 0.0f, 1.0f, 1.0f,
		1.0f, 0.0f, 1.0f, 1.0f,
		1.0f, 0.0f, 1.0f, 1.0f,
		/* top */
		0.0f, 1.0f, 0.0f, 1.0f,
		0.0f, 1.0f, 0.0f, 1.0f,
		0.0f, 1.0f, 0.0f, 1.0f,
		0.0f, 1.0f, 0.0f, 1.0f,
		/* bottom */
		1.0f, 0.5f, 0.0f, 1.0f,
		1.0f, 0.5f, 0.0f, 1.0f,
		1.0f, 0.5f, 0.0f, 1.0f,
		1.0f, 0.5f, 0.0f, 1.0f,
	};

	static const GLushort indices[] = {
		/* front */
		 0,  1,  2,
		 0,  2,  3,
		/* back */
		 4,  5,  6,
		 4,  6,  7,
		/* left */
		 8,  9, 10,
		 8, 10, 11,
		/* right */
		12, 13, 14,
		12, 14, 15,
		/* top */
		16, 17, 18,
		16, 18, 19,
		/* bottom */
		20, 21, 22,
		20, 22, 23,
	};

	struct mat4 matrix, modelview, projection, transform, result;
	static GLfloat x = 0.0f, y = 0.0f, z = 0.0f;
	GLint position, color, mvp;
	GLuint vs, fs, program;
	GLfloat aspect;

	vs = glsl_shader_load(GL_VERTEX_SHADER, vertex_shader,
			      ARRAY_SIZE(vertex_shader));
	fs = glsl_shader_load(GL_FRAGMENT_SHADER, fragment_shader,
			      ARRAY_SIZE(fragment_shader));
	program = glsl_program_create(vs, fs);
	glsl_program_link(program);
	glUseProgram(program);

	position = glGetAttribLocation(program, "position");
	color = glGetAttribLocation(program, "color");
	mvp = glGetUniformLocation(program, "mvp");

	aspect = window->width / (GLfloat)window->height;

	mat4_perspective(&projection, 60.0f, aspect, 1.0f, 1024.0f);
	mat4_identity(&modelview);

	mat4_rotate_x(&transform, x);
	mat4_multiply(&result, &modelview, &transform);
	mat4_rotate_y(&transform, y);
	mat4_multiply(&modelview, &result, &transform);
	mat4_rotate_z(&transform, z);
	mat4_multiply(&result, &modelview, &transform);
	mat4_translate(&transform, 0.0f, 0.0f, -2.0f);
	mat4_multiply(&modelview, &transform, &result);

	mat4_multiply(&matrix, &projection, &modelview);
	glUniformMatrix4fv(mvp, 1, GL_FALSE, (GLfloat *)&matrix);

	glViewport(0, 0, window->width, window->height);
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	glVertexAttribPointer(position, 4, GL_FLOAT, GL_FALSE,
			      4 * sizeof(GLfloat), vertices);
	glEnableVertexAttribArray(position);

	glVertexAttribPointer(color, 4, GL_FLOAT, GL_FALSE,
			      4 * sizeof(GLfloat), colors);
	glEnableVertexAttribArray(color);

	glDrawElements(GL_TRIANGLES, ARRAY_SIZE(indices), GL_UNSIGNED_SHORT,
		       indices);

	eglSwapBuffers(window->egl.display, window->egl.surface);

	x += 0.3f;
	y += 0.2f;
	z += 0.4f;
}
示例#12
0
int main(int argc, char **argv){
	/* vertices for a triangle */
	/* Why does triangle[] = { vec4_new(...) } result in a segfault when returning
	 * from _mm_load_ps?
	 */
	/* Just want a sort of 3D object, but not a closed object otherwise it's hard
	 * to tell what's going on w/ flat shading */
	vec4_t object[18];
	//+Z face
	object[0] = vec4_new(-1, -1, 1, 1);
	object[1] = vec4_new(1, -1, 1, 1);
	object[2] = vec4_new(1, 1, 1, 1);
	object[3] = vec4_new(1, 1, 1, 1);
	object[4] = vec4_new(-1, 1, 1, 1);
	object[5] = vec4_new(-1, -1, 1, 1);
	//+X face
	object[6] = vec4_new(1, -1, 1, 1);
	object[7] = vec4_new(1, -1, -1, 1);
	object[8] = vec4_new(1, 1, -1, 1);
	object[9] = vec4_new(1, 1, -1, 1);
	object[10] = vec4_new(1, 1, 1, 1);
	object[11] = vec4_new(1, -1, 1, 1);
	//-X face
	object[12] = vec4_new(-1, -1, 1, 1);
	object[13] = vec4_new(-1, -1, -1, 1);
	object[14] = vec4_new(-1, 1, -1, 1);
	object[15] = vec4_new(-1, 1, -1, 1);
	object[16] = vec4_new(-1, 1, 1, 1);
	object[17] = vec4_new(-1, -1, 1, 1);

	if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
		fprintf(stderr, "SDL Init error: %s\n", SDL_GetError());
		return 1;
	}

	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
#ifdef DEBUG
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
#endif

	SDL_Window *win = SDL_CreateWindow("SSE GL Test", SDL_WINDOWPOS_CENTERED,
		SDL_WINDOWPOS_CENTERED, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(win);

	if (check_GL_error("Opened win + context")){
		SDL_GL_DeleteContext(context);
		SDL_DestroyWindow(win);
		return 1;
	}

	glewExperimental = GL_TRUE;
	GLenum err = glewInit();
	if (err != GLEW_OK){
		fprintf(stderr, "GLEW init error %d\n", err);
		SDL_GL_DeleteContext(context);
		SDL_DestroyWindow(win);
		return 1;
	}
	check_GL_error("Post GLEW init");
#ifdef DEBUG
	glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
	glDebugMessageCallbackARB(gl_debug_callback, NULL);
	glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE,
		0, NULL, GL_TRUE);
#endif
	glClearColor(0, 0, 0, 1);
	glClearDepth(1);
	glEnable(GL_DEPTH_TEST);

	//Model's vao and vbo
	GLuint model[2];
	glGenVertexArrays(1, model);
	glBindVertexArray(model[0]);
	glGenBuffers(1, model + 1);
	glBindBuffer(GL_ARRAY_BUFFER, model[1]);
	glBufferData(GL_ARRAY_BUFFER, 4 * 6 * 3 * sizeof(GLfloat), object, GL_STATIC_DRAW);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
	if (check_GL_error("Setup buffers")){
		return 1;
	}

	GLint vshader = make_shader(vert_shader_src, GL_VERTEX_SHADER);
	GLint fshader = make_shader(frag_shader_src, GL_FRAGMENT_SHADER);
	if (vshader == -1 || fshader == -1){
		return 1;
	}
	GLint program = make_program(vshader, fshader);
	if (program == -1){
		return 1;
	}
	glDeleteShader(vshader);
	glDeleteShader(fshader);

	mat4_t model_mat = mat4_mult(mat4_rotate(45, vec4_new(1, 1, 0, 0)), mat4_scale(2, 2, 2));
	model_mat = mat4_mult(mat4_translate(vec4_new(0, 2, -5, 1)), model_mat);
	mat4_t view_mat = mat4_look_at(vec4_new(0, 0, 5, 0), vec4_new(0, 0, 0, 0), vec4_new(0, 1, 0, 0));
	mat4_t proj_mat = mat4_perspective(75, ((float)WIN_WIDTH) / WIN_HEIGHT, 1, 100);
	glUseProgram(program);
	GLuint model_unif = glGetUniformLocation(program, "model");
	GLuint view_unif = glGetUniformLocation(program, "view");
	GLuint proj_unif = glGetUniformLocation(program, "proj");
	glUniformMatrix4fv(model_unif, 1, GL_FALSE, (GLfloat*)&model_mat);
	glUniformMatrix4fv(view_unif, 1, GL_FALSE, (GLfloat*)&view_mat);
	glUniformMatrix4fv(proj_unif, 1, GL_FALSE, (GLfloat*)&proj_mat);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glDrawArrays(GL_TRIANGLES, 0, 18);
	SDL_GL_SwapWindow(win);
	check_GL_error("Post Draw");

	SDL_Event e;
	int quit = 0;
	while (!quit){
		while (SDL_PollEvent(&e)){
			if (e.type == SDL_QUIT || e.type == SDL_KEYDOWN){
				quit = 1;
			}
		}
	}

	glDeleteProgram(program);
	glDeleteVertexArrays(1, model);
	glDeleteBuffers(1, model + 1);
	SDL_GL_DeleteContext(context);
	SDL_DestroyWindow(win);

	return 0;
}
示例#13
0
文件: main.c 项目: tmpvar/cpu-voxels
int main(void)
{

  _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
  _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);

  int width = 800, height = 600;
  GLFWwindow* window;
  glfwSetErrorCallback(error_callback);
  if (!glfwInit())
    exit(EXIT_FAILURE);
  window = glfwCreateWindow(width, height, "cpu-voxels", NULL, NULL);
  if (!window)
  {
    glfwTerminate();
    return 1;
  }
  glfwMakeContextCurrent(window);
  glfwSwapInterval(0);
  glfwSetKeyCallback(window, key_callback);
  glfwSetMouseButtonCallback(window, mouse_button_callback);
  glfwSetCursorPosCallback(window, mouse_move_callback);
  glfwSetKeyCallback(window, key_callback);

  vec3 eye = vec3_create(0.0f, 0.0f, VOXEL_BRICK_SIZE * 4);
  vec3 center = vec3f(0.0f);
  vec3 up = vec3_create(0.0, 1.0, 0.0 );

  orbit_camera_init(eye, center, up);

  // TODO: handle resize

  int dw, dh;
  glfwGetFramebufferSize(window, &dw, &dh);
  int stride = 3;
  int total = dw*dh*stride;
  uint8_t *data = malloc(total);

  vec3 ro; //, rd;
  mat4 m4inverted, view;
  mat4 projection;
  mat4_perspective(
    projection,
    M_PI/4.0,
    (float)width/(float)height,
    0.1,
    1000.0
  );
  GLuint texture[1];

#ifdef ENABLE_THREADS
  screen_area areas[TOTAL_THREADS];
  threadpool thpool = thpool_init(TOTAL_THREADS);
#else
  screen_area areas[1];
#endif

  glGenTextures(1, texture);
  float start = glfwGetTime();
  int fps = 0;
  voxel_brick my_first_brick = voxel_brick_create();
  // TODO: make this work when the brick lb corner is not oriented at 0,0,0
  voxel_brick_position(my_first_brick, vec3f(0.0f));
  voxel_brick_fill(my_first_brick, &brick_fill);

  while (!glfwWindowShouldClose(window)) {
    if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
      orbit_camera_rotate(0, 0, -.1, 0);
    }

    if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
      orbit_camera_rotate(0, 0, .1, 0);
    }

    if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
      orbit_camera_rotate(0, 0, 0, .1);
    }

    if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
      orbit_camera_rotate(0, 0, 0, -.1);
    }

    glfwGetFramebufferSize(window, &width, &height);
    float now = glfwGetTime();
    if (now - start > 1) {
      unsigned long long total_rays = (fps * width * height);
      printf("fps: %i (%f Mrays/s)@%ix%i - %i threads\n", fps, total_rays/1000000.0, width, height, TOTAL_THREADS);
      start = now;
      fps = 0;
    }
    fps++;


    orbit_camera_view(view);
    ro = mat4_get_eye(view);

    mat4_mul(m4inverted, projection, view);
    mat4_invert(m4inverted, m4inverted);

    // compute 3 points so that we can interpolate instead of unprojecting
    // on every point
    vec3 rda, rdb, planeYPosition, dcol, drow;

    vec3 t0 = vec3_create(0, 0, 0), tx = vec3_create(1, 0, 0), ty = vec3_create(0, 1, 0);
    vec4 viewport = { 0, 0, width, height };

    rda = orbit_camera_unproject(t0, viewport, m4inverted);
    rdb = orbit_camera_unproject(tx, viewport, m4inverted);
    planeYPosition = orbit_camera_unproject(ty, viewport, m4inverted);
    dcol = planeYPosition - rda;
    drow = rdb - rda;

    int i=0, bh = height;
#ifdef ENABLE_THREADS
    bh = (height/TOTAL_THREADS);

    for (i; i<TOTAL_THREADS; i++) {
#endif
      areas[i].dcol = dcol;
      areas[i].drow = drow;
      areas[i].pos = planeYPosition;
      areas[i].ro = ro;
      areas[i].x = 0;
      areas[i].y = i*bh;
      areas[i].width = width;
      areas[i].height = areas[i].y + (int)(bh);
      areas[i].screen_height = (int)(height);
      areas[i].stride = stride;
      areas[i].data = data;
      areas[i].render_id = i;
      areas[i].brick = my_first_brick;
#ifdef ENABLE_THREADS
      thpool_add_work(thpool, (void *)render_screen_area, (void *)(&areas[i]));
    }

    thpool_wait(thpool);
#else
    render_screen_area((void *)(&areas[i]));
#endif

#ifdef RENDER
    glViewport(0, 0, width, height);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDisable(GL_CULL_FACE);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();
    glScalef(1.0f, -1.0f, 1.0f);

    glEnable(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    glBegin(GL_QUADS);
      glTexCoord2f(0.0f, 0.0f); glVertex2f( -1, -1);
      glTexCoord2f(1.0f, 0.0f); glVertex2f(  1, -1);
      glTexCoord2f(1.0f, 1.0f); glVertex2f(  1,  1);
      glTexCoord2f(0.0f, 1.0f); glVertex2f( -1,  1);
    glEnd();

    glfwSwapBuffers(window);

    glDeleteTextures(1, &texture[0]);
#endif

    glfwPollEvents();
  }
  glfwDestroyWindow(window);
  glfwTerminate();
  exit(EXIT_SUCCESS);
}