Skip to content

Commit

Permalink
Remove OpenGL extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-graj committed Apr 29, 2023
1 parent 354f1ca commit f34b9e1
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 56 deletions.
19 changes: 9 additions & 10 deletions res/shader/earth.frag
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#version 410 core
#extension GL_ARB_explicit_uniform_location : enable

out vec4 frag_color;

in vec2 uv;
in vec3 pos;

layout (location = 0) uniform sampler2D texture_day;
layout (location = 1) uniform sampler2D texture_night;
layout (location = 2) uniform sampler2D texture_specular;
layout (location = 3) uniform sampler2D texture_clouds;
layout (location = 4) uniform sampler1D texture_gradient;
layout (location = 6) uniform vec3 sun_dir;
layout (location = 7) uniform vec3 look_pos;
layout (location = 8) uniform bool clouds;
layout (location = 9) uniform bool lighting;
uniform sampler2D texture_day;
uniform sampler2D texture_night;
uniform sampler2D texture_specular;
uniform sampler2D texture_clouds;
uniform sampler1D texture_gradient;
uniform vec3 sun_dir;
uniform vec3 look_pos;
uniform bool clouds;
uniform bool lighting;

const float PI = 3.14159;
const float TRANSITION_WIDTH = 0.1;
Expand Down
4 changes: 2 additions & 2 deletions res/shader/earth.vert
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#version 410 core
#extension GL_ARB_explicit_uniform_location : enable

layout (location = 0) in vec3 in_pos;
layout (location = 1) in vec2 in_uv;

out vec3 pos;
out vec2 uv;

layout (location = 5) uniform mat4 transform;
uniform mat4 transform;

