]> git.sesse.net Git - movit/commitdiff
Make the demo program run with core contexts.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 7 Oct 2015 18:12:25 +0000 (20:12 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 7 Oct 2015 18:12:25 +0000 (20:12 +0200)
Also, if SDL2 is in use, actually _ask_ for a core context.

color.130.frag [new file with mode: 0644]
color.150.frag [new file with mode: 0644]
demo.cpp
vs-color.130.vert [new file with mode: 0644]
vs-color.150.vert [new file with mode: 0644]
widgets.cpp
widgets.h

diff --git a/color.130.frag b/color.130.frag
new file mode 100644 (file)
index 0000000..d546844
--- /dev/null
@@ -0,0 +1,11 @@
+#version 130
+
+in vec2 tc;
+in vec4 frag_color;
+
+out vec4 FragColor;
+
+void main()
+{
+       FragColor = frag_color;
+}
diff --git a/color.150.frag b/color.150.frag
new file mode 100644 (file)
index 0000000..9d3d2db
--- /dev/null
@@ -0,0 +1,11 @@
+#version 150
+
+in vec2 tc;
+in vec4 frag_color;
+
+out vec4 FragColor;
+
+void main()
+{
+       FragColor = frag_color;
+}
index ba96291f6636f4270fe8714951fff88c6fa0bd8c..dbbb4cba01ca5267413c4f3b56d58c624c84102f 100644 (file)
--- a/demo.cpp
+++ b/demo.cpp
@@ -191,6 +191,9 @@ int main(int argc, char **argv)
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 
 #ifdef HAVE_SDL2
+       SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
+       SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
+       SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
        // SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
        SDL_Window *window = SDL_CreateWindow("OpenGL window",
                SDL_WINDOWPOS_UNDEFINED,
@@ -223,13 +226,6 @@ int main(int argc, char **argv)
        EffectChain chain(WIDTH, HEIGHT);
        glViewport(0, 0, WIDTH, HEIGHT);
 
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
-
-       glMatrixMode(GL_MODELVIEW);
-       glLoadIdentity();
-
        ImageFormat inout_format;
        inout_format.color_space = COLORSPACE_sRGB;
        inout_format.gamma_curve = GAMMA_sRGB;
@@ -254,7 +250,8 @@ int main(int argc, char **argv)
        glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
        glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
 
-       make_hsv_wheel_texture();
+       init_hsv_resources();
+       check_error();
 
        int frame = 0;
        bool screenshot = false;
@@ -302,7 +299,6 @@ int main(int argc, char **argv)
                glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
                check_error();
 
-               glLoadIdentity();
                draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
                draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
                draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
@@ -361,5 +357,6 @@ int main(int argc, char **argv)
                }
 #endif
        }
+       cleanup_hsv_resources();
        return 0; 
 }
diff --git a/vs-color.130.vert b/vs-color.130.vert
new file mode 100644 (file)
index 0000000..2f46e2c
--- /dev/null
@@ -0,0 +1,17 @@
+#version 130
+
+in vec2 position;
+in vec4 color;
+out vec4 frag_color;
+
+void main()
+{
+       // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is:
+       //
+       //   2.000  0.000  0.000 -1.000
+       //   0.000  2.000  0.000 -1.000
+       //   0.000  0.000 -2.000 -1.000
+       //   0.000  0.000  0.000  1.000
+       gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0);
+       frag_color = color;
+}
diff --git a/vs-color.150.vert b/vs-color.150.vert
new file mode 100644 (file)
index 0000000..0bdd74d
--- /dev/null
@@ -0,0 +1,17 @@
+#version 150
+
+in vec2 position;
+in vec4 color;
+out vec4 frag_color;
+
+void main()
+{
+       // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is:
+       //
+       //   2.000  0.000  0.000 -1.000
+       //   0.000  2.000  0.000 -1.000
+       //   0.000  0.000 -2.000 -1.000
+       //   0.000  0.000  0.000  1.000
+       gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0);
+       frag_color = color;
+}
index 87260215e2b1b4bb3f2e916c98e9e1d4adb37a37..529d902e878443c704ae5721a78aa1f4df40b34b 100644 (file)
@@ -1,6 +1,7 @@
 #include <epoxy/gl.h>
 #include <math.h>
 
+#include "resource_pool.h"
 #include "widgets.h"
 #include "util.h"
 
 
 namespace movit {
 
-GLuint hsv_wheel_num;
+GLuint hsv_wheel_texnum = 0;
+GLuint textured_program_num = 0, colored_program_num = 0, hsv_vao = 0;
+ResourcePool resource_pool;
 
-void draw_hsv_wheel(float y, float rad, float theta, float value)
+void draw_black_point(float x, float y, float point_size)
 {
-       glUseProgram(0);
-       check_error();
-       glActiveTexture(GL_TEXTURE0);
+       glUseProgram(colored_program_num);
        check_error();
-       glEnable(GL_TEXTURE_2D);
+
+       float vertices[] = { x, y };
+       float colors[] = { 0.0f, 0.0f, 0.0f };
+
+       glPointSize(point_size);
        check_error();
-       glBindTexture(GL_TEXTURE_2D, hsv_wheel_num);
+       GLuint position_vbo = fill_vertex_attribute(colored_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
+       GLuint color_vbo = fill_vertex_attribute(colored_program_num, "color", 3, GL_FLOAT, sizeof(colors), colors);
        check_error();
-       glActiveTexture(GL_TEXTURE1);
+       glDrawArrays(GL_POINTS, 0, 1);
        check_error();
-       glBindTexture(GL_TEXTURE_2D, 0);
+       cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
+       cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
+}
+
+void draw_hsv_wheel(float y, float rad, float theta, float value)
+{
+       glUseProgram(textured_program_num);
        check_error();
        glActiveTexture(GL_TEXTURE0);
+       check_error();
+       glBindTexture(GL_TEXTURE_2D, hsv_wheel_texnum);
+       check_error();
+       glUniform1i(glGetUniformLocation(textured_program_num, "tex"), 0);  // Bind the 2D sampler.
+       check_error();
        glEnable(GL_BLEND);
        check_error();
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        check_error();
 
-       // wheel
-       glBegin(GL_QUADS);
-
-       glTexCoord2f(0.0f, 1.0f);
-       glVertex2f(0.0f, y);
-
-       glTexCoord2f(1.0f, 1.0f);
-       glVertex2f(0.2f * 9.0f / 16.0f, y);
+       GLuint vao;
+       glGenVertexArrays(1, &vao);
+       check_error();
+       glBindVertexArray(vao);
+       check_error();
 
-       glTexCoord2f(1.0f, 0.0f);
-       glVertex2f(0.2f * 9.0f / 16.0f, y + 0.2f);
+       // wheel
+       float wheel_vertices[] = {
+               0.0f, y,
+               0.0f, y + 0.2f,
+               0.2f * 9.0f / 16.0f, y,
+               0.2f * 9.0f / 16.0f, y + 0.2f,
+       };
+       float wheel_texcoords[] = {
+               0.0f, 1.0f,
+               0.0f, 0.0f,
+               1.0f, 1.0f,
+               1.0f, 0.0f,
+       };
+       GLuint position_vbo = fill_vertex_attribute(textured_program_num, "position", 2, GL_FLOAT, sizeof(wheel_vertices), wheel_vertices);
+       GLuint texcoord_vbo = fill_vertex_attribute(textured_program_num, "texcoord", 2, GL_FLOAT, sizeof(wheel_texcoords), wheel_texcoords);
+       check_error();
 
-       glTexCoord2f(0.0f, 0.0f);
-       glVertex2f(0.0f, y + 0.2f);
+       glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+       check_error();
 
-       glEnd();
+       cleanup_vertex_attribute(textured_program_num, "position", position_vbo);
+       cleanup_vertex_attribute(textured_program_num, "texcoord", texcoord_vbo);
 
        // wheel selector
-       glDisable(GL_TEXTURE_2D);
-       glColor3f(0.0f, 0.0f, 0.0f);
-       glPointSize(5.0f);
-       glBegin(GL_POINTS);
-       glVertex2f((0.1f + rad * cos(theta) * 0.1f) * 9.0f / 16.0f, y + 0.1f - rad * sin(theta) * 0.1f);
-       glEnd();
-       
-       // value slider
-       glDisable(GL_TEXTURE_2D);
-       glBegin(GL_QUADS);
-
-       glColor3f(0.0f, 0.0f, 0.0f);
-       glVertex2f(0.22f * 9.0f / 16.0f, y);
-       glVertex2f(0.24f * 9.0f / 16.0f, y);
+       draw_black_point(
+           (0.1f + rad * cos(theta) * 0.1f) * 9.0f / 16.0f,
+           y + 0.1f - rad * sin(theta) * 0.1f,
+           5.0f);
 
-       glColor3f(1.0f, 1.0f, 1.0f);
-       glVertex2f(0.24f * 9.0f / 16.0f, y + 0.2f);
-       glVertex2f(0.22f * 9.0f / 16.0f, y + 0.2f);
-
-       glEnd();
+       // value slider
+       glUseProgram(colored_program_num);
+       float value_vertices[] = {
+               0.22f * 9.0f / 16.0f, y,
+               0.22f * 9.0f / 16.0f, y + 0.2f,
+               0.24f * 9.0f / 16.0f, y,
+               0.24f * 9.0f / 16.0f, y + 0.2f,
+       };
+       float value_colors[] = {
+               0.0f, 0.0f, 0.0f,
+               1.0f, 1.0f, 1.0f,
+               0.0f, 0.0f, 0.0f,
+               1.0f, 1.0f, 1.0f,
+       };
+       position_vbo = fill_vertex_attribute(colored_program_num, "position", 2, GL_FLOAT, sizeof(value_vertices), value_vertices);
+       GLuint color_vbo = fill_vertex_attribute(colored_program_num, "color", 3, GL_FLOAT, sizeof(value_colors), value_colors);
+       check_error();
+       glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+       check_error();
+       cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
+       cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
 
        // value selector
-       glColor3f(0.0f, 0.0f, 0.0f);
-       glPointSize(5.0f);
-       glBegin(GL_POINTS);
-       glVertex2f(0.23f * 9.0f / 16.0f, y + value * 0.2f);
-       glEnd();
+       draw_black_point(0.23f * 9.0f / 16.0f, y + value * 0.2f, 5.0f);
 
-       glColor3f(1.0f, 1.0f, 1.0f);
+       glDeleteVertexArrays(1, &vao);
+       check_error();
+       glUseProgram(0);
+       check_error();
 }
 
 void draw_saturation_bar(float y, float saturation)
 {
-       glUseProgram(0);
+       GLuint vao;
+       glGenVertexArrays(1, &vao);
+       check_error();
+       glBindVertexArray(vao);
        check_error();
 
        // value slider
-       glDisable(GL_TEXTURE_2D);
-       glBegin(GL_QUADS);
-
-       glColor3f(0.0f, 0.0f, 0.0f);
-       glVertex2f(0.0f * 9.0f / 16.0f, y + 0.02f);
-       glVertex2f(0.0f * 9.0f / 16.0f, y);
-
-       glColor3f(1.0f, 1.0f, 1.0f);
-       glVertex2f(0.2f * 9.0f / 16.0f, y);
-       glVertex2f(0.2f * 9.0f / 16.0f, y + 0.02f);
-
-       glEnd();
+       glUseProgram(colored_program_num);
+       float value_vertices[] = {
+               0.0f * 9.0f / 16.0f, y + 0.02f,
+               0.2f * 9.0f / 16.0f, y + 0.02f,
+               0.0f * 9.0f / 16.0f, y,
+               0.2f * 9.0f / 16.0f, y,
+       };
+       float value_colors[] = {
+               0.0f, 0.0f, 0.0f,
+               1.0f, 1.0f, 1.0f,
+               0.0f, 0.0f, 0.0f,
+               1.0f, 1.0f, 1.0f,
+       };
+       GLuint position_vbo = fill_vertex_attribute(colored_program_num, "position", 2, GL_FLOAT, sizeof(value_vertices), value_vertices);
+       GLuint color_vbo = fill_vertex_attribute(colored_program_num, "color", 3, GL_FLOAT, sizeof(value_colors), value_colors);
+       check_error();
+       glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
+       check_error();
+       cleanup_vertex_attribute(colored_program_num, "position", position_vbo);
+       cleanup_vertex_attribute(colored_program_num, "color", color_vbo);
 
        // value selector
-       glColor3f(0.0f, 0.0f, 0.0f);
-       glPointSize(5.0f);
-       glBegin(GL_POINTS);
-       glVertex2f(0.2f * saturation * 9.0f / 16.0f, y + 0.01f);
-       glEnd();
+       draw_black_point(0.2f * saturation * 9.0f / 16.0f, y + 0.01f, 5.0f);
 
-       glColor3f(1.0f, 1.0f, 1.0f);
+       glDeleteVertexArrays(1, &vao);
+       check_error();
+       glUseProgram(0);
+       check_error();
 }
 
 void make_hsv_wheel_texture()
 {
-       glGenTextures(1, &hsv_wheel_num);
+       glGenTextures(1, &hsv_wheel_texnum);
 
        static unsigned char hsv_pix[HSV_WHEEL_SIZE * HSV_WHEEL_SIZE * 4];
        for (int y = 0; y < HSV_WHEEL_SIZE; ++y) {
@@ -134,7 +178,7 @@ void make_hsv_wheel_texture()
                }
        }
 
-       glBindTexture(GL_TEXTURE_2D, hsv_wheel_num);
+       glBindTexture(GL_TEXTURE_2D, hsv_wheel_texnum);
        check_error();
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        check_error();
@@ -142,6 +186,23 @@ void make_hsv_wheel_texture()
        check_error();
 }
 
+void init_hsv_resources()
+{
+       textured_program_num = resource_pool.compile_glsl_program(
+               read_version_dependent_file("vs", "vert"),
+               read_version_dependent_file("texture1d", "frag"));
+       colored_program_num = resource_pool.compile_glsl_program(
+               read_version_dependent_file("vs-color", "vert"),
+               read_version_dependent_file("color", "frag"));
+       make_hsv_wheel_texture();
+}
+
+void cleanup_hsv_resources()
+{
+       resource_pool.release_glsl_program(textured_program_num);
+       resource_pool.release_glsl_program(colored_program_num);
+}
+
 void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value)
 {
        if (xf < 0.2f && yf < 0.2f) {
index eb89a743642dfb4e1d03f91a2ff7f8e86005d3df..ddf997ccc3323c55264d86afbee0a73001555a74 100644 (file)
--- a/widgets.h
+++ b/widgets.h
@@ -7,7 +7,8 @@ namespace movit {
 
 void draw_hsv_wheel(float y, float rad, float theta, float value);
 void draw_saturation_bar(float y, float saturation);
-void make_hsv_wheel_texture();
+void init_hsv_resources();
+void cleanup_hsv_resources();
 void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value);
 
 }  // namespace movit