Implemented rendering vertex data to gbuffer

dev
Shariq Shah 9 years ago
parent bb3abf5e00
commit f815dafd82
  1. 2
      assets/shaders/commonFrag.glsl
  2. 12
      assets/shaders/commonVert.glsl
  3. 17
      assets/shaders/unshaded.frag
  4. 2
      build/linux/makefile
  5. 11
      orgfile.org
  6. 6
      src/bounding_volumes.c
  7. 2
      src/framebuffer.c
  8. 9
      src/game.c
  9. 12
      src/material.c
  10. 17
      src/model.c
  11. 103
      src/renderer.c

@ -7,4 +7,4 @@ in vec3 vertex;
//in vec4 vertLightSpace;
// Fragment Shader Output
out vec4 frag_color;
//out vec4 frag_color;

@ -11,8 +11,8 @@ out vec3 vertCamSpace;
out vec4 vertLightSpace;
// Common uniforms
uniform mat4 modelMat;
uniform mat4 viewMat;
uniform mat4 model_mat;
uniform mat4 view_mat;
uniform mat4 mvp;
uniform mat4 lightVPMat;
@ -25,8 +25,8 @@ void setOutputs()
{
uv = vUV;
//Normal and vertex sent to the fragment shader should be in the same space!
normal = vec4(modelMat * vec4(vNormal, 0.0)).xyz;
vertex = vec4(modelMat * vec4(vPosition, 1.0)).xyz;
vertCamSpace = vec4(viewMat * vec4(vPosition, 1.0)).xyz;
vertLightSpace = vec4((lightVPMat * modelMat) * vec4(vPosition, 1.0));
normal = vec4(model_mat * vec4(vNormal, 0.0)).xyz;
vertex = vec4(model_mat * vec4(vPosition, 1.0)).xyz;
vertCamSpace = vec4(view_mat * vec4(vPosition, 1.0)).xyz;
vertLightSpace = vec4((lightVPMat * model_mat) * vec4(vPosition, 1.0));
}

@ -2,8 +2,23 @@
uniform sampler2D diffuse_texture;
out vec4 gbuffer[4];
void main()
{
frag_color = diffuse_color * texture(diffuse_texture, uv);
// gl_FragData[0] = diffuse_color * texture(diffuse_texture, uv);
// gl_FragData[1] = vec4(vertex.xyz, 1.0);
// gl_FragData[2] = vec4(normal.xyz, 1.0);
// gl_FragData[3] = vec4(uv, 0.0, 1.0);
gbuffer[0] = diffuse_color * texture(diffuse_texture, uv);
gbuffer[1] = vec4(vertex.xyz, 1.0);
gbuffer[2] = vec4(normal.xyz, 1.0);
gbuffer[3] = vec4(uv, 0.0, 1.0);
// gbuffer[0] = (diffuse_color * texture(diffuse_texture, uv)).xyz;
// gbuffer[1] = vertex.xyz;
// gbuffer[2] = normal.xyz;
// gbuffer[3] = vec3(uv, 0.0);
//frag_color = vec4(1, 0, 0, 1);
}

@ -12,7 +12,7 @@ all: $(OBJS)
$(CC) $(CFLAGS) -o $(PROJECT_NAME) $^ $(LIBS)
%.o: $(SRC_DIR)/%.c
$(CC) -c $< -o $@
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm $(PROJECT_NAME)

@ -41,8 +41,10 @@ All the code in this repository is under GPLv3, see LICENSE for more information
- State "DONE" from "TODO" [2015-08-02 Sun 19:09]
** DONE Fix readme markdown
- State "DONE" from "TODO" [2015-12-10 Thu 16:36]
** TODO Framebuffer and resolution independent rendering
** TODO A simpler build system without dependencies
** DONE Framebuffer and resolution independent rendering
- State "DONE" from "TODO" [2016-05-27 Fri 18:02]
** DONE A simpler build system without dependencies
- State "DONE" from "TODO" [2016-05-27 Fri 14:50]
** TODO Remove dependencies
- glfw3
- glew
@ -67,9 +69,12 @@ All the code in this repository is under GPLv3, see LICENSE for more information
** DONE Heirarchical Transforms
** DONE Materials with textures
- State "DONE" from "TODO" [2015-10-15 Thu 21:21]
** TODO Fix problems with frustrum culling
** TODO 2d drawing routines
** TODO Gui
** TODO Image based lighting?
** TODO Deferred rendering?
** TODO Physics
** TODO Improve the readme
** TODO Improve this readme
** TODO ???
** TODO Profit!

@ -3,7 +3,7 @@
#include <math.h>
int bv_intersect_frustum_box(vec4* frustum, struct Bounding_Box* box, struct Transform* transform)
int bv_intersect_frustum_box(vec4* frustum, struct Bounding_Box* box, struct Transform* transform)
{
vec3 min, max, size, center, half_ext, half_size;
vec3_add(&min, &box->min, &transform->position);
@ -12,9 +12,9 @@ int bv_intersect_frustum_box(vec4* frustum, struct Bounding_Box* box, struct Tr
vec3_mul(&min, &min, &transform->scale);
vec3_sub(&size, &max, &min);
vec3_add(&center, &max, &min);
vec3_scale(&center, &center, (1.f/ 2.f));
vec3_scale(&center, &center, 0.5f);
vec3_assign(&half_ext, &size);
vec3_scale(&half_size, &size, (1.f / 2.f));
vec3_scale(&half_size, &size, 0.5f);
for(int i = 0; i < 6; i++)
{
vec3 normal = {frustum[i].x, frustum[i].y, frustum[i].z};

@ -139,7 +139,7 @@ void framebuffer_set_texture(int index, int texture, int attachment)
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &current_fbo);
renderer_check_glerror("framebuffer:set_texture:glGet");
framebuffer_bind(index);
glFramebufferTexture2D(GL_FRAMEBUFFER,
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
attachment,
GL_TEXTURE_2D,
texture_get_texture_handle(texture),

@ -92,7 +92,7 @@ void scene_setup(void)
render_width = 800;
render_height = 600;
struct Camera* camera = entity_component_add(player, C_CAMERA, render_width, render_height);
camera_attach_fbo(camera, render_width, render_height, 1, 1);
//camera_attach_fbo(camera, render_width, render_height, 1, 1);
vec4_fill(&camera->clear_color, 0.3f, 0.6f, 0.9f, 1.0f);
camera_set_primary_viewer(camera);
@ -118,7 +118,7 @@ void scene_setup(void)
model_set_material_param(ground_model, "diffuse_color", &color);
struct Transform* ground_tran = entity_component_get(ground, C_TRANSFORM);
vec3 pos = {0, -3, -3};
vec3 scale_ground = {0.5f, 0.5f, 3.f};
vec3 scale_ground = {20.f, 10.f, 20.f};
transform_set_position(ground_tran, &pos);
transform_scale(ground_tran, &scale_ground);
@ -155,14 +155,13 @@ void debug(float dt)
if(input_map_state_get("Turn_Down", GLFW_PRESS)) turn_up_down -= turn_speed;
if(input_map_state_get("Turn_Right", GLFW_PRESS)) turn_left_right += turn_speed;
if(input_map_state_get("Turn_Left", GLFW_PRESS)) turn_left_right -= turn_speed;
double cursor_lr, cursor_ud;
input_cursor_pos_get(&cursor_lr, &cursor_ud);
if(input_mousebutton_state_get(GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS))
{
input_cursor_mode_set(CM_LOCKED);
const double scale = 0.25;
double cursor_lr, cursor_ud;
input_cursor_pos_get(&cursor_lr, &cursor_ud);
turn_up_down = -cursor_ud * turn_speed * dt * scale;
turn_left_right = cursor_lr * turn_speed * dt * scale;
input_cursor_pos_set(0.0, 0.0);

@ -28,11 +28,23 @@ void material_init(void)
unshaded_mat->pipeline_params = array_new(struct Uniform);
unshaded_mat->active = 1;
/* Pipeline params/uniforms */
struct Uniform* uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform);
uniform->name = str_new("mvp");
uniform->type = UT_MAT4;
uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name);
uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform);
uniform->name = str_new("model_mat");
uniform->type = UT_MAT4;
uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name);
uniform = array_grow(unshaded_mat->pipeline_params, struct Uniform);
uniform->name = str_new("view_mat");
uniform->type = UT_MAT4;
uniform->location = shader_get_uniform_location(unshaded_mat->shader, uniform->name);
/* Materail params */
uniform = array_grow(unshaded_mat->model_params, struct Uniform);
uniform->name = str_new("diffuse_color");
uniform->type = UT_VEC4;

