The OPENGL Basic Graphics Primitives
The OPENGL Basic Graphics Primitives
The OPENGL Basic Graphics Primitives
By
T. Sree Sharmila Assistant Professor, Department of Information Technology, SSN College of Engineering.
Email: [email protected]
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 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
dimensions
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
v2
v0 v6
v1
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
p0 p7 p6
p1
p2
p5 p4
p3
p0 p7 p6
p1
p2
p5 p4
p3
p0 p7 p6
p1
p2
p5 p4
p3
p0 p7 p6
p1
p2
p5 p4
p3
p0 p7 p6
p1
p2
p5 p4
p3
p0 p7 p6
p1
p2
p5 p4
p3
p0 p7 p6
p1
p2
p5 p4
p3
p0 p7 p6
p1
p2
p5 p4
p3
p0 p7 p6
p1
p2
p5 p4
p3
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};
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(); }
// // // // // //
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 Scene creation
Mainly Concentrate on Adding some Perspective Building a Camera Lighting the Scene
Parallel Projection
Perspective Projection
Principles of perspective projection
Near Plane
Window
Far Plane
Window
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).
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.
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.