- Bresenham Line
- Bresenham Circle
- Double buffer (spin rectangle)
- Cylinder and Parallellopiped
- 3D Sierpinski gasket
- 3D sphere approximation
- Scan-line filling algorithm
- Cohen Sutherland line clipping
- Liang-Barsky line clipping
- Cohen-hedgeman polygon clipping
- House rotation and reflection
- Color cube and spinning it
I followed this tutorial (https://medium.com/swlh/setting-opengl-for-windows-d0b45062caf)
And instead of running on codeblocks, programs are compiled directly with this command
gcc prog.c -I"C:\msys64\mingw64\include" -lopengl32 -lglu32 -lfreeglut
Some general info to be kept in mind
gluOrtho2D(int left, int right, int bottom, int top)
glOrtho(int left, int right, int bottom, int top, int near, int far)
It is responsinle for pushing whatever is in display buffer onto screen. If not called, rendering might not be seen. Generally, call this whenever anything is drawn or updated onto screen.
When any 3D stuff is happening or some so rt of animation, use this always. In addition to this, add GLUT_DEPTH to glutInitDisplay() and GL_DEPTH_BUFFER_BIT to glClear().
Never forget glFlush()
. Some programs might need glutPostRedisplay()
, remember this also.
Use glRotatef / glTranslatef / glScalef. These apply transformations on whole scene. To apply to just one drawing, use this method -
glPushMatrix();
glRotatef(...) or whatever trasformations
... draw stuff ...
glPopMatrix();
About fixed point, you need to carry out Translation, then Rotation, then inverse Translation
glTranslatef(x,y,0);
glRotatef(angle, x,y,z);
glTranslatef(-x,-y,0);
We do Translaton, Rotation, Scale, inverse Rotate, inverse Translate
glTranslatef(0,c,0); // c is the y-intercept
glRotatef(angle, 0, 0, 1); // angle = atan(slope) * RADIAN2DEGREE
glScalef(1,-1,1);
glRotatef(angle, 0, 0, 1);
glTranslatef(0,-c,0);
Keep reference of window_id of each window, and possibly assign different display() functions.
Within display(), call glutSetWindow(window_id)
first.
To view the sphere properly, use these functions
// this makes sure only lines are colored and polygons are not filled
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// removal of back faces so sphere can be viewed easily
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
In case camera is set using gluOrtho2D(-WIDTH/2, WIDTH/2, -HEIGHT/2, HEIGHT/2)
, use this to draw line
drawLine(xglobal-WIDTH/2, HEIGHT/2-yglobal, x-WIDTH/2, HEIGHT/2-y);
In case camera is set using gluOrtho2D(0, WIDTH, 0, HEIGHT)
, use this to draw line
drawLine(xglobal, HEIGHT-yglobal, x, HEIGHT-y);