@ -123,19 +123,20 @@ void model_render_all(struct Camera* camera)
struct Model* model = &model_list[material->registered_models[j]];
struct Entity* entity = entity_get(model->node);
struct Transform* transform = entity_component_get(entity, C_TRANSFORM);
/* set material params for the model */
for(int k = 0; k < array_len(model->material_params); k++)
{
/* set material params for the model */
struct Material_Param* param = &model->material_params[k];
struct Uniform* uniform = &material->model_params[param->uniform_index];
shader_set_uniform(uniform->type, uniform->location, param->value);
renderer_check_glerror("model:render_all:material_param");
}
/* Set pipeline uniforms */
for(int k = 0; k < array_len(material->pipeline_params); k++)
{
/* TODO: change this into something better */
/* Set pipeline uniforms */
struct Uniform* uniform = &material->pipeline_params[k];
if(strcmp(uniform->name, "mvp") == 0)
{
@ -144,9 +145,19 @@ void model_render_all(struct Camera* camera)
shader_set_uniform(uniform->type, uniform->location, &mvp);
renderer_check_glerror("model:render_all:material_pipeline");
}
else if(strcmp(uniform->name, "model_mat") == 0)
{
shader_set_uniform(uniform->type, uniform->location, &transform->trans_mat);
renderer_check_glerror("model:render_all:material_pipeline");
}
else if(strcmp(uniform->name, "view_mat") == 0)
{
shader_set_uniform(uniform->type, uniform->location, &camera->view_mat);
renderer_check_glerror("model:render_all:material_pipeline");
}
}
/* Render the geometry */
geom_render_in_frustum(model->geometry_index, &camera->frustum, transform);
geom_render_in_frustum(model->geometry_index, &camera->frustum[0], transform);
for(int k = 0; k < array_len(model->material_params); k++)
{

@ -1,4 +1,5 @@
#include "renderer.h"
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "log.h"
@ -13,7 +14,10 @@
#include "window_system.h"
static int def_fbo = -1;
static int def_render_tex = -1;
static int def_albedo_tex = -1;
static int def_position_tex = -1;
static int def_normal_tex = -1;
static int def_uv_tex = -1;
static int def_depth_tex = -1;
static int quad_geo = -1;
static int fbo_shader = -1;
@ -62,24 +66,60 @@ void renderer_init(GLFWwindow* window)
/* Textues for default fbo */
int width = -1, height = -1;
window_get_size(&width, &height);
def_render_tex = texture_create("def_render_texture",
def_albedo_tex = texture_create("def_albedo_texture",
TU_DIFFUSE,
width, height,
GL_RGBA,
GL_RGBA8,
GL_UNSIGNED_BYTE,
GL_RGB,
GL_RGB16F,
GL_FLOAT,
NULL);
texture_set_param(def_render_tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
texture_set_param(def_render_tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
texture_set_param(def_render_tex, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
texture_set_param(def_render_tex, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
texture_set_param(def_albedo_tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
texture_set_param(def_albedo_tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
texture_set_param(def_albedo_tex, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
texture_set_param(def_albedo_tex, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
def_position_tex = texture_create("def_position_texture",
TU_DIFFUSE,
width, height,
GL_RGB,
GL_RGB16F,
GL_FLOAT,
NULL);
texture_set_param(def_position_tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
texture_set_param(def_position_tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
texture_set_param(def_position_tex, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
texture_set_param(def_position_tex, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
def_normal_tex = texture_create("def_normal_texture",
TU_DIFFUSE,
width, height,
GL_RGB,
GL_RGB16F,
GL_FLOAT,
NULL);
texture_set_param(def_normal_tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
texture_set_param(def_normal_tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
texture_set_param(def_normal_tex, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
texture_set_param(def_normal_tex, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
def_uv_tex = texture_create("def_uv_texture",
TU_DIFFUSE,
width, height,
GL_RGB,
GL_RGB16F,
GL_FLOAT,
NULL);
texture_set_param(def_uv_tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
texture_set_param(def_uv_tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
texture_set_param(def_uv_tex, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
texture_set_param(def_uv_tex, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
def_depth_tex = texture_create("def_depth_texture",
TU_SHADOWMAP1,
width, height,
GL_DEPTH_COMPONENT,
GL_DEPTH_COMPONENT,
GL_UNSIGNED_BYTE,
GL_DEPTH_COMPONENT32F,
GL_FLOAT,
NULL);
texture_set_param(def_depth_tex, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
texture_set_param(def_depth_tex, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
@ -88,8 +128,11 @@ void renderer_init(GLFWwindow* window)
texture_set_param(def_depth_tex, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
texture_set_param(def_depth_tex, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
def_fbo = framebuffer_create(width, height, 0, 1);
framebuffer_set_texture(def_fbo, def_render_tex, GL_COLOR_ATTACHMENT0);
def_fbo = framebuffer_create(width, height, 1, 1);
framebuffer_set_texture(def_fbo, def_albedo_tex, GL_COLOR_ATTACHMENT0);
framebuffer_set_texture(def_fbo, def_position_tex, GL_COLOR_ATTACHMENT0 + 1);
framebuffer_set_texture(def_fbo, def_normal_tex, GL_COLOR_ATTACHMENT0 + 2);
framebuffer_set_texture(def_fbo, def_uv_tex, GL_COLOR_ATTACHMENT0 + 3);
framebuffer_set_texture(def_fbo, def_depth_tex, GL_DEPTH_ATTACHMENT);
fbo_shader = shader_create("fbo.vert", "fbo.frag");
@ -108,7 +151,13 @@ void renderer_draw(void)
framebuffer_bind(fbo);
{
glViewport(0, 0, framebuffer_get_width(fbo), framebuffer_get_height(fbo));
glDrawBuffer(GL_COLOR_ATTACHMENT0);
GLenum draw_buffers[] = {GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT1,
GL_COLOR_ATTACHMENT2,
GL_COLOR_ATTACHMENT3};
glDrawBuffers(4, &draw_buffers[0]);
//glDrawBuffer(GL_COLOR_ATTACHMENT0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
@ -125,15 +174,33 @@ void renderer_draw(void)
int width, height;
window_get_size(&width, &height);
glViewport(0, 0, width, height);
//glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
shader_bind(fbo_shader);
struct Camera* primary_camera = camera_get_primary();
texture_bind(primary_camera->render_tex);
//texture_bind(primary_camera->render_tex);
glViewport(0, height / 2, width / 2, height / 2);
texture_bind(def_albedo_tex);
geom_render(quad_geo);
texture_unbind(def_albedo_tex);
glViewport(width / 2, height / 2, width / 2, height / 2);
texture_bind(def_position_tex);
geom_render(quad_geo);
texture_unbind(def_render_tex);
texture_unbind(def_position_tex);
glViewport(0, 0, width / 2, height / 2);
texture_bind(def_normal_tex);
geom_render(quad_geo);
texture_unbind(def_normal_tex);
glViewport(width / 2, 0, width / 2, height / 2);
texture_bind(def_uv_tex);
geom_render(quad_geo);
texture_unbind(def_uv_tex);
shader_unbind();
}
@ -141,7 +208,7 @@ void renderer_cleanup(void)
{
geom_remove(quad_geo);
framebuffer_remove(def_fbo);
texture_remove(def_render_tex);
texture_remove(def_albedo_tex);
texture_remove(def_depth_tex);
}

Loading…
Cancel
Save