SkinRenderer - Added simple skybox rendering

This commit is contained in:
miku-666
2024-01-31 20:02:07 +01:00
parent 7b94f02f55
commit a242c7fceb
7 changed files with 184 additions and 8 deletions

View File

@@ -641,6 +641,9 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\shader\skyboxFragmentShader.glsl" />
<None Include="Resources\shader\skyboxVertexShader.glsl" />
<None Include="Resources\skybox_texture.png" />
<None Include="Resources\shader\skinVertexShader.glsl" />
<None Include="Resources\entityData.json" />
<None Include="Resources\TexturePackIcon.png" />

View File

@@ -222,6 +222,16 @@ namespace PckStudio.Properties {
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
public static System.Drawing.Bitmap DefaultSkyTexture {
get {
object obj = ResourceManager.GetObject("DefaultSkyTexture", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
@@ -590,6 +600,7 @@ namespace PckStudio.Properties {
///
///layout(location = 0) in vec4 vertexPosition;
///layout(location = 1) in vec2 texCoord;
///layout(location = 2) in float scale;
///
///uniform mat4 u_ViewProjection;
///uniform mat4 u_Model;
@@ -599,7 +610,9 @@ namespace PckStudio.Properties {
///void main()
///{
/// v_TexCoord = texCoord;
/// gl_Position = u_ViewProjection * u_Model * vertexPosition;
/// vec4 scaledVertex = scale * vertexPosition;
/// vec4 invertedVertex = vec4(scaledVertex.x, scaledVertex.y * -1.0, scaledVertex.z * -1.0, 1.0);
/// gl_Position = u_ViewProjection * u_Model * invertedVertex;
///};.
/// </summary>
public static string skinVertexShader {
@@ -608,6 +621,48 @@ namespace PckStudio.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to #version 330 core
///
///layout(location = 0) out vec4 color;
///
///uniform samplerCube skybox;
///
///in vec3 texCoords;
///
///void main()
///{
/// color = texture(skybox, texCoords);
///}.
/// </summary>
public static string skyboxFragmentShader {
get {
return ResourceManager.GetString("skyboxFragmentShader", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to #version 330 core
///
///layout(location = 0) in vec4 a_Pos;
///
///uniform mat4 viewProjection;
///
///out vec3 texCoords;
///
///void main()
///{
/// vec4 pos = viewProjection * a_Pos;
/// gl_Position = vec4(pos.x, pos.y, pos.ww);
/// texCoords = vec3(a_Pos.x, a_Pos.y, -a_Pos.z);
///};.
/// </summary>
public static string skyboxVertexShader {
get {
return ResourceManager.GetString("skyboxVertexShader", resourceCulture);
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>

View File

@@ -334,4 +334,13 @@
<data name="skinFragmentShader" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\shader\skinFragmentShader.glsl;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="DefaultSkyTexture" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\skybox_texture.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="skyboxFragmentShader" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\shader\skyboxFragmentShader.glsl;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="skyboxVertexShader" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\shader\skyboxVertexShader.glsl;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
</root>

View File

@@ -31,6 +31,7 @@ using PckStudio.Forms.Editor;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Drawing.Imaging;
using System.IO;
using PckStudio.Rendering.Camera;
using PckStudio.Rendering.Texture;
@@ -99,7 +100,7 @@ namespace PckStudio.Rendering
set
{
_globalModelRotation.X = MathHelper.Clamp(value.X, -90f, 90f);
_globalModelRotation.Y = MathHelper.Clamp(value.Y, -180f, 180f);
_globalModelRotation.Y = value.Y % 360f;
}
}
@@ -114,8 +115,9 @@ namespace PckStudio.Rendering
{
set
{
if (HasValidContext && _skinShader is not null)
if (value is not null && HasValidContext && _skinShader is not null)
{
MakeCurrent();
TextureSize = value.Width == value.Height ? new Size(64, 64) : new Size(64, 32);
UvTranslation = value.Width == value.Height ? new Vector2(1f / 64) : new Vector2(1f / 64, 1f / 32);
var texture = new Texture2D(value);
@@ -146,6 +148,12 @@ namespace PckStudio.Rendering
private SkinANIM _anim;
private Image _texture;
private Shader _skyboxShader;
private RenderBuffer _skyboxRenderBuffer;
private CubeTexture _skyboxTexture;
private float skyboxRotation = 0f;
private float skyboxRotationStep = 0.5f;
private Dictionary<string, CubeRenderGroup> additionalModelRenderGroups;
private Dictionary<string, float> partOffset;
private CubeRenderGroup head;
@@ -328,15 +336,76 @@ namespace PckStudio.Rendering
Trace.TraceInformation(GL.GetString(StringName.Version));
// Initialize skybox shader
{
string customSkyboxFilepath = Path.Combine(Program.AppData, "cubemap.png");
Image skyboxImage = File.Exists(customSkyboxFilepath)
? Image.FromFile(customSkyboxFilepath)
: Resources.DefaultSkyTexture;
_skyboxShader = Shader.Create(Resources.skyboxVertexShader, Resources.skyboxFragmentShader);
_skyboxTexture = new CubeTexture(skyboxImage);
_skyboxTexture.Bind(1);
_skyboxShader.Bind();
_skyboxShader.SetUniform1("skybox", 1);
_skyboxShader.Validate();
Vector3[] cubeVertices = new Vector3[]
{
// front
new Vector3(-1.0f, -1.0f, 1.0f),
new Vector3( 1.0f, -1.0f, 1.0f),
new Vector3( 1.0f, 1.0f, 1.0f),
new Vector3(-1.0f, 1.0f, 1.0f),
// back
new Vector3(-1.0f, -1.0f, -1.0f),
new Vector3( 1.0f, -1.0f, -1.0f),
new Vector3( 1.0f, 1.0f, -1.0f),
new Vector3(-1.0f, 1.0f, -1.0f)
};
var skyboxVAO = new VertexArray();
var skyboxVBO = new VertexBuffer<Vector3>(cubeVertices, cubeVertices.Length * Vector3.SizeInBytes);
var vboLayout = new VertexBufferLayout();
vboLayout.Add<float>(3);
skyboxVAO.AddBuffer(skyboxVBO, vboLayout);
var skybocIBO = IndexBuffer.Create(
// front
0, 1, 2,
2, 3, 0,
// right
1, 5, 6,
6, 2, 1,
// back
7, 6, 5,
5, 4, 7,
// left
4, 0, 3,
3, 7, 4,
// bottom
4, 5, 1,
1, 0, 4,
// top
3, 2, 6,
6, 7, 3);
_skyboxRenderBuffer = new RenderBuffer(skyboxVAO, skybocIBO, PrimitiveType.Triangles);
skyboxVAO.Unbind();
skybocIBO.Unbind();
}
// Initialize skin shader
{
_skinShader = Shader.Create(Resources.skinVertexShader, Resources.skinFragmentShader);
_skinShader.Validate();
_skinShader.Bind();
Texture ??= Resources.classic_template;
RenderTexture = Texture;
GLErrorCheck();
}
}
public void UpdateModelData()
{
@@ -472,9 +541,10 @@ namespace PckStudio.Rendering
MakeCurrent();
camera.Update(ClientSize.Width / (float)ClientSize.Height);
camera.Update(AspectRatio);
var viewProjection = camera.GetViewProjection();
_skinShader.Bind();
_skinShader.SetUniformMat4("u_ViewProjection", ref viewProjection);
_skinShader.SetUniform2("u_TexScale", UvTranslation);
@@ -537,6 +607,20 @@ namespace PckStudio.Rendering
RenderBodyPart(new Vector3(0f, 12f, 0f), new Vector3(-2f, -12f, 0f), RightLegMatrix * legRightMatrix, modelMatrix, "LEG0", "PANTS0");
RenderBodyPart(new Vector3(0f, 12f, 0f), new Vector3( 2f, -12f, 0f), LeftLegMatrix * legLeftMatrix , modelMatrix, "LEG1", "PANTS1");
if (true)
{
GL.DepthFunc(DepthFunction.Lequal);
_skyboxShader.Bind();
var view = new Matrix4(new Matrix3(Matrix4.LookAt(camera.WorldPosition, camera.WorldPosition + camera.Orientation, camera.Up)))
* Matrix4.CreateRotationY(MathHelper.DegreesToRadians(skyboxRotation));
var proj = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(camera.Fov), AspectRatio, 1f, 1000f);
var viewproj = view * proj;
_skyboxShader.SetUniformMat4("viewProjection", ref viewproj);
Renderer.Draw(_skyboxShader, _skyboxRenderBuffer);
GL.DepthFunc(DepthFunction.Less);
}
SwapBuffers();
}
@@ -605,9 +689,8 @@ namespace PckStudio.Rendering
private void animationTimer_Tick(object sender, EventArgs e)
{
if (!Focused)
return;
skyboxRotation += skyboxRotationStep;
skyboxRotation %= 360f;
animationCurrentRotationAngle += animationRotationStep;
if (animationCurrentRotationAngle >= animationMaxAngleInDegrees || animationCurrentRotationAngle <= -animationMaxAngleInDegrees)
animationRotationStep = -animationRotationStep;

View File

@@ -0,0 +1,12 @@
#version 330 core
layout(location = 0) out vec4 color;
uniform samplerCube skybox;
in vec3 texCoords;
void main()
{
color = texture(skybox, texCoords);
}

View File

@@ -0,0 +1,14 @@
#version 330 core
layout(location = 0) in vec4 a_Pos;
uniform mat4 viewProjection;
out vec3 texCoords;
void main()
{
vec4 pos = viewProjection * a_Pos;
gl_Position = vec4(pos.x, pos.y, pos.ww);
texCoords = vec3(a_Pos.x, a_Pos.y, -a_Pos.z);
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB