Add OpenGL quad integration test with texture mapping
- Add tests/RHI/OpenGL/integration/quad/ with main.cpp, CMakeLists.txt - Add GLSL shaders (quad.vert, quad.frag) for textured quad rendering - Use OpenGLVertexArray, OpenGLBuffer, OpenGLShader, OpenGLPipelineState - Add OpenGLTexture::LoadFromFile for texture loading (earth.png) - Add OpenGLSampler for texture sampling configuration - Disable depth test for 2D quad rendering - GT.ppm generated from OpenGL rendering output (0% diff on re-run)
This commit is contained in:
@@ -8,3 +8,4 @@ enable_testing()
|
||||
|
||||
add_subdirectory(minimal)
|
||||
add_subdirectory(triangle)
|
||||
add_subdirectory(quad)
|
||||
|
||||
57
tests/RHI/OpenGL/integration/quad/CMakeLists.txt
Normal file
57
tests/RHI/OpenGL/integration/quad/CMakeLists.txt
Normal file
@@ -0,0 +1,57 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
project(OpenGL_Quad)
|
||||
|
||||
set(ENGINE_ROOT_DIR ${CMAKE_SOURCE_DIR}/engine)
|
||||
set(PACKAGE_DIR ${CMAKE_SOURCE_DIR}/tests/OpenGL/package)
|
||||
|
||||
add_executable(OpenGL_Quad
|
||||
WIN32
|
||||
main.cpp
|
||||
${PACKAGE_DIR}/src/glad.c
|
||||
)
|
||||
|
||||
target_include_directories(OpenGL_Quad PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${ENGINE_ROOT_DIR}/include
|
||||
${PACKAGE_DIR}/include
|
||||
)
|
||||
|
||||
target_link_libraries(OpenGL_Quad PRIVATE
|
||||
opengl32
|
||||
XCEngine
|
||||
)
|
||||
|
||||
target_compile_definitions(OpenGL_Quad PRIVATE
|
||||
UNICODE
|
||||
_UNICODE
|
||||
)
|
||||
|
||||
add_custom_command(TARGET OpenGL_Quad POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Res
|
||||
$<TARGET_FILE_DIR:OpenGL_Quad>/Res
|
||||
)
|
||||
|
||||
add_custom_command(TARGET OpenGL_Quad POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${CMAKE_SOURCE_DIR}/tests/RHI/OpenGL/integration/run_integration_test.py
|
||||
$<TARGET_FILE_DIR:OpenGL_Quad>/
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${CMAKE_SOURCE_DIR}/tests/RHI/OpenGL/integration/compare_ppm.py
|
||||
$<TARGET_FILE_DIR:OpenGL_Quad>/
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/GT.ppm
|
||||
$<TARGET_FILE_DIR:OpenGL_Quad>/
|
||||
)
|
||||
|
||||
add_test(NAME OpenGL_Quad_Integration
|
||||
COMMAND ${Python3_EXECUTABLE} $<TARGET_FILE_DIR:OpenGL_Quad>/run_integration_test.py
|
||||
$<TARGET_FILE:OpenGL_Quad>
|
||||
quad.ppm
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/GT.ppm
|
||||
5
|
||||
WORKING_DIRECTORY $<TARGET_FILE_DIR:OpenGL_Quad>
|
||||
)
|
||||
BIN
tests/RHI/OpenGL/integration/quad/GT.ppm
Normal file
BIN
tests/RHI/OpenGL/integration/quad/GT.ppm
Normal file
Binary file not shown.
BIN
tests/RHI/OpenGL/integration/quad/Res/Image/earth.png
Normal file
BIN
tests/RHI/OpenGL/integration/quad/Res/Image/earth.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 189 KiB |
10
tests/RHI/OpenGL/integration/quad/Res/Shader/quad.frag
Normal file
10
tests/RHI/OpenGL/integration/quad/Res/Shader/quad.frag
Normal file
@@ -0,0 +1,10 @@
|
||||
#version 460
|
||||
|
||||
in vec2 vTexcoord;
|
||||
out vec4 fragColor;
|
||||
|
||||
uniform sampler2D uTexture;
|
||||
|
||||
void main() {
|
||||
fragColor = texture(uTexture, vTexcoord);
|
||||
}
|
||||
11
tests/RHI/OpenGL/integration/quad/Res/Shader/quad.vert
Normal file
11
tests/RHI/OpenGL/integration/quad/Res/Shader/quad.vert
Normal file
@@ -0,0 +1,11 @@
|
||||
#version 460
|
||||
|
||||
layout(location = 0) in vec4 aPosition;
|
||||
layout(location = 1) in vec2 aTexcoord;
|
||||
|
||||
out vec2 vTexcoord;
|
||||
|
||||
void main() {
|
||||
gl_Position = aPosition;
|
||||
vTexcoord = aTexcoord;
|
||||
}
|
||||
230
tests/RHI/OpenGL/integration/quad/main.cpp
Normal file
230
tests/RHI/OpenGL/integration/quad/main.cpp
Normal file
@@ -0,0 +1,230 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLSwapChain.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLCommandList.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLBuffer.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLVertexArray.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLShader.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLPipelineState.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLTexture.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLSampler.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLScreenshot.h"
|
||||
#include "XCEngine/Debug/Logger.h"
|
||||
#include "XCEngine/Debug/ConsoleLogSink.h"
|
||||
#include "XCEngine/Containers/String.h"
|
||||
|
||||
#pragma comment(lib, "opengl32.lib")
|
||||
|
||||
using namespace XCEngine::RHI;
|
||||
using namespace XCEngine::Debug;
|
||||
using namespace XCEngine::Containers;
|
||||
|
||||
static const int gWidth = 1280;
|
||||
static const int gHeight = 720;
|
||||
|
||||
void Log(const char* format, ...) {
|
||||
char buffer[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buffer, sizeof(buffer), format, args);
|
||||
va_end(args);
|
||||
Logger::Get().Debug(LogCategory::Rendering, String(buffer));
|
||||
}
|
||||
|
||||
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
||||
switch (msg) {
|
||||
case WM_CLOSE:
|
||||
PostQuitMessage(0);
|
||||
break;
|
||||
}
|
||||
return DefWindowProc(hwnd, msg, wParam, lParam);
|
||||
}
|
||||
|
||||
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
|
||||
Logger::Get().Initialize();
|
||||
Logger::Get().AddSink(std::make_unique<ConsoleLogSink>());
|
||||
Logger::Get().SetMinimumLevel(LogLevel::Debug);
|
||||
|
||||
Log("[INFO] OpenGL Quad Test Starting");
|
||||
|
||||
WNDCLASSEXW wc = {};
|
||||
wc.cbSize = sizeof(WNDCLASSEXW);
|
||||
wc.style = CS_HREDRAW | CS_VREDRAW;
|
||||
wc.lpfnWndProc = WindowProc;
|
||||
wc.hInstance = hInstance;
|
||||
wc.lpszClassName = L"XCEngine_OpenGL_Quad";
|
||||
|
||||
if (!RegisterClassExW(&wc)) {
|
||||
Log("[ERROR] Failed to register window class");
|
||||
return -1;
|
||||
}
|
||||
|
||||
RECT rect = { 0, 0, gWidth, gHeight };
|
||||
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
|
||||
|
||||
HWND hwnd = CreateWindowExW(
|
||||
0,
|
||||
L"XCEngine_OpenGL_Quad",
|
||||
L"OpenGL Quad Test",
|
||||
WS_OVERLAPPEDWINDOW,
|
||||
CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
rect.right - rect.left, rect.bottom - rect.top,
|
||||
NULL, NULL, hInstance, NULL
|
||||
);
|
||||
|
||||
if (!hwnd) {
|
||||
Log("[ERROR] Failed to create window");
|
||||
return -1;
|
||||
}
|
||||
|
||||
OpenGLDevice device;
|
||||
if (!device.InitializeWithExistingWindow(hwnd)) {
|
||||
Log("[ERROR] Failed to initialize OpenGL device");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ShowWindow(hwnd, nShowCmd);
|
||||
UpdateWindow(hwnd);
|
||||
|
||||
Log("[INFO] OpenGL Device: %S", device.GetDeviceInfo().renderer.c_str());
|
||||
Log("[INFO] OpenGL Version: %S", device.GetDeviceInfo().version.c_str());
|
||||
|
||||
OpenGLSwapChain swapChain;
|
||||
swapChain.Initialize(hwnd, gWidth, gHeight);
|
||||
|
||||
OpenGLCommandList commandList;
|
||||
|
||||
struct Vertex {
|
||||
float pos[4];
|
||||
float texcoord[2];
|
||||
};
|
||||
|
||||
Vertex vertices[] = {
|
||||
{ { -0.5f, -0.5f, 0.0f, 1.0f }, { 0.0f, 1.0f } },
|
||||
{ { -0.5f, 0.5f, 0.0f, 1.0f }, { 0.0f, 0.0f } },
|
||||
{ { 0.5f, -0.5f, 0.0f, 1.0f }, { 1.0f, 1.0f } },
|
||||
{ { 0.5f, 0.5f, 0.0f, 1.0f }, { 1.0f, 0.0f } },
|
||||
};
|
||||
|
||||
OpenGLBuffer vertexBuffer;
|
||||
if (!vertexBuffer.InitializeVertexBuffer(vertices, sizeof(vertices))) {
|
||||
Log("[ERROR] Failed to initialize vertex buffer");
|
||||
return -1;
|
||||
}
|
||||
vertexBuffer.SetStride(sizeof(Vertex));
|
||||
|
||||
OpenGLVertexArray vertexArray;
|
||||
vertexArray.Initialize();
|
||||
|
||||
VertexAttribute posAttr = {};
|
||||
posAttr.index = 0;
|
||||
posAttr.count = 4;
|
||||
posAttr.type = GL_FLOAT;
|
||||
posAttr.normalized = GL_FALSE;
|
||||
posAttr.stride = sizeof(Vertex);
|
||||
posAttr.offset = 0;
|
||||
vertexArray.AddVertexBuffer(vertexBuffer.GetID(), posAttr);
|
||||
|
||||
VertexAttribute texAttr = {};
|
||||
texAttr.index = 1;
|
||||
texAttr.count = 2;
|
||||
texAttr.type = GL_FLOAT;
|
||||
texAttr.normalized = GL_FALSE;
|
||||
texAttr.stride = sizeof(Vertex);
|
||||
texAttr.offset = sizeof(float) * 4;
|
||||
vertexArray.AddVertexBuffer(vertexBuffer.GetID(), texAttr);
|
||||
|
||||
OpenGLShader shader;
|
||||
if (!shader.CompileFromFile("Res/Shader/quad.vert", "Res/Shader/quad.frag")) {
|
||||
Log("[ERROR] Failed to compile shaders");
|
||||
return -1;
|
||||
}
|
||||
Log("[INFO] Shaders compiled successfully");
|
||||
|
||||
shader.SetInt("uTexture", 0);
|
||||
|
||||
OpenGLPipelineState pipelineState;
|
||||
OpenGLRasterizerState rasterizerState;
|
||||
rasterizerState.cullFaceEnable = false;
|
||||
pipelineState.SetRasterizerState(rasterizerState);
|
||||
|
||||
OpenGLDepthStencilState depthStencilState;
|
||||
depthStencilState.depthTestEnable = false;
|
||||
depthStencilState.depthWriteEnable = false;
|
||||
pipelineState.SetDepthStencilState(depthStencilState);
|
||||
|
||||
ViewportState viewportState = { 0.0f, 0.0f, (float)gWidth, (float)gHeight, 0.0f, 1.0f };
|
||||
pipelineState.SetViewport(viewportState);
|
||||
|
||||
pipelineState.AttachShader(shader.GetID());
|
||||
pipelineState.Apply();
|
||||
|
||||
OpenGLTexture texture;
|
||||
if (!texture.LoadFromFile("Res/Image/earth.png", true)) {
|
||||
Log("[ERROR] Failed to load texture");
|
||||
return -1;
|
||||
}
|
||||
Log("[INFO] Texture loaded successfully");
|
||||
|
||||
OpenGLSampler sampler;
|
||||
OpenGLSamplerDesc samplerDesc = {};
|
||||
samplerDesc.minFilter = SamplerFilter::Linear;
|
||||
samplerDesc.magFilter = SamplerFilter::Linear;
|
||||
samplerDesc.wrapS = SamplerWrapMode::ClampToEdge;
|
||||
samplerDesc.wrapT = SamplerWrapMode::ClampToEdge;
|
||||
sampler.Initialize(samplerDesc);
|
||||
|
||||
MSG msg = {};
|
||||
int frameCount = 0;
|
||||
const int targetFrameCount = 30;
|
||||
|
||||
while (frameCount < targetFrameCount) {
|
||||
if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
|
||||
if (msg.message == WM_QUIT) {
|
||||
break;
|
||||
}
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessageW(&msg);
|
||||
} else {
|
||||
commandList.SetViewport(0, 0, gWidth, gHeight);
|
||||
commandList.Clear(0.0f, 0.0f, 1.0f, 1.0f, 1);
|
||||
|
||||
pipelineState.Bind();
|
||||
vertexArray.Bind();
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
texture.Bind(0);
|
||||
sampler.Bind(0);
|
||||
|
||||
commandList.Draw(PrimitiveType::TriangleStrip, 4, 0);
|
||||
|
||||
swapChain.Present(0, 0);
|
||||
frameCount++;
|
||||
}
|
||||
}
|
||||
|
||||
Log("[INFO] Reached target frame count %d - taking screenshot!", targetFrameCount);
|
||||
OpenGLScreenshot::Capture(device, swapChain, "quad.ppm");
|
||||
|
||||
sampler.Shutdown();
|
||||
texture.Shutdown();
|
||||
vertexArray.Shutdown();
|
||||
vertexBuffer.Shutdown();
|
||||
shader.Shutdown();
|
||||
pipelineState.Shutdown();
|
||||
|
||||
swapChain.Shutdown();
|
||||
device.Shutdown();
|
||||
Logger::Get().Shutdown();
|
||||
|
||||
Log("[INFO] OpenGL Quad Test Finished");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user