0
\$\begingroup\$

I'm using a scenegraph with the visitor pattern to draw sprites in my 2D GameEngine. It works great.

Now I'd like to render the lines of the cube the sprite is rendered onto.

// initialization somewhere
GLfloat vertices[] = {
// Pos      // Tex
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,

0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f
};

 // the actual drawing
bool SpriteRenderer::Visit(GameObject & node)
{       
        m_matrixStack.push_back(m_modelMatrix);

        // projection & view matrix have already been set
        // ... calculate modelMatrix

        m_shader->Use();
        m_shader->SetMatrix4("model", m_modelMatrix);
        m_shader->SetVector3f("spriteColor", textureComponent->GetColor());

        glActiveTexture(GL_TEXTURE0);
        textureComponent->Bind();

        glBindVertexArray(m_quadVAO);
        glDrawArrays(GL_TRIANGLES, 0, 6);

        // I'd like to draw the outline here

        glBindVertexArray(0);

        m_modelMatrix = m_matrixStack.back();
        m_matrixStack.pop_back();

        return true;
}

I've tried the following at the " //I'd like to draw the outline here" -position:

       // projection & viewmatrix have been set at the same time as with the 
       // normal spriteShader program

        glActiveTexture(0);
        m_ShapeShader->Use();
        m_ShapeShader->SetMatrix4("model", m_modelMatrix);
        m_ShapeShader->SetVector3f("spriteColor", glm::vec3(1, 0, 0));
        glDrawArrays(GL_LINES, 0, 6);

The ShapeShader program consists of the following shaders :

#vertex
#version 330 core
layout (location = 0) in vec4 vertex; // <vec2 position, vec2 texCoords>

uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;

void main()
{
    gl_Position = projection * view* model * vec4(vertex.xy, 0.0, 1.0);
}

#fragment
#version 330 core    
out vec4 color;

uniform vec3 spriteColor;

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

The result were no changes ( no outline ) . Switching from GL_LINES to GL_POINTS yielded to a single static red pixel on the screen.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.