Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing Circle with OpenGL

Tags:

c++

opengl

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!

like image 488
tonyhlav Avatar asked Mar 16 '14 23:03

tonyhlav


People also ask

How do you draw a circle in Glfw?

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.

How do you draw a circle with a mouse?

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.


1 Answers

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().

like image 135
user1118321 Avatar answered Oct 20 '22 05:10

user1118321