use float instead of double

This commit is contained in:
Nishi 2026-01-11 23:10:13 +09:00
commit e81666e657
Signed by: nishi
GPG key ID: 27EF69B208EB9343
10 changed files with 114 additions and 115 deletions

View file

@ -41,90 +41,26 @@ void GBClientDestroy(GBClient client) {
static double r = 0;
static void scene(void) {
glColor3f(1, 1, 1);
int i;
double u = 1;
glPushMatrix();
glTranslatef(0, fabs(sin(r)), 1);
glBegin(GL_QUADS);
glNormal3f(0, -1, 0);
for(i = 0; i < 4; i++) {
glVertex3f(cos(90 * i / 180.0 * GBMathPi), u, sin(90 * i / 180.0 * GBMathPi));
}
glEnd();
for(i = 0; i < 4; i++) {
double n[3];
double v0[3];
double v1[3];
double v2[3];
v0[0] = 0;
v0[1] = 1 + u;
v0[2] = 0;
v1[0] = cos(90 * (i + 1) / 180.0 * GBMathPi);
v1[1] = u;
v1[2] = sin(90 * (i + 1) / 180.0 * GBMathPi);
v2[0] = cos(90 * i / 180.0 * GBMathPi);
v2[1] = u;
v2[2] = sin(90 * i / 180.0 * GBMathPi);
GBMathNormal3x3(n, v0, v1, v2);
glBegin(GL_TRIANGLES);
glNormal3f(n[0], n[1], n[2]);
glVertex3f(v0[0], v0[1], v0[2]);
glVertex3f(v1[0], v1[1], v1[2]);
glVertex3f(v2[0], v2[1], v2[2]);
glEnd();
}
glPopMatrix();
static void scene(GBClient client) {
}
void GBClientStep(GBClient client) {
GLfloat f[4];
int i;
for(i = 0; i < 4; i++) f[i] = client->light0[i];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GBGLPerspective(client->gl, client->engine->width, client->engine->height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GBGLSetCamera(client->gl);
glLightfv(GL_LIGHT0, GL_POSITION, f);
GBGLClear(client->gl);
GBGLCameraSetup(client->gl);
GBGLSetLight(client->gl);
if(GBGLShadowBeforeMapping(client->gl)) {
scene();
scene(client);
GBGLShadowAfterMapping(client->gl);
}
glDisable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
glNormal3f(0, 1, 0);
glVertex3f(-10, -0, -10);
glVertex3f(-10, -0, 10);
glVertex3f(10, -0, 10);
glVertex3f(10, -0, -10);
glEnd();
glEnable(GL_DEPTH_TEST);
scene();
scene(client);
GBGLShadowEnd(client->gl);
client->engine->param->gl_swapbuffer();
client->light0[0] = cos(r) * 5;
client->light0[2] = sin(r) * 5;
client->camera[0] = cos(r) * 5;
client->camera[2] = sin(r) * 5;
r += 0.1;
}

View file

@ -39,3 +39,24 @@ void GBGLDestroy(GBGL gl) {
free(gl);
}
void GBGLClear(GBGL gl) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void GBGLIgnoreDepth(GBGL gl) {
glDisable(GL_DEPTH_TEST);
}
void GBGLCareDepth(GBGL gl) {
glEnable(GL_DEPTH_TEST);
}
void GBGLSetLight(GBGL gl) {
GLfloat f[4];
int i;
for(i = 0; i < 4; i++) f[i] = gl->engine->client->light0[i];
glLightfv(GL_LIGHT0, GL_POSITION, f);
}

View file

@ -2,14 +2,14 @@
#include <GearBox/Math.h>
void GBGLPerspective(GBGL gl, double width, double height) {
double aspect;
double f;
void GBGLCameraPerspective(GBGL gl, GBNumber width, GBNumber height) {
GBNumber aspect;
GBNumber f;
GLdouble matrix[16];
int i;
double fovy = 60.0;
double znear = 0.01;
double zfar = 100.0;
GBNumber fovy = 60.0;
GBNumber znear = 0.01;
GBNumber zfar = 100.0;
aspect = width / height;
f = GBMathCot(fovy / 180 * GBMathPi / 2);
@ -18,17 +18,17 @@ void GBGLPerspective(GBGL gl, double width, double height) {
matrix[4 * 0 + 0] = f / aspect;
matrix[4 * 1 + 1] = f;
matrix[4 * 2 + 2] = (zfar + znear) / (znear - zfar);
matrix[4 * 3 + 2] = ((double)2 * zfar * znear) / (znear - zfar);
matrix[4 * 3 + 2] = ((GLdouble)2 * zfar * znear) / (znear - zfar);
matrix[4 * 2 + 3] = -1;
glMultMatrixd(matrix);
}
void GBGLSetCamera(GBGL gl) {
GBGLLookAt(gl, gl->engine->client->camera, gl->engine->client->look_at);
void GBGLCameraSet(GBGL gl) {
GBGLCameraLookAt(gl, gl->engine->client->camera, gl->engine->client->look_at);
}
void GBGLLookAt(GBGL gl, GBVector3 camera, GBVector3 look_at) {
void GBGLCameraLookAt(GBGL gl, GBVector3 camera, GBVector3 look_at) {
GLdouble matrix[16];
GBVector3 f;
GBVector3 up;
@ -64,3 +64,13 @@ void GBGLLookAt(GBGL gl, GBVector3 camera, GBVector3 look_at) {
glMultMatrixd(matrix);
glTranslated(-camera[0], -camera[1], -camera[2]);
}
void GBGLCameraSetup(GBGL gl) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GBGLCameraPerspective(gl, gl->engine->width, gl->engine->height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GBGLCameraSet(gl);
}

View file

@ -29,6 +29,7 @@ void GBGLShadowInit(GBGL gl) {
glUseProgram(gl->shadow_shader);
glUniform1i(glGetUniformLocation(gl->shadow_shader, "depth_texture"), 7);
glUniform1i(glGetUniformLocation(gl->shadow_shader, "current_texture"), 0);
glUniform1f(glGetUniformLocation(gl->shadow_shader, "enable_lighting"), 1);
glUseProgram(0);
gl->shadow_use_shader = 1;
@ -44,21 +45,21 @@ int GBGLShadowBeforeMapping(GBGL gl) {
if(!gl->shadow_use_shader) return 0;
glClear(GL_DEPTH_BUFFER_BIT);
GBGLClear(gl);
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
glMatrixMode(GL_PROJECTION);
glGetDoublev(GL_PROJECTION_MATRIX, gl->shadow_old_projection);
glLoadIdentity();
GBGLPerspective(gl, SHADOW_WIDTH, SHADOW_HEIGHT);
GBGLCameraPerspective(gl, SHADOW_WIDTH, SHADOW_HEIGHT);
glGetDoublev(GL_PROJECTION_MATRIX, gl->shadow_projection);
glMatrixMode(GL_MODELVIEW);
glGetDoublev(GL_MODELVIEW_MATRIX, gl->shadow_old_modelview);
glLoadIdentity();
GBGLLookAt(gl, gl->engine->client->light0, zero);
GBGLCameraLookAt(gl, gl->engine->client->light0, zero);
glGetDoublev(GL_MODELVIEW_MATRIX, gl->shadow_modelview);
@ -125,20 +126,39 @@ void GBGLShadowAfterMapping(GBGL gl) {
glMatrixMode(GL_MODELVIEW);
glActiveTexture(GL_TEXTURE7);
glBindTexture(GL_TEXTURE_2D, gl->shadow_texture);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
GBGLClear(gl);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GBGLShadowEnable(gl);
}
glUseProgram(gl->shadow_shader);
void GBGLShadowDisable(GBGL gl) {
glDisable(GL_LIGHTING);
if(gl->shadow_use_shader) {
glUseProgram(0);
glActiveTexture(GL_TEXTURE7);
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE0);
}
}
void GBGLShadowEnable(GBGL gl) {
glEnable(GL_LIGHTING);
if(gl->shadow_use_shader) {
glUseProgram(gl->shadow_shader);
glActiveTexture(GL_TEXTURE7);
glBindTexture(GL_TEXTURE_2D, gl->shadow_texture);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
}
}
void GBGLShadowEnd(GBGL gl) {
if(!gl->shadow_use_shader) return;
glUseProgram(0);
GBGLShadowDisable(gl);
glActiveTexture(GL_TEXTURE7);
glDisable(GL_TEXTURE_2D);

View file

@ -7,15 +7,15 @@ void GBMathCross3(GBVector3 r, GBVector3 v0, GBVector3 v1) {
}
void GBMathNormalize3(GBVector3 vec) {
double l = 0;
int i;
GBNumber l = 0;
int i;
for(i = 0; i < 3; i++) l += vec[i] * vec[i];
l = sqrt(l);
if(l > 0) {
l = (double)1 / l;
l = (GBNumber)1 / l;
} else {
l = 0;
}
@ -69,13 +69,13 @@ void GBMathNormal3x4(GBVector3 r, GBVector3 v0, GBVector3 v1, GBVector3 v2, GBVe
GBMathNormalize3(r);
}
double GBMathCot(double x) {
return (double)1 / tan(x);
GBNumber GBMathCot(GBNumber x) {
return (GBNumber)1 / tan(x);
}
void GBMathInvert4x4(GBMatrix4x4 out, GBMatrix4x4 in) {
double inv[16], det;
int i;
GBNumber inv[16], det;
int i;
inv[0] = in[5] * in[10] * in[15] - in[5] * in[11] * in[14] - in[9] * in[6] * in[15] + in[9] * in[7] * in[14] + in[13] * in[6] * in[11] - in[13] * in[7] * in[10];
inv[4] = -in[4] * in[10] * in[15] + in[4] * in[11] * in[14] + in[8] * in[6] * in[15] - in[8] * in[7] * in[14] - in[12] * in[6] * in[11] + in[12] * in[7] * in[10];