Thursday, March 26, 2020

OpenGL: Drawing a Circle in C

C program to draw a circle using OpenGL on Windows in Code::Blocks

// C program to demonstrate
// drawing a circle using
// OpenGL
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
#define pi 3.142857
// function to initialize
void myInit (void)
{
 // making background color black as first
 // 3 arguments all are 0.0
 glClearColor(0.0, 0.0, 0.0, 1.0);
 // making picture color green (in RGB mode), as middle argument is 1.0
 glColor3f(0.0, 1.0, 0.0);
 // breadth of picture boundary is 1 pixel
 glPointSize(1.0);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 // setting window dimension in X- and Y- direction
 gluOrtho2D(-780, 780, -420, 420);
}
void display (void)
{
 glClear(GL_COLOR_BUFFER_BIT);
 glBegin(GL_POINTS);
 float x, y, i;
 // iterate y up to 2*pi, i.e., 360 degree
 // with small increment in angle as
 // glVertex2i just draws a point on specified co-ordinate
 for ( i = 0; i < (2 * pi); i += 0.001)
 {
  // let 200 is radius of circle and as,
  // circle is defined as x=r*cos(i) and y=r*sin(i)
  x = 200 * cos(i);
  y = 200 * sin(i);
  glVertex2i(x, y);
 }
 glEnd();
 glFlush();
}
int main (int argc, char** argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
 // giving window size in X- and Y- direction
 glutInitWindowSize(640, 480);
 glutInitWindowPosition(0, 0);
 // Giving name to window
 glutCreateWindow("Circle Drawing");
 myInit();
 glutDisplayFunc(display);
 glutMainLoop();
}

output:






source : Detail on how to install OpenGL in codeblocks for windows

Install OpenGL on windows in Code::Blocks

  1. Download code block and install it
  2. Go to the link and download zip file from the download link that appears after freeglut MinGW package with having link name as Download freeglut 3.0.0 for MinGW and extract it.
  3. Open notepad with run as administrator and open file from
    1. This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates, (then click to show All Files)
    2. Next, open glut.cbp and and search all glut32 and replace with freeglut.
    3. Then, open from This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > share > CodeBlocks > templates > wizard > glut (then click to show All Files)
    4. Open wizard.script and here, also replace all glut32 with freeglut 
  4. Then go to freeglut folder (where it was downloaded) and
    1. Include > GL and copy all four file from there
    2. Go to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > include > GL and paste it.
    3. Then, from download folder freeglut > lib, copy two files and go to This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW > lib and paste it.
    4. Again go to downloaded folder freeglut > bin and copy one file (freeglut.dll) from here and go to This PC > C:(C-drive) > Windows > SysWOW64 and paste this file.
  5. Now open Code::Blocks.
    1. Select File > New > Project > GLUT project > Next.
    2. Give project title anything and then choose Next.
    3. For selecting GLUT’s location : This PC > C:(C-drive) > Program Files(x86) > CodeBlocks > MinGW.
    4. Press OK > Next > Finish.

Now, Code::Blocks is ready to test for OpenGL File
NOTE: Once you save your file in the GLUT project in codeblocks, comment the main() method of the 'main.cpp' file, because there can be only one main() method i.e. the one in your C file.

source : https://www.geeksforgeeks.org/getting-started-with-opengl/



1 comment:

  1. unable to open my glut.cbp , which app should i use to open it

    ReplyDelete

इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया - सुदर्शन फ़ाकिर

 इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया वरना क्या बात थी किस बात ने रोने ना दिया आप कहते थे कि रोने से ना बदलेंगे नसीब उमर भर आप की इस बात...