Skip to main content
improved code indentation, fixed typos
Source Link
liggiorgio
  • 5.5k
  • 6
  • 27
  • 38

Rendering multiple objectobjects in openglOpenGL

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

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

Here is Object2D::render():

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 drawdrawing function  (assume the two 2D objects are 'obj'obj and 'box'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();
}

andAnd this is the shader binded to box: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);
}
#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);
}

problem: 1.two of the objects aren't being rendered if until and unless both the shaders My problems are same 2.unbinding the shader result in no rendering??:

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

Rendering multiple object in opengl

I am using advance opengl 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.What i saw that when same shader is applied to both object it works fine,but if the two objects are rendered with two different shader script..the one which is rendered last is only 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 draw 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);
}
#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);
}

problem: 1.two of the objects aren't being rendered if until and unless both the shaders are same 2.unbinding the shader result in no rendering??

Rendering multiple objects in OpenGL

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);
}
#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?
Source Link

Rendering multiple object in opengl

I am using advance opengl 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.What i saw that when same shader is applied to both object it works fine,but if the two objects are rendered with two different shader script..the one which is rendered last is only 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 draw 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);
}

problem: 1.two of the objects aren't being rendered if until and unless both the shaders are same 2.unbinding the shader result in no rendering??