dev-resources.site
for different kinds of informations.
Drawing a Circle using OpenGL - glfw/glew c++
Published at
11/8/2021
Categories
cpp
opengl
glfw
glew
Author
ishaqkassam
Author
11 person written this
ishaqkassam
open
Hoping that you have already setup glfw & glew, and are ready to learn how to draw 2D shapes, let's dive straight into this. In this article we are going to learn how to draw a circle in openGL using glfw, c++ language.
Here is the code:
void drawCircle(float cx, float cy, float r, int num_segments)
{
float theta = 3.1415926 * 2 / float(num_segments);
float tangetial_factor = tanf(theta);//calculate the tangential factor
float radial_factor = cosf(theta);//calculate the radial factor
float x = r;//we start at angle = 0
float y = 0;
glLineWidth(2);
glBegin(GL_LINE_LOOP);
for (int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex
//calculate the tangential vector
//remember, the radial vector is (x, y)
//to get the tangential vector we flip those coordinates and negate one of them
float tx = -y;
float ty = x;
//add the tangential vector
x += tx * tangetial_factor;
y += ty * tangetial_factor;
//correct using the radial factor
x *= radial_factor;
y *= radial_factor;
}
glEnd();
}
The function above can then be called in the main() function as such:
drawCircle(250, 250, 100, 360);
To change the circle color, you can use the glColor3f() method which takes in 3 parameters, for the RGB values.
Add this line before calling the drawCircle
function
glColor3f(0.0, 0.5, 0.5);
glfw Article's
8 articles in total
Introducing RGFW: A lightweight Single Header Windowing framework & GLFW alternative
read article
Implementing a Particle System in C++ for Visual Effects
read article
RGFW | Singler-Header Lightweight framework for basic window and graphics context handling (like GLFW)
read article
m1 macのc++でOpenGLを使ってテトリスを作ってみた
read article
M1 macのc++でGLFWを使ってみる
read article
Drawing a Crescent Moon with OpenGL - GLFW/GLEW C++
read article
Drawing a Semi-Circle using OpenGL - glfw/glew c++
read article
Drawing a Circle using OpenGL - glfw/glew c++
currently reading
Featured ones: