0% found this document useful (0 votes)
19 views8 pages

OpenGL Basic Shapes and Setup

The document contains multiple C++ code snippets using OpenGL and GLUT to create various graphical shapes and objects, including a basic window setup, drawing points, lines, axes, triangles, and polygons. Each code segment initializes a window and defines a display function to render the specified shapes with different colors. Additionally, there are assignments for creating a rainbow flag and rendering 'AIUB' text.

Uploaded by

Faria Nourin
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)
19 views8 pages

OpenGL Basic Shapes and Setup

The document contains multiple C++ code snippets using OpenGL and GLUT to create various graphical shapes and objects, including a basic window setup, drawing points, lines, axes, triangles, and polygons. Each code segment initializes a window and defines a display function to render the specified shapes with different colors. Additionally, there are assignments for creating a rainbow flag and rendering 'AIUB' text.

Uploaded by

Faria Nourin
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

Problem-1

#basic
#include <windows.h>
#include <GL/glut.h>
void display();
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(320, 320);
glutCreateWindow("OpenGL Setup Test");
glutDisplayFunc(display);
glClearColor(1.0,1.0,0.0,1.0);
glutMainLoop();
return 0;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT); glFlush();
}

2. #Draw Sample Window (White)

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


#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(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)

glFlush(); // Render now


}

/* 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
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}

3. //Draw Points

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


#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)
glPointSize(5.0);
// Draw a Red 1x1 Square centered at origin
glBegin(GL_POINTS); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.0f, -0.0f); // x, y

glEnd();

glFlush(); // Render now


}

/* 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
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}

4. //Draw Line

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


#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)
glLineWidth(7.5);
// Draw a Red 1x1 Square centered at origin
glBegin(GL_LINES); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(0.0f, 0.0f); // x, y
glVertex2f(1.0f, 0.0f); // x, y

glEnd();

glFlush(); // Render now


}

/* 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"); // Create a window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}

5. Draw X, Y Axis

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


#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)
glLineWidth(.5);
// Draw a Red 1x1 Square centered at origin
glBegin(GL_LINES); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red

glVertex2f(0.0f, 0.0f); // x, y
glVertex2f(1.0f, 0.0f); // x, y

glVertex2f(0.0f, 0.0f); // x, y
glVertex2f(0.0f, 1.0f); // x, y

glVertex2f(0.0f, 0.0f); // x, y
glVertex2f(-1.0f, 0.0f); // x, y

glVertex2f(0.0f, 0.0f); // x, y
glVertex2f(0.0f, -1.0f);

glEnd();

glFlush(); // Render now


}

/* 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"); // Create a window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}

6.# draw triangle

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


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

/* Initialize OpenGL Graphics */


void initGL() {
// Set "clearing" or background color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
}

/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer with current clearing color

glBegin(GL_TRIANGLES); // Start defining a triangle


glVertex2f(0.0f, 0.0f);
glVertex2f(0.4f, 0.2f);
glVertex2f(-0.4f, 0.2f);
glEnd();

glFlush(); // Render now


}

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


int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("Vertex, Primitive & Color"); // Create 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 callback handler for window re-paint event
initGL(); // Our own OpenGL initialization
glutMainLoop(); // Enter the event-processing loop
return 0;
}
Draw Ploygon

/*
* [Link]: Vertex, Primitive and Color
* Draw Simple 2D colored Shapes: quad, triangle and polygon.
*/
#include <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h

/* Initialize OpenGL Graphics */


void initGL() {
// Set "clearing" or background color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
}

/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer with current clearing color

glBegin(GL_POLYGON); // These vertices form a closed polygon


glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex2f(0.4f, 0.2f);
glVertex2f(0.6f, 0.2f);
glVertex2f(0.7f, 0.4f);
glVertex2f(0.6f, 0.6f);
glVertex2f(0.4f, 0.6f);
glVertex2f(0.3f, 0.4f);
glEnd();

glFlush(); // Render now


}

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


int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("Vertex, Primitive & Color"); // Create 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 callback handler for window re-paint event
initGL(); // Our own OpenGL initialization
glutMainLoop(); // Enter the event-processing loop
return 0;
}

4 Object in 4 axis

/*
* [Link]: Vertex, Primitive and Color
* Draw Simple 2D colored Shapes: quad, triangle and polygon.
*/
#include <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h

/* Initialize OpenGL Graphics */


void initGL() {
// Set "clearing" or background color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
}
/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer with current clearing color

glBegin(GL_POLYGON); // These vertices form a closed polygon


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

glVertex2f(0.4f, 0.2f);
glVertex2f(0.6f, 0.2f);
glVertex2f(0.7f, 0.4f);
glVertex2f(0.6f, 0.6f);
glVertex2f(0.4f, 0.6f);
glVertex2f(0.3f, 0.4f);
glEnd();

// Draw a Red 1x1 Square centered at origin


glBegin(GL_TRIANGLES); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red

glVertex2f(-0.9f, 0.3f); // x, y
glVertex2f(-0.5f, 0.3f);
glVertex2f(-.7f, 0.6f);

glEnd();

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


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

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

glEnd();

glBegin(GL_TRIANGLES);//
glColor3ub(232, 133, 20);//rgb color picker

glVertex2f(+.5f, -.8f); // x, y
glVertex2f(+0.7f,-.8f);
glVertex2f(+.6f, -0.4f);
glEnd();

glFlush(); // Render now


}

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


int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("Vertex, Primitive & Color"); // Create window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutDisplayFunc(display); // Register callback handler for window re-paint event
initGL(); // Our own OpenGL initialization
glutMainLoop(); // Enter the event-processing loop
return 0;
}

Assignment:
1. Rainbow Flag
2. AIUB Text

Common questions

Powered by AI

The `glFlush` function forces the execution of OpenGL commands in finite time. In immediate mode, it ensures that commands buffered by the system are processed and effects are seen on the screen without unnecessary delay. This is important for applications that may not use double buffering and need to display their results as soon as pending operations are completed .

In OpenGL, the `glVertex2f` command specifies the 2D coordinates of a vertex within the OpenGL context. These vertices are sequentially processed to form primitives - basic shapes like points, lines, and triangles. For instance, specifying two vertices with `glVertex2f` commands can form a line if processed within a `glBegin(GL_LINES)` block, while a triangle requires three vertices specified in sequence. This command is foundational for rendering any structured shape by defining the critical points in its geometry .

`GL_POINTS`, `GL_LINES`, and `GL_TRIANGLES` specify different primitive types that OpenGL uses to interpret vertex data. `GL_POINTS` draws a point at each vertex position. `GL_LINES` connects pairs of vertices to form line segments, affecting the output by creating linear connections between specified points. `GL_TRIANGLES` uses groups of three vertices to form triangular facets, which are fundamental for complex 2D or 3D shapes. Each mode impacts the geometric interpretation and rendering of vertices in the graphics pipeline .

The `glLineWidth` function sets the width, in pixels, of lines rendered in an OpenGL context. Adjusting this value changes the thickness of lines drawn, impacting the graphic style and visibility, especially on high-resolution displays or when highlighting important graphical elements. This function is part of the OpenGL state and affects all subsequent lines until changed .

OpenGL commands can be combined within blocks like `glBegin` and `glEnd` to sequentially render multiple graphic primitives. By defining vertices for each shape type separately, distinct graphical elements like a triangle (`GL_TRIANGLES`) and a polygon (`GL_POLYGON`) can be displayed simultaneously in a single scene. Using `glVertex2f` for each vertex and `glColor3f` to distinguish elements by color allows for a clear and full-featured graphical presentation as demonstrated in the sources where separate shapes are constructed and flushed to display .

OpenGL uses a display function registered by `glutDisplayFunc(display)` to handle window repaint events. This function is initially called when the window is created and each time the window needs to be redrawn, such as when it's uncovered after being hidden by another window. Handling these events is crucial to maintain the correct graphics output as interactions with the user, and changes to the window state require the graphical content to be refreshed .

OpenGL applications require an event-processing loop to handle user interactions, update behaviors, and manage system events. `glutMainLoop` initiates this loop, allowing the application to process events like keyboard inputs, window resizing, and repaint requests continuously. It ensures the application remains responsive and interactive, crucial for user experience and real-time graphics operations .

The `glColor3f` function establishes the current drawing color in OpenGL by specifying red, green, and blue color components. These colors affect the rendering of all subsequent vertices or primitives until the color state changes, allowing dynamic and colorful graphical displays. It's essential for distinguishing different graphical elements and enhancing visual clarity in applications .

Setting a background color with `glClearColor` defines the color used to clear the color buffer to preset all pixels to a specific color each time the scene is redrawn. This ensures that any graphics not redrawn in a frame won't remain from the prior frame, thus resetting the canvas completely, which is vital for accurate rendering in OpenGL applications .

Incorrect color modes or misalignment between specified and intended colors in OpenGL can lead to visually misleading outputs, such as a red object appearing in an unintended color like blue or black. For instance, using `glColor3f(1.0f, 0.0f, 0.0f)` sets the color to red, but if a user mistakenly sets another value thinking it changes red, the output might not match the intended visual design, impacting clarity and potentially causing misunderstandings in user interpretation .

You might also like