CG Module 1.4 - Basics of 2D and 3D Objects

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Basics of 2D and 3D objects

CSE2066 – Computer Graphics


Module 1
Contents
• OpenGL syntax
• Header Files
• Display-Window Management Using GLUT
• A Simple program – Line segment
OpenGL syntax
• OpenGL basic / Core library – prefixed with gl
• Function name convention - glBegin, glClear, glCopyPixels, glPolygonMode
• symbolic constant - GL_2D, GL_RGB, GL_CCW, GL_POLYGON, GL_AMBIENT_AND_DIFFUSE
• data types - GLbyte, GLshort, GLint, GLfloat, GLdouble, Glboolean
• Library to handle special operations - OpenGL Utility (GLU)
• Object oriented toolkit based on OpenGL - Open Inventor
• provides routines and predefined object shapes for interactive three-dimensional applications
• This toolkit is written in C++
• OpenGL functions for a variety of machines
• OpenGL Extension to the X Window System (GLX) provides a set of routines
• prefixed with the letters glX
• window-system libraries
• Apple systems use the Apple GL (AGL) interface for window-management operations
• prefixed with agl
OpenGL syntax
• For Microsoft Windows systems - the WGL routines
• provide a Windows-to-OpenGL interface.
• prefixed with the letters wgl.
• The Presentation Manager to OpenGL (PGL) is an interface for the IBM OS/2
• uses the prefix pgl for the library routines. The OpenGL Utility Toolkit
• The library of functions for interacting with any screen-windowing System –
Open GL Utility Toolkit (GLUT)
• prefixed with glut
• contains methods for describing and rendering quadric curves and surfaces
• For source code - http://www.opengl.org/resources/libraries/glut/
Header Files
• windows.h - accesses the WGL routines for Microsoft Windows
• must be listed before the OpenGL and GLU
• it contains macros needed by the Microsoft Windows version of the OpenGL libraries.
• The source file would begin with
• #include <windows.h>
• #include <GL/gl.h>
• #include <GL/glu.h>
• If we use GLUT to handle the window-managing operations, we do not need to include
gl.h and glu.h because GLUT ensures that these will be included correctly.
• #include <windows.h>
• #include <GL/glut.h>
• In addition, we will often need to include header files that are required by the C++ code
• #include <stdio.h>
• #include <stdlib.h>
• #include <math.h>
Display-Window Management Using GLUT
• Consider a simplified, minimal number of operations for displaying a picture
• Initialize GLUT - glutInit (&argc, argv);
• Display window creation - glutCreateWindow (“Title");
• Title - any character string that is used for the display-window title
• Example - An Example OpenGL Program
• create a picture using OpenGL functions and pass the picture definition to the GLUT routine -
glutDisplayFunc (functionName);
• functionName may be like lineSegment
• the OpenGL code for describing a line segment in a procedure called lineSegment
• Activate the display function - glutMainLoop ( );
• This function must be the last one in the program
• displays the initial graphics and puts the program into an infinite loop
• checks for input from devices such as a mouse or keyboard
• Create the window at specific location with a defined size
• glutInitWindowPosition (50, 100);
• glutInitWindowSize (400, 300);
• Changes the predefined values
Additional functions
• glClearColor (1.0, 1.0, 1.0, 0.0);
• Set the background color – R, G, B and Alpha value
• the alpha value is as a “blending” parameter
• can be used to determine the resulting color for two overlapping objects
• 1.0 – transparent object
• 0.0 – opaque object
• glClear (GL_COLOR_BUFFER_BIT);
• OpenGL symbolic constant, GL_COLOR_BUFFER_BIT specifying that it is the bit values in the color buffer (refresh
buffer)
• glColor3f (0.0, 0.4, 0.2);
• specifying the three RGB color components using floating-point (f) values
• glMatrixMode (GL_PROJECTION);
• how we want to “project” our picture onto the display window
• generating a two-dimensional picture is treated by OpenGL as a special case of three-dimensional viewing
• gluOrtho2D (0.0, 200.0, 0.0, 150.0);
• maps the contents of a two-dimensional rectangular area of world coordinates to the screen
• Anything outside this coordinate range will not be displayed
• pastes the picture into the display window
A Simple program – Line segment
#include<windows.h>
#include <GL/glut.h> // (or others, depending on the system in use)
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
void lineSegment (void)
{
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (0.0, 0.4, 0.2); // Set line segment color to green.
glBegin (GL_LINES);
glVertex2i (180, 15); // Specify line-segment geometry.
glVertex2i (10, 145);
glEnd ( );
glFlush ( ); // Process all OpenGL routines as quickly as possible.
}
A Simple program – Line segment
void main (int argc, char** argv)
{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (50, 100); // Set top-left display-window position.
glutInitWindowSize (400, 300); // Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); // Create display window.
init ( ); // Execute initialization procedure.
glutDisplayFunc (lineSegment); // Send graphics to display window.
glutMainLoop ( ); // Display everything and wait.
}
The display window and line segment produced by the example
program

Kindly note: - Please install codeblock or visual studio to execute the code
- try implementing the code for DDA and Bresenham’s algorithms

You might also like