I'm trying to draw simple circle with C++/OpenGl
my code is:
#include <GL/glut.h> #include <math.h> void Draw() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glBegin(GL_QUADS); glColor3f (0.0, 0.0, 0.0); glVertex3f (0.1, 0.1, 0.0); glVertex3f (0.9, 0.1, 0.0); glVertex3f (0.9, 0.9, 0.0); glVertex3f (0.1, 0.9, 0.0); glEnd(); glFlush(); } void DrawCircle(float cx, float cy, float r, int num_segments) { glBegin(GL_LINE_LOOP); for(int ii = 0; ii < num_segments; ii++) { float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle float x = r * cosf(theta);//calculate the x component float y = r * sinf(theta);//calculate the y component glVertex2f(x + cx, y + cy);//output vertex } glEnd(); } void Initialize() { glClearColor(1.0, 1.0, 1.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); } int main(int iArgc, char** cppArgv) { glutInit(&iArgc, cppArgv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(950, 500); glutInitWindowPosition(200, 200); glutCreateWindow("Universum"); Initialize(); glutDisplayFunc(Draw); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); DrawCircle(0.5, 0.5, 0.2, 5); glutMainLoop(); return 0; }
I'm beginner with OpenGL and now i'm starting to learn, Can someone please explain me why i don't get the circle (i only see the black box), Thanks!
Drawing a Circle using OpenGL - glfw/glew c++ 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.
To draw a circle or ellipse, click and drag the mouse diagonally, using the same motion as when dragging a selection box. The circle will appear immediately after you release the mouse button. To draw a perfect circle, hold down the Ctrl key while you drag the mouse.
It looks like immediately after you draw the circle, you go into the main glut loop, where you've set the Draw()
function to draw every time through the loop. So it's probably drawing the circle, then erasing it immediately and drawing the square. You should probably either make DrawCircle()
your glutDisplayFunc()
, or call DrawCircle()
from Draw()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With