]> git.tdb.fi Git - geometrycompositor.git/commitdiff
Some refactoring of OpenGL code
authorMikko Rasa <tdb@tdb.fi>
Sat, 12 Dec 2015 11:17:02 +0000 (13:17 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 12 Dec 2015 11:27:41 +0000 (13:27 +0200)
In preparation for geometry correction

source/main.c

index 0992542fa960d3bcf1ef400b3685925b1b4cf98e..f4704aead4e0aef22c003bd38cc1507fc7d9c85f 100644 (file)
@@ -63,29 +63,31 @@ static const char *vshader =
        "#version 150\n"
        "uniform vec4 geometry;\n"
        "in vec2 vertex;\n"
+       "in vec2 texture_coord;\n"
        "out vec2 texcoord;\n"
        "void main()\n"
        "{\n"
        "  gl_Position = vec4((geometry.xy+vertex*geometry.zw)*2.0-1.0, 0.0, 1.0);\n"
-       "  texcoord = vec2(vertex.x, 1.0-vertex.y);\n"
+       "  texcoord = texture_coord;\n"
        "}\n";
 
 static const char *fshader =
        "#version 150\n"
-       "uniform sampler2D window;\n"
+       "uniform sampler2D image;\n"
        "in vec2 texcoord;\n"
        "out vec4 frag_color;\n"
        "void main()\n"
        "{\n"
-       "       frag_color = texture(window, texcoord);\n"
+       "       frag_color = texture(image, texcoord);\n"
        "}\n";
 
 static const float vertices[] =
 {
-       0.0f, 1.0f,
-       0.0f, 0.0f,
-       1.0f, 1.0f,
-       1.0f, 0.0f
+       /* vertex    texcoord */
+       0.0f, 1.0f,  0.0f, 0.0f,
+       0.0f, 0.0f,  0.0f, 1.0f,
+       1.0f, 1.0f,  1.0f, 0.0f,
+       1.0f, 0.0f,  1.0f, 1.0f
 };
 
 int terminate_requested = 0;
@@ -193,6 +195,8 @@ unsigned link_program(unsigned *shaders, unsigned nshaders)
        for(i=0; i<nshaders; ++i)
                glAttachShader(program, shaders[i]);
        glBindAttribLocation(program, 0, "vertex");
+       glBindAttribLocation(program, 1, "texture_coord");
+       glBindFragDataLocation(program, 0, "frag_color");
        glLinkProgram(program);
 
        glGetProgramiv(program, GL_LINK_STATUS, &status);
@@ -211,6 +215,8 @@ unsigned link_program(unsigned *shaders, unsigned nshaders)
 
 int create_gl_resources(Compositor *compositor, CompositedScreen *screen)
 {
+       unsigned stride;
+
        use_gl(compositor, screen);
 
        screen->shaders[0] = compile_shader(GL_VERTEX_SHADER, vshader);
@@ -228,10 +234,14 @@ int create_gl_resources(Compositor *compositor, CompositedScreen *screen)
        glBindBuffer(GL_ARRAY_BUFFER, screen->vertex_buffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
 
+       stride = 4*sizeof(float);
        glGenVertexArrays(1, &screen->vertex_array);
        glBindVertexArray(screen->vertex_array);
-       glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
+       glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, stride, NULL);
        glEnableVertexAttribArray(0);
+       glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void *)(2*sizeof(float)));
+       glEnableVertexAttribArray(1);
+       glBindVertexArray(0);
 
        glBindBuffer(GL_ARRAY_BUFFER, 0);