Compare commits
No commits in common. "de9c93663ba9327b4252e1aa017728c14e6a7d7d" and "3ee682f6f26dd4344f0701ae346d10250c040d8e" have entirely different histories.
de9c93663b
...
3ee682f6f2
@ -11,10 +11,20 @@ if(GLES_COMPATIBILITY_LAYER_USE_DEFAULT_INCLUDE_PATH)
|
||||
target_include_directories(gles-compatibility-layer PUBLIC include)
|
||||
endif()
|
||||
|
||||
# GL Version
|
||||
option(GLES_COMPATIBILITY_LAYER_USE_ES3 "Use OpenGL ES 3" TRUE)
|
||||
if(GLES_COMPATIBILITY_LAYER_USE_ES3)
|
||||
target_compile_definitions(gles-compatibility-layer PUBLIC GLES_COMPATIBILITY_LAYER_USE_ES3)
|
||||
endif()
|
||||
|
||||
# Shaders
|
||||
include(cmake/util.cmake)
|
||||
embed_resource(gles-compatibility-layer "src/shaders/main.vsh")
|
||||
embed_resource(gles-compatibility-layer "src/shaders/main.fsh")
|
||||
set(SHADER_FOLDER "es2")
|
||||
if(GLES_COMPATIBILITY_LAYER_USE_ES3)
|
||||
set(SHADER_FOLDER "es3")
|
||||
endif()
|
||||
embed_resource(gles-compatibility-layer "src/shaders/${SHADER_FOLDER}/main.vsh")
|
||||
embed_resource(gles-compatibility-layer "src/shaders/${SHADER_FOLDER}/main.fsh")
|
||||
|
||||
# Warnings
|
||||
target_compile_options(gles-compatibility-layer PRIVATE -Wall -Wextra -Werror -Wpointer-arith -Wshadow -Wnull-dereference)
|
@ -81,10 +81,22 @@ extern "C" {
|
||||
#define GL_DEPTH_BUFFER_BIT 0x100
|
||||
#define GL_COLOR_BUFFER_BIT 0x4000
|
||||
#define GL_NO_ERROR 0
|
||||
#define GL_NORMAL_ARRAY 0x8075
|
||||
#define GL_LIGHTING 0xb50
|
||||
#define GL_LIGHT0 0x4000
|
||||
#define GL_LIGHT1 0x4001
|
||||
#define GL_RESCALE_NORMAL 0x803a
|
||||
#define GL_AMBIENT 0x1200
|
||||
#define GL_DIFFUSE 0x1201
|
||||
#define GL_SPECULAR 0x1202
|
||||
#define GL_POSITION 0x1203
|
||||
#define GL_LIGHT_MODEL_AMBIENT 0xb53
|
||||
#define GL_BYTE 0x1400
|
||||
#define GL_ACCUM 0x100
|
||||
#define GL_ALPHA 0x1906
|
||||
#define GL_NONE 0
|
||||
#define GL_LINE_SMOOTH 0xb20
|
||||
#define GL_SMOOTH_LINE_WIDTH_RANGE 0xb22
|
||||
#define GL_ALIASED_LINE_WIDTH_RANGE 0x846e
|
||||
|
||||
typedef float GLfloat;
|
||||
@ -148,15 +160,32 @@ void glDisable(GLenum cap);
|
||||
void glCullFace(GLenum mode);
|
||||
void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
|
||||
void glViewport(GLint x, GLint y, GLsizei width, GLsizei height);
|
||||
__attribute__((unavailable)) void glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz);
|
||||
void glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz);
|
||||
GLboolean glIsEnabled(GLenum cap);
|
||||
void glGetIntegerv(GLenum pname, GLint *data);
|
||||
void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *data);
|
||||
void glGenBuffers(GLsizei n, GLuint *buffers);
|
||||
GLenum glGetError();
|
||||
void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const void *data);
|
||||
void glNormalPointer(GLenum type, GLsizei stride, const void *pointer);
|
||||
void glLightfv(GLenum light, GLenum pname, const GLfloat *params);
|
||||
void glLightModelfv(GLenum pname, const GLfloat *params);
|
||||
void glPixelStorei(GLenum pname, GLint param);
|
||||
|
||||
// Not Part Of OpenGL ES 1.1
|
||||
#ifdef GLES_COMPATIBILITY_LAYER_USE_ES3
|
||||
#define GL_SAMPLES_PASSED_ARB 0x8c2f // GL_ANY_SAMPLES_PASSED
|
||||
#define GL_QUERY_RESULT_AVAILABLE_ARB 0x8867 // GL_QUERY_RESULT_AVAILABLE
|
||||
#define GL_QUERY_RESULT_ARB 0x8866 // GL_QUERY_RESULT
|
||||
void glGenQueriesARB(GLsizei n, GLuint *ids);
|
||||
void glDeleteQueriesARB(GLsizei n, const GLuint *ids);
|
||||
void glBeginQueryARB(GLenum target, GLuint id);
|
||||
void glEndQueryARB(GLenum target);
|
||||
void glGetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params);
|
||||
#endif
|
||||
void extra_enable_highlight_mode(float red, float green, float blue, float alpha);
|
||||
void extra_disable_highlight_mode();
|
||||
|
||||
// Init
|
||||
typedef void *(*getProcAddress_t)(const char *);
|
||||
void init_gles_compatibility_layer(getProcAddress_t);
|
||||
|
61
src/draw.c
61
src/draw.c
@ -115,6 +115,10 @@ static GLuint get_shader() {
|
||||
}
|
||||
|
||||
// Init
|
||||
#ifdef GLES_COMPATIBILITY_LAYER_USE_ES3
|
||||
GL_FUNC(glGenVertexArrays, void, (GLsizei n, GLuint *arrays));
|
||||
GL_FUNC(glBindVertexArray, void, (GLuint array));
|
||||
#endif
|
||||
void init_gles_compatibility_layer(getProcAddress_t new_getProcAddress) {
|
||||
// Setup Passthrough
|
||||
getProcAddress = new_getProcAddress;
|
||||
@ -125,6 +129,13 @@ void init_gles_compatibility_layer(getProcAddress_t new_getProcAddress) {
|
||||
// Reset Static Variables
|
||||
reset_variables();
|
||||
|
||||
// Setup VAO
|
||||
#ifdef GLES_COMPATIBILITY_LAYER_USE_ES3
|
||||
GLuint vao;
|
||||
real_glGenVertexArrays()(1, &vao);
|
||||
real_glBindVertexArray()(vao);
|
||||
#endif
|
||||
|
||||
// Load Shader
|
||||
GLuint program = get_shader();
|
||||
real_glUseProgram()(program);
|
||||
@ -161,6 +172,10 @@ void glDrawArrays(GLenum mode, GLint first, GLsizei count) {
|
||||
if (use_color_pointer && (gl_state.array_pointers.color.size != 4 || gl_state.array_pointers.color.type != GL_UNSIGNED_BYTE)) {
|
||||
ERR("Unsupported Color Conifguration");
|
||||
}
|
||||
int use_normal_pointer = gl_state.lighting.enabled && gl_state.array_pointers.normal.enabled;
|
||||
if (use_normal_pointer && gl_state.array_pointers.normal.type != GL_BYTE) {
|
||||
ERR("Unsupported Normal Conifguration");
|
||||
}
|
||||
int use_texture = gl_state.texture_2d && gl_state.array_pointers.tex_coord.enabled;
|
||||
if (use_texture && (gl_state.array_pointers.tex_coord.size != 2 || gl_state.array_pointers.tex_coord.type != GL_FLOAT)) {
|
||||
ERR("Unsupported Texture Conifguration");
|
||||
@ -205,6 +220,49 @@ void glDrawArrays(GLenum mode, GLint first, GLsizei count) {
|
||||
real_glVertexAttrib4f()(a_color_handle, gl_state.color.red, gl_state.color.green, gl_state.color.blue, gl_state.color.alpha);
|
||||
}
|
||||
|
||||
// Highlight Mode
|
||||
lazy_uniform(u_highlight_mode);
|
||||
real_glUniform1i()(u_highlight_mode_handle, gl_state.highlight_mode.enabled);
|
||||
if (gl_state.highlight_mode.enabled) {
|
||||
lazy_uniform(u_highlight_mode_color);
|
||||
real_glUniform4f()(u_highlight_mode_color_handle, gl_state.highlight_mode.color.red, gl_state.highlight_mode.color.green, gl_state.highlight_mode.color.blue, gl_state.highlight_mode.color.alpha);
|
||||
}
|
||||
|
||||
// Lighting
|
||||
lazy_uniform(u_lighting);
|
||||
real_glUniform1i()(u_lighting_handle, gl_state.lighting.enabled);
|
||||
lazy_attrib(a_normal);
|
||||
if (gl_state.lighting.enabled) {
|
||||
lazy_uniform(u_lighting_ambient);
|
||||
real_glUniform4f()(u_lighting_ambient_handle, gl_state.lighting.ambient.red, gl_state.lighting.ambient.green, gl_state.lighting.ambient.blue, gl_state.lighting.ambient.alpha);
|
||||
light_source_t *light_source;
|
||||
#define light_source_uniforms(number) \
|
||||
light_source = &gl_state.lighting.light_sources[number]; \
|
||||
lazy_uniform(u_lighting_light_source_##number); \
|
||||
real_glUniform1i()(u_lighting_light_source_##number##_handle, light_source->enabled); \
|
||||
lazy_uniform(u_lighting_light_source_##number##_position); \
|
||||
real_glUniform3f()(u_lighting_light_source_##number##_position_handle, light_source->position.x, light_source->position.y, light_source->position.z); \
|
||||
lazy_uniform(u_lighting_light_source_##number##_diffuse); \
|
||||
real_glUniform4f()(u_lighting_light_source_##number##_diffuse_handle, light_source->diffuse.red, light_source->diffuse.green, light_source->diffuse.blue, light_source->diffuse.alpha);
|
||||
light_source_uniforms(0);
|
||||
light_source_uniforms(1);
|
||||
|
||||
// Normal
|
||||
if (use_normal_pointer) {
|
||||
real_glVertexAttribPointer()(a_normal_handle, 3, gl_state.array_pointers.normal.type, 1, gl_state.array_pointers.normal.stride, gl_state.array_pointers.normal.pointer);
|
||||
real_glEnableVertexAttribArray()(a_normal_handle);
|
||||
} else {
|
||||
real_glVertexAttrib3f()(a_normal_handle, gl_state.normal.x, gl_state.normal.y, gl_state.normal.z);
|
||||
}
|
||||
// Normal Rescale Factor (See Pages 46-47 Of https://registry.khronos.org/OpenGL/specs/gl/glspec15.pdf)
|
||||
float normal_rescale_factor = 1;
|
||||
if (gl_state.rescale_normal) {
|
||||
normal_rescale_factor = 1.0f / sqrtf(powf(model_view->data[0][2], 2) + powf(model_view->data[1][2], 2) + powf(model_view->data[2][2], 2));
|
||||
}
|
||||
lazy_uniform(u_normal_rescale_factor);
|
||||
real_glUniform1f()(u_normal_rescale_factor_handle, normal_rescale_factor);
|
||||
}
|
||||
|
||||
// Fog
|
||||
lazy_uniform(u_fog);
|
||||
real_glUniform1i()(u_fog_handle, gl_state.fog.enabled);
|
||||
@ -240,6 +298,9 @@ void glDrawArrays(GLenum mode, GLint first, GLsizei count) {
|
||||
if (use_color_pointer) {
|
||||
real_glDisableVertexAttribArray()(a_color_handle);
|
||||
}
|
||||
if (use_normal_pointer) {
|
||||
real_glDisableVertexAttribArray()(a_normal_handle);
|
||||
}
|
||||
real_glDisableVertexAttribArray()(a_vertex_coords_handle);
|
||||
if (use_texture) {
|
||||
real_glDisableVertexAttribArray()(a_texture_coords_handle);
|
||||
|
@ -121,10 +121,29 @@ GL_FUNC(glBufferSubData, void, (GLenum target, GLintptr offset, GLsizeiptr size,
|
||||
void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const void *data) {
|
||||
real_glBufferSubData()(target, offset, size, data);
|
||||
}
|
||||
#ifdef GLES_COMPATIBILITY_LAYER_USE_ES3
|
||||
GL_FUNC(glGenQueries, void, (GLsizei n, GLuint *ids));
|
||||
void glGenQueriesARB(GLsizei n, GLuint *ids) {
|
||||
real_glGenQueries()(n, ids);
|
||||
}
|
||||
GL_FUNC(glDeleteQueries, void, (GLsizei n, const GLuint *ids));
|
||||
void glDeleteQueriesARB(GLsizei n, const GLuint *ids) {
|
||||
real_glDeleteQueries()(n, ids);
|
||||
}
|
||||
GL_FUNC(glBeginQuery, void, (GLenum target, GLuint id));
|
||||
void glBeginQueryARB(GLenum target, GLuint id) {
|
||||
real_glBeginQuery()(target, id);
|
||||
}
|
||||
GL_FUNC(glEndQuery, void, (GLenum target));
|
||||
void glEndQueryARB(GLenum target) {
|
||||
real_glEndQuery()(target);
|
||||
}
|
||||
GL_FUNC(glGetQueryObjectuiv, void, (GLuint id, GLenum pname, GLuint *params));
|
||||
void glGetQueryObjectuivARB(GLuint id, GLenum pname, GLuint *params) {
|
||||
real_glGetQueryObjectuiv()(id, pname, params);
|
||||
}
|
||||
#endif
|
||||
GL_FUNC(glPixelStorei, void, (GLenum pname, GLint param));
|
||||
void glPixelStorei(GLenum pname, GLint param) {
|
||||
real_glPixelStorei()(pname, param);
|
||||
}
|
||||
void glNormal3f(__attribute__((unused)) GLfloat nx, __attribute__((unused)) GLfloat ny, __attribute__((unused)) GLfloat nz) {
|
||||
// Ignore
|
||||
}
|
||||
|
@ -6,6 +6,9 @@ uniform sampler2D u_texture_unit;
|
||||
// Color
|
||||
varying vec4 v_color;
|
||||
varying vec4 v_texture_pos;
|
||||
// Highlight Mode
|
||||
uniform bool u_highlight_mode;
|
||||
uniform vec4 u_highlight_mode_color;
|
||||
// Alpha Test
|
||||
uniform bool u_alpha_test;
|
||||
// Fog
|
||||
@ -21,7 +24,13 @@ void main(void) {
|
||||
// Texture
|
||||
if (u_has_texture) {
|
||||
vec4 texture_color = texture2D(u_texture_unit, v_texture_pos.xy);
|
||||
gl_FragColor *= texture_color;
|
||||
if (u_highlight_mode) {
|
||||
texture_color.rgb = u_highlight_mode_color.rgb;
|
||||
texture_color.a *= u_highlight_mode_color.a;
|
||||
gl_FragColor = texture_color;
|
||||
} else {
|
||||
gl_FragColor *= texture_color;
|
||||
}
|
||||
}
|
||||
// Fog
|
||||
if (u_fog) {
|
48
src/shaders/es2/main.vsh
Normal file
48
src/shaders/es2/main.vsh
Normal file
@ -0,0 +1,48 @@
|
||||
#version 100
|
||||
precision highp float;
|
||||
// Matrices
|
||||
uniform mat4 u_projection;
|
||||
uniform mat4 u_model_view;
|
||||
uniform mat4 u_texture;
|
||||
// Texture
|
||||
attribute vec3 a_vertex_coords;
|
||||
attribute vec2 a_texture_coords;
|
||||
varying vec4 v_texture_pos;
|
||||
// Color
|
||||
attribute vec4 a_color;
|
||||
varying vec4 v_color;
|
||||
// Normal
|
||||
attribute vec3 a_normal;
|
||||
uniform float u_normal_rescale_factor;
|
||||
// Lighting
|
||||
uniform bool u_lighting;
|
||||
uniform vec4 u_lighting_ambient;
|
||||
uniform bool u_lighting_light_source_0;
|
||||
uniform vec3 u_lighting_light_source_0_position;
|
||||
uniform vec4 u_lighting_light_source_0_diffuse;
|
||||
uniform bool u_lighting_light_source_1;
|
||||
uniform vec3 u_lighting_light_source_1_position;
|
||||
uniform vec4 u_lighting_light_source_1_diffuse;
|
||||
// Fog
|
||||
varying vec4 v_fog_eye_position;
|
||||
// Main
|
||||
void main(void) {
|
||||
v_texture_pos = u_texture * vec4(a_texture_coords.xy, 0.0, 1.0);
|
||||
gl_Position = u_projection * u_model_view * vec4(a_vertex_coords.xyz, 1.0);
|
||||
v_color = a_color;
|
||||
v_fog_eye_position = u_model_view * vec4(a_vertex_coords.xyz, 1.0);
|
||||
|
||||
// Lighting
|
||||
if (u_lighting) {
|
||||
vec4 transformed_normal = u_model_view * vec4(a_normal, 0.0) * u_normal_rescale_factor;
|
||||
vec4 total_light = u_lighting_ambient;
|
||||
if (u_lighting_light_source_0) {
|
||||
total_light += max(0.0, dot(transformed_normal.xyz, u_lighting_light_source_0_position)) * u_lighting_light_source_0_diffuse;
|
||||
}
|
||||
if (u_lighting_light_source_1) {
|
||||
total_light += max(0.0, dot(transformed_normal.xyz, u_lighting_light_source_1_position)) * u_lighting_light_source_1_diffuse;
|
||||
}
|
||||
total_light = clamp(total_light, 0.0, 1.0);
|
||||
v_color *= total_light;
|
||||
}
|
||||
}
|
52
src/shaders/es3/main.fsh
Normal file
52
src/shaders/es3/main.fsh
Normal file
@ -0,0 +1,52 @@
|
||||
#version 300 es
|
||||
precision highp float;
|
||||
// Result
|
||||
out vec4 frag_color;
|
||||
// Texture
|
||||
uniform bool u_has_texture;
|
||||
uniform sampler2D u_texture_unit;
|
||||
// Color
|
||||
in vec4 v_color;
|
||||
in vec4 v_texture_pos;
|
||||
// Highlight Mode
|
||||
uniform bool u_highlight_mode;
|
||||
uniform vec4 u_highlight_mode_color;
|
||||
// Alpha Test
|
||||
uniform bool u_alpha_test;
|
||||
// Fog
|
||||
uniform bool u_fog;
|
||||
uniform vec4 u_fog_color;
|
||||
uniform bool u_fog_is_linear;
|
||||
uniform float u_fog_start;
|
||||
uniform float u_fog_end;
|
||||
in vec4 v_fog_eye_position;
|
||||
// Main
|
||||
void main(void) {
|
||||
frag_color = v_color;
|
||||
// Texture
|
||||
if (u_has_texture) {
|
||||
vec4 texture_color = texture(u_texture_unit, v_texture_pos.xy);
|
||||
if (u_highlight_mode) {
|
||||
texture_color.rgb = u_highlight_mode_color.rgb;
|
||||
texture_color.a *= u_highlight_mode_color.a;
|
||||
frag_color = texture_color;
|
||||
} else {
|
||||
frag_color *= texture_color;
|
||||
}
|
||||
}
|
||||
// Fog
|
||||
if (u_fog) {
|
||||
float fog_factor;
|
||||
if (u_fog_is_linear) {
|
||||
fog_factor = (u_fog_end - length(v_fog_eye_position)) / (u_fog_end - u_fog_start);
|
||||
} else {
|
||||
fog_factor = exp(-u_fog_start * length(v_fog_eye_position));
|
||||
}
|
||||
fog_factor = clamp(fog_factor, 0.0, 1.0);
|
||||
frag_color.rgb = mix(frag_color, u_fog_color, 1.0 - fog_factor).rgb;
|
||||
}
|
||||
// Alpha Test
|
||||
if (u_alpha_test && frag_color.a <= 0.1) {
|
||||
discard;
|
||||
}
|
||||
}
|
48
src/shaders/es3/main.vsh
Normal file
48
src/shaders/es3/main.vsh
Normal file
@ -0,0 +1,48 @@
|
||||
#version 300 es
|
||||
precision highp float;
|
||||
// Matrices
|
||||
uniform mat4 u_projection;
|
||||
uniform mat4 u_model_view;
|
||||
uniform mat4 u_texture;
|
||||
// Texture
|
||||
in vec3 a_vertex_coords;
|
||||
in vec2 a_texture_coords;
|
||||
out vec4 v_texture_pos;
|
||||
// Color
|
||||
in vec4 a_color;
|
||||
out vec4 v_color;
|
||||
// Normal
|
||||
in vec3 a_normal;
|
||||
uniform float u_normal_rescale_factor;
|
||||
// Lighting
|
||||
uniform bool u_lighting;
|
||||
uniform vec4 u_lighting_ambient;
|
||||
uniform bool u_lighting_light_source_0;
|
||||
uniform vec3 u_lighting_light_source_0_position;
|
||||
uniform vec4 u_lighting_light_source_0_diffuse;
|
||||
uniform bool u_lighting_light_source_1;
|
||||
uniform vec3 u_lighting_light_source_1_position;
|
||||
uniform vec4 u_lighting_light_source_1_diffuse;
|
||||
// Fog
|
||||
out vec4 v_fog_eye_position;
|
||||
// Main
|
||||
void main(void) {
|
||||
v_texture_pos = u_texture * vec4(a_texture_coords.xy, 0.0, 1.0);
|
||||
gl_Position = u_projection * u_model_view * vec4(a_vertex_coords.xyz, 1.0);
|
||||
v_color = a_color;
|
||||
v_fog_eye_position = u_model_view * vec4(a_vertex_coords.xyz, 1.0);
|
||||
|
||||
// Lighting
|
||||
if (u_lighting) {
|
||||
vec4 transformed_normal = u_model_view * vec4(a_normal, 0.0) * u_normal_rescale_factor;
|
||||
vec4 total_light = u_lighting_ambient;
|
||||
if (u_lighting_light_source_0) {
|
||||
total_light += max(0.0, dot(transformed_normal.xyz, u_lighting_light_source_0_position)) * u_lighting_light_source_0_diffuse;
|
||||
}
|
||||
if (u_lighting_light_source_1) {
|
||||
total_light += max(0.0, dot(transformed_normal.xyz, u_lighting_light_source_1_position)) * u_lighting_light_source_1_diffuse;
|
||||
}
|
||||
total_light = clamp(total_light, 0.0, 1.0);
|
||||
v_color *= total_light;
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
#version 100
|
||||
precision highp float;
|
||||
// Matrices
|
||||
uniform mat4 u_projection;
|
||||
uniform mat4 u_model_view;
|
||||
uniform mat4 u_texture;
|
||||
// Texture
|
||||
attribute vec3 a_vertex_coords;
|
||||
attribute vec2 a_texture_coords;
|
||||
varying vec4 v_texture_pos;
|
||||
// Color
|
||||
attribute vec4 a_color;
|
||||
varying vec4 v_color;
|
||||
// Fog
|
||||
varying vec4 v_fog_eye_position;
|
||||
// Main
|
||||
void main(void) {
|
||||
v_texture_pos = u_texture * vec4(a_texture_coords.xy, 0.0, 1.0);
|
||||
gl_Position = u_projection * u_model_view * vec4(a_vertex_coords.xyz, 1.0);
|
||||
v_color = a_color;
|
||||
v_fog_eye_position = u_model_view * vec4(a_vertex_coords.xyz, 1.0);
|
||||
}
|
136
src/state.c
136
src/state.c
@ -10,6 +10,13 @@
|
||||
{ \
|
||||
.enabled = 0 \
|
||||
}
|
||||
#define init_light_source \
|
||||
.enabled = 0, \
|
||||
.position = { \
|
||||
.x = 0, \
|
||||
.y = 0, \
|
||||
.z = 1 \
|
||||
}
|
||||
static gl_state_t init_gl_state = {
|
||||
.color = {
|
||||
.red = 1,
|
||||
@ -17,10 +24,17 @@ static gl_state_t init_gl_state = {
|
||||
.blue = 1,
|
||||
.alpha = 1
|
||||
},
|
||||
.normal = {
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.z = 1
|
||||
},
|
||||
.rescale_normal = 0,
|
||||
.array_pointers = {
|
||||
.vertex = init_array_pointer,
|
||||
.color = init_array_pointer,
|
||||
.tex_coord = init_array_pointer
|
||||
.tex_coord = init_array_pointer,
|
||||
.normal = init_array_pointer
|
||||
},
|
||||
.matrix_stacks = {
|
||||
.mode = GL_MODELVIEW
|
||||
@ -38,6 +52,38 @@ static gl_state_t init_gl_state = {
|
||||
},
|
||||
.start = 0,
|
||||
.end = 1
|
||||
},
|
||||
.lighting = {
|
||||
.enabled = 0,
|
||||
.light_sources = {
|
||||
{
|
||||
init_light_source,
|
||||
.diffuse = {
|
||||
.red = 1,
|
||||
.green = 1,
|
||||
.blue = 1,
|
||||
.alpha = 1
|
||||
}
|
||||
},
|
||||
{
|
||||
init_light_source,
|
||||
.diffuse = {
|
||||
.red = 0,
|
||||
.green = 0,
|
||||
.blue = 0,
|
||||
.alpha = 0
|
||||
}
|
||||
}
|
||||
},
|
||||
.ambient = {
|
||||
.red = 0.2f,
|
||||
.green = 0.2f,
|
||||
.blue = 0.2f,
|
||||
.alpha = 0.2f
|
||||
}
|
||||
},
|
||||
.highlight_mode = {
|
||||
.enabled = 0
|
||||
}
|
||||
};
|
||||
gl_state_t gl_state;
|
||||
@ -54,6 +100,13 @@ void glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {
|
||||
gl_state.color.alpha = alpha;
|
||||
}
|
||||
|
||||
// Change Normal
|
||||
void glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz) {
|
||||
gl_state.normal.x = nx;
|
||||
gl_state.normal.y = ny;
|
||||
gl_state.normal.z = nz;
|
||||
}
|
||||
|
||||
// Array Pointer Storage
|
||||
#define ARRAY_POINTER_FUNC(func, name) \
|
||||
void func(GLint size, GLenum type, GLsizei stride, const void *pointer) { \
|
||||
@ -65,6 +118,10 @@ void glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {
|
||||
ARRAY_POINTER_FUNC(glVertexPointer, vertex)
|
||||
ARRAY_POINTER_FUNC(glColorPointer, color)
|
||||
ARRAY_POINTER_FUNC(glTexCoordPointer, tex_coord)
|
||||
static ARRAY_POINTER_FUNC(fake_glNormalPointer, normal)
|
||||
void glNormalPointer(GLenum type, GLsizei stride, const void *pointer) {
|
||||
fake_glNormalPointer(0, type, stride, pointer);
|
||||
}
|
||||
static array_pointer_t *get_array_pointer(GLenum array) {
|
||||
switch (array) {
|
||||
case GL_VERTEX_ARRAY: {
|
||||
@ -76,6 +133,9 @@ static array_pointer_t *get_array_pointer(GLenum array) {
|
||||
case GL_TEXTURE_COORD_ARRAY: {
|
||||
return &gl_state.array_pointers.tex_coord;
|
||||
}
|
||||
case GL_NORMAL_ARRAY: {
|
||||
return &gl_state.array_pointers.normal;
|
||||
}
|
||||
default: {
|
||||
ERR("Unsupported Array Pointer: %i", array);
|
||||
}
|
||||
@ -100,6 +160,10 @@ void glEnable(GLenum cap) {
|
||||
gl_state.texture_2d = 1;
|
||||
break;
|
||||
}
|
||||
case GL_RESCALE_NORMAL: {
|
||||
gl_state.rescale_normal = 1;
|
||||
break;
|
||||
}
|
||||
case GL_COLOR_MATERIAL: {
|
||||
// Ignore
|
||||
break;
|
||||
@ -108,6 +172,15 @@ void glEnable(GLenum cap) {
|
||||
gl_state.fog.enabled = 1;
|
||||
break;
|
||||
}
|
||||
case GL_LIGHTING: {
|
||||
gl_state.lighting.enabled = 1;
|
||||
break;
|
||||
}
|
||||
case GL_LIGHT0:
|
||||
case GL_LIGHT1: {
|
||||
gl_state.lighting.light_sources[cap - GL_LIGHT0].enabled = 1;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
real_glEnable()(cap);
|
||||
break;
|
||||
@ -125,6 +198,10 @@ void glDisable(GLenum cap) {
|
||||
gl_state.texture_2d = 0;
|
||||
break;
|
||||
}
|
||||
case GL_RESCALE_NORMAL: {
|
||||
gl_state.rescale_normal = 0;
|
||||
break;
|
||||
}
|
||||
case GL_COLOR_MATERIAL: {
|
||||
// Ignore
|
||||
break;
|
||||
@ -133,6 +210,15 @@ void glDisable(GLenum cap) {
|
||||
gl_state.fog.enabled = 0;
|
||||
break;
|
||||
}
|
||||
case GL_LIGHTING: {
|
||||
gl_state.lighting.enabled = 0;
|
||||
break;
|
||||
}
|
||||
case GL_LIGHT0:
|
||||
case GL_LIGHT1: {
|
||||
gl_state.lighting.light_sources[cap - GL_LIGHT0].enabled = 0;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
real_glDisable()(cap);
|
||||
break;
|
||||
@ -200,3 +286,51 @@ void glGetFloatv(GLenum pname, GLfloat *params) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Configure Light Sources
|
||||
void glLightfv(GLenum light, GLenum pname, const GLfloat *params) {
|
||||
light_source_t *light_source = &gl_state.lighting.light_sources[light - GL_LIGHT0];
|
||||
if (pname == GL_DIFFUSE) {
|
||||
light_source->diffuse.red = params[0];
|
||||
light_source->diffuse.green = params[1];
|
||||
light_source->diffuse.blue = params[2];
|
||||
light_source->diffuse.alpha = params[3];
|
||||
} else if (pname == GL_POSITION) {
|
||||
// Transform Position By Modelview Matrix
|
||||
matrix_t model_view = gl_state.matrix_stacks.model_view.stack[gl_state.matrix_stacks.model_view.i];
|
||||
GLfloat out[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
GLfloat result = 0;
|
||||
for (int j = 0; j < 4; j++) {
|
||||
result += model_view.data[j][i] * params[j];
|
||||
}
|
||||
out[i] = result;
|
||||
}
|
||||
// Store
|
||||
light_source->position.x = out[0];
|
||||
light_source->position.y = out[1];
|
||||
light_source->position.z = out[2];
|
||||
}
|
||||
}
|
||||
|
||||
// Global Ambient Lighting
|
||||
void glLightModelfv(GLenum pname, const GLfloat *params) {
|
||||
if (pname == GL_LIGHT_MODEL_AMBIENT) {
|
||||
gl_state.lighting.ambient.red = params[0];
|
||||
gl_state.lighting.ambient.green = params[1];
|
||||
gl_state.lighting.ambient.blue = params[2];
|
||||
gl_state.lighting.ambient.alpha = params[3];
|
||||
}
|
||||
}
|
||||
|
||||
// Highlight Mode
|
||||
void extra_enable_highlight_mode(float red, float green, float blue, float alpha) {
|
||||
gl_state.highlight_mode.enabled = 1;
|
||||
gl_state.highlight_mode.color.red = red;
|
||||
gl_state.highlight_mode.color.green = green;
|
||||
gl_state.highlight_mode.color.blue = blue;
|
||||
gl_state.highlight_mode.color.alpha = alpha;
|
||||
}
|
||||
void extra_disable_highlight_mode() {
|
||||
gl_state.highlight_mode.enabled = 0;
|
||||
}
|
||||
|
19
src/state.h
19
src/state.h
@ -33,9 +33,18 @@ typedef struct {
|
||||
const void *pointer;
|
||||
} array_pointer_t;
|
||||
|
||||
// Light
|
||||
typedef struct {
|
||||
GLboolean enabled;
|
||||
position_t position;
|
||||
color_t diffuse;
|
||||
} light_source_t;
|
||||
|
||||
// GL State
|
||||
typedef struct {
|
||||
color_t color;
|
||||
position_t normal;
|
||||
GLboolean rescale_normal;
|
||||
struct {
|
||||
GLenum mode;
|
||||
matrix_stack_t model_view;
|
||||
@ -46,6 +55,7 @@ typedef struct {
|
||||
array_pointer_t vertex;
|
||||
array_pointer_t color;
|
||||
array_pointer_t tex_coord;
|
||||
array_pointer_t normal;
|
||||
} array_pointers;
|
||||
GLboolean alpha_test;
|
||||
GLboolean texture_2d;
|
||||
@ -56,6 +66,15 @@ typedef struct {
|
||||
GLfloat start;
|
||||
GLfloat end;
|
||||
} fog;
|
||||
struct {
|
||||
GLboolean enabled;
|
||||
light_source_t light_sources[2];
|
||||
color_t ambient;
|
||||
} lighting;
|
||||
struct {
|
||||
GLboolean enabled;
|
||||
color_t color;
|
||||
} highlight_mode;
|
||||
} gl_state_t;
|
||||
extern gl_state_t gl_state;
|
||||
void _init_gles_compatibility_layer_state();
|
||||
|
@ -50,7 +50,11 @@ static void load_header(std::string filename) {
|
||||
}
|
||||
static void load_headers() {
|
||||
header_lines.clear();
|
||||
#ifdef GLES_COMPATIBILITY_LAYER_USE_ES3
|
||||
load_header("/usr/include/GLES3/gl3.h");
|
||||
#else
|
||||
load_header("/usr/include/GLES2/gl2.h");
|
||||
#endif
|
||||
}
|
||||
|
||||
// Run Test
|
||||
|
Reference in New Issue
Block a user