0

I have some code to render GUI's, and if I dont use a vertex shader then it renders exactly where its meant to : Working as intended

However, as soon as I use a vertex shader, even simply one that calls

gl_position = vec4(position,1.0);

It hides, or moves, or otherwise makes my GUI disappear

Whats the correct way to have a shader for GUIs in OpenGL?

GUI render :

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(0, width, 0, height, -10, 10);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glDisable(GL_CULL_FACE);
    glDisable(GL_DEPTH_TEST);
    RendererUtils.setWireframeMode(false);
    for (Interface i : interfaces)
    {
        i.updateShaderForThisB();
        if (i instanceof InterfaceContainer)
        {
            ((InterfaceContainer) i).draw();
        }
        else
        {
            ((InterfaceControl) i).draw();
        }           
    }
    InterfaceShader.getInstance().unbind();
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

InterfaceContainer and InterfaceControl's draw call is largely the same, so I'll only add one of them.

InterfaceControl.draw()

public void draw()
{
    this.updateShaderForThisB();
    this.getMesh().draw();
    if (this.hasText)
    {
        //this.updateShaderForThisF();
        //drawText();
    }

}

InterfaceControl.updateShaderForThisB()

public void updateShaderForThisB()
{
    InterfaceShader shader = InterfaceShader.getInstance();
    shader.bind();
    shader.setColour(this.getActingColour());
    shader.setLocation(this.getLocation());
    shader.setSize(this.getBounds());
    shader.setGradient(this.getShouldGradient());
    shader.updateUniforms();
}

Mesh.draw()

public void draw()
{
    glEnableVertexAttribArray(0); //Vertices
    glEnableVertexAttribArray(1); //Tex coords
    glEnableVertexAttribArray(2); //Normals

    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, false, Vertex.SIZE * 4, 12);
    glVertexAttribPointer(2, 3, GL_FLOAT, false, Vertex.SIZE * 4, 20);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT,0);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);
}

InterfaceShader.vs

#version 330

layout(location = 0) in vec3 position;
layout(location = 1) in vec2 texCoord;
uniform mat4 viewMatrix;

out vec2 texCoord0;

void main()
{
    gl_Position = viewMatrix * vec4(position,1.0);
    texCoord0 = texCoord;
}

Can anyone see an obvious problem that i've overlooked? My first thought was that the shader was translating my interface-oriented coordinates (ie 50,50,1) into real world coordinates, but I dont know

Edit : As requested, updated shader code and added matrix projection code

https://pastebin.com/gKdewDVi

pastebin for Transform class, for getting the view matrix and such, and how it's related back to the shader

DuskFall
  • 422
  • 2
  • 14
  • Knew it would be something simple, my other shader works fine but i thought i wouldnt need any matrices in this one because i didnt want any transformations to happen on the vertices – DuskFall Nov 22 '17 at 18:51
  • The first step is to replace with "#version 130\n\n gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;", which should give the same result as without vertex shader; and then to change to a higher version and uniform parameters instead of the matrices stacks. – Sebastian Nov 26 '21 at 09:56

1 Answers1

2

The fixed function vertex processing pipeline multiplies the vertex positions by the current matrix given by GL_PROJECTION * GL_MODELVIEW. You have to do the same thing in your vertex shader like so:

gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0);

Please note that you are using outdated OpenGL programming practices by using the fixed-function processing. This has been deprecated for a while now.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
bernie
  • 9,820
  • 5
  • 62
  • 92
  • I'm passing in the transformed view matrix and now its rendering in worldspace rather than in your face, do I need to apply a transform to each individual part of the interface and bind that per draw, or can I specify a single transformation, send that to the shader, and then have that applied until I change it? – DuskFall Nov 22 '17 at 19:20
  • please update your question with your vertex shader code – bernie Nov 22 '17 at 20:16