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;
}
Example #2
0
static void test1_lod_bias(enum grate_textute_filter min_filter,
			   enum grate_textute_filter mag_filter)
{
	float bias = powf(sinf(elapsed * 0.3f), 2.0f) * 13.0f - 1.0f;
	struct mat4 mvp, transform;

	mat4_translate(&transform, 0.0f, 0.0f, -1.0f);
	mat4_multiply(&mvp, &projection, &transform);

	location = grate_get_vertex_uniform_location(quad_program, "mvp");
	grate_3d_ctx_set_vertex_mat4_uniform(ctx, location, &mvp);

	grate_texture_set_min_filter(tex[active_tex], min_filter);
	grate_texture_set_mag_filter(tex[active_tex], mag_filter);
	grate_texture_set_max_lod(tex[active_tex], max_lod);
	grate_3d_ctx_bind_texture(ctx, 0, tex[active_tex]);

	location = grate_get_fragment_uniform_location(quad_program, "lod_bias");
	grate_3d_ctx_set_fragment_float_uniform(ctx, location, bias);

	grate_3d_draw_elements(ctx, TGR3D_PRIMITIVE_TYPE_TRIANGLES,
			       idx_bo, TGR3D_INDEX_MODE_UINT16,
			       ARRAY_SIZE(indices));
	grate_flush(grate);

	grate_3d_printf(grate, ctx, font, 1, -0.85f, 0.85f, font_scale,
			"LOD BIAS: %f\nMAX LOD: %d\nMIN: %s\nMAG: %s\nMIPMAP: %s\n"
			"Texture id: %d",
			bias, max_lod,
			texfilter[min_filter], texfilter[mag_filter],
			mipdesc[active_tex], active_tex);

