我正在学习opengl并尝试使用gldrawelements绘制彩色索引圆,但由于某种原因它不起作用。尽管指定的颜色为红色,但圆圈显示为白色。
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:%dn", 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:%fn", heading);
}
//printf("size:%dn", (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,%dn", 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"
};
着色器不能链接,因为顶点着色器输出fcolor
没有片段着色器输入
着色器阶段的输出通过其名称链接到下一个着色器阶段的输入(除非你使用布局限定符)。参见着色器阶段之间的接口匹配规则。
将帧着色器输入从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);