diff options
| -rw-r--r-- | resources/shader.frag | 17 | ||||
| -rw-r--r-- | resources/shader.vert | 13 | ||||
| -rw-r--r-- | state_mainstate/include/states/mainstate.h | 2 | ||||
| -rw-r--r-- | state_mainstate/src/mainstate.c | 154 |
4 files changed, 81 insertions, 105 deletions
diff --git a/resources/shader.frag b/resources/shader.frag index f81277d..0b81464 100644 --- a/resources/shader.frag +++ b/resources/shader.frag @@ -2,10 +2,25 @@ // Ouput data in vec2 UV; +in vec3 FragmentPos; +in vec3 Normal; out vec3 color; uniform sampler2D textureSampler; void main() { - color = texture(textureSampler, UV).rgb; + vec3 light_color_ambient = vec3(0.4, 0.6, 0.9); + vec3 light_color_diffuse = vec3(0.8, 1.0, 1.0); + + vec3 lightpos = vec3(16, 26, 32); + + float ambient_strength = 0.3; + vec3 ambient = ambient_strength * light_color_ambient; + + vec3 norm = normalize(Normal); + vec3 light_dir = normalize(lightpos - FragmentPos); + + float diff = max(dot(norm, light_dir), 0.0); + vec3 diffuse = diff * light_color_diffuse; + color = (ambient + diffuse) * texture(textureSampler, UV).rgb; } diff --git a/resources/shader.vert b/resources/shader.vert index e5aff9a..50e8218 100644 --- a/resources/shader.vert +++ b/resources/shader.vert @@ -3,21 +3,20 @@ // Input vertex data, different for all executions of this shader. layout(location = 0) in vec3 vertexPosition_modelspace; layout(location = 1) in vec2 vertexUV; +layout(location = 2) in vec3 vNorm; out vec2 UV; +out vec3 FragmentPos; +out vec3 Normal; uniform mat4 MVP; +uniform mat4 modelPosition; void main() { gl_Position = MVP * vec4(vertexPosition_modelspace, 1); UV = vertexUV; - - //fragmentcolor.x = vertexPosition_modelspace.x; //= vertexColor; - //fragmentcolor.y = vertexPosition_modelspace.y; //= vertexColor; - //fragmentcolor.z = vertexPosition_modelspace.z; //= vertexColor; - - //fragmentcolor += 1; - //fragmentcolor /= 2; + FragmentPos = vec3(modelPosition * vec4(vertexPosition_modelspace,1)); + Normal = vNorm; } diff --git a/state_mainstate/include/states/mainstate.h b/state_mainstate/include/states/mainstate.h index dc1cb7c..03c75d9 100644 --- a/state_mainstate/include/states/mainstate.h +++ b/state_mainstate/include/states/mainstate.h @@ -20,7 +20,7 @@ typedef struct mainstate_state { RenderBatch terrain; RenderObject objects[10]; u8 world[WORLD_HEIGHT * WORLD_WIDTH * WORLD_LENGTH]; - i_ctx input_ctx; + i_ctx input_ctx; binding_t input_bindings[10]; vec3 cam_dir; f32 cam_dir_dt; diff --git a/state_mainstate/src/mainstate.c b/state_mainstate/src/mainstate.c index 2b12d9c..eea1f18 100644 --- a/state_mainstate/src/mainstate.c +++ b/state_mainstate/src/mainstate.c @@ -48,7 +48,7 @@ enum blocktypes { static f32 crate_texture_coords[36*2]; static f32 crate_texture_coords2[36*2]; static f32 crate[36*3]; -//static f32 crate_normals[36*2]; +static f32 crate_normals[36*3]; // TODO: Fix rendering positions on models with IBOs static f32 quad[8]; @@ -57,16 +57,19 @@ static u16 quad_ibo[6]; #define COUNT(a) sizeof(a) / sizeof(a[0]) /* this is an unfortunate way of declaring this, unfortunately we _really_ don't * want to force compile-time known sizes of the data */ +/* The order here matters tremendously, as they're passed to the shader pipeline + * in the same order. I've wasted many hours due to IBO being passed before + * other data:) */ #define staticdraw ShaderBuffer_AccessFrequency_static | ShaderBuffer_AccessType_draw ShaderBuffer shaderbuf[] = { SHADERBUFFER_NEW(f32, COUNT(crate), 3, crate, staticdraw | ShaderBuffer_Type_vertexPosition), SHADERBUFFER_NEW(f32, COUNT(crate_texture_coords), 2, crate_texture_coords, staticdraw), - //SHADERBUFFER_NEW(f32, COUNT(crate_normals), 2, crate_normals, staticdraw), + SHADERBUFFER_NEW(f32, COUNT(crate_normals), 3, crate_normals, staticdraw), }; ShaderBuffer shaderbuf2[] = { - SHADERBUFFER_NEW(f32, COUNT(crate), 3, crate, staticdraw | ShaderBuffer_Type_vertexPosition), + SHADERBUFFER_NEW(f32, COUNT(crate), 3, crate, staticdraw | ShaderBuffer_Type_vertexPosition), SHADERBUFFER_NEW(f32, COUNT(crate_texture_coords2), 2, crate_texture_coords2, staticdraw), - //SHADERBUFFER_NEW(f32, COUNT(crate_normals), 2, crate_normals, staticdraw), + SHADERBUFFER_NEW(f32, COUNT(crate_normals), 3, crate_normals, staticdraw), }; ShaderBuffer shaderbuf_quad[] = { SHADERBUFFER_NEW(f32, COUNT(quad), 2, quad, staticdraw | ShaderBuffer_Type_vertexPosition), @@ -119,8 +122,10 @@ void fov_decrement(mainstate_state *s) { perspective_update(s); } +static const float cam_transition_dt = 0.8f; + void cam_rotate_l(mainstate_state *s) { - s->cam_dir_dt = 0.300f; + s->cam_dir_dt = cam_transition_dt; glm_vec3_rotate(s->cam_dir, 3.141f / 4.f, GLM_YUP); //glm_rotate_at(s->c.per, s->cam_pos, 3.141 / 4., GLM_YUP); //glm_rotate(s->c.per, 1, GLM_YUP); @@ -132,7 +137,7 @@ void cam_rotate_l(mainstate_state *s) { } void cam_rotate_r(mainstate_state *s) { - s->cam_dir_dt = 0.300f; + s->cam_dir_dt = cam_transition_dt; glm_vec3_rotate(s->cam_dir, -(3.141f / 4.f), GLM_YUP); //glm_rotate(s->c.per, -(3.141 / 4.), GLM_YUP); //perspective_update(s); @@ -149,10 +154,31 @@ void mainstate_init(mainstate_state *state, void* arg) { INFO("Arg was not null!"); } + // ----------- Calculate normals TODO MOVE TO ENGINE + for (usize i = 0; i < sizeof(crate) / sizeof(crate[0]) / 3; i++) { + // Face index + const usize f = i / 3; + // Face offset + const usize offset = f * 9; // 3 floats pr. vertex, 3 vertices per face. + vec3 A, B, C; + glm_vec3_copy(&crate[offset + ((i + 0) % 3) * 3], A); + glm_vec3_copy(&crate[offset + ((i + 1) % 3) * 3], B); + glm_vec3_copy(&crate[offset + ((i + 2) % 3) * 3], C); + + // We could allocate vec3's for AB, AC, and the norm, but this is more memory friendly:) + glm_vec3_sub(A,B,B); + glm_vec3_sub(A,C,C); + + glm_vec3_cross(B,C,A); + glm_vec3_normalize(A); + + glm_vec3_copy(A, &crate_normals[i * 3]); + } + // Use the INDICES of the assets to specify the shaders to be composed static u32 default_shader[] = {MyVertexShader, MyFragmentShader}; static u32 dither_shader[] = {MyVertexShader, MyDitherFragShader}; - static u32 quad_shader[] = {MyQuadVertexShader, MyQuadFragShader}; + static u32 quad_shader[] = {MyQuadVertexShader, MyQuadFragShader}; /* 0. Declare resources */ static asset_t mainstate_assets[] = { @@ -239,7 +265,7 @@ void mainstate_init(mainstate_state *state, void* arg) { // Shader get_asset(&state->resources, MyQuadShader), // Texture - 0, + ((Texture*)get_asset(&state->resources, MyQuadTexture))->id, // Vertices shaderbuf_quad, sizeof(shaderbuf_quad) / sizeof(ShaderBuffer) @@ -377,11 +403,9 @@ StateType mainstate_update(f64 dt, mainstate_state *state) { if (state->cam_dir_dt - dsec > 0) { //Lerp: a + f * (b - a); // or: a * (1-f) + f*b; - // 300ms - const f32 lerpduration = 0.300f; state->cam_dir_dt -= dsec; vec3 a, b; - const f32 f = 1.f - state->cam_dir_dt / lerpduration; + const f32 f = 1.f - state->cam_dir_dt / cam_transition_dt; glm_vec3_copy(state->c.dir, a); glm_vec3_copy(state->cam_dir, b); @@ -534,103 +558,41 @@ static f32 crate_texture_coords2[] = { 17.f*px, 0.5f, }; -//static f32 crate_normals[] = { -// // BEHIND 0 -// 49.f*px, 1.0f, -// 65.f*px, 1.0f, -// 65.f*px, 0.0f, -// -// // REAL LEFT 0 -// 33.f*px, 0.0f, -// 49.f*px, 1.0f, -// 49.f*px, 0.0f, -// -// // BOTTOM 0 -// 81*px, 0, -// 96*px, 1, -// 96*px, 0, -// -// // REAL LEFT 1 -// 33.f*px, 0.0f, -// 33.f*px, 1.0f, -// 49.f*px, 1.0f, -// -// // BEHIND 1 -// 49.f*px, 1.0f, -// 65.f*px, 0.0f, -// 49.f*px, 0.0f, -// -// // BOTTOM 1 -// 81*px, 0, -// 81*px, 1, -// 96*px, 1, -// -// // LEFT 0 -// 0.0f, 0.0f, -// 0.0f, 1.0f, -// 17.f*px, 1.0f, -// -// // RIGHT 0 -// 17.f*px, 0.0f, -// 33.f*px, 1.0f, -// 33.f*px, 0.0f, -// -// // RIGHT 1 -// 33.f*px, 1.0f, -// 17.f*px, 0.0f, -// 17.f*px, 1.0f, -// -// // TOP 0 -// 80.f*px, 1.0f, -// 65.f*px, 1.0f, -// 65.f*px, 0.0f, -// -// // TOP 1 -// 65.f*px, 1.f, -// 80.f*px, 0.f, -// 65.f*px, 0.f, -// -// // LEFT 1 -// 17.f*px, 0.0f, -// 0.f*px, 0.0f, -// 17.f*px, 1.0f, -//}; - static f32 crate[] = { - -1.0f, -1.0f, -1.0f, // triangle 1 : begin + -1.0f, -1.0f, -1.0f, // 1 -x -1.0f, -1.0f, 1.0f, - -1.0f, 1.0f, 1.0f, // triangle 1 : end - 1.0f, 1.0f, -1.0f, // triangle 2 : begin - -1.0f, -1.0f, -1.0f, - -1.0f, 1.0f, -1.0f, // triangle 2 : end - 1.0f, -1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, -1.0f, // 2 -z -1.0f, -1.0f, -1.0f, - 1.0f, -1.0f, -1.0f, - 1.0f, 1.0f, -1.0f, - 1.0f, -1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + 1.0f, -1.0f, 1.0f, // 3 down -1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, // 4 -z + 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, -1.0f, // 5 -x -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, - 1.0f, -1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, // 6 down -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, - -1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, // 7 +z -1.0f, -1.0f, 1.0f, - 1.0f, -1.0f, 1.0f, - 1.0f, 1.0f, 1.0f, - 1.0f, -1.0f, -1.0f, - 1.0f, 1.0f, -1.0f, - 1.0f, -1.0f, -1.0f, - 1.0f, 1.0f, 1.0f, - 1.0f, -1.0f, 1.0f, - 1.0f, 1.0f, 1.0f, - 1.0f, 1.0f, -1.0f, + 1.0f, -1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, // 8 +x + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, // 9 +x + 1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, // 10 up + 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, - 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, // 11 up -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, - 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, // 12 +z -1.0f, 1.0f, 1.0f, - 1.0f, -1.0f, 1.0f + 1.0f, -1.0f, 1.0f }; |
