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

Open GL

This document discusses drawing and filling shapes in OpenGL. It provides code to initialize OpenGL, define a display callback function, and render a red quad, green triangle, blue triangle, and yellow hexagon. The main function initializes GLUT and registers the display callback. It also defines OpenGL data types like integers, floats, and enums.

Uploaded by

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

Open GL

This document discusses drawing and filling shapes in OpenGL. It provides code to initialize OpenGL, define a display callback function, and render a red quad, green triangle, blue triangle, and yellow hexagon. The main function initializes GLUT and registers the display callback. It also defines OpenGL data types like integers, floats, and enums.

Uploaded by

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

OPEN GL

Drawing and filling image in openGL


/*
* GL02Primitive.cpp: 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
// Define shapes enclosed within a pair of glBegin and glEnd
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.8f, 0.1f); // Define vertices in counter-clockwise (CCW) order
glVertex2f(-0.2f, 0.1f); // so that the normal (front-face) is facing you
glVertex2f(-0.2f, 0.7f);
glVertex2f(-0.8f, 0.7f);

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


glVertex2f(-0.7f, -0.6f);
glVertex2f(-0.1f, -0.6f);
glVertex2f(-0.1f, 0.0f);
glVertex2f(-0.7f, 0.0f);
glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray
glVertex2f(-0.9f, -0.7f);
glColor3f(1.0f, 1.0f, 1.0f); // White
glVertex2f(-0.5f, -0.7f);
glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray
glVertex2f(-0.5f, -0.3f);
glColor3f(1.0f, 1.0f, 1.0f); // White
glVertex2f(-0.9f, -0.3f);
glEnd();

glBegin(GL_TRIANGLES); // Each set of 3 vertices form a triangle


glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex2f(0.1f, -0.6f);
glVertex2f(0.7f, -0.6f);
glVertex2f(0.4f, -0.1f);
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;
}
OpenGL defines its own data types:

• Signed Integers: GLbyte (8-bit), GLshort (16-bit), GLint (32-bit).


• Unsigned Integers: GLubyte (8-bit), GLushort (16-bit), GLuint (32-bit).
• Floating-point numbers: GLfloat (32-bit), GLdouble (64-bit), GLclampf
and GLclampd (between 0.0 and 1.0).
• GLboolean (unsigned char with 0 for false and non-0 for true).
• GLsizei (32-bit non-negative integers).
• GLenum (32-bit enumerated integers).

You might also like