0% found this document useful (0 votes)
8 views

openGLlab

The document provides examples of OpenGL programs for drawing lines and polygons using the GLUT library. It includes code snippets for initializing OpenGL settings, defining colors, and rendering shapes with vertex coloring. Additionally, it demonstrates how to modify the display function to color each vertex differently.

Uploaded by

abiysol95
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 views

openGLlab

The document provides examples of OpenGL programs for drawing lines and polygons using the GLUT library. It includes code snippets for initializing OpenGL settings, defining colors, and rendering shapes with vertex coloring. Additionally, it demonstrates how to modify the display function to color each vertex differently.

Uploaded by

abiysol95
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

Example 1 OpenGL Program to Draw Lines

#include <windows.h>

#include <GL/glut.h>

// Function to initialize OpenGL settings

void init() {

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

glColor3f(1.0f, 1.0f, 1.0f); // Set line color to white

// Function to handle the drawing of lines

void display() {

glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

glBegin(GL_LINES); // Start drawing lines

glVertex2f(-0.5f, 0.0f); // Line start point

glVertex2f(0.5f, 0.0f); // Line end point

glVertex2f(0.0f, -0.5f); // Line start point

glVertex2f(0.0f, 0.5f); // Line end point

glVertex2f(-0.5f, -0.5f); // Line start point

glVertex2f(0.5f, 0.5f); // Line end point

glEnd(); // End drawing lines

glFlush(); // Render now

// Main function

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

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


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

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

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

init(); // Call the initialization function

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

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

return 0;

Example2:-OpenGL drawing polygon

#include <windows.h>

#include <GL/glut.h>

// Function to initialize OpenGL settings

void init() {

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

glColor3f(1.0f, 0.0f, 0.0f); // Set polygon color to red


}

// Function to handle the drawing of the polygon

void display() {

glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

glBegin(GL_POLYGON); // Start drawing a polygon

glVertex2f(-0.5f, 0.5f); // Vertex 1

glVertex2f(0.5f, 0.5f); // Vertex 2

glVertex2f(1.0f, 0.0f); // Vertex 3

glVertex2f(0.5f, -0.5f); // Vertex 4

glVertex2f(-0.5f, -0.5f);// Vertex 5

glVertex2f(-1.0f, 0.0f); // Vertex 6

glEnd(); // End drawing the polygon

glFlush(); // Render now

// Main function

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

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

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

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

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

init(); // Call the initialization function

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

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

return 0;

}
Example3 of Enhanced Vertex Coloring

If you want to color each vertex differently, you can modify the display function like this:

void display() {

glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

glBegin(GL_POLYGON); // Start drawing a polygon

glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.5f, 0.5f); // Vertex 1 (Red)

glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.5f, 0.5f); // Vertex 2 (Green)

glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(1.0f, 0.0f); // Vertex 3 (Blue)

glColor3f(1.0f, 1.0f, 0.0f); glVertex2f(0.5f, -0.5f); // Vertex 4 (Yellow)

glColor3f(1.0f, 0.5f, 0.0f); glVertex2f(-0.5f, -0.5f);// Vertex 5 (Orange)

glColor3f(0.5f, 0.0f, 0.5f); glVertex2f(-1.0f, 0.0f); // Vertex 6 (Purple)

glEnd(); // End drawing the polygon

glFlush(); // Render now

You might also like