3D: névoa e transparência nos 3 renderizadores
- Tb3dFrame ganha fog_color/fog_start/fog_end e opaque_count; versão 8. - Os draws saem com os opacos na frente e os transparentes (color[3] < 1) depois, do mais longe ao mais perto. - Software mistura por alfa e não grava o z; OpenGL e Vulkan usam um pipeline de blend separado (o alfa mistura a cor, sem gravar o z). - Blocos novos: "mude a opacidade de %1 para %2" (3D.Opacity) e "névoa de %1 até %2" (3D.Fog). - d3_test compara névoa e transparência nos três renderizadores. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cea7cb5ba4
commit
7b3cf8ce9b
11 files changed
+829
-647
No files matched your search
@@ -38,7 +38,7 @@ static struct {
|
||||
static EGLDisplay s_dpy = EGL_NO_DISPLAY;
|
||||
static EGLContext s_ctx = EGL_NO_CONTEXT;
|
||||
static GLuint s_prog, s_fbo, s_color, s_depth;
|
||||
static GLint u_mvp, u_nrm, u_color, u_light, u_ambient;
|
||||
static GLint u_mvp, u_nrm, u_color, u_light, u_ambient, u_fog, u_fog_end;
|
||||
static int s_w, s_h;
|
||||
static uint8_t* s_rows; /* leitura (de baixo para cima) */
|
||||
static size_t s_rows_cap;
|
||||
@@ -53,7 +53,7 @@ static GLuint s_white;
|
||||
static GLuint s_flat; /* normal map neutro (0,0,1) */
|
||||
|
||||
/* Programas dos shaders feitos com blocos: cache pelo id (id % 64) */
|
||||
typedef struct { GLuint prog; int id; GLint mvp, model, nrm, color, light, ambient, time, albedo, normalmap; } GpuShader;
|
||||
typedef struct { GLuint prog; int id; GLint mvp, model, nrm, color, light, ambient, time, albedo, normalmap, fog, fog_end; } GpuShader;
|
||||
static GpuShader s_shd[64];
|
||||
|
||||
static void fail(const char* fmt, const char* a) { snprintf(G.err, sizeof(G.err), fmt, a); }
|
||||
@@ -84,6 +84,13 @@ static const char* FS =
|
||||
"uniform float u_ambient;\n"
|
||||
"uniform sampler2D u_albedo;\n"
|
||||
"uniform sampler2D u_normalmap;\n"
|
||||
"uniform vec4 u_fog;\n" /* rgb = cor da nevoa, a = inicio */
|
||||
"uniform float u_fog_end;\n"
|
||||
"vec3 tb_fog(vec3 c) {\n"
|
||||
" if (u_fog_end <= u_fog.w) return c;\n"
|
||||
" float t = clamp((1.0 / gl_FragCoord.w - u_fog.w) / (u_fog_end - u_fog.w), 0.0, 1.0);\n"
|
||||
" return mix(c, u_fog.rgb, t);\n"
|
||||
"}\n"
|
||||
"out vec4 o_color;\n"
|
||||
"void main() {\n"
|
||||
" vec3 No = normalize(v_nobj);\n"
|
||||
@@ -94,7 +101,7 @@ static const char* FS =
|
||||
" vec3 nm = texture(u_normalmap, v_uv).xyz * 2.0 - 1.0;\n"
|
||||
" vec3 N = normalize(u_nrm * normalize(mat3(T, B, No) * nm));\n"
|
||||
" float I = u_ambient + (1.0 - u_ambient) * max(dot(N, u_light), 0.0);\n"
|
||||
" o_color = vec4(min(u_color.rgb * I * texture(u_albedo, v_uv).rgb, vec3(1.0)), 1.0);\n"
|
||||
" o_color = vec4(tb_fog(min(u_color.rgb * I * texture(u_albedo, v_uv).rgb, vec3(1.0))), u_color.a);\n"
|
||||
"}\n";
|
||||
|
||||
static const char* VS_VERT_HEAD =
|
||||
@@ -144,6 +151,13 @@ static const char* FS_HEAD =
|
||||
"uniform float u_time;\n"
|
||||
"uniform sampler2D u_albedo;\n"
|
||||
"uniform sampler2D u_normalmap;\n"
|
||||
"uniform vec4 u_fog;\n" /* rgb = cor da nevoa, a = inicio */
|
||||
"uniform float u_fog_end;\n"
|
||||
"vec3 tb_fog(vec3 c) {\n"
|
||||
" if (u_fog_end <= u_fog.w) return c;\n"
|
||||
" float t = clamp((1.0 / gl_FragCoord.w - u_fog.w) / (u_fog_end - u_fog.w), 0.0, 1.0);\n"
|
||||
" return mix(c, u_fog.rgb, t);\n"
|
||||
"}\n"
|
||||
"out vec4 o_color;\n";
|
||||
static const char* FS_MAIN =
|
||||
"void main() {\n"
|
||||
@@ -156,7 +170,7 @@ static const char* FS_MAIN =
|
||||
" vec3 N = normalize(u_nrm * normalize(mat3(T, B, No) * nm));\n"
|
||||
" float light = u_ambient + (1.0 - u_ambient) * max(dot(N, u_light), 0.0);\n"
|
||||
" vec3 tex = texture(u_albedo, v_uv).rgb;\n"
|
||||
" o_color = vec4(tb_shade(v_pos, N, v_local, v_uv, u_color.rgb, tex, light, u_time), 1.0);\n"
|
||||
" o_color = vec4(tb_fog(min(tb_shade(v_pos, N, v_local, v_uv, u_color.rgb, tex, light, u_time), vec3(1.0))), u_color.a);\n"
|
||||
"}\n";
|
||||
|
||||
static GLuint shader(GLenum kind, const char* src) {
|
||||
@@ -266,6 +280,8 @@ static GpuShader* gpu_shader(const Tb3dShader* sh) {
|
||||
g->time = glGetUniformLocation(g->prog, "u_time");
|
||||
g->albedo = glGetUniformLocation(g->prog, "u_albedo");
|
||||
g->normalmap = glGetUniformLocation(g->prog, "u_normalmap");
|
||||
g->fog = glGetUniformLocation(g->prog, "u_fog");
|
||||
g->fog_end = glGetUniformLocation(g->prog, "u_fog_end");
|
||||
return g;
|
||||
}
|
||||
|
||||
@@ -306,6 +322,8 @@ static bool gl_setup(void) {
|
||||
u_color = glGetUniformLocation(s_prog, "u_color");
|
||||
u_light = glGetUniformLocation(s_prog, "u_light");
|
||||
u_ambient = glGetUniformLocation(s_prog, "u_ambient");
|
||||
u_fog = glGetUniformLocation(s_prog, "u_fog");
|
||||
u_fog_end = glGetUniformLocation(s_prog, "u_fog_end");
|
||||
glGenTextures(1, &s_white);
|
||||
glBindTexture(GL_TEXTURE_2D, s_white);
|
||||
static const uint8_t branco[4] = { 255, 255, 255, 255 };
|
||||
@@ -392,10 +410,17 @@ static bool draw(const Tb3dFrame* f, uint8_t* out) {
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LESS);
|
||||
glDisable(GL_BLEND); /* um quadro que parou no meio */
|
||||
glDepthMask(GL_TRUE);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glUseProgram(s_prog);
|
||||
glUniform3fv(u_light, 1, f->light_dir);
|
||||
glUniform1f(u_ambient, f->ambient);
|
||||
float fog[4] = { f->fog_color[0], f->fog_color[1], f->fog_color[2], f->fog_start };
|
||||
glUniform4fv(u_fog, 1, fog);
|
||||
glUniform1f(u_fog_end, f->fog_end);
|
||||
/* transparentes: misturam a cor (o alfa da imagem fica 1) sem gravar o z */
|
||||
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glUniform1i(glGetUniformLocation(s_prog, "u_albedo"), 0);
|
||||
glUniform1i(glGetUniformLocation(s_prog, "u_normalmap"), 1);
|
||||
@@ -403,6 +428,7 @@ static bool draw(const Tb3dFrame* f, uint8_t* out) {
|
||||
const Tb3dDraw* d = &f->draws[i];
|
||||
GpuMesh* g = gpu_mesh(d->mesh);
|
||||
if (!g) continue;
|
||||
if (i == f->opaque_count) { glEnable(GL_BLEND); glDepthMask(GL_FALSE); }
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, gpu_tex(d->albedo));
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
@@ -420,6 +446,8 @@ static bool draw(const Tb3dFrame* f, uint8_t* out) {
|
||||
glUniform1f(sh->time, f->time);
|
||||
glUniform1i(sh->albedo, 0);
|
||||
glUniform1i(sh->normalmap, 1);
|
||||
glUniform4fv(sh->fog, 1, fog);
|
||||
glUniform1f(sh->fog_end, f->fog_end);
|
||||
glBindVertexArray(g->vao);
|
||||
glDrawElements(GL_TRIANGLES, g->icount, GL_UNSIGNED_INT, (void*)0);
|
||||
glUseProgram(s_prog);
|
||||
@@ -431,6 +459,8 @@ static bool draw(const Tb3dFrame* f, uint8_t* out) {
|
||||
glBindVertexArray(g->vao);
|
||||
glDrawElements(GL_TRIANGLES, g->icount, GL_UNSIGNED_INT, (void*)0);
|
||||
}
|
||||
glDisable(GL_BLEND);
|
||||
glDepthMask(GL_TRUE);
|
||||
size_t row = (size_t)f->width * 4, n = row * (size_t)f->height;
|
||||
if (n > s_rows_cap) {
|
||||
uint8_t* p = realloc(s_rows, n);
|
||||
|
||||
Reference in new issue
Block a user