Skip to content

Commit

Permalink
Fix debug & refactor shader. 3018 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSunCat committed Sep 6, 2023
1 parent 2dddd1d commit 8de5350
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CFLAGS = -Os -Winline -Wall -Wextra \
ifeq ($(DEBUG),false)
CFLAGS += #-nostdlib # needed for rand()
else
CFLAGS += -DDEBUG=true -g
CFLAGS += -DDEBUG_GL=true -g
LDFLAGS += -g
endif

Expand Down
27 changes: 23 additions & 4 deletions Minecraft4k.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ glUniform3f_t sym_glUniform3f;
glUniform3i_t sym_glUniform3i;
glRecti_t sym_glRecti;

#ifdef DEBUG_GL
typedef void(*glGetShaderInfoLog_t)(GLuint, GLsizei, GLsizei*, GLchar*);
typedef void(*glGetProgramInfoLog_t)(GLuint, GLsizei, GLsizei*, GLchar*);
typedef void(*glGetProgramiv_t)(GLuint, GLenum, GLint*);
typedef void(*glGetShaderiv_t)(GLuint, GLenum, GLint*);

glGetShaderInfoLog_t sym_glGetShaderInfoLog;
glGetProgramInfoLog_t sym_glGetProgramInfoLog;
glGetProgramiv_t sym_glGetProgramiv;
glGetShaderiv_t sym_glGetShaderiv;
#endif

// OpenGL IDs
GLuint shader;
GLuint worldTex;
Expand Down Expand Up @@ -516,11 +528,11 @@ static void on_realize()

#ifdef DEBUG_GL
GLint isCompiled = 0;
glGetShaderiv(f, GL_COMPILE_STATUS, &isCompiled);
sym_glGetShaderiv(f, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE) {
const int maxLength = 1024;
GLchar* error[maxLength];
glGetShaderInfoLog(f, maxLength, &maxLength, error);
sym_glGetShaderInfoLog(f, maxLength, &maxLength, error);
printf("%s\n", error);

exit(-10);
Expand All @@ -534,11 +546,11 @@ static void on_realize()

#ifdef DEBUG_GL
GLint isLinked = 0;
glGetProgramiv(shader, GL_LINK_STATUS, (int *)&isLinked);
sym_glGetProgramiv(shader, GL_LINK_STATUS, (int *)&isLinked);
if (isLinked == GL_FALSE) {
const int maxLength = 1024;
GLchar* error[maxLength];
glGetProgramInfoLog(shader, maxLength, &maxLength,error);
sym_glGetProgramInfoLog(shader, maxLength, &maxLength,error);
printf("%s\n", error);

exit(-10);
Expand Down Expand Up @@ -592,6 +604,13 @@ void _start() {
sym_glUniform3i = (glUniform3i_t)dlsym(libGL, "glUniform3i");
sym_glRecti = (glRecti_t)dlsym(libGL, "glRecti");

#ifdef DEBUG_GL
sym_glGetShaderInfoLog = (glGetShaderInfoLog_t)dlsym(libGL, "glGetShaderInfoLog");
sym_glGetProgramInfoLog = (glGetProgramInfoLog_t)dlsym(libGL, "glGetProgramInfoLog");
sym_glGetProgramiv = (glGetProgramiv_t)dlsym(libGL, "glGetProgramiv");
sym_glGetShaderiv = (glGetShaderiv_t)dlsym(libGL, "glGetShaderiv");
#endif

// technically not needed
//SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
window = sym_SDL_CreateWindow("", 0, 0,
Expand Down
38 changes: 11 additions & 27 deletions shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,17 @@ void main()

// raymarch outputs

vec3 ijk = ivec3(P);

// the amount to increase i, j and k in each axis (either 1 or -1)
vec3 ijkStep = sign(rayDir);

vec3 vInverted = abs(1 / rayDir);

// the distance to the closest voxel boundary in units of rayTravelDist
vec3 dist = (-fract(P) + max(ijkStep, vec3(0))) / rayDir;

int axis; // X
// the distance to the closest voxel boundary in units of rayDir
vec3 dist = (-fract(P) + step(vec3(0), rayDir)) / rayDir;

float rayTravelDist;

while (rayTravelDist < 20) // TODO replace RENDER_DIST
{
int axis = (dist.y < dist.x) ? 1 + int(dist.y > dist.z) : 2 * int(dist.x > dist.z);
rayTravelDist = dist[axis];
dist[axis] += abs(1 / rayDir)[axis];

// exit check for performance, removed for code size :c
//if(!inWorld(ijk))
// break;
Expand All @@ -72,37 +67,26 @@ void main()
texFetch.y += 2;
}

// bit confusing: we repurpose hitPos to fetch the block that was hit
hitPos[axis] -= step(0., -rayDir[axis]);

// get block from world
// TODO replace WORLD_DIMENSIONS
texFetch.x += texture(W, ijk.yxz / 64).x * 0xFF;
texFetch.x += texture(W, hitPos.yxz / 64).x * 0xFF;

// TODO replace TEXTURE_RES
vec4 textureColor = texture(T, (trunc(texFetch * 16) + .5) / vec2(112, 48));

// highlight hovered block
// multiply by 9 to make sure it's white
textureColor += int(ijk == b && max(abs(fract(texFetch) - .5).x, abs(fract(texFetch) - .5).y) > .44) * 9;
textureColor += int(ivec3(hitPos) == b && max(abs(fract(texFetch) - .5).x, abs(fract(texFetch) - .5).y) > .44) * 9;

if (textureColor.a > 0) { // pixel is not transparent, so output color

// TODO replace RENDER_DIST
Z += mix(textureColor, vec4(0), rayTravelDist / 20);
return;
}

// determine the closest voxel boundary
axis = (dist.y < dist.x) ? 1 + int(dist.y > dist.z) : 2 * int(dist.x > dist.z);

// advance to the closest voxel boundary in the axis direction

// increment the block access position
ijk[axis] += ijkStep[axis];

// update our progress in the ray
rayTravelDist = dist[axis];

// set the new distance to the next voxel Y boundary
dist[axis] += vInverted[axis];
}

//Z = vec4(0);
Expand Down

0 comments on commit 8de5350

Please sign in to comment.