Skip to content

Commit

Permalink
Add texture transfroms to sokol renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Feb 8, 2024
1 parent 3492314 commit 41bb77c
Show file tree
Hide file tree
Showing 9 changed files with 857 additions and 580 deletions.
6 changes: 6 additions & 0 deletions Source/Render/sokol/SokolRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class cSokolRender: public cInterfaceRenderDevice {
sColor4f activeLightAmbient;
sColor4f activeLightSpecular;

Mat4f activeTex0Transform;
Mat4f activeTex1Transform;

//Commands handling
void ClearCommands();
void FinishActiveDrawBuffer();
Expand Down Expand Up @@ -210,6 +213,9 @@ class cSokolRender: public cInterfaceRenderDevice {
void SetMaterialTilemapShadow() override;
void SetTileColor(sColor4f color) override;

void setTexture0Transform(const Mat4f& tex0Transform);
void setTexture1Transform(const Mat4f& tex1Transform);

// //// cInterfaceRenderDevice impls end ////
};

Expand Down
20 changes: 8 additions & 12 deletions Source/Render/sokol/SokolRenderDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,19 @@ void cSokolRender::DrawNoMaterialMesh(cObjMesh* mesh, sDataRenderMaterial* data)
//TODO SetPointLight(mesh->GetRootNode()->GetLight());

SetWorldMatXf(mesh->GetGlobalMatrix());
if(data->mat&MAT_TEXMATRIX_STAGE1)
{
Mat4f mat;
activeTex0Transform = Mat4f::ID;
if(data->mat&MAT_TEXMATRIX_STAGE1) {
MatXf &m=data->TexMatrix;
memset(&mat,0,sizeof(mat));
mat.xx = m.rot()[0][0], mat.xy = m.rot()[0][1];
mat.yx = m.rot()[1][0], mat.yy = m.rot()[1][1];
mat.zx = m.trans().x, mat.zy = m.trans().y;
//TODO gb_RenderDevice3D->lpD3DDevice->SetTransform(D3DTS_TEXTURE0, reinterpret_cast<const D3DMATRIX*>(&mat));
activeTex0Transform.xx = m.rot()[0][0], activeTex0Transform.xy = m.rot()[0][1];
activeTex0Transform.yx = m.rot()[1][0], activeTex0Transform.yy = m.rot()[1][1];
activeTex0Transform.zx = m.trans().x, activeTex0Transform.zy = m.trans().y;
}

if(data->mat&MAT_RENDER_SPHEREMAP)
{ // сферический мапинг
activeTex1Transform = Mat4f::ID;
if(data->mat&MAT_RENDER_SPHEREMAP) { // сферический мапинг
Mat4f mat;
memset(&mat,0,sizeof(mat));
mat.xx=mat.yy=mat.wx=mat.wy=0.5f;
//TODO gb_RenderDevice3D->lpD3DDevice->SetTransform(D3DTS_TEXTURE1, reinterpret_cast<const D3DMATRIX*>(&mat));
activeTex1Transform.xx=activeTex1Transform.yy=activeTex1Transform.wx=activeTex1Transform.wy=0.5f;
}

cMeshTri* Tri = mesh->GetTri();
Expand Down
13 changes: 13 additions & 0 deletions Source/Render/sokol/SokolRenderState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ void cSokolRender::CreateCommand(VertexBuffer* vb, size_t vertices, IndexBuffer*
auto vs_params = reinterpret_cast<color_texture_vs_params_t*>(cmd->vs_params);
auto fs_params = reinterpret_cast<color_texture_fs_params_t*>(cmd->fs_params);
shader_set_common_params(vs_params, fs_params);
vs_params->tex0_mat = activeTex0Transform;
vs_params->tex1_mat = activeTex1Transform;
fs_params->un_color_mode = activeCommandColorMode;
fs_params->un_tex2_lerp = activeCommandTex2Lerp;
break;
Expand All @@ -407,6 +409,7 @@ void cSokolRender::CreateCommand(VertexBuffer* vb, size_t vertices, IndexBuffer*
auto fs_params = reinterpret_cast<normal_texture_fs_params_t*>(cmd->fs_params);
shader_set_common_params(vs_params, fs_params);
vs_params->model = isOrthographicProjSet ? Mat4f::ID : activeCommandW;
vs_params->tex0_mat = activeTex0Transform;
fs_params->material = activeGlobalLight ? activeMaterial : SOKOL_MAT_NONE;
memcpy(fs_params->diffuse, &activeDiffuse, sizeof(float) * 4);
memcpy(fs_params->ambient, &activeAmbient, sizeof(float) * 4);
Expand Down Expand Up @@ -444,6 +447,8 @@ void cSokolRender::CreateCommand(VertexBuffer* vb, size_t vertices, IndexBuffer*
activeCommand.base_elements = 0;
activeCommand.vertices = 0;
activeCommand.indices = 0;
activeTex0Transform = Mat4f::ID;
activeTex1Transform = Mat4f::ID;

//Submit command
commands.emplace_back(cmd);
Expand Down Expand Up @@ -583,6 +588,14 @@ void cSokolRender::SetMaterial(SOKOL_MATERIAL_TYPE material, const sColor4f& dif
}
}

void cSokolRender::setTexture0Transform(const Mat4f& tex0Transform) {
activeTex0Transform = tex0Transform;
}

void cSokolRender::setTexture1Transform(const Mat4f& tex1Transform) {
activeTex1Transform = tex1Transform;
}

void cSokolRender::SetBlendState(eBlendMode blend) {
if (activePipelineMode.blend != blend) {
FinishActiveDrawBuffer();
Expand Down
329 changes: 192 additions & 137 deletions Source/Render/sokol/shaders/color_tex1.h

Large diffs are not rendered by default.

419 changes: 252 additions & 167 deletions Source/Render/sokol/shaders/color_tex2.h

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions Source/Render/sokol/shaders/color_texture.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//Uniforms
uniform color_texture_vs_params {
mat4 un_mvp;
mat4 tex0_mat;
mat4 tex1_mat;
};

//Vertex Buffer inputs
Expand All @@ -24,9 +26,9 @@ layout(location=2) out vec2 fs_uv1;
void main() {
gl_Position = un_mvp * vec4(vs_position, 1.0f);
fs_color = vs_color;
fs_uv0 = vs_texcoord0;
fs_uv0 = (tex0_mat * vec4(vs_texcoord0, 1, 1)).xy;
#if defined(SHADER_TEX_2)
fs_uv1 = vs_texcoord1;
fs_uv1 = (tex1_mat * vec4(vs_texcoord1, 1, 1)).xy;
#endif
}
@end
Expand Down
Loading

0 comments on commit 41bb77c

Please sign in to comment.