Remove GL Lighting

This commit is contained in:
TheBrokenRail 2024-06-18 17:56:01 -04:00
parent 4156520e88
commit de9c93663b
6 changed files with 5 additions and 219 deletions

View File

@ -81,22 +81,10 @@ 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;
@ -160,16 +148,13 @@ 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);
void glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz);
__attribute__((unavailable)) 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);
// Init

View File

@ -161,10 +161,6 @@ 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");
@ -209,41 +205,6 @@ 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);
}
// 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);
@ -279,9 +240,6 @@ 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);

View File

@ -125,3 +125,6 @@ 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
}

View File

@ -11,18 +11,6 @@ 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
@ -31,18 +19,4 @@ void main(void) {
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;
}
}

View File

@ -10,13 +10,6 @@
{ \
.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,
@ -24,17 +17,10 @@ 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,
.normal = init_array_pointer
.tex_coord = init_array_pointer
},
.matrix_stacks = {
.mode = GL_MODELVIEW
@ -52,35 +38,6 @@ 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
}
}
};
gl_state_t gl_state;
@ -97,13 +54,6 @@ 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) { \
@ -115,10 +65,6 @@ void glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz) {
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: {
@ -130,9 +76,6 @@ 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);
}
@ -157,10 +100,6 @@ 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;
@ -169,15 +108,6 @@ 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;
@ -195,10 +125,6 @@ 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;
@ -207,15 +133,6 @@ 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;
@ -283,39 +200,3 @@ 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];
}
}

View File

@ -33,18 +33,9 @@ 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;
@ -55,7 +46,6 @@ 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;
@ -66,11 +56,6 @@ typedef struct {
GLfloat start;
GLfloat end;
} fog;
struct {
GLboolean enabled;
light_source_t light_sources[2];
color_t ambient;
} lighting;
} gl_state_t;
extern gl_state_t gl_state;
void _init_gles_compatibility_layer_state();