Files
XCEngine/tests/RHI/OpenGL/unit/test_vertex_array.cpp
ssdfasd 1797e7fe17 fix: encapsulate OpenGL types in VertexAttribute to eliminate raw GL API usage in tests
- Add VertexAttributeType and VertexAttributeNormalized enums in OpenGLVertexArray.h
- Add ToGLAttributeType() converter in OpenGLVertexArray.cpp
- Remove glActiveTexture() call from quad test (already handled by texture.Bind())
- Remove #include <glad/glad.h> from triangle test
- Update unit tests to use encapsulated enums

All three OpenGL integration tests (minimal, triangle, quad) pass with 0% pixel difference.
2026-03-22 14:33:57 +08:00

116 lines
3.0 KiB
C++

#include "fixtures/OpenGLTestFixture.h"
#include "XCEngine/RHI/OpenGL/OpenGLVertexArray.h"
#include "XCEngine/RHI/OpenGL/OpenGLBuffer.h"
using namespace XCEngine::RHI;
TEST_F(OpenGLTestFixture, VertexArray_Initialize_CreatesVAO) {
OpenGLVertexArray vao;
bool result = vao.Initialize();
ASSERT_TRUE(result);
EXPECT_NE(vao.GetID(), 0u);
vao.Shutdown();
}
TEST_F(OpenGLTestFixture, VertexArray_AddVertexBuffer) {
OpenGLVertexArray vao;
vao.Initialize();
OpenGLBuffer buffer;
buffer.InitializeVertexBuffer(nullptr, 64);
VertexAttribute attr;
attr.index = 0;
attr.count = 3;
attr.type = VertexAttributeType::Float;
attr.normalized = VertexAttributeNormalized::False;
attr.stride = sizeof(float) * 3;
attr.offset = 0;
vao.AddVertexBuffer(buffer.GetID(), attr);
EXPECT_EQ(vao.GetID(), 1u);
buffer.Shutdown();
vao.Shutdown();
}
TEST_F(OpenGLTestFixture, VertexArray_SetIndexBuffer) {
OpenGLVertexArray vao;
vao.Initialize();
OpenGLBuffer buffer;
buffer.InitializeIndexBuffer(nullptr, 12);
vao.SetIndexBuffer(buffer.GetID(), GL_UNSIGNED_INT);
EXPECT_NE(vao.GetIndexBuffer(), 0u);
buffer.Shutdown();
vao.Shutdown();
}
TEST_F(OpenGLTestFixture, VertexArray_Bind_Unbind) {
OpenGLVertexArray vao;
vao.Initialize();
vao.Bind();
GLint boundVAO = 0;
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &boundVAO);
EXPECT_EQ(boundVAO, static_cast<GLint>(vao.GetID()));
vao.Unbind();
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &boundVAO);
EXPECT_EQ(boundVAO, 0);
vao.Shutdown();
}
TEST_F(OpenGLTestFixture, VertexArray_Bind_MultipleAttributes) {
OpenGLVertexArray vao;
vao.Initialize();
OpenGLBuffer buffer1;
float positions[] = { 0.0f, 0.0f, 0.5f, 0.5f };
buffer1.InitializeVertexBuffer(positions, sizeof(positions));
OpenGLBuffer buffer2;
float colors[] = { 1.0f, 0.0f, 0.0f, 1.0f };
buffer2.InitializeVertexBuffer(colors, sizeof(colors));
VertexAttribute attrPos;
attrPos.index = 0;
attrPos.count = 2;
attrPos.type = VertexAttributeType::Float;
attrPos.normalized = VertexAttributeNormalized::False;
attrPos.stride = sizeof(float) * 2;
attrPos.offset = 0;
VertexAttribute attrColor;
attrColor.index = 1;
attrColor.count = 4;
attrColor.type = VertexAttributeType::Float;
attrColor.normalized = VertexAttributeNormalized::False;
attrColor.stride = sizeof(float) * 4;
attrColor.offset = 0;
vao.AddVertexBuffer(buffer1.GetID(), attrPos);
vao.AddVertexBuffer(buffer2.GetID(), attrColor);
vao.Bind();
GLint boundVAO = 0;
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &boundVAO);
EXPECT_EQ(boundVAO, static_cast<GLint>(vao.GetID()));
vao.Unbind();
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &boundVAO);
EXPECT_EQ(boundVAO, 0);
buffer1.Shutdown();
buffer2.Shutdown();
vao.Shutdown();
}