diff options
author | Omniscient <omniscient.oce@gmail.com> | 2024-06-09 16:51:46 +1000 |
---|---|---|
committer | Omniscient <omniscient.oce@gmail.com> | 2024-06-09 16:51:46 +1000 |
commit | b9a7166d9a7e2b0eed948353c134e72dfa86ab58 (patch) | |
tree | a32d0253f45c927f141e3f4cbc1f58a0f6ca3adb | |
parent | 9c79df522980eabdc5e52592cbd152e2a285c4cc (diff) |
fix texture on cube with opengl
-rw-r--r-- | assets/shaders/cube.frag | 5 | ||||
-rw-r--r-- | assets/shaders/cube.vert | 7 | ||||
-rw-r--r-- | examples/cube/ex_cube.c | 6 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.c | 43 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.h | 1 |
5 files changed, 45 insertions, 17 deletions
diff --git a/assets/shaders/cube.frag b/assets/shaders/cube.frag index 9d8ab28..02cb9b3 100644 --- a/assets/shaders/cube.frag +++ b/assets/shaders/cube.frag @@ -4,10 +4,11 @@ layout(location = 0) in vec3 fragColor; layout(location = 1) in vec2 fragTexCoord; layout(binding = 1) uniform sampler2D texSampler; +// uniform sampler2D texSampler; layout(location = 0) out vec4 outColor; void main() { - // outColor = texture(texSampler, fragTexCoord); // vec4(fragTexCoord, 0.0); - outColor = vec4(fragColor, 1.0); + outColor = texture(texSampler, fragTexCoord); // vec4(fragTexCoord, 0.0); + // outColor = vec4(fragColor, 1.0); } diff --git a/assets/shaders/cube.vert b/assets/shaders/cube.vert index e7d6a79..2f81e9c 100644 --- a/assets/shaders/cube.vert +++ b/assets/shaders/cube.vert @@ -5,9 +5,6 @@ layout( binding = 0) uniform Matrices { mat4 view; mat4 proj; } ubo; -uniform mat4 model; -uniform mat4 view; -uniform mat4 projection; layout(location = 0) in vec3 inPosition; layout(location = 1) in vec3 inNormal; @@ -17,9 +14,7 @@ layout(location = 0) out vec3 fragColor; layout(location = 1) out vec2 fragTexCoord; void main() { - // gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0); - gl_Position = projection * view * model * vec4(inPosition, 1.0); - // gl_Position = vec4(inPosition, 1.0); + gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0); fragColor = abs(inNormal); fragTexCoord = inTexCoords; } diff --git a/examples/cube/ex_cube.c b/examples/cube/ex_cube.c index e85b5c1..8789b00 100644 --- a/examples/cube/ex_cube.c +++ b/examples/cube/ex_cube.c @@ -142,11 +142,7 @@ int main() { mvp_uniforms mvp_data = { .model = model, .view = view, .projection = proj }; my_shader_bind_group shader_bind_data = { .mvp = mvp_data, .tex = texture }; mvp_uniforms_data.data = &shader_bind_data; - /* encode_bind_shader_data(enc, 0, &mvp_uniforms_data); */ - - uniform_mat4f(enc->pipeline->shader_id, "model", &model); - uniform_mat4f(enc->pipeline->shader_id, "view", &view); - uniform_mat4f(enc->pipeline->shader_id, "projection", &proj); + encode_bind_shader_data(enc, 0, &mvp_uniforms_data); // Record draw calls draw_mesh(&cube, &model); diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c index 17587a2..ba9435e 100644 --- a/src/renderer/backends/opengl/backend_opengl.c +++ b/src/renderer/backends/opengl/backend_opengl.c @@ -3,6 +3,7 @@ #include "colours.h" #include "opengl_helpers.h" #include "ral_types.h" +#define CEL_REND_BACKEND_OPENGL #if defined(CEL_REND_BACKEND_OPENGL) #include <assert.h> #include <stdlib.h> @@ -178,9 +179,10 @@ void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* d gpu_buffer* ubo_buf = BUFFER_GET(b); glBindBuffer(GL_UNIFORM_BUFFER, ubo_buf->id.ubo); glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo_buf->size, data->data); - /* printf("Size %d\n", ubo_buf->size); */ - - /* glBindBuffer(GL_UNIFORM_BUFFER, 0); */ + } else if (binding.type == SHADER_BINDING_TEXTURE) { + gpu_texture* tex = TEXTURE_GET(binding.data.texture.handle); + glActiveTexture(GL_TEXTURE0 + i); + glBindTexture(GL_TEXTURE_2D, tex->id); } } } @@ -260,7 +262,40 @@ buffer_handle gpu_buffer_create(u64 size, gpu_buffer_type buf_type, gpu_buffer_f void gpu_buffer_destroy(buffer_handle buffer) {} void gpu_buffer_upload(const void* data) {} -texture_handle gpu_texture_create(texture_desc desc, bool create_view, const void* data) {} +texture_handle gpu_texture_create(texture_desc desc, bool create_view, const void* data) { + // "allocating" the cpu-side struct + texture_handle handle; + gpu_texture* texture = texture_pool_alloc(&context.resource_pools->textures, &handle); + DEBUG("Allocated texture with handle %d", handle.raw); + + GLuint gl_texture_id; + glGenTextures(1, &gl_texture_id); + texture->id = gl_texture_id; + + glBindTexture(GL_TEXTURE_2D, gl_texture_id); + + // set the texture wrapping parameters + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, + GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + // set texture filtering parameters + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + if (data) { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, desc.extents.x, desc.extents.y, 0, + GL_RGBA, // TODO: convert format to GL enum + GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + } else { + WARN("No image data provided"); + } + + glBindTexture(GL_TEXTURE_2D, 0); + + return handle; +} + void gpu_texture_destroy(texture_handle) {} void gpu_texture_upload(texture_handle texture, const void* data) {} diff --git a/src/renderer/backends/opengl/backend_opengl.h b/src/renderer/backends/opengl/backend_opengl.h index 91de38d..348eb41 100644 --- a/src/renderer/backends/opengl/backend_opengl.h +++ b/src/renderer/backends/opengl/backend_opengl.h @@ -41,6 +41,7 @@ typedef struct gpu_buffer { u64 size; } gpu_buffer; typedef struct gpu_texture { + u32 id; void *pad } gpu_texture; |