gpt4 book ai didi

c++ - 我可以在 OpenGL (C++) 的缓冲区中混合数据类型吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:03:27 24 4
gpt4 key购买 nike

我有一个 GLfloats 数组,在我绘制四边形时用作位置和颜色(因此每个顶点有 4 个 float )。我也想添加为四边形着色的功能,我想我会将 RGBA 打包到一个 GLuint 中,然后将其与位置一起发送到 GPU。

那么,我能否以某种方式向 GPU 发送 4 个 GLfloat,然后发送 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;
}

最佳答案

你的问题在于:

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));

请注意,我将规范化标志设置为 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));

一旦您开始混合数据类型,这就变得更加重要。请注意结构填充;使用 offsetof 将确保一切正常,但您不想浪费缓冲区对象空间。

关于c++ - 我可以在 OpenGL (C++) 的缓冲区中混合数据类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17222867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com