Align OpenGL textured integration baselines

This commit is contained in:
2026-03-26 01:07:36 +08:00
parent 9adac63b4c
commit c5605c2a32
8 changed files with 41 additions and 65 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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();

View File

@@ -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);
vTexcoord = aTexcoord;
}

View File

@@ -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,22 +318,10 @@ 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);
break;
}
if (frameCount == captureStartFrame) {
RenderDocCapture::Get().BeginCapture("OpenGL_Sphere_Test");
Log("[INFO] RenderDoc capture started at frame %d", frameCount);
}
}
}
Log("[INFO] Rendered %d frames (capture was %d-%d)", renderCount, captureStartFrame, captureEndFrame);
Log("[INFO] Capture complete - taking screenshot!");
@@ -365,6 +331,17 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
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);
}
}
}
sampler.Shutdown();
texture.Shutdown();