我有一个GLfloats数组,当我绘制四边形时,我用它作为位置和颜色(每个顶点有4个浮点数)。我想添加为我的四边形着色的能力,并且认为我将RGBA打包到一个GLuint中,然后将其与位置一起发送到GPU。
那么,我可以以某种方式向GPU发送4个GLfloats然后1个GLuint吗?
以下是相关代码:
void SpriteRenderer::Init()
{
vertexBufferArrayInserts = 0;
hasBegun = GL_FALSE;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * MAX_VERTEX_BUFFER_SIZE, 0, GL_STREAM_DRAW);
glGenBuffers(1, &elementBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * MAX_ELEMENT_BUFFER_SIZE, 0, GL_STREAM_DRAW);
//////////////////////////////////////////////////////////////////////////
//LOAD SHADER, CREATE AND USE PROGRAM
//////////////////////////////////////////////////////////////////////////
glGenVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
vertexShaderID = LoadShaderFromFile("Shader", GL_VERTEX_SHADER);
fragmentShaderID = LoadShaderFromFile("Shader", GL_FRAGMENT_SHADER);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShaderID);
glAttachShader(shaderProgram, fragmentShaderID);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
shaderPosAttrib = glGetAttribLocation(shaderProgram, "position");
glVertexAttribPointer(shaderPosAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, 0);
glEnableVertexAttribArray(shaderPosAttrib);
shaderTexCoordAttrib = glGetAttribLocation(shaderProgram, "texCoord");
glVertexAttribPointer(shaderTexCoordAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (void*)(sizeof(GLfloat) * 2));
glEnableVertexAttribArray(shaderTexCoordAttrib);
shaderColorAttrib = glGetAttribLocation(shaderProgram, "Color");
glVertexAttribPointer(shaderColorAttrib, 1, GL_UNSIGNED_INT, GL_FALSE, sizeof(GLfloat) * 5, (void*)(sizeof(GLfloat) * 4));
glEnableVertexAttribArray(shaderColorAttrib);
shaderProjMatAttrib = glGetUniformLocation(shaderProgram, "projMat");
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void SpriteRenderer::Draw(Vector2<GLfloat> position, Rect clipRect)
{
//TOP LEFT
vertexBufferArray[vertexBufferArrayInserts * 16] = position.X;
vertexBufferArray[vertexBufferArrayInserts * 16 + 1] = position.Y;
vertexBufferArray[vertexBufferArrayInserts * 16 + 2] = clipRect.GetLeftX() / 512.0f;
vertexBufferArray[vertexBufferArrayInserts * 16 + 3] = clipRect.GetTopY() / 512.0f;
colorBufferArray[vertexBufferArrayInserts * 4] = PackColor(255, 255, 255, 255);
//TOP RIGHT
vertexBufferArray[vertexBufferArrayInserts * 16 + 4] = position.X + clipRect.GetWidth();
vertexBufferArray[vertexBufferArrayInserts * 16 + 5] = position.Y;
vertexBufferArray[vertexBufferArrayInserts * 16 + 6] = clipRect.GetRightX() / 512.0f;
vertexBufferArray[vertexBufferArrayInserts * 16 + 7] = clipRect.GetTopY() / 512.0f;
colorBufferArray[vertexBufferArrayInserts * 4 + 1] = PackColor(255, 255, 255, 255);
//BOTTOM RIGHT
vertexBufferArray[vertexBufferArrayInserts * 16 + 8] = position.X + clipRect.GetWidth();
vertexBufferArray[vertexBufferArrayInserts * 16 + 9] = position.Y + clipRect.GetHeight();
vertexBufferArray[vertexBufferArrayInserts * 16 + 10] = clipRect.GetRightX() / 512.0f;
vertexBufferArray[vertexBufferArrayInserts * 16 + 11] = clipRect.GetBottomY() / 512.0f;
colorBufferArray[vertexBufferArrayInserts * 4 + 2] = PackColor(255, 255, 255, 255);
//BOTTOM LEFT
vertexBufferArray[vertexBufferArrayInserts * 16 + 12] = position.X;
vertexBufferArray[vertexBufferArrayInserts * 16 + 13] = position.Y + clipRect.GetHeight();
vertexBufferArray[vertexBufferArrayInserts * 16 + 14] = clipRect.GetLeftX() / 512.0f;
vertexBufferArray[vertexBufferArrayInserts * 16 + 15] = clipRect.GetBottomY() / 512.0f;
colorBufferArray[vertexBufferArrayInserts * 4 + 3] = PackColor(255, 255, 255, 255);
//ELEMENT BUFFER
elementBufferArray[vertexBufferArrayInserts * 6] = vertexBufferArrayInserts * 4;
elementBufferArray[vertexBufferArrayInserts * 6 + 1] = vertexBufferArrayInserts * 4 + 1;
elementBufferArray[vertexBufferArrayInserts * 6 + 2] = vertexBufferArrayInserts * 4 + 2;
elementBufferArray[vertexBufferArrayInserts * 6 + 3] = vertexBufferArrayInserts * 4 + 2;
elementBufferArray[vertexBufferArrayInserts * 6 + 4] = vertexBufferArrayInserts * 4 + 3;
elementBufferArray[vertexBufferArrayInserts * 6 + 5] = vertexBufferArrayInserts * 4;
vertexBufferArrayInserts++;
if(vertexBufferArrayInserts == MAX_BUFFER_INSERTS)
Draw();
}
void SpriteRenderer::Draw()
{
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * vertexBufferArrayInserts * 16, vertexBufferArray);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertexBufferArrayInserts * 16, sizeof(GLuint) * vertexBufferArrayInserts * 4, colorBufferArray);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(GLint) * vertexBufferArrayInserts * 6, elementBufferArray);
glDrawElements(GL_TRIANGLES, vertexBufferArrayInserts * 6, GL_UNSIGNED_INT, 0);
vertexBufferArrayInserts = 0;
}
GLuint SpriteRenderer::PackColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a) {
GLint returnVal = (r << 24) + (g << 16) + (b << 8) + a;
return returnVal;
}
答案 0 :(得分:3)
你的问题在于:
glVertexAttribPointer(shaderColorAttrib, 1, GL_UNSIGNED_INT,
GL_FALSE, sizeof(GLfloat) * 5, (void*)(sizeof(GLfloat) * 4));
这将作为单个组件解压缩。
告诉GPU您要发送四个单字节组件(最多可添加一个32位值):
glVertexAttribPointer(shaderColorAttrib, 4, GL_UNSIGNED_BYTE,
GL_TRUE, sizeof(GLfloat) * 5, (void*)(sizeof(GLfloat) * 4));
请注意,我将normalized标志设置为true。如果您将其发送到着色器上的vec4
,请执行此操作,以便将每个组件转换为您可能需要的[0,1]
范围。
此外,不要使用sizeof(float)
进行非常容易出错的数学运算,而是考虑制作一个结构并使用该结构的大小和offsetof
来生成步幅和顶点偏移:
struct vertex {
float4 position;
ubyte4 color;
};
glVertexAttribPointer(shaderColorAttrib, 4, GL_FLOAT,
GL_FALSE, sizeof(vertex), offsetof(vertex, position));
glVertexAttribPointer(shaderColorAttrib, 4, GL_UNSIGNED_BYTE,
GL_TRUE, sizeof(vertex), offsetof(vertex, color));
一旦开始混合数据类型,这就变得更加重要。只要注意struct padding;使用offsetof
将确保工作正常,但您不希望浪费缓冲区对象空间。