简体   繁体   English

GLSL Shader不渲染任何东西,VAO填充,VBO填充顶点

[英]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? 我已经用顶点(2x位置,2x大小,4x uv,2x纹理数据(1个float设置了正确的纹理数组,并设置了2个数组中的纹理索引))填充了VBO,仍然没有渲染,我还缺少什么?

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 我尝试设置几何着色器以渲染简单的四边形,什么都没有显示,我只尝试渲染片段中的颜色,什么都没有显示,我检查了OPENGL调试消息,那里什么都没有,在顶点着色器中我检查了矩阵的计算,一切都应该很好

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. 使用glBufferData每帧将数据加载到同一缓冲区,由于调整大小,此代码用于实体,并且它们在屏幕上的数量可以轻松更改。

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: C ++代码:

VBO data inicialization: VBO数据初始化:

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. 如果绑定了一个非零命名数组缓冲区对象,则glVertexAttribPointer的最后一个参数将被视为缓冲区对象数据存储区中的字节偏移量。 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 : 使用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)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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