OpenGL
Code review
Sanuri Karunarathna
Example 1
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_POLYGON);
glVertex2f(100,500);
glVertex2f(100,100);
glVertex2f(200,100);
glVertex2f(200,300);
glEnd();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512,512);
glutCreateWindow("");
glutDisplayFunc(display);
gluOrtho2D(0,640,0,640);
glClearColor(0.5,0.7,0.5,0);
glutMainLoop();
return 0;
}
Output – OpenGL
include <GL/glut.h>
void display(){
glClear(GL_COLOR_BUFFER_BIT);
Adding required libraries to the project
Define a method to display
glClear — clear buffers to preset values
3 values :
• GL_COLOR_BUFFER_BIT- Indicates the buffers currently
enabled for color writing.
• GL_DEPTH_BUFFER_BIT- Indicates the depth buffer.
• GL_STENCIL_BUFFER_BIT - Indicates the stencil buffer.
glColor — set the current color
glColor3f(1,0,0);
glBegin(GL_POLYGON); glBegin — delimit the vertices of a primitive or a group of
like primitives
Code review cont…
glVertex2f(100,500);
glVertex2f(100,100);
glVertex2f(200,100);
glVertex2f(200,300);
glVertex — specify a vertex
C specifications
Code review cont…
glEnd(); glBegin and glEnd delimit the vertices that define a primitive
or a group of like primitives.
glFlush — force execution of GL commands in finite timeglFlush();
glutSwapBuffers(); glutSwapBuffers- swaps the buffers of the current window
if double buffered.
} End of Display function
Code review cont…
main function
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512,512);
glutCreateWindow("");
glutDisplayFunc(display);
gluOrtho2D(0,640,0,640);
glClearColor(0.5,0.7,0.5,0);
glutMainLoop();
return 0;
}
main()
The entry point into any console application. As you delve into graphics
development, you'll notice that it isn't always this. However, to keep
things simple, let's just stick with main on its own.
int argc;
Stands for argument count. In other words, how many things are being
passed into the program.
char **argv; or char *argv[];
Both do the exact same thing. Short for argument vector, or in other
words, this is a multidimensional array that will store all arguments
being passed to the function.
is used to initialize the GLUT library.glutInit(&argc, argv);
glutInitDisplayMode - sets the initial display mode.glutInitDisplayMode(GLUT_RGBA);
Code review cont…
Code review cont…
glutInitWindowSize(512,512); glutInitWindowPosition and glutInitWindowSize set the initial
window position and size respectively.
glutCreateWindow creates a top-level window.
glutCreateWindow("");
glutDisplayFunc sets the display callback for the current window.
glutDisplayFunc(display);
Code review cont…
gluOrtho2D(0,640,0,640); gluOrtho2D — define a 2D orthographic projection matrix
glClearColor(0.5,0.7,0.5,0); glClearColor — specify clear values for the color buffers
Code review cont…
glutMainLoop(); glutMainLoop enters the GLUT event processing loop.
return 0;
}
Return 0 and end of the main function
Open gl polygon code review

Open gl polygon code review

  • 1.
  • 2.
    Example 1 #include <GL/glut.h> voiddisplay() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1,0,0); glBegin(GL_POLYGON); glVertex2f(100,500); glVertex2f(100,100); glVertex2f(200,100); glVertex2f(200,300); glEnd(); glFlush(); glutSwapBuffers(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA); glutInitWindowSize(512,512); glutCreateWindow(""); glutDisplayFunc(display); gluOrtho2D(0,640,0,640); glClearColor(0.5,0.7,0.5,0); glutMainLoop(); return 0; }
  • 3.
  • 4.
    include <GL/glut.h> void display(){ glClear(GL_COLOR_BUFFER_BIT); Addingrequired libraries to the project Define a method to display glClear — clear buffers to preset values 3 values : • GL_COLOR_BUFFER_BIT- Indicates the buffers currently enabled for color writing. • GL_DEPTH_BUFFER_BIT- Indicates the depth buffer. • GL_STENCIL_BUFFER_BIT - Indicates the stencil buffer. glColor — set the current color glColor3f(1,0,0);
  • 5.
    glBegin(GL_POLYGON); glBegin —delimit the vertices of a primitive or a group of like primitives Code review cont…
  • 6.
  • 7.
    glEnd(); glBegin andglEnd delimit the vertices that define a primitive or a group of like primitives. glFlush — force execution of GL commands in finite timeglFlush(); glutSwapBuffers(); glutSwapBuffers- swaps the buffers of the current window if double buffered. } End of Display function Code review cont…
  • 8.
    main function int main(intargc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA); glutInitWindowSize(512,512); glutCreateWindow(""); glutDisplayFunc(display); gluOrtho2D(0,640,0,640); glClearColor(0.5,0.7,0.5,0); glutMainLoop(); return 0; } main() The entry point into any console application. As you delve into graphics development, you'll notice that it isn't always this. However, to keep things simple, let's just stick with main on its own. int argc; Stands for argument count. In other words, how many things are being passed into the program. char **argv; or char *argv[]; Both do the exact same thing. Short for argument vector, or in other words, this is a multidimensional array that will store all arguments being passed to the function.
  • 9.
    is used toinitialize the GLUT library.glutInit(&argc, argv); glutInitDisplayMode - sets the initial display mode.glutInitDisplayMode(GLUT_RGBA); Code review cont…
  • 10.
    Code review cont… glutInitWindowSize(512,512);glutInitWindowPosition and glutInitWindowSize set the initial window position and size respectively. glutCreateWindow creates a top-level window. glutCreateWindow(""); glutDisplayFunc sets the display callback for the current window. glutDisplayFunc(display);
  • 11.
    Code review cont… gluOrtho2D(0,640,0,640);gluOrtho2D — define a 2D orthographic projection matrix glClearColor(0.5,0.7,0.5,0); glClearColor — specify clear values for the color buffers
  • 12.
    Code review cont… glutMainLoop();glutMainLoop enters the GLUT event processing loop. return 0; } Return 0 and end of the main function