All opengl tutorials I've seen that show how to create a circle in OpenGL use the "Fan" method (or some derivative), and I've not seen one use the following method:
Create a set of vertices like so:
float vertices[] = {
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 1.0f, -1.0f, // bottom right
-0.5f, -0.5f, 0.0f, -1.0f, -1.0f,// bottom left
-0.5f, 0.5f, 0.0f, -1.0f, 1.0f// top left
};
Create the following Vertex Shader which passes out the bounds information in varying format to fragment shader:
#version 330 core
in vec3 apos;
in vec2 abound;
out vec3 ourColor;
out vec2 ourBound;
void main() {
gl_Position = vec4(apos, 1.0);
ourBound = abound;
}
Create the following fragment shader which takes the interpolated bounds values to figure out the current fragment distance from center
#version 330 core
in vec2 ourBound;
uniform vec4 setColor;
void main() {
float dist = sqrt((ourBound.x * ourBound.x) + (ourBound.y * ourBound.y));
vec4 color_used = vec4(vec3(1.0), 0.0);
if(dist < 1.0){
color_used = setColor;
}
gl_FragColor = color_used;
}
- Load Shaders
- Enable Blending
- Initialize VBO/EBO objects for apos (offset = 0, stride = 5 * sizeof(float)) and for abound (offset = 3, stride = 5 * sizeof(float))
- Intialize colorLocation
- Store in VBA
in draw loop load program:
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader_program);
glUniform4f(colorLocation, 0.0f, 1.0f, 0.0f, 1.0f);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
I don't understand the benefit for creating a fan instead. Is there some performance benefit? It certainly doesn't seem to be simpler. I simply cannot find alternative solutions via google besides the one here.
