Skip to content

Commit

Permalink
Refactored rendering code, unified shader for both TSP and BSD render…
Browse files Browse the repository at this point in the history
…objects,fixed color values not being loaded correctly
  • Loading branch information
AdrianoDiDio committed Apr 13, 2024
1 parent 946bded commit 01ad0bd
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 533 deletions.
8 changes: 5 additions & 3 deletions src/Common/VAO.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ VAO_t *VAOInitXYZUVRGB(float *Data,int DataSize,int Stride,int VertexOffset,int

return VAO;
}
VAO_t *VAOInitXYZUVRGBCLUTColorModeInteger(int *Data,int DataSize,int Stride,int VertexOffset,int TextureOffset,int ColorOffset,int CLUTOffset,
int ColorModeOffset,int Count)
VAO_t *VAOInitXYZUVRGBCLUTColorModeTexturedInteger(int *Data,int DataSize,int Stride,int VertexOffset,int TextureOffset,int ColorOffset,int CLUTOffset,
int ColorModeOffset,int TexturedOffset,int Count)
{
VAO_t *VAO;

Expand Down Expand Up @@ -111,7 +111,9 @@ VAO_t *VAOInitXYZUVRGBCLUTColorModeInteger(int *Data,int DataSize,int Stride,int
glEnableVertexAttribArray(3);
glVertexAttribIPointer(4,1,GL_INT,Stride,BUFFER_INT_OFFSET(ColorModeOffset));
glEnableVertexAttribArray(4);

glVertexAttribIPointer(5,1,GL_INT,Stride,BUFFER_INT_OFFSET(TexturedOffset));
glEnableVertexAttribArray(5);

VAO->Next = NULL;
VAO->CurrentSize = 0;
VAO->Stride = Stride;
Expand Down
4 changes: 2 additions & 2 deletions src/Common/VAO.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ typedef struct VAO_s

// 3D
VAO_t *VAOInitXYZUVRGB(float *Data,int DataSize,int Stride,int VertexOffset,int TextureOffset,int ColorOffset,int Count);
VAO_t *VAOInitXYZUVRGBCLUTColorModeInteger(int *Data,int DataSize,int Stride,int VertexOffset,int TextureOffset,int ColorOffset,int CLUTOffset,
int ColorModeOffset,int Count);
VAO_t *VAOInitXYZUVRGBCLUTColorModeTexturedInteger(int *Data,int DataSize,int Stride,int VertexOffset,int TextureOffset,int ColorOffset,int CLUTOffset,
int ColorModeOffset,int TexturedOffset,int Count);
VAO_t *VAOInitXYZUV(float *Data,int DataSize,int Stride,int VertexOffset,int TextureOffset,int Count);
VAO_t *VAOInitXYZRGB(float *Data,int DataSize,int Stride,int VertexOffset,int ColorOffset,int DynamicDraw);
VAO_t *VAOInitXYZ(float *Data,int DataSize,int Stride,int VertexOffset,int Count);
Expand Down
214 changes: 60 additions & 154 deletions src/JPModelViewer/BSD.c

Large diffs are not rendered by default.

72 changes: 41 additions & 31 deletions src/JPModelViewer/BSD.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ typedef struct BSDUv2_s {

typedef struct RenderObjectShader_s {
int MVPMatrixId;
int MVMatrixId;
int EnableLightingId;
int EnableFogId;
int FogNearId;
int FogColorId;
int PaletteTextureId;
int TextureIndexId;
Shader_t *Shader;
Expand Down Expand Up @@ -211,39 +207,54 @@ typedef struct BSDEntryTable_s {

typedef struct BSDFaceGT3Packet_s
{
unsigned int Tag;
Byte r0, g0, b0, code;
short x0, y0;
BSDUv_t UV0; unsigned short CBA;
Byte r1, g1, b1, p1;
short x1, y1;
BSDUv_t UV1; unsigned short TexInfo;
Byte r2, g2, b2, p2;
short x2, y2;
BSDUv_t UV2; unsigned short pad2;
unsigned int Tag;
BSDColor_t RGB0;
short X0;
short Y0;
BSDUv_t UV0;
unsigned short CBA;
BSDColor_t RGB1;
short X1;
short Y1;
BSDUv_t UV1;
unsigned short TexInfo;
BSDColor_t RGB2;
short X2;
short Y2;
BSDUv_t UV2;
unsigned short Pad1;
} BSDFaceGT3Packet_t;

typedef struct BSDFaceG3Packet_s {
unsigned int Tag;
Byte r0, g0, b0, code;
short x0, y0;
Byte r1, g1, b1, p1;
short x1, y1;
Byte r2, g2, b2, p2;
short x2, y2;
unsigned int Tag;
BSDColor_t RGB0;
short X0;
short Y0;
BSDColor_t RGB1;
short X1;
short Y1;
BSDColor_t RGB2;
short X2;
short Y2;
} BSDFaceG3Packet_t;

typedef struct BSDFaceFT3Packet_s
{
unsigned int Tag;
Byte r0, g0, b0, code;
short x0, y0;
BSDUv_t UV0; unsigned short CBA;
short x1, y1;
BSDUv_t UV1; unsigned short TexInfo;
short x2, y2;
BSDUv_t UV2; unsigned short pad1;
} BSDFaceFT3Packet_t;
unsigned int Tag;
BSDColor_t RGB0;
short X0;
short Y0;
BSDUv_t UV0;
unsigned short CBA;
short X1;
short Y1;
BSDUv_t UV1;
unsigned short TexInfo;
short X2;
short Y2;
BSDUv_t UV2;
unsigned short Pad1;
} BSDFaceFT3Packet_t;

typedef struct BSDFace_s {
BSDUv_t UV0;
Expand All @@ -259,7 +270,6 @@ typedef struct BSDFace_s {
unsigned int Vert0;
unsigned int Vert1;
unsigned int Vert2;
bool IsTextured;

} BSDFace_t;

Expand Down
54 changes: 0 additions & 54 deletions src/JPModelViewer/Shaders/BSDRenderObjectFragmentShader.glsl

This file was deleted.

27 changes: 0 additions & 27 deletions src/JPModelViewer/Shaders/BSDRenderObjectVertexShader.glsl

This file was deleted.

55 changes: 22 additions & 33 deletions src/JPModelViewer/Shaders/RenderObjectFragmentShader.glsl
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
#version 330 core
out vec4 FragColor;
out vec4 fragColor;

in vec3 ourColor;
in vec2 TexCoord;
in float LightingEnabled;
in float FogEnabled;
in float FogFactor;
in float ourFogNear;
in vec3 ourFogColor;
in vec3 color;
in vec2 texCoord;
in float lightingEnabled;
in vec2 CLUTCoord;
flat in int ourColorMode;
flat in int colorMode;
flat in int STPMode;
flat in int textured;

uniform usampler2D ourIndexTexture;
uniform sampler2D ourPaletteTexture;
uniform usampler2D indexTexture;
uniform sampler2D paletteTexture;

const float FogMax = 6000.f;

uint InternalToPsxColor(vec4 c) {
uint a = uint(floor(c.a + 0.5));
Expand All @@ -27,42 +23,35 @@ uint InternalToPsxColor(vec4 c) {

void main()
{
uvec4 TexColor;
uvec4 texColor;
uint CLUTIndex;
vec4 CLUTTexel;
uint CLUTX;
uint CLUTY;
float FogMin;
float InterpolationFactor;

if( ourColorMode != 50 ) {
if( textured == 1 ) {
//NOTE(Adriano):16-bpp mode textures are encoded directly into CLUT.
if( ourColorMode == 2 ) {
CLUTTexel = texelFetch(ourPaletteTexture, ivec2(TexCoord), 0);
if( colorMode == 2 ) {
CLUTTexel = texelFetch(paletteTexture, ivec2(texCoord), 0);
} else {
TexColor = texelFetch(ourIndexTexture, ivec2(TexCoord), 0);
CLUTIndex = TexColor.r;
texColor = texelFetch(indexTexture, ivec2(texCoord), 0);
CLUTIndex = texColor.r;
CLUTX = uint(CLUTCoord.x) + CLUTIndex;
CLUTY = uint(CLUTCoord.y);
CLUTTexel = texelFetch(ourPaletteTexture, ivec2(CLUTX,CLUTY), 0);
CLUTTexel = texelFetch(paletteTexture, ivec2(CLUTX,CLUTY), 0);
}
if( InternalToPsxColor(CLUTTexel) == 0x0000u) {
discard;
}

if( LightingEnabled > 0.5 ) {
CLUTTexel.r = clamp(CLUTTexel.r * ourColor.r * 2.f, 0.f, 1.f);
CLUTTexel.g = clamp(CLUTTexel.g * ourColor.g * 2.f, 0.f, 1.f);
CLUTTexel.b = clamp(CLUTTexel.b * ourColor.b * 2.f, 0.f, 1.f);
if( lightingEnabled > 0.5 ) {
CLUTTexel.r = clamp(CLUTTexel.r * color.r * 2.f, 0.f, 1.f);
CLUTTexel.g = clamp(CLUTTexel.g * color.g * 2.f, 0.f, 1.f);
CLUTTexel.b = clamp(CLUTTexel.b * color.b * 2.f, 0.f, 1.f);
}
} else {
CLUTTexel = vec4(ourColor.rgb,1);
CLUTTexel = vec4(color.rgb,1);
}
// if( FogEnabled > 0.5 ) {
// FogMin = (ourFogNear / 4096.f) * 20.f;
// InterpolationFactor = 1.0 - clamp((FogMax - FogFactor) / (FogMax - FogMin), 0.0, 1.0);
// CLUTTexel = mix(CLUTTexel, vec4(ourFogColor,1.0), InterpolationFactor);
// }

FragColor = vec4(CLUTTexel.rgb,1);

fragColor = vec4(CLUTTexel.rgb,1);
}
51 changes: 19 additions & 32 deletions src/JPModelViewer/Shaders/RenderObjectVertexShader.glsl
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
#version 330 core
layout (location = 0) in ivec3 aPos;
layout (location = 1) in ivec2 aTexCoord;
layout (location = 2) in ivec3 aColor;
layout (location = 3) in ivec2 aCLUTCoord;
layout (location = 4) in int aColorMode;
layout (location = 0) in ivec3 inPos;
layout (location = 1) in ivec2 inTexCoord;
layout (location = 2) in ivec3 inColor;
layout (location = 3) in ivec2 inCLUTCoord;
layout (location = 4) in int inColorMode;
layout (location = 5) in int inTextured;

uniform mat4 MVPMatrix;
uniform mat4 MVMatrix;
uniform bool EnableLighting;
uniform bool EnableFog;
uniform int ColorMode;
uniform vec3 FogColor;
uniform float FogNear;
out vec3 ourColor;
out vec2 TexCoord;
out float LightingEnabled;
out float FogEnabled;
out float FogFactor;
out vec3 ourFogColor;
out float ourFogNear;
uniform bool enableLighting;
out vec3 color;
out vec2 texCoord;
out float lightingEnabled;
out vec2 CLUTCoord;
flat out int ourColorMode;
flat out int colorMode;
flat out int textured;

void main()
{
vec4 EyeSpacePosition;
gl_Position = MVPMatrix * vec4(aPos, 1.0);
EyeSpacePosition = MVMatrix * vec4(aPos, 1.0);
FogFactor = abs(EyeSpacePosition.z / EyeSpacePosition.w);

ourColor = vec3(aColor) / 255.f;
TexCoord = vec2(aTexCoord) + vec2(0.001, 0.001);
LightingEnabled = EnableLighting ? 1.0 : 0.0;
FogEnabled = EnableFog ? 1.0 : 0.0;
CLUTCoord = vec2(aCLUTCoord) + vec2(0.001, 0.001);
ourColorMode = aColorMode;
ourFogColor = FogColor;
ourFogNear = FogNear;
gl_Position = MVPMatrix * vec4(inPos, 1.0);
color = vec3(inColor) / 255.f;
texCoord = vec2(inTexCoord) + vec2(0.001, 0.001);
lightingEnabled = enableLighting ? 1.0 : 0.0;
CLUTCoord = vec2(inCLUTCoord) + vec2(0.001, 0.001);
colorMode = inColorMode;
textured = inTextured;
}
Loading

0 comments on commit 01ad0bd

Please sign in to comment.