0
\$\begingroup\$

I am using advanced OpenDL methods (VBO, VBA, ...) to render over screen. Currently I abstracted vbo, vba, index buffer, texture, and shader to a class called 'Object2D'.

I tried to render two objects on screen. I saw that when the same shader is applied to both objects it works fine; but if the two objects are rendered with two different shader script, only the one rendered last is visible to screen.

Here is Object2D::render():

void Render()
{
    if(check_status())
    {
        sh.bind(); //shader
        vb.Bind(); //vertex buffer
        ib.Bind(); //index buffer
        tex.bind(); //texture buffer
        glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,NULL);
    }
    else{}
    //sh.unbind();
}

And here is the drawing function (assume the two 2D objects are obj and box):

void display()
{
    glClear(GL_COLOR_BUFFER_BIT); //refresh/..
    box.sh.setUniformMat4x4("m_projection",GL_FALSE,1,&m_PROJ[0][0]);
    box.Render();
    obj.sh.setUniformMat4x4("projection_matrix",GL_FALSE,1,&m_PROJ[0][0]);
    obj.sh.setUniformMat4x4("view_matrix",GL_FALSE,1,&view_transform[0][0]);
    obj.Render();
    glFlush();
}

And this is the shader binded to box:

#shader vertex
#version 330 core 

layout(location = 0) in vec4 positions;
uniform mat4 m_projection;
void main(void)
{
    gl_Position=m_projection*positions;
}

#shader fragment
#version 330 core

layout(location=0) out vec4 color;

void main(void)
{
    color=vec4(1.0,0.4,0.3,1.0);
}

and this shader is binded to obj:

#shader vertex
#version 330 core 

layout(location = 0) in vec4 positions;
layout(location=0) in vec2 texture_coodrinate;
out vec4 p;
out vec2 TexCoord;
uniform mat4 projection_matrix;
uniform mat4 view_matrix;

void main(void)
{
    gl_Position=projection_matrix*view_matrix*positions; //modelvi
    p=projection_matrix*view_matrix*positions;;
    TexCoord=texture_coodrinate;
}

#shader fragment  
#version 330 core

layout(location=0) out vec4 color;
in vec4 p;
in vec2 TexCoord;
uniform vec4 U_color;   
uniform sampler2D tex_;

void main(void)
{
    ;
    U_color;
    color=texture(tex_,TexCoord);
}

My problems are:

  • Two of the objects aren't being rendered if until and unless both the shaders are same
  • Unbinding the shader result in no rendering?
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Before setting up uniforms, you should bind the shader as well (with glUseProgram, i guess that's what the .bind() method does in your case). See GLSL common mistakes.

\$\endgroup\$
2
  • \$\begingroup\$ int bind() { if(isready()) glUseProgram(ID); else return -1; } ID is the unsigned program generated by opengl (shader program) \$\endgroup\$ May 27, 2018 at 13:23
  • \$\begingroup\$ But why the both objects with different shader are not being rendered toughter...i mean one which is last rendered remaines other is missing \$\endgroup\$ May 27, 2018 at 13:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .