diff --git a/engine/include/XCEngine/RHI/OpenGL/OpenGLTexture.h b/engine/include/XCEngine/RHI/OpenGL/OpenGLTexture.h index e7ec41f4..339b3a8a 100644 --- a/engine/include/XCEngine/RHI/OpenGL/OpenGLTexture.h +++ b/engine/include/XCEngine/RHI/OpenGL/OpenGLTexture.h @@ -51,7 +51,7 @@ public: bool Initialize(OpenGLTextureType type, int width, int height, int depth, int mipLevels, OpenGLFormat format, const void* data = nullptr); bool Initialize2D(int width, int height, int channels, const void* data, bool generateMipmap = true); bool InitializeCubeMap(int size, int mipLevels, OpenGLFormat format, const void* data = nullptr); - bool LoadFromFile(const char* path, bool flipVertical = true); + bool LoadFromFile(const char* path, bool flipVertical = false); void Shutdown() override; void Bind(int slot = 0) const; diff --git a/engine/src/RHI/OpenGL/OpenGLTexture.cpp b/engine/src/RHI/OpenGL/OpenGLTexture.cpp index 875605aa..a6cfa8e9 100644 --- a/engine/src/RHI/OpenGL/OpenGLTexture.cpp +++ b/engine/src/RHI/OpenGL/OpenGLTexture.cpp @@ -121,13 +121,14 @@ bool OpenGLTexture::InitializeCubeMap(int size, int mipLevels, OpenGLFormat form bool OpenGLTexture::LoadFromFile(const char* path, bool flipVertical) { stbi_set_flip_vertically_on_load(flipVertical ? 1 : 0); - unsigned char* data = stbi_load(path, &m_width, &m_height, &m_channels, 0); + unsigned char* data = stbi_load(path, &m_width, &m_height, &m_channels, STBI_rgb_alpha); if (!data) { std::cout << "Failed to load texture: " << path << std::endl; return false; } - bool result = Initialize2D(m_width, m_height, m_channels, data, true); + m_channels = 4; + bool result = Initialize2D(m_width, m_height, m_channels, data, false); stbi_image_free(data); return result; } diff --git a/tests/RHI/OpenGL/integration/quad/GT.ppm b/tests/RHI/OpenGL/integration/quad/GT.ppm index b8426a67..a8920605 100644 Binary files a/tests/RHI/OpenGL/integration/quad/GT.ppm and b/tests/RHI/OpenGL/integration/quad/GT.ppm differ diff --git a/tests/RHI/OpenGL/integration/quad/Res/Shader/quad.vert b/tests/RHI/OpenGL/integration/quad/Res/Shader/quad.vert index 3bfb4ff1..ab1c299d 100644 --- a/tests/RHI/OpenGL/integration/quad/Res/Shader/quad.vert +++ b/tests/RHI/OpenGL/integration/quad/Res/Shader/quad.vert @@ -8,4 +8,4 @@ out vec2 vTexcoord; void main() { gl_Position = aPosition; vTexcoord = aTexcoord; -} \ No newline at end of file +} diff --git a/tests/RHI/OpenGL/integration/quad/main.cpp b/tests/RHI/OpenGL/integration/quad/main.cpp index c7c93c99..7b695731 100644 --- a/tests/RHI/OpenGL/integration/quad/main.cpp +++ b/tests/RHI/OpenGL/integration/quad/main.cpp @@ -175,7 +175,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine pipelineState.Apply(); OpenGLTexture texture; - if (!texture.LoadFromFile("Res/Image/earth.png", true)) { + if (!texture.LoadFromFile("Res/Image/earth.png")) { Log("[ERROR] Failed to load texture"); return -1; } @@ -215,15 +215,17 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine commandList.Draw(PrimitiveType::TriangleStrip, 4, 0); - swapChain.Present(0, 0); frameCount++; - if (frameCount >= captureEndFrame) { RenderDocCapture::Get().EndCapture(); Log("[INFO] RenderDoc capture ended at frame %d", frameCount); + Log("[INFO] Capture complete - taking screenshot!"); + OpenGLScreenshot::Capture(device, swapChain, "quad.ppm"); break; } + swapChain.Present(0, 0); + if (frameCount == captureStartFrame) { RenderDocCapture::Get().BeginCapture("OpenGL_Quad_Test"); Log("[INFO] RenderDoc capture started at frame %d", frameCount); @@ -231,9 +233,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } } - Log("[INFO] Capture complete - taking screenshot!"); - OpenGLScreenshot::Capture(device, swapChain, "quad.ppm"); - sampler.Shutdown(); texture.Shutdown(); vertexArray.Shutdown(); @@ -249,4 +248,4 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine Log("[INFO] OpenGL Quad Test Finished"); return 0; -} \ No newline at end of file +} diff --git a/tests/RHI/OpenGL/integration/sphere/GT.ppm b/tests/RHI/OpenGL/integration/sphere/GT.ppm index d582791d..37a204fe 100644 Binary files a/tests/RHI/OpenGL/integration/sphere/GT.ppm and b/tests/RHI/OpenGL/integration/sphere/GT.ppm differ diff --git a/tests/RHI/OpenGL/integration/sphere/Res/Shader/sphere.vert b/tests/RHI/OpenGL/integration/sphere/Res/Shader/sphere.vert index 72260d3e..601ae6a6 100644 --- a/tests/RHI/OpenGL/integration/sphere/Res/Shader/sphere.vert +++ b/tests/RHI/OpenGL/integration/sphere/Res/Shader/sphere.vert @@ -11,8 +11,7 @@ uniform mat4 gProjectionMatrix; void main() { vec4 positionWS = gModelMatrix * aPosition; - positionWS.x = -positionWS.x; vec4 positionVS = gViewMatrix * positionWS; gl_Position = gProjectionMatrix * positionVS; - vTexcoord = vec2(aTexcoord.x, 1.0 - aTexcoord.y); -} \ No newline at end of file + vTexcoord = aTexcoord; +} diff --git a/tests/RHI/OpenGL/integration/sphere/main.cpp b/tests/RHI/OpenGL/integration/sphere/main.cpp index cb10dcd6..08e0ac0d 100644 --- a/tests/RHI/OpenGL/integration/sphere/main.cpp +++ b/tests/RHI/OpenGL/integration/sphere/main.cpp @@ -22,6 +22,8 @@ #include "XCEngine/Debug/ConsoleLogSink.h" #include "XCEngine/Debug/RenderDocCapture.h" #include "XCEngine/Core/Containers/String.h" +#include "XCEngine/Core/Math/Matrix4.h" +#include "XCEngine/Core/Math/Vector3.h" #pragma comment(lib, "opengl32.lib") @@ -30,6 +32,7 @@ using namespace XCEngine::RHI; using namespace XCEngine::Debug; using namespace XCEngine::Containers; +using namespace XCEngine::Math; static const int gWidth = 1280; static const int gHeight = 720; @@ -52,30 +55,6 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { return DefWindowProc(hwnd, msg, wParam, lParam); } -void IdentityMatrix(float* m) { - memset(m, 0, 16 * sizeof(float)); - m[0] = m[5] = m[10] = m[15] = 1.0f; -} - -void TranslationMatrix(float* m, float x, float y, float z) { - memset(m, 0, 16 * sizeof(float)); - m[0] = 1.0f; m[12] = x; - m[5] = 1.0f; m[13] = y; - m[10] = 1.0f; m[14] = z; - m[15] = 1.0f; -} - -void PerspectiveMatrix(float* m, float fov, float aspect, float nearZ, float farZ) { - memset(m, 0, 16 * sizeof(float)); - float tanHalfFov = tanf(fov / 2.0f); - m[0] = 1.0f / (aspect * tanHalfFov); // m[0][0] - m[5] = 1.0f / tanHalfFov; // m[1][1] - m[10] = -(farZ + nearZ) / (farZ - nearZ); // m[2][2] - m[14] = -(2.0f * farZ * nearZ) / (farZ - nearZ); // m[2][3] - m[15] = 0.0f; - m[11] = -1.0f; // m[3][2] = -1 for OpenGL clip space -} - struct Vertex { float pos[4]; float texcoord[2]; @@ -235,24 +214,23 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } Log("[INFO] Shaders compiled successfully"); - float modelMatrix[16]; - float viewMatrix[16]; - float projectionMatrix[16]; - - IdentityMatrix(viewMatrix); - TranslationMatrix(modelMatrix, 0.0f, 0.0f, -5.0f); - float aspect = 1280.0f / 720.0f; - PerspectiveMatrix(projectionMatrix, 45.0f * 3.14159265f / 180.0f, aspect, 0.1f, 1000.0f); + const float aspect = 1280.0f / 720.0f; + const Matrix4x4 projectionMatrix = Matrix4x4::Perspective(45.0f * 3.141592f / 180.0f, aspect, 0.1f, 1000.0f); + const Matrix4x4 viewMatrix = Matrix4x4::Identity(); + const Matrix4x4 modelMatrix = Matrix4x4::Translation(Vector3(0.0f, 0.0f, 5.0f)); + const Matrix4x4 projectionMatrixT = projectionMatrix.Transpose(); + const Matrix4x4 viewMatrixT = viewMatrix.Transpose(); + const Matrix4x4 modelMatrixT = modelMatrix.Transpose(); Log("[DEBUG] ProjectionMatrix:"); for (int i = 0; i < 4; i++) { Log("[DEBUG] row%d: %.4f %.4f %.4f %.4f", i, - projectionMatrix[i*4], projectionMatrix[i*4+1], projectionMatrix[i*4+2], projectionMatrix[i*4+3]); + projectionMatrixT.m[i][0], projectionMatrixT.m[i][1], projectionMatrixT.m[i][2], projectionMatrixT.m[i][3]); } Log("[DEBUG] ModelMatrix:"); for (int i = 0; i < 4; i++) { Log("[DEBUG] row%d: %.4f %.4f %.4f %.4f", i, - modelMatrix[i*4], modelMatrix[i*4+1], modelMatrix[i*4+2], modelMatrix[i*4+3]); + modelMatrixT.m[i][0], modelMatrixT.m[i][1], modelMatrixT.m[i][2], modelMatrixT.m[i][3]); } OpenGLPipelineState pipelineState; @@ -278,12 +256,12 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine int projLoc = shader.GetUniformLocation("gProjectionMatrix"); Log("[DEBUG] Uniform locations - gModelMatrix: %d, gViewMatrix: %d, gProjectionMatrix: %d", modelLoc, viewLoc, projLoc); commandList.SetShader(&shader); - commandList.SetUniformMat4("gModelMatrix", modelMatrix); - commandList.SetUniformMat4("gViewMatrix", viewMatrix); - commandList.SetUniformMat4("gProjectionMatrix", projectionMatrix); + commandList.SetUniformMat4("gModelMatrix", &modelMatrixT.m[0][0]); + commandList.SetUniformMat4("gViewMatrix", &viewMatrixT.m[0][0]); + commandList.SetUniformMat4("gProjectionMatrix", &projectionMatrixT.m[0][0]); OpenGLTexture texture; - if (!texture.LoadFromFile("Res/Image/earth.png", true)) { + if (!texture.LoadFromFile("Res/Image/earth.png")) { Log("[ERROR] Failed to load texture"); return -1; } @@ -340,15 +318,24 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine commandList.DrawIndexed(PrimitiveType::Triangles, (uint32_t)indices.size(), 0, 0); - swapChain.Present(0, 0); frameCount++; - if (frameCount >= captureEndFrame) { RenderDocCapture::Get().EndCapture(); Log("[INFO] RenderDoc capture ended at frame %d", frameCount); + Log("[INFO] Rendered %d frames (capture was %d-%d)", renderCount, captureStartFrame, captureEndFrame); + Log("[INFO] Capture complete - taking screenshot!"); + + char exePath[MAX_PATH]; + GetModuleFileNameA(NULL, exePath, MAX_PATH); + char* lastSlash = strrchr(exePath, '\\'); + if (lastSlash) *lastSlash = '\0'; + strcat_s(exePath, "\\sphere.ppm"); + OpenGLScreenshot::Capture(device, swapChain, exePath); break; } + swapChain.Present(0, 0); + if (frameCount == captureStartFrame) { RenderDocCapture::Get().BeginCapture("OpenGL_Sphere_Test"); Log("[INFO] RenderDoc capture started at frame %d", frameCount); @@ -356,16 +343,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } } - Log("[INFO] Rendered %d frames (capture was %d-%d)", renderCount, captureStartFrame, captureEndFrame); - Log("[INFO] Capture complete - taking screenshot!"); - - char exePath[MAX_PATH]; - GetModuleFileNameA(NULL, exePath, MAX_PATH); - char* lastSlash = strrchr(exePath, '\\'); - if (lastSlash) *lastSlash = '\0'; - strcat_s(exePath, "\\sphere.ppm"); - OpenGLScreenshot::Capture(device, swapChain, exePath); - sampler.Shutdown(); texture.Shutdown(); indexBuffer.Shutdown(); @@ -382,4 +359,4 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine Log("[INFO] OpenGL Sphere Test Finished"); return 0; -} \ No newline at end of file +}