我正在学习opengl并尝试使用gldrawelements绘制索引圆,但由于某种原因它不起作用。然而,当我使用glDrawElements绘制三角形时(见注释代码),它绘制三角形只是很好。我认为这与我的元素/索引有关,但我不知道的是
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("GL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
glewExperimental = true;
GLenum error = glewInit();
if (error != GLEW_OK)
return -1;
if (context == NULL)
return -1;
SDL_Event event;
GLuint vShader = 0;
GLuint fShader = 0;
static const GLchar* fragText[] = {
"#version 450 core n"
"n"
"out vec4 color;n"
"void main(void)n"
"{n"
" color = vec4(0.5,0.8,1.0,0.7);n"
"}n"
};
static const GLchar* vertText[] = {
"#version 450 core n"
"layout (location = 0) in vec4 position;n"
"void main(void)n"
"{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");
return -1;
}
glShaderSource(fShader, 1, fragText, NULL);
glCompileShader(fShader);
if (status != GL_TRUE) {
printf("f bust");
return -1;
}
GLuint programID = glCreateProgram();
glAttachShader(programID, vShader);
glAttachShader(programID, fShader);
glLinkProgram(programID);
glm::vec4 circleVerts[33];
int size = (sizeof(circleVerts) / sizeof(glm::vec4));
circleVerts[0] = glm::vec4(0,0,0,1.0f);
for (int i = 1; i < size; i++) {
float heading = 360.f * ((float)i / (float)(size));
circleVerts[i] = glm::vec4(0.5f * glm::cos(heading), 0.5f * 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);
}
int circleIndices[96];
size = (sizeof(circleIndices) / sizeof(int));
for (int i = 0; i < size; i++) {
circleIndices[i] = 0;
circleIndices[i + 1] = 1;// i % 32;
circleIndices[i + 2] = 2;// (i + 1) % 32;
}
/* draw triangle
glm::vec4 circleVerts[3];
circleVerts[0] = glm::vec4(0, .5f, 0, 1);
circleVerts[1] = glm::vec4(0.5f, -.5f,0, 1);
circleVerts[2] = glm::vec4(-0.5f, -0.5f, 0, 1);
GLushort circleIndices[3];
circleIndices[0] = 0;
circleIndices[1] = 1;
circleIndices[2] = 2;
*/
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(circleVerts), circleVerts, GL_STATIC_DRAW);
glVertexAttribPointer(0,4, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
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);
while (true) {
SDL_PollEvent(&event);
if (event.type == SDL_QUIT) {
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
glViewport(0, 0, 500, 500);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(programID);
//glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0);
glDrawElements(GL_TRIANGLES,96,GL_UNSIGNED_INT,0);
//glDrawArrays(GL_POINTS,0,32);
SDL_GL_SwapWindow(window);
}
它也可以很好地绘制点。
glDrawElements
的type参数必须对应索引的数据类型。GL_UNSIGNED_INT
对应GLuint
。如果数据类型为GLushort
,则类型参数必须为GL_UNSIGNED_USHORT
。