	grate_3d_printf(grate, ctx, font, 1, 0.55f, 0.85f, font_scale, "Test #1");
}
Example #3
0
void renderer_prerender(struct renderer *r)
{
	struct mat4 tmp;

	mat4_lookat(&r->view, &r->pos, &r->tar, &r->up);
	mat4_persp(
		&r->proj,
		70.0f,
		(float)(r->width) / r->height,
		0.1f,
		500.0f
	);
	mat4_id(&r->model);
	mat4_multiply(&tmp, &r->proj, &r->view);
	memcpy(&r->prev_mvp, &r->mvp, sizeof(r->mvp));
	mat4_multiply(&r->mvp, &tmp, &r->model);
}
Example #4
0
void render_debug(geometry *in, mat4_t model, mat4_t view, mat4_t projection)
{
    glUseProgram(in->debug_geometry.program);

    float mvp[16];
    mat4_multiply(projection, view, mvp);
    mat4_multiply(mvp, model , mvp);

    float normal_matrix[16];
    mat4_multiply(view, model , normal_matrix);

    mat4_inverse(normal_matrix, normal_matrix);
    mat4_transpose(normal_matrix, normal_matrix);

    glUniformMatrix4fv(in->debug_geometry.uniform.mvp_matrix, 1, GL_FALSE, mvp);

    glDrawArrays(GL_LINES, 0, in->vertex_count * 2);
}
Example #5
0
void mat4_rotateY(mat4 m, float theta) {
    mat4 transform = {
         cosf(theta), 0,  sinf(theta), 0,
                  0,  1,            0, 0,
        -sinf(theta), 0,  cosf(theta), 0,
                  0,  0,            0, 1
    };
    mat4 tmp;
    mat4_multiply(m, transform, tmp);
    mat4_copy(m, tmp);
}
Example #6
0
void mat4_translate(mat4 m, float x, float y, float z) {
    mat4 transform = {
        1, 0, 0, x,
        0, 1, 0, y,
        0, 0, 1, z,
        0, 0, 0, 1
    };
    mat4 tmp;
    mat4_multiply(m, transform, tmp);
    mat4_copy(m, tmp);
}
Example #7
0
void keyboard(unsigned char key, int x, int y)
{
    float direction[3];
    float rotation[16];
    mat4_identity(rotation);

    switch (key) {
        case 'q':
            vec3_subtract(center, eye, direction);
            vec3_normalize(direction, direction);
            vec3_add(eye, direction, eye);
            break;
        case 'e':
            vec3_subtract(center, eye, direction);
            vec3_normalize(direction, direction);
            vec3_subtract(eye, direction, eye);
            break;
        case 'a':
            mat4_rotateY(rotation, 0.1f, rotation);
            mat4_multiply(rotation, model, model);
            break;
        case 'd':
            mat4_rotateY(rotation, -0.1f, rotation);
            mat4_multiply(rotation, model, model);
            break;
        case 'w':
            mat4_rotateX(rotation, 0.1f, rotation);
            mat4_multiply(rotation, model, model);
            break;
        case 's':
            mat4_rotateX(rotation, -0.1f, rotation);
            mat4_multiply(rotation, model, model);
            break;
        case 27:
            exit(0);
        default:
            break;
    }
}
Example #8
0
vec3_t vec3_unproject(vec3_t vec, mat4_t view, mat4_t proj, vec4_t viewport, vec3_t dest) {
    if (!dest) { dest = vec; }

    mat4_t m = mat4_create(NULL);
    double *v = malloc(sizeof(double) * 4);
    
    v[0] = (vec[0] - viewport[0]) * 2.0 / viewport[2] - 1.0;
    v[1] = (vec[1] - viewport[1]) * 2.0 / viewport[3] - 1.0;
    v[2] = 2.0 * vec[2] - 1.0;
    v[3] = 1.0;
    
    mat4_multiply(proj, view, m);
    if(!mat4_inverse(m, NULL)) { return NULL; }
    
    mat4_multiplyVec4(m, v, NULL);
    if(v[3] == 0.0) { return NULL; }

    dest[0] = v[0] / v[3];
    dest[1] = v[1] / v[3];
    dest[2] = v[2] / v[3];
    
    return dest;
}
Example #9
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;
}
Example #10
0
void renderer_bind_geometry(struct renderer *r)
{
	struct mat4 tmp;
	GLuint prog;

	prog = r->geom.prog;
	/* Prepare for rendering geometry */
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, r->fb.fbo);
	glDrawBuffer(GL_COLOR_ATTACHMENT0 + NUM_FRAMEBUFFER_ATTACHMENTS);
	glClear(GL_COLOR_BUFFER_BIT);
	glDrawBuffers(
		sizeof(drawbuffers) / sizeof(drawbuffers[0]),
		drawbuffers
	);
	glDepthMask(GL_TRUE);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);

	mat4_multiply(&tmp, &r->proj, &r->view);

	/* Set up shader variables */
	glUseProgram(prog);
	glUniformMatrix4fv(
		glGetUniformLocation(prog, "ModelMatrix"),
		1,
		GL_FALSE,
		r->model.a
	);
	mat4_multiply(&tmp, &r->proj, &r->view);
	glUniformMatrix4fv(
		glGetUniformLocation(prog, "ViewProjectionMatrix"),
		1,
		GL_FALSE,
		tmp.a
	);

	glUniformMatrix4fv(
		glGetUniformLocation(prog, "PreviousMVP"),
		1,
		GL_FALSE,
		r->prev_mvp.a
	);
	
	glBindBuffer(GL_ARRAY_BUFFER, r->cube_attr);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, r->cube_idx);
	/* Position attrib */
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(
		0,
		3,
		GL_FLOAT,
		GL_FALSE,
		sizeof(struct attribs),
		0
	);
	/* Normal attrib */
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(
		1,
		3,
		GL_FLOAT,
		GL_FALSE,
		sizeof(struct attribs),
		(GLvoid *)(sizeof(struct vec3))
	);
	/* Vertex color attrib */
	glEnableVertexAttribArray(2);
	glVertexAttribPointer(
		2,
		3,
		GL_FLOAT,
		GL_FALSE,
		sizeof(struct attribs),
		(GLvoid *)(sizeof(struct vec3) * 2)
	);
	/* Texcoord attrib */
	glEnableVertexAttribArray(3);
	glVertexAttribPointer(
		3,
		2,
		GL_FLOAT,
		GL_FALSE,
		sizeof(struct attribs),
		(GLvoid *)(sizeof(struct vec3) * 3)
	);
}