我已经用顶点(2x位置,2x大小,4x uv,2x纹理数据(1个float设置了正确的纹理数组,并设置了2个数组中的纹理索引))填充了VBO,仍然没有渲染,我还缺少什么?
我尝试设置几何着色器以渲染简单的四边形,什么都没有显示,我只尝试渲染片段中的颜色,什么都没有显示,我检查了OPENGL调试消息,那里什么都没有,在顶点着色器中,我检查了矩阵的计算,一切都应该很好
使用glBufferData每帧将数据加载到相同的缓冲区,由于调整大小,此代码用于实体,并且它们在屏幕上的数量可以轻松更改。
片段着色器:
void main()
{
texColor = vec4(1.0,0.0,0.0,1.0); //Red fillment
FragColor = texColor;
}
顶点着色器:
布局:
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
界面:
out VS_OUT
{
mat4 screenMatrix;
vec2 position;
vec4 uv;
vec2 textureData;
vec2 size;
} vs_out;
矩阵:
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);
几何着色器:
布局:
layout (points) in;
layout(triangle_strip,max_vertices = 4) out;
退出:
out vec2 TexData;
out vec2 TexCoord;
代码:
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();
}
主要:
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 ++代码:
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++;
}
}
渲染部分:
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);
初始化部分:
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);
答案 0 :(得分:0)
如果绑定了一个非零命名数组缓冲区对象,则glVertexAttribPointer
的最后一个参数将被视为缓冲区对象数据存储区中的字节偏移量。
Vertex Array Object中说明了通用顶点属性数据和引用缓冲区的定义。
仅使用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)));