The OPENGL Basic Graphics Primitives
By
T. Sree Sharmila Assistant Professor, Department of Information Technology, SSN College of Engineering.
Email: sreesharmilat@[Link]
Agenda
About OPENGL How to start OPENGL? OpenGL Libraries Geometric Primitives in OpenGL 3D Object Creation 3D Scene Creation
History of OpenGL
Silicon Graphics (SGI) revolutionized the graphics workstation by implementing the pipeline in hardware (1982) To access the system, application programmers used a library called GL With GL, it was relatively simple to program three dimensional interactive applications
OpenGL: What is It?
The success of GL lead to OpenGL (1992), a platform-independent API that was Easy to use Close enough to the hardware to get excellent performance Focus on rendering
OpenGL Evolution
Controlled by an Architectural Review Board (ARB) Members include SGI, Microsoft, Nvidia, HP, 3DLabs, IBM,.
Getting Started
In OPENGL->dll,include,lib Copy these Opengl->lib->VC98->lib Opengl->include->VC98->include Opengl->dll->c:\windows\system\->dll
Getting Started
To start your own program in VC++ 6.0 do the following. 1) Start VC++ 2) File->New->Open a console application 3) Select an "empty project" and pick a name and directory 4) File->New->C++ source (pick a name and directory) 5) You are ready to go! To compile, build and execute, see the Build menu (or toolbar) If some particular errors goto project->Settings->c/c++>precompiled headers->Not using precompiled headers
OpenGL Libraries
GL (Graphics Library): Library of 2-D, 3-D drawing primitives and operations GLU (GL Utilities): Miscellaneous functions dealing with camera set-up and higher-level shape descriptions GLUT (GL Utility Toolkit): Window-system independent toolkit with numerous utility functions, mostly dealing with user interface
Software Organization
Application Program
OpenGL Motif widget or similar
GLUT GLU GL
GLX, AGL or WGL
X, Win32, Mac O/S software and/or hardware
Simple Window Creation
#include <GL/glut.h> void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); glFlush(); } int main(int argc, char** argv) { glutCreateWindow("simple"); glutDisplayFunc(mydisplay); glutMainLoop(); return 0; }
OpenGL Command Formats glVertex2f(x, y)
Add v for vector form number of Components/ Dimensions 2 (x,y) 3 (x,y,z) b ub s us i ui f d byte unsigned byte short unsigned short int unsigned int float double glVertex2fv(v)
OpenGL function format
function name glVertex3f(x,y,z) belongs to GL library x,y,z are floats
dimensions
glVertex3fv(p) p is a pointer to an array
Simple window with 2D Square
#include <GL/glut.h> void mydisplay(){ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); glFlush(); } int main(int argc, char** argv){ glutCreateWindow("simple Window with 2D Square"); glutDisplayFunc(mydisplay); glutMainLoop(); return 0; }
Simple Window Creation-Revisited
#include <GL/gl.h> #include <GL/glut.h> void display (void){ glClearColor (0.0, 0.0, 0.0, 1.0); glClear (GL_COLOR_BUFFER_BIT); glFlush(); } int main (int argc, char **argv){ glutInit (&argc, argv); glutInitDisplayMode (GLUT_SINGLE); glutInitWindowSize (500,500); glutInitWindowPosition (100, 100); glutCreateWindow ("Simple Window"); glutDisplayFunc (display); glutMainLoop(); return 0; }
Specifying Geometric primitives
Primitives are specified using glBegin(primType); // define your vertices here glEnd();
primType: GL_POINTS, GL_LINES, GL_TRIANGLES, GL_QUADS,
OpenGL: Drawing Triangles
You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd(): float v1[3], v2[3], v3[3], v4[3]; glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd();
OpenGL: Triangle Strips
An OpenGL triangle strip primitive reduces this redundancy by sharing vertices:
glBegin(GL_TRIANGLE_STRIP); glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v4); glVertex3fv(v5); glEnd();
v0
v2
v4 v5
v1
v3
triangle 0 is v0, v1, v2 triangle 1 is v2, v1, v3 triangle 2 is v2, v3, v4 triangle 3 is v4, v3, v5
OpenGL: Triangle Fan
The GL_TRIANGLE_FAN primitive is another way to reduce vertex redundancy: v3 v4 v5
v2
v0 v6
v1
Primitive Types All primitives are specified by vertices:
GL_LINES GL_POINTS GL_LINE_STRIP GL_LINE_LOOP GL_POLYGON
GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP
Points in OpenGL
glBegin(GL_POINTS); glBegin(GL_POINTS); glVertex2fv(p0); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p6); glVertex2fv(p7); glVertex2fv(p7); glEnd(); glEnd();
p7 p6 p5
p0
p1 p2 p3
p4
Lines in OpenGL (1/3)
Line Segments
glBegin(GL_LINES); glBegin(GL_LINES); glVertex2fv(p0); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p6); glVertex2fv(p7); glVertex2fv(p7); glEnd(); glEnd();
p0 p7 p6
p1
p2
p5 p4
p3
Lines in OpenGL (2/3)
Polylines Line Strip
glBegin(GL_LINE_STRIP); glBegin(GL_LINE_STRIP); glVertex2fv(p0); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p6); glVertex2fv(p7); glVertex2fv(p7); glEnd(); glEnd();
p0 p7 p6
p1
p2
p5 p4
p3
Lines in OpenGL (3/3)
Polylines Line Loop
glBegin(GL_LINE_LOOP); glBegin(GL_LINE_LOOP); glVertex2fv(p0); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p6); glVertex2fv(p7); glVertex2fv(p7); glEnd(); glEnd();
p0 p7 p6
p1
p2
p5 p4
p3
Polygons in OpenGL (1/6)
Polygon
glBegin(GL_POLYGON); glBegin(GL_POLYGON); glVertex2fv(p0); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p6); glVertex2fv(p7); glVertex2fv(p7); glEnd(); glEnd();
p0 p7 p6
p1
p2
p5 p4
p3
Polygons in OpenGL (2/6)
Quadrilaterals
glBegin(GL_QUADS); glBegin(GL_QUADS); glVertex2fv(p0); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p6); glVertex2fv(p7); glVertex2fv(p7); glEnd(); glEnd();
p0 p7 p6
p1
p2
p5 p4
p3
Polygons in OpenGL (3/6)
Quadstrip
glBegin(GL_QUAD_STRIP); glBegin(GL_QUAD_STRIP); glVertex2fv(p1); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p3); glVertex2fv(p0); glVertex2fv(p0); glVertex2fv(p4); glVertex2fv(p4); glVertex2fv(p7); glVertex2fv(p7); glVertex2fv(p5); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p6); glEnd(); glEnd();
p0 p7 p6
p1
p2
p5 p4
p3
Polygons in OpenGL (4/6)
Triangles
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES); glVertex2fv(p0); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p6); glVertex2fv(p7); glVertex2fv(p7); glEnd(); glEnd();
p0 p7 p6
p1
p2
p5 p4
p3
Polygons in OpenGL (5/6)
Triangle Strip
glBegin(GL_TRIANGLE_STRIP); glBegin(GL_TRIANGLE_STRIP); glVertex2fv(p0); glVertex2fv(p0); glVertex2fv(p7); glVertex2fv(p7); glVertex2fv(p1); glVertex2fv(p1); glVertex2fv(p6); glVertex2fv(p6); glVertex2fv(p2); glVertex2fv(p2); glVertex2fv(p5); glVertex2fv(p5); glVertex2fv(p3); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p4); glEnd(); glEnd();
p0 p7 p6
p1
p2
p5 p4
p3
Polygons in OpenGL (6/6)
Triangle Fan
glBegin(GL_TRIANGLE_FAN); glBegin(GL_TRIANGLE_FAN); glVertex2fv(p0); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p6); glVertex2fv(p7); glVertex2fv(p7); glEnd(); glEnd();
p0 p7 p6
p1
p2
p5 p4
p3
3D Object With OpenGL
Creation of 3D Cube
Create an array for each vertices (x,y,z): GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble V0[] V1[] V2[] V3[] V4[] V5[] V6[] V7[] = = = = = = = = { { { { { { { { 0.0, 0.0, 0.0}; 1.0f, 0.0, 0.0}; 1.0f, 1.0f, 0.0}; 0.0, 1.0f, 0.0}; 0.0, 0.0, -1.0f}; 1.0f, 0.0, -1.0f}; 1.0f, 1.0f, -1.0f}; 0.0, 1.0f, -1.0f};
OpenGL: Front/Back Rendering
Each polygon has two sides, front and back OpenGL can render the two differently The ordering of vertices in the list determines which is the front side: When looking at the front side, the vertices go counterclockwise This is basically the right-hand rule Note that this still holds after perspective projection
Creation of 3D Cube Create surface 1
// Surface 1 glVertex3dv(V0); glVertex3dv(V1); glVertex3dv(V2); glVertex3dv(V3);
glVertex3f( glVertex3f( glVertex3f( glVertex3f(
0.0, 0.0, 0.0); // V0 0,0,0) 1.0f, 0.0, .0); // V1 (1,0,0) 1.0f, 1.0f, 0.0);// V2 (1,1,0) 0.0, 1.0f, 0.0);// V3(0,1,0)
Creation of 3D Cube
3D Object Creation-Program
//===============================================// // Title : 3D Object in OpenGL //===============================================// #include <stdio.h> #include <gl/glut.h> GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble V0[] V1[] V2[] V3[] V4[] V5[] V6[] V7[] = = = = = = = = { { { { { { { { 0.0, 0.0, 0.0}; 1.0f, 0.0, 0.0}; 1.0f, 1.0f, 0.0}; 0.0, 1.0f, 0.0}; 0.0, 0.0, -1.0f}; 1.0f, 0.0, -1.0f}; 1.0f, 1.0f, -1.0f}; 0.0, 1.0f, -1.0f};
void Display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(3.0, 2.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glColor3f(0.0, 1.0, 0.0); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBegin(GL_QUADS); glVertex3dv(V0); glVertex3dv(V1); glVertex3dv(V1); glVertex3dv(V5); glVertex3dv(V5); glVertex3dv(V4); glVertex3dv(V4); glVertex3dv(V0); glVertex3dv(V3); glVertex3dv(V2); glVertex3dv(V0); glVertex3dv(V4); glEnd(); glutSwapBuffers(); }
glVertex3dv(V2); glVertex3dv(V6); glVertex3dv(V7); glVertex3dv(V3); glVertex3dv(V6); glVertex3dv(V5);
glVertex3dv(V3); glVertex3dv(V2); glVertex3dv(V6); glVertex3dv(V7); glVertex3dv(V7); glVertex3dv(V1);
// // // // // //
Surface Surface Surface Surface Surface Surface
1 2 3 4 5 6
3D Object Creation-Program
void Init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); } void Resize(int width, int height) { glViewport(0, 0, width, height);//set the viewport glMatrixMode(GL_PROJECTION); //specify which matrix is the current matrix glLoadIdentity(); gluPerspective(60.0, width/height, 0.1, 1000.0); //set up a perspective projection matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(400, 400); glutInitWindowPosition(200, 200); glutCreateWindow("3D Object in OpenGL"); Init(); glutDisplayFunc(Display); glutReshapeFunc(Resize);//sets the reshape call back for the current window glutMainLoop(); return 0; }
3D-Object Creation output
3D Scene creation
Mainly Concentrate on Adding some Perspective Building a Camera Lighting the Scene
Parallel Projection
Principles of parallel projection
Far Plane Window Near Plane
Window
Perspective Projection
Principles of perspective projection
Near Plane
Window
Far Plane
Window
The Camera and Perspective Projection
The camera has an eye (or view reference point VRP) at some point in space. Its view volume is a portion of a pyramid, whose apex is at the eye. The straight line from a point P to the eye is called the projector of P. (All projectors of a point meet at the eye.) The axis of the view volume is called the view plane normal, or VPN. The opening of the pyramid is set by the viewangle .
The Camera and Perspective Projection
Three planes are defined perpendicular to the VPN: the near plane, the view plane, and the far plane. Where the planes intersect the VPN they form rectangular windows. The windows have an aspect ratio which can be set in a program. OpenGL clips points of the scene lying outside the view volume. Points P inside the view volume are projected onto the view plane to a corresponding point P (part c). Finally, the image formed on the view plane is mapped into the viewport (part c), and becomes visible on the display device.
Setting the View Volume
The default camera position has the eye at the origin and the VPN aligned with the z-axis. The programmer defines a look point as a point of particular interest in the scene, and together the two points eye and look define the VPN as eye look. This is normalized to become the vector n, which is central in specifying the camera properly. (VPN points from look to eye.)
Setting the View Volume
Setting the View Volume
To view a scene, we move the camera and aim it in a particular direction. To do this, perform a rotation and a translation, which become part of the modelview matrix. glMatrixMode(GL_MODELVIEW); // make the modelview matrix current glLoadIdentity(); // start with a unit matrix gluLookAt(eye.x, eye.y, eye.z, look.x, look.y, look.z, up.x, up.y, up.z);
Setting the View Volume
As before, this moves the camera so its eye resides at point eye, and it looks towards the point of interest, look. The upward direction is generally suggested by the vector up, which is most often set simply to (0, 1, 0).
Camera with Arbitrary Orientation and Position
A camera can have any position and orientation in the scene. Imagine a transformation that picks up the camera and moves it somewhere in space, then rotates it around to aim it as desired. To do this we need a coordinate system attached to the camera: u, v, and n.
Camera with Arbitrary Orientation and Position
v points vertically upward, n away from the view volume, and u at right angles to both n and v. The camera looks toward -n. All are normalized.
gluLookAt and the Camera Coordinate System
gluLookAt takes the points eye and look, and the vector up n must be parallel to eye - look, so it sets n = eye - look u points "off to the side", so it makes u perpendicular to both n and up: u = up x n v must be perpendicular to n and u, so it lets v = nxu
gluLookAt and the Camera Coordinate System
Effect of gluLookAt
Lighting the Scene
Assume to start with that light has no color, only brightness: R = G = B Assume we also have a point source of light (sun or lamp) and general ambient light, which doesn't come directly from a source, but through windows or scattered by the air. Ambient light comes equally from all directions. Point-source light comes from a single point.
Lighting the Scene
When light hits an object, some light is absorbed (and turns into heat), some is reflected, and some may penetrate the interior (e.g., of a clear glass object). If all the light is absorbed, the object appears black and is called a blackbody. If all the light is transmitted, the object is visible only through refraction
When light is reflected from an object, some of the reflected light reaches our eyes, and we see the object.
Diffuse reflection: some of the light slightly penetrates the surface and is re-radiated uniformly in all directions. The light takes on some fraction of the color of the surface. Specular reflection: more mirror-like. Light is reflected directly from the objects outer surface, giving rise to highlights of approximately the same color as the source. The surface looks shiny.
Lighting the Scene
To compute the ambient, diffuse, and specular light contribution to the scene. The total amount of light I that reaches the eye from point P,
I
a
I
d
I
sP
Summary
Graphics Rendering API high-quality color images composed of geometric and image primitives device independent operating system independent Geometric primitives points, lines and polygons Image Primitives images and bitmaps Rendering depends on state colors, materials, light sources, etc.