From 31e2447db4f87e6ba2ab99925813db5bade7de94 Mon Sep 17 00:00:00 2001 From: baldurk Date: Fri, 21 Feb 2020 15:39:51 +0000 Subject: [PATCH] Extract out some common code from GL_Simple_Triangle --- util/test/demos/gl/gl_simple_triangle.cpp | 55 +--------------------- util/test/demos/gl/gl_test.cpp | 56 +++++++++++++++++++++++ util/test/demos/gl/gl_test.h | 5 ++ 3 files changed, 63 insertions(+), 53 deletions(-) diff --git a/util/test/demos/gl/gl_simple_triangle.cpp b/util/test/demos/gl/gl_simple_triangle.cpp index 76569c90a..681fd1410 100644 --- a/util/test/demos/gl/gl_simple_triangle.cpp +++ b/util/test/demos/gl/gl_simple_triangle.cpp @@ -30,50 +30,6 @@ RD_TEST(GL_Simple_Triangle, OpenGLGraphicsTest) "Just draws a simple triangle, using normal pipeline. Basic test that can be used " "for any dead-simple tests that don't require any particular API use"; - std::string common = R"EOSHADER( - -#version 420 core - -#define v2f v2f_block \ -{ \ - vec4 pos; \ - vec4 col; \ - vec4 uv; \ -} - -)EOSHADER"; - - std::string vertex = R"EOSHADER( - -layout(location = 0) in vec3 Position; -layout(location = 1) in vec4 Color; -layout(location = 2) in vec2 UV; - -out v2f vertOut; - -void main() -{ - vertOut.pos = vec4(Position.xyz, 1); - gl_Position = vertOut.pos; - vertOut.col = Color; - vertOut.uv = vec4(UV.xy, 0, 1); -} - -)EOSHADER"; - - std::string pixel = R"EOSHADER( - -in v2f vertIn; - -layout(location = 0, index = 0) out vec4 Color; - -void main() -{ - Color = vertIn.col; -} - -)EOSHADER"; - int main() { // initialise, create window, create context, etc @@ -87,16 +43,9 @@ void main() glBindBuffer(GL_ARRAY_BUFFER, vb); glBufferStorage(GL_ARRAY_BUFFER, sizeof(DefaultTri), DefaultTri, 0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(DefaultA2V), (void *)(0)); - glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(DefaultA2V), (void *)(sizeof(Vec3f))); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(DefaultA2V), - (void *)(sizeof(Vec3f) + sizeof(Vec4f))); + ConfigureDefaultVAO(); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - - GLuint program = MakeProgram(common + vertex, common + pixel); + GLuint program = MakeProgram(GLDefaultVertex, GLDefaultPixel); // make a simple texture so that the structured data includes texture initial states GLuint tex = MakeTexture(); diff --git a/util/test/demos/gl/gl_test.cpp b/util/test/demos/gl/gl_test.cpp index 0406f2be3..581ab7d46 100644 --- a/util/test/demos/gl/gl_test.cpp +++ b/util/test/demos/gl/gl_test.cpp @@ -25,6 +25,50 @@ #include "gl_test.h" #include +std::string common = R"EOSHADER( + +#version 420 core + +#define v2f v2f_block \ +{ \ + vec4 pos; \ + vec4 col; \ + vec4 uv; \ +} + +)EOSHADER"; + +std::string GLDefaultVertex = common + R"EOSHADER( + +layout(location = 0) in vec3 Position; +layout(location = 1) in vec4 Color; +layout(location = 2) in vec2 UV; + +out v2f vertOut; + +void main() +{ + vertOut.pos = vec4(Position.xyz, 1); + gl_Position = vertOut.pos; + vertOut.col = Color; + vertOut.uv = vec4(UV.xy, 0, 1); +} + +)EOSHADER"; + +std::string GLDefaultPixel = common + R"EOSHADER( + +in v2f vertIn; + +layout(location = 0, index = 0) out vec4 Color; + +void main() +{ + Color = vertIn.col; +} + +)EOSHADER"; + static void APIENTRY debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam) { @@ -299,6 +343,18 @@ GLuint OpenGLGraphicsTest::MakeFBO() return fbos[fbos.size() - 1]; } +void OpenGLGraphicsTest::ConfigureDefaultVAO() +{ + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(DefaultA2V), (void *)(0)); + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(DefaultA2V), (void *)(sizeof(Vec3f))); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(DefaultA2V), + (void *)(sizeof(Vec3f) + sizeof(Vec4f))); + + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); +} + void OpenGLGraphicsTest::pushMarker(const std::string &name) { if(glPushDebugGroup) diff --git a/util/test/demos/gl/gl_test.h b/util/test/demos/gl/gl_test.h index b7900315c..1beea9119 100644 --- a/util/test/demos/gl/gl_test.h +++ b/util/test/demos/gl/gl_test.h @@ -53,6 +53,8 @@ struct OpenGLGraphicsTest : public GraphicsTest GLuint MakeVAO(); GLuint MakeFBO(); + void ConfigureDefaultVAO(); + void pushMarker(const std::string &name); void setMarker(const std::string &name); void popMarker(); @@ -74,3 +76,6 @@ struct OpenGLGraphicsTest : public GraphicsTest std::vector bufs, texs, progs, pipes, vaos, fbos; } managedResources; }; + +extern std::string GLDefaultVertex; +extern std::string GLDefaultPixel;