简体   繁体   中英

GLSL Shader not rendering anything, VAO is filled, VBO filled with vertices

I have filled VBO with my vertices (2x position, 2x size, 4x uv, 2x texture data(1 float sets correct texture array and 2. sets index of texture in array), still not rendering, what am i missing?

I tried setting geometry shader to render simple quad, nothing showed up, i tried just rendering color in fragment, nothing showed up, I checked OPENGL debug message, nothing there, in vertex shader i checked calculation for my matrix, everything should be fine

Data is being loaded every frame using glBufferData to same buffer, because of resizing, this code is for entities and amount of them on screen can change easily.

Fragment shader:

void main()
{
    texColor = vec4(1.0,0.0,0.0,1.0); //Red fillment
    FragColor = texColor;
}

Vertex shader:

layout:

layout(location = 0) in vec2 position;
layout(location = 1) in vec2 size;
layout(location = 2) in vec4 uv;
layout(location = 3) in vec2 textureData; //1. data is to pick correct atlas, 2. is index of texture in atlas

interface:

out VS_OUT
{
    mat4 screenMatrix;
    vec2 position;
    vec4 uv;
    vec2 textureData;
    vec2 size;
} vs_out;

matrix:

mat4 screenMatrix = mat4((2.0/screenRatio.x),0.0,0.0,0.0,0.0,(2.0/screenRatio.y),0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0);

Geometry Shader:

Layout:

layout (points) in;
layout(triangle_strip,max_vertices = 4) out;

out:

out vec2 TexData;
out vec2 TexCoord;

code:

void renderPlayer(mat4 screenMatrix, vec4 position,vec2 textureData,vec2 size,vec4 uv)
{
    gl_Position = nullPos+((position+vec4(0.0,0.0,0.0,0.0))*screenMatrix); // 1:bottom-left
    TexData = textureData;
    TexCoord = vec2(uv.xy);
    EmitVertex();
    gl_Position = nullPos+((position+vec4(size.x,0.0,0.0,0.0))*screenMatrix); // 2:bottom-right
    TexData = textureData;
    TexCoord = vec2(uv.zy);
    EmitVertex();
    gl_Position = nullPos+((position+vec4(0.0,size.y,0.0,0.0))*screenMatrix); // 3:top-left
    TexData = textureData;
    TexCoord = vec2(uv.xw);
    EmitVertex();
    gl_Position = nullPos+((position+vec4(size.x,size.y,0.0,0.0))*screenMatrix);  // 4:top-right 
    TexData = textureData;
    TexCoord = vec2(uv.zw);
    EmitVertex();
    EndPrimitive();
}

main:

void main() {    
    renderPlayer(gs_in[0].screenMatrix,vec4(gs_in[0].position,0.0,0.0),gs_in[0].textureData,gs_in[0].size,gs_in[0].uv);
}  

C++ code:

VBO data inicialization:

float* vertices = new float[totalVertices];
    for (EntityRenderData renderDataCurrent : renderData)
    {
        for (int layerI = 0; layerI < renderDataCurrent.getLayerCount(); layerI++)
        {
            int i = 0;
            vertices[layerI * 10 + i++] = renderDataCurrent.getLayer(layerI).xPos;
            vertices[layerI * 10 + i++] = renderDataCurrent.getLayer(layerI).yPos;
            vertices[layerI * 10 + i++] = renderDataCurrent.getLayer(layerI).xSize;
            vertices[layerI * 10 + i++] = renderDataCurrent.getLayer(layerI).ySize;
            vertices[layerI * 10 + i++] = renderDataCurrent.getLayer(layerI).U0;
            vertices[layerI * 10 + i++] = renderDataCurrent.getLayer(layerI).V0;
            vertices[layerI * 10 + i++] = renderDataCurrent.getLayer(layerI).U1;
            vertices[layerI * 10 + i++] = renderDataCurrent.getLayer(layerI).V1;
            vertices[layerI * 10 + i++] = (float)renderDataCurrent.getLayer(layerI).textureAtlasId;
            vertices[layerI * 10 + i++] = (float)renderDataCurrent.getLayer(layerI).textureIndex;
            drawCount++;
        }
    }

Render Part:

    glBindVertexArray(playerVAO);
    glBindBuffer(GL_ARRAY_BUFFER, playerVBO);
    glBufferData(GL_ARRAY_BUFFER, totalVertices * sizeof(float), vertices, GL_STREAM_DRAW);
    game->renderer->entityShader->use();
    glDrawArrays(GL_POINTS, 0, drawCount);

Inicialization Part:

glGenVertexArrays(1, &playerVAO);
    glGenBuffers(1, &playerVBO);

    glBindVertexArray(playerVAO);
    // position
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    // size
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)(2 * sizeof(float)));
    glEnableVertexAttribArray(1);
    // uv
    glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)(4 * sizeof(float)));
glEnableVertexAttribArray(2);
    // texture data
    glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)(8 * sizeof(float)));
    glEnableVertexAttribArray(3);

If a non-zero named array buffer object is bound, then the last parameter of glVertexAttribPointer is treated as a byte offset into the buffer object's data store. The definition of the generic vertex attribute data and the referenced buffer is stated in the Vertex Array Object .

It is not sufficient to generate a buffer object, the array buffer object has to be the currently bound buffer object, when using glVertexAttribPointer :

glGenVertexArrays(1, &playerVAO);
glGenBuffers(1, &playerVBO);

glBindVertexArray(playerVAO);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);

glBindBuffer(GL_ARRAY_BUFFER, playerVBO); // <--- bind the buffer object

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)(2 * sizeof(float)));
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)(4 * sizeof(float)));
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)(8 * sizeof(float)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM