I'm learning opengl and trying to draw an indexed circle using glDrawEmelents, but for some reason it does not work. However when I draw a triangle using glDrawElements (see the commented code) it draws the triangle just fine. I think it has something to do with my elements/indices, but what I don't know.
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
"
"
"
"out vec4 color;
"
"void main(void)
"
"{
"
" color = vec4(0.5,0.8,1.0,0.7);
"
"}
"
};
static const GLchar* vertText[] = {
"#version 450 core
"
"layout (location = 0) in vec4 position;
"
"void main(void)
"
"{
"
" gl_Position = position;
"
"}
"
};
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)
",i, circleVerts[i].x,circleVerts[i].y,circleVerts[i].z,circleVerts[i].w);
printf("Heading:%f
", 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);
}
It also draws the points fine as well.
question from:
https://stackoverflow.com/questions/65843652/why-isnt-gldrawelements-drawing-my-circle