void main()
{
Expand Down
1 change: 0 additions & 1 deletion res/shader/satellite.frag
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#version 410 core
#extension GL_ARB_explicit_uniform_location : enable

out vec4 frag_color;

Expand Down
5 changes: 2 additions & 3 deletions res/shader/satellite.vert
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#version 410 core
#extension GL_ARB_explicit_uniform_location : enable

layout (location = 0) in vec3 in_pos;
layout (location = 1) in vec3 in_color;

layout (location = 0) uniform mat4 transform;
layout (location = 1) uniform float scl;
uniform mat4 transform;
uniform float scl;

out vec3 color;

Expand Down
3 changes: 1 addition & 2 deletions res/shader/sky.frag
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#version 410 core
#extension GL_ARB_explicit_uniform_location : enable

out vec4 frag_color;

in vec2 uv;

layout (location = 0) uniform sampler2D texture_sky;
uniform sampler2D texture_sky;

void main()
{
Expand Down
3 changes: 1 addition & 2 deletions res/shader/sky.vert
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#version 410 core
#extension GL_ARB_explicit_uniform_location : enable

layout (location = 0) in vec3 in_pos;
layout (location = 1) in vec2 in_uv;

out vec2 uv;

layout (location = 1) uniform mat4 transform;
uniform mat4 transform;

void main()
{
Expand Down
79 changes: 57 additions & 22 deletions src/entity/earth.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ enum LayoutLoc {
LOCL_ATEXCOORD,
};

enum UniformLoc {
LOCU_TEXTURE_DAY = 0,
LOCU_TEXTURE_NIGHT,
LOCU_TEXTURE_SPECULAR,
LOCU_TEXTURE_CLOUDS,
LOCU_TEXTURE_GRADIENT,
LOCU_TRANSFORM,
LOCU_SUN_DIR,
LOCU_LOOK_POS,
LOCU_CLOUDS,
LOCU_LIGHTING,
enum UniformIdx {
U_TEXTURE_DAY = 0,
U_TEXTURE_NIGHT,
U_TEXTURE_SPECULAR,
U_TEXTURE_CLOUDS,
U_TEXTURE_GRADIENT,
U_TRANSFORM,
U_SUN_DIR,
U_LOOK_POS,
U_CLOUDS,
U_LIGHTING,
N_UNIFORMS,
};

extern const unsigned char res_shader_earth_vert[];
Expand Down Expand Up @@ -73,6 +74,39 @@ static struct Texture texture_specular;
static struct Texture texture_clouds;
static struct Texture texture_gradient;

static struct ShaderAttr uniforms[] = {
[U_TEXTURE_DAY] = {
.name = "texture_day",
},
[U_TEXTURE_NIGHT] = {
.name = "texture_night",
},
[U_TEXTURE_SPECULAR] = {
.name = "texture_specular",
},
[U_TEXTURE_CLOUDS] = {
.name = "texture_clouds",
},
[U_TEXTURE_GRADIENT] = {
.name = "texture_gradient",
},
[U_TRANSFORM] = {
.name = "transform",
},
[U_SUN_DIR] = {
.name = "sun_dir",
},
[U_LOOK_POS] = {
.name = "look_pos",
},
[U_CLOUDS] = {
.name = "clouds",
},
[U_LIGHTING] = {
.name = "lighting",
},
};

void earth_init(void)
{
icosphere_generate(3, &obj_earth.verts, &obj_earth.faces, &obj_earth.uv, &obj_earth.n_verts, &obj_earth.n_faces);
Expand All @@ -85,7 +119,8 @@ void earth_init(void)
shader_init(&shader, (const char *)res_shader_earth_vert, res_shader_earth_vert_len, (const char *)res_shader_earth_frag, res_shader_earth_frag_len, 2, (struct ShaderAttr[]){
{ LOCL_APOS, "in_pos" },
{ LOCL_ATEXCOORD, "in_uv" },
});
},
N_UNIFORMS, uniforms);

texture_init_from_image(&texture_day, res_texture_earth_daymap_jpg, res_texture_earth_daymap_jpg_len, GL_TEXTURE0, GL_TEXTURE_2D);
texture_init_from_image(&texture_night, res_texture_earth_nightmap_jpg, res_texture_earth_nightmap_jpg_len, GL_TEXTURE1, GL_TEXTURE_2D);
Expand Down Expand Up @@ -123,16 +158,16 @@ void earth_render(void)
camera_mvp_generate(&e_camera, &model.model_mat, transform);

shader_bind(&shader);
glUniform3fv(LOCU_SUN_DIR, 1, e_phys.sun_dir);
glUniform1i(LOCU_TEXTURE_DAY, 0);
glUniform1i(LOCU_TEXTURE_NIGHT, 1);
glUniform1i(LOCU_TEXTURE_SPECULAR, 2);
glUniform1i(LOCU_TEXTURE_CLOUDS, 3);
glUniform1i(LOCU_TEXTURE_GRADIENT, 4);
glUniform3fv(LOCU_LOOK_POS, 1, e_camera.pos);
glUniformMatrix4fv(LOCU_TRANSFORM, 1, GL_FALSE, (const GLfloat *)&transform);
glUniform1i(LOCU_CLOUDS, es_clouds);
glUniform1i(LOCU_LIGHTING, es_lighting);
glUniform3fv(uniforms[U_SUN_DIR].index, 1, e_phys.sun_dir);
glUniform1i(uniforms[U_TEXTURE_DAY].index, 0);
glUniform1i(uniforms[U_TEXTURE_NIGHT].index, 1);
glUniform1i(uniforms[U_TEXTURE_SPECULAR].index, 2);
glUniform1i(uniforms[U_TEXTURE_CLOUDS].index, 3);
glUniform1i(uniforms[U_TEXTURE_GRADIENT].index, 4);
glUniform3fv(uniforms[U_LOOK_POS].index, 1, e_camera.pos);
glUniformMatrix4fv(uniforms[U_TRANSFORM].index, 1, GL_FALSE, (const GLfloat *)&transform);
glUniform1i(uniforms[U_CLOUDS].index, es_clouds);
glUniform1i(uniforms[U_LIGHTING].index, es_lighting);

glDrawElements(GL_TRIANGLES, obj_earth.n_faces * 3, GL_UNSIGNED_INT, (GLvoid *)0);
}
23 changes: 17 additions & 6 deletions src/entity/satellite.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ enum LayoutLoc {
LOCL_COLOR,
};

enum UniformLoc {
LOCU_TRANSFORM = 0u,
LOCU_SCALE,
enum UniformIdx {
U_TRANSFORM = 0u,
U_SCALE,
N_UNIFORMS,
};

enum DLMultiLoc {
Expand Down Expand Up @@ -88,6 +89,15 @@ static struct BO vbo_orbit_colors;

static struct Shader shader;

static struct ShaderAttr uniforms[] = {
[U_TRANSFORM] = {
.name = "transform",
},
[U_SCALE] = {
.name = "scl",
}
};

static mat4 transform;

static size_t n_satellites_sync = 0;
Expand Down Expand Up @@ -153,7 +163,8 @@ void satellite_init(void)
shader_init(&shader, (const char *)res_shader_satellite_vert, res_shader_satellite_vert_len, (const char *)res_shader_satellite_frag, res_shader_satellite_frag_len, 2, (struct ShaderAttr[]){
{ LOCL_APOS, "in_pos" },
{ LOCL_COLOR, "in_color" },
});
},
N_UNIFORMS, uniforms);

alloc_orbit_arrays();

Expand Down Expand Up @@ -474,8 +485,8 @@ void satellites_render(void)
if (satellites_renderable) {
shader_bind(&shader);
camera_mvp_generate(&e_camera, &e_phys.teme_to_world, transform);
glUniformMatrix4fv(LOCU_TRANSFORM, 1, GL_FALSE, (const GLfloat *)&transform);
glUniform1f(LOCU_SCALE, es_satellite_scale);
glUniformMatrix4fv(uniforms[U_TRANSFORM].index, 1, GL_FALSE, (const GLfloat *)&transform);
glUniform1f(uniforms[U_SCALE].index, es_satellite_scale);

vao_bind(&vao_satellites);
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
Expand Down
23 changes: 17 additions & 6 deletions src/entity/sky.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ enum LayoutLoc {
LOCL_ATEXCOORD,
};

enum UniformLoc {
LOCU_TEXTURE = 0u,
LOCU_TRANSFORM,
enum UniformIdx {
U_TEXTURE = 0u,
U_TRANSFORM,
N_UNIFORMS,
};

extern const unsigned char res_shader_sky_vert[];
Expand All @@ -51,6 +52,15 @@ static struct Model model;
static struct Shader shader;
static struct Texture texture;

static struct ShaderAttr uniforms[] = {
[U_TEXTURE] = {
.name = "texture_sky",
},
[U_TRANSFORM] = {
.name = "transform",
},
};

void sky_init(void)
{
icosphere_generate(1, &obj.verts, &obj.faces, &obj.uv, &obj.n_verts, &obj.n_faces);
Expand All @@ -64,7 +74,8 @@ void sky_init(void)
shader_init(&shader, (const char *)res_shader_sky_vert, res_shader_sky_vert_len, (const char *)res_shader_sky_frag, res_shader_sky_frag_len, 2, (struct ShaderAttr[]){
{ LOCL_APOS, "in_pos" },
{ LOCL_ATEXCOORD, "in_uv" },
});
},
N_UNIFORMS, uniforms);

texture_init_from_image(&texture, res_texture_stars_jpg, res_texture_stars_jpg_len, GL_TEXTURE0, GL_TEXTURE_2D);

Expand Down Expand Up @@ -92,8 +103,8 @@ void sky_render(void)
camera_mvp_generate(&e_camera, &model.model_mat, transform);

shader_bind(&shader);
glUniform1i(LOCU_TEXTURE, 0);
glUniformMatrix4fv(LOCU_TRANSFORM, 1, GL_FALSE, (const GLfloat *)&transform);
glUniform1i(uniforms[U_TEXTURE].index, 0);
glUniformMatrix4fv(uniforms[U_TRANSFORM].index, 1, GL_FALSE, (const GLfloat *)&transform);

glDrawElements(GL_TRIANGLES, obj.n_faces * 3, GL_UNSIGNED_INT, (GLvoid *)0);
}
8 changes: 7 additions & 1 deletion src/gfx/shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ GLint shader_compile(const char *str, unsigned int len, GLenum type)
return handle;
}

void shader_init(struct Shader *shader, const char *vs, unsigned int vs_len, const char *fs, unsigned int fs_len, size_t n_vertex_attr, struct ShaderAttr vertex_attr[])
void shader_init(struct Shader *shader, const char *vs, unsigned int vs_len, const char *fs, unsigned int fs_len, size_t n_vertex_attr, struct ShaderAttr vertex_attr[], size_t n_uniforms, struct ShaderAttr uniforms[])
{
GLuint vs_handle = shader_compile(vs, vs_len, GL_VERTEX_SHADER);
GLuint fs_handle = shader_compile(fs, fs_len, GL_FRAGMENT_SHADER);
Expand All @@ -52,6 +52,12 @@ void shader_init(struct Shader *shader, const char *vs, unsigned int vs_len, con
glGetProgramiv(shader->handle, GL_LINK_STATUS, &linked);
gfx_error_check(linked, glGetProgramInfoLog, shader->handle, "Failed to link shaders.");

for (i = 0; i < n_uniforms; i++) {
GLint loc = glGetUniformLocation(shader->handle, uniforms[i].name);
error_check(loc != -1, "Failed to locate uniform %s in shader.", uniforms[i].name);
uniforms[i].index = loc;
}

glDetachShader(shader->handle, vs_handle);
glDetachShader(shader->handle, fs_handle);

Expand Down
2 changes: 1 addition & 1 deletion src/gfx/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct Shader {
GLuint handle;
};

void shader_init(struct Shader *shader, const char *vs, unsigned int vs_len, const char *fs, unsigned int fs_len, size_t n_vertex_attr, struct ShaderAttr vertex_attr[]);
void shader_init(struct Shader *shader, const char *vs, unsigned int vs_len, const char *fs, unsigned int fs_len, size_t n_vertex_attr, struct ShaderAttr vertex_attr[], size_t n_uniforms, struct ShaderAttr uniforms[]);
void shader_deinit(struct Shader *shader);
void shader_bind(struct Shader *shader);

Expand Down

0 comments on commit f34b9e1

Please sign in to comment.