0% found this document useful (0 votes)
8 views4 pages

Open GL

The document contains three Open GL programs that draw basic shapes: a line, triangle, and square. Each program imports the necessary libraries, defines a display function to render the shape, and includes a main function to initialize the window and main loop.

Uploaded by

Yatra Nepal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Open GL

The document contains three Open GL programs that draw basic shapes: a line, triangle, and square. Each program imports the necessary libraries, defines a display function to render the shape, and includes a main function to initialize the window and main loop.

Uploaded by

Yatra Nepal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Open GL program to Draw Line

#include <windows.h> // for MS Windows

#include <GL/glu.h>

#include <GL/glut.h> // GLUT, include glu.h and gl.h

/* Handler for window-repaint event. Call back when the window first appears and whenever the
window needs to be re-painted. */

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque

glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)

glBegin(GL_LINES);

glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5);

glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);

glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);

glEnd();

glFlush(); // Flush drawing command buffer to make drawing happen as soon as possible.

/* Main function: GLUT runs as a console application starting at main() */

int main(int argc, char** argv) {

glutInit(&argc, argv); // Initialize GLUT

glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title

glutInitWindowSize(320, 320); // Set the window's initial width & height

glutInitWindowPosition(50, 50); // Position the window's initial top-left corner

glutDisplayFunc(display); // Register display callback handler for window re-paint

glutMainLoop(); // Enter the event-processing loop

return 0;

}
Open GL program to Draw triangle
#include <windows.h> // for MS Windows

#include <GL/glu.h>

#include <GL/glut.h> // GLUT, include glu.h and gl.h

/* Handler for window-repaint event. Call back when the window first appears and whenever the
window needs to be re-painted. */

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque

glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)

glBegin(GL_POLYGON);

glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5);

glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);

glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);

glEnd();

glFlush(); // Flush drawing command buffer to make drawing happen as soon as possible.

/* Main function: GLUT runs as a console application starting at main() */

int main(int argc, char** argv) {

glutInit(&argc, argv); // Initialize GLUT

glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title

glutInitWindowSize(320, 320); // Set the window's initial width & height

glutInitWindowPosition(50, 50); // Position the window's initial top-left corner

glutDisplayFunc(display); // Register display callback handler for window re-paint

glutMainLoop(); // Enter the event-processing loop

return 0;

}
Open GL program to Draw Square
#include <windows.h> // for MS Windows

#include <GL/glu.h>

#include <GL/glut.h> // GLUT, include glu.h and gl.h

/* Handler for window-repaint event. Call back when the window first appears and whenever the
window needs to be re-painted. */

void display() {

glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque

glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)

glBegin(GL_QUADS); // Each set of 4 vertices form a quad

glColor3f(1.0f, 0.0f, 0.0f); // Red

glVertex2f(-0.5f, -0.5f); // x, y

glVertex2f( 0.5f, -0.5f);

glVertex2f( 0.5f, 0.5f);

glVertex2f(-0.5f, 0.5f);

glEnd();

glFlush(); // Flush drawing command buffer to make drawing happen as soon as possible.

/* Main function: GLUT runs as a console application starting at main() */

int main(int argc, char** argv) {

glutInit(&argc, argv); // Initialize GLUT

glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title

glutInitWindowSize(320, 320); // Set the window's initial width & height

glutInitWindowPosition(50, 50); // Position the window's initial top-left corner

glutDisplayFunc(display); // Register display callback handler for window re-paint

glutMainLoop(); // Enter the event-processing loop


return 0;

You might also like