0% found this document useful (0 votes)
30 views3 pages

OpenGL Code

Uploaded by

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

OpenGL Code

Uploaded by

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

Write open GL code to draw a point at (0,0)

#include <GL/glut.h> // Include the GLUT library

void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

glPointSize(10.0f); // Set the point size to be larger

glBegin(GL_POINTS);
glColor3f(1.0f, 0.0f, 0.0f); // Point color (red)
glVertex2f(0.0f, 0.0f); // Point position
glEnd();

glFlush(); // Flush the OpenGL pipeline


}

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


glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("OpenGL Line Example"); // Create a window with the specified title
glutDisplayFunc(display); // Set the display function
glutMainLoop(); // Enter the GLUT event loop

return 0;
}

Write open GL code to draw line starting from (-1, -1) to (3,4)
#include <GL/glut.h> // Include the GLUT library
void display()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glColor3f(1.0f, 0.0f, 0.0f); // Set the line color to red

glBegin(GL_LINES); // Begin drawing lines


glVertex2f(-1.0f, -1.0f); // Specify the first endpoint of the line
glVertex2f(3.0f, 4.0f); // Specify the second endpoint of the line
glEnd(); // End drawing lines

glFlush(); // Flush the OpenGL pipeline


}
int main(int argc, char** argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("OpenGL Line Example"); // Create a window with the specified title
glutDisplayFunc(display); // Set the display function
glutMainLoop(); // Enter the GLUT event loop
return 0;
}
Write open GL code to draw Triangle
#include <GL/glut.h>

// Function to initialize OpenGL


void init() {
glClearColor(0.0, 0.0, 0.0, 0.0); // Set the background color to black
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); // Define the coordinate system
}

// Function to display the triangle


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

glBegin(GL_TRIANGLES); // Begin drawing triangles


glColor3f(1.0, 0.0, 0.0); // Set color to red
glVertex2f(0.0, 0.8); // Vertex 1
glColor3f(0.0, 1.0, 0.0); // Set color to green
glVertex2f(-0.6, -0.2); // Vertex 2
glColor3f(0.0, 0.0, 1.0); // Set color to blue
glVertex2f(0.6, -0.2); // Vertex 3
glEnd(); // End drawing triangles

glFlush(); // Flush the OpenGL pipeline to display the triangle


}

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


glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(400, 400); // Set the window size
glutCreateWindow("OpenGL Triangle"); // Create the window with the specified title
init(); // Initialize OpenGL settings
glutDisplayFunc(display); // Set the display callback function
glutMainLoop(); // Enter the main loop
return 0;
}

Write open GL code to draw Circle


#include <GL/glut.h>
#include <cmath>

const int numSegments = 100; // Number of segments for the circle


const float radius = 0.5; // Radius of the circle

// Function to initialize OpenGL


void init() {
glClearColor(0.0, 0.0, 0.0, 0.0); // Set the background color to black
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); // Define the coordinate system
}

// Function to display the filled circle


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glBegin(GL_TRIANGLE_FAN); // Begin drawing a filled circle
glColor3f(1.0, 1.0, 1.0); // Set color to white

glVertex2f(0.0, 0.0); // Center of the circle

for (int i = 0; i <= numSegments; i++) {


float angle = 2.0 * M_PI * float(i) / float(numSegments);
float x = radius * cos(angle);
float y = radius * sin(angle);
glVertex2f(x, y); // Vertex at (x, y)
}

glEnd(); // End drawing the filled circle

glFlush(); // Flush the OpenGL pipeline to display the circle


}

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


glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(400, 400); // Set the window size
glutCreateWindow("OpenGL Circle"); // Create the window with the specified title
init(); // Initialize OpenGL settings
glutDisplayFunc(display); // Set the display callback function
glutMainLoop(); // Enter the main loop
return 0;
}

You might also like