我有一个 GLfloats 数组,我在绘制四边形时用作位置和颜色(所以每个顶点有 4 个浮点数)。我还想添加为四边形着色的功能,并认为我会将 RGBA 打包到单个 GLuint 中,然后将其与位置一起发送到 GPU。
那么,我可以以某种方式将 4 个 GLfloats 和 1 个 GLuint 发送到 GPU 吗?
以下是相关代码:
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;
}