【问题标题】:Why isn't glDrawElements coloring my circle?为什么 glDrawElements 不为我的圆圈着色?
【发布时间】:2021-05-01 07:29:24
【问题描述】:

我正在学习 opengl 并尝试使用 glDrawEmelents 绘制彩色索引圆,但由于某种原因它不起作用。尽管指定的颜色是红色,但圆圈显示为白色。

GLuint vShader = 0;
GLuint fShader = 0;
static const GLchar* fragText[] = {
    "#version 450 core \n"
    "in vec3 icolor;\n"
    "out vec3 ocolor;\n"
    "void main(void)\n"
    "{\n"
    "   ocolor = icolor;\n"
    "}\n"
};

static const GLchar* vertText[] = {
    "#version 450 core \n"
    "layout (location = 0) in vec4 position;\n"
    "layout (location = 1) in vec3 icolor;\n"

    "out vec3 fcolor;\n"
    "void main(void)\n"
    "{\n"
    "   fcolor = icolor;\n"
    "   gl_Position = position;\n"
    "}\n"
};

vShader = glCreateShader(GL_VERTEX_SHADER);
fShader = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(vShader, 1, vertText, NULL);

glCompileShader(vShader);

GLint status;
glGetShaderiv(vShader, GL_COMPILE_STATUS, &status);

if (status != GL_TRUE) {
    printf("v bust");
}

glShaderSource(fShader, 1, fragText, NULL);

glCompileShader(fShader);

if (status != GL_TRUE) {
    printf("f bust");
}


GLuint programID = glCreateProgram();
glAttachShader(programID, vShader);
glAttachShader(programID, fShader);

glLinkProgram(programID);



int size = (sizeof(circleVerts) / sizeof(glm::vec4));
//printf("size:%d\n", size);
circleVerts[0] = glm::vec4(0, 0, 0, 1.0f);
for (int i = 0; i < size; i++) {
    float heading = 360.f * ((float)i / (float)(size));
    heading = glm::radians(heading);
    circleVerts[i] = glm::vec4(glm::cos(heading), glm::sin(heading), 0, 1);
    //printf("%d:(%f,%f,%f,%f)\n", i, circleVerts[i].x, circleVerts[i].y, circleVerts[i].z, circleVerts[i].w);
    //printf("Heading:%f\n", heading);
}

//printf("size:%d\n", (sizeof(circleIndices) / sizeof(GLuint)));

for (int i = 0; i + 2 < size; ++i) {
    int index = i * 3;
    circleIndices[index] = 0;
    circleIndices[index + 1] = i + 1;
    circleIndices[index + 2] = i + 2;
    //printf("i=%d,Indices:%d,%d,%d\n", i, circleIndices[i], circleIndices[i + 1], circleIndices[i + 2]);
}




GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(circleVerts), circleVerts, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);

int size = sizeof(circleColor) / sizeof(glm::vec3);
for (int i = 0; i < size; i++) {
    circleColor[i] = glm::vec3(1.0f, 0, 0);
}


GLuint cbo;
glGenBuffers(1, &cbo);
glBindBuffer(GL_ARRAY_BUFFER, cbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(circleColor), circleColor, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);

GLuint ibo;
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(circleIndices), circleIndices, GL_STATIC_DRAW);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC1_ALPHA);
glUseProgram(programID);
glDrawElements(GL_TRIANGLES, 90, GL_UNSIGNED_INT, 0);

我已将片段着色器编辑为以下内容。即使使用此着色器,它仍然显示为白色。我最初预计颜色缓冲区有问题,但片段着色器似乎有问题。

static const GLchar* fragText[] = {
    "#version 450 core \n"
    "in vec3 icolor;\n"
    "out vec4 color;\n"
    "void main(void)\n"
    "{\n"
    "   color = vec4(1,0,0,1);\n"
    "}\n"
};

【问题讨论】:

  • 问题解决了吗?

标签: c++ opengl glsl shader opengl-4


【解决方案1】:

着色器无法链接,因为顶点着色器输出fcolor没有片段着色器输入。
着色器阶段的输出通过其名称链接到下一个着色器阶段的输入(使用布局限定符时除外)。见interface matching rules between shader stages.

将帧根着色器输入从icolor重命名为fcolor

#version 450 core
in vec3 fcolor;
out vec4 ocolor;
void main(void)
{
    ocolor = vec4(fcolor, 1.0);
}  

此外,您的混合功能存在问题。您很可能想使用GL_ONE_MINUS_SRC_ALPHA 而不是GL_ONE_MINUS_SRC1_ALPHA

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC1_ALPHA);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

【讨论】:

  • 在片段着色器中将 icolor 重命名为 fcolor 不起作用。
  • 你试过ocolor = vec4(fcolor, 1.0);
  • 是的,没有变化。
  • 看起来是这样。在我的问题结束时,我提到甚至没有为片段着色器输出颜色变量分配一个常数值。也许这与它有关。
  • @DohnJoe GL_ONE_MINUS_SRC1_ALPHA -> GL_ONE_MINUS_SRC_ALPHA
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多