OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.

Slides:



Advertisements
Similar presentations
Chapter 2: Graphics Programming
Advertisements

OPENGL.
© 2004, Tom Duff and George Ledin Jr1 Lectures OpenGL Introduction By Tom Duff Pixar Animation Studios Emeryville, California and George Ledin Jr Sonoma.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
CSE 494/598 Intro to Applied Computer Graphics Anshuman Razdan DCST AR's Web Page AR's Web Page
Computer Graphics CS 385 February 7, Fundamentals of OpenGl and Glut Today we will go through the basics of a minimal OpenGl Glut project, explaining.
Introduction to OpenGL M. Ramanathan STTP CAD 2011Introduction to OpenGL.
Introduction to OpenGL Jian Huang This set of slides are extracted from the Interactive OpenGL Programming course given by Dave Shreine, Ed Angel and Vicki.
OpenGL A Brief Overview. What is OpenGL?  It is NOT a programming language.  It is a Graphics Rendering API consisting of a set of function with a well.
Reference1. [OpenGL course slides by Rasmus Stenholt]
CS380 LAB I OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2.
COS 397 Computer Graphics Assoc. Prof. Svetla Boytcheva AUBG 2013 COS 397 Computer Graphics Practical Session №1 Introduction to OpenGL, GLFW and CG.
Basic OpenGL Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, September 10, 2003.
Drawing Basic Graphics Primitives Lecture 4 Wed, Sep 3, 2003.
Computer Graphics CS 385 January 31, Fractals Some definitions Object which is self-similar at all scales. Regardless of scale the same level of.
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
Draw a Simple Object. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.
CA 302 Computer Graphics and Visual Programming Lecture 2: Introduction to OpenGL Aydın Öztürk
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
NoufNaief.net TA: Nouf Al-harbi.
OpenGL: The Open Graphics Language Introduction By Ricardo Veguilla.
NoufNaief.net 1 TA: Nouf Al-Harbi.
Doç. Dr. Cemil Öz SAÜ Bilgisayar Mühendisliği Dr. Cemil Öz.
Chap 3 Viewing and Transformation
Geometric Primitives Used in Open GL Drawing Triangles glBegin(GL_TRIANGELS); glVertex2i(p0); glVertex2i(p1); glVertex2i(p2); glVertex2i(p3); glVertex2i(p4);
CSCE 441: Computer Graphics
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
CS552: Computer Graphics Lecture 6: Viewing in 2D.
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
OpenGL: Introduction #include main() { OpenWindow() ; glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0,
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
CIS 681 Review: OpenGL. CIS 681 Command Syntax OpenGL commands start with a gl. This is followed by the base command such as Color. Followed by the number.
Introduction to OpenGL Muhammad Aamir Khan Lecturer, DCS, UOP.
Introduction to Graphics Programming. Graphics API.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when.
OpenGL: The Open Graphics Language Technology and Historical Overview By Ricardo Veguilla.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Computer Graphics -practical- Lecture 6. (visual c++) open gl library To use open GL with VC++ we add these files:- 1)Glut.h C:\program files\ Microsoft.
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
CSC Graphics Programming
The Human Visual System vs The Pinhole camera
Programming with OpenGL Part 2: Complete Programs
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
OpenGL A Brief Overview.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
Starting to draw dealing with Windows which libraries? clipping
גרפיקה ממוחשבת: מבוא ל-OpenGL
Drawing in the plane 455.
Introduction to OpenGL
Line and Curve Drawing Algorithms
OpenGL program.
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Starting to draw dealing with Windows which libraries? clipping
Programming with OpenGL Part 2: Complete Programs
Introduction to OpenGL Programming
OpenGL A Brief Overview.
Presentation transcript:

OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE

GRAPHICS PROGRAMMING USING OPENGL OpenGL is a software interface that allows you to access the graphics hardware without taking care of the hardware details or which graphics adapter is in the system. OpenGL is a low-level graphics library specification. It makes available to the programmer a small set of geomteric primitives - points, lines, polygons, images, and bitmaps.

Include Files For all OpenGL applications, you want to include the gl.h header file in every file. Almost all OpenGL applications use GLU, the aforementioned OpenGL Utility Library, which also requires inclusion of the glu.h header file. So almost every OpenGL source file begins with: #include #include If you are using the OpenGL Utility Toolkit (GLUT) for managing your window manager tasks, you should include: #include

Working with OpenGL Opening a window for Drawing The First task in making pictures is to open a screen window for drawing. The following five functions initialize and display the screen window in our program. 1. glutInit(&argc, argv) The first thing we need to do is call the glutInit() procedure. It should be called before any other GLUT routine because it initializes the GLUT library.

2. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) The next thing we need to do is call the glutInitDisplayMode() procedure to specify the display mode for a window. 3. glutInitWindowSize(640,480) We need to create the characteristics of our window. A call to glutInitWindowSize() will be used to specify the size, in pixels, of our inital window. The arguments indicate the width and height (in pixels) of the requested window.

4. glutInitWindowPosition(100,150) Similarly, glutInitWindowPosition() is used to specify the screen location for the upper-left corner of our initial window 5. glutCreateWindow(“Example”) To create a window, the with the previously set characteristics (display mode, size, location, etc), theprogrammer uses the glutCreateWindow() command. 6. glutMainLoop() The window is not actually displayed until the glutMainLoop() is entered.

The parameter mode of the function glBegin() can be one of the following: GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP GL_POLYGON

The following list explains the function of each of the five constants: GL_TRIANGLES: takes the listed vertices three at a time, and draws a separate triangle for each; GL_QUADS: takes the vertices four at a time and draws a separate quadrilateral for each GL_TRIANGLE_STRIP: draws a series of triangles based on triplets of vertices: v0, v1, v2, then v2, v1, v3, then v2, v3, v4, etc. (in an order so that all triangles are “traversed” in the same way;e.g.counterclockwise). GL_TRIANGLE_FAN: draws a series of connected triangles based on triplets of vertices: v0, v1, v2, thenv0, v2, v3, then v0, v3, v4, etc. GL_QUAD_STRIP: draws a series of quadrilaterals based on foursomes of vertices: first v0, v1, v3, v2, then v2, v3, v5, v4, then v4, v5, v7, v6 (in an order so that all quadrilateralsare “traversed” in the same way; e.g. counterclockwise).

//the following code plots three dots glBegin(GL_POINTS); glVertex2i(100, 50); glVertex2i(100, 130); glVertex2i(150, 130); glEnd( ); // the following code draws a triangle glBegin(GL_TRIANGLES); glVertex3f(100.0f, 100.0f, 0.0f); glVertex3f(150.0f, 100.0f, 0.0f); glVertex3f(125.0f, 50.0f, 0.0f); glEnd( ); // the following code draw a lines glBegin(GL_LINES); glVertex3f(100.0f, 100.0f, 0.0f); // origin of the line glVertex3f(200.0f, 140.0f, 5.0f); // ending point of the line glEnd( );

OpenGL Data Types

OpenGl State OpenGl keeps track of many state variables, such as current size of a point, the current color of a drawing, the current background color, etc. The value of a state variable remains active until new value is given. glPointSize() : The size of a point can be set with glPointSize(), which takes one floating point argument Example : glPointSize(4.0); glClearColor() : establishes what color the window will be cleared to. The background color is set with glClearColor(red, green, blue, alpha), where alpha specifies a degree of transparency Example : glClearColor (0.0, 0.0, 0.0, 0.0); //set black background color glClear(GL_COLOR_BUFFER_BIT);

Example: glColor3f(0.0, 0.0, 0.0); //black glColor3f(1.0, 0.0, 0.0); //red glColor3f(0.0, 1.0, 0.0); //green glColor3f(1.0, 1.0, 0.0); //yellow glColor3f(0.0, 0.0, 1.0); //blue glColor3f(1.0, 0.0, 1.0); //magenta glColor3f(0.0, 1.0, 1.0); //cyan glColor3f(1.0, 1.0, 1.0); //white

gluOrtho2D(): specifies the coordinate system in two dimension void gluOrtho2D (GLdouble left, GLdouble right, GLdouble bottom,GLdouble top); Example : gluOrtho2D(0.0, 640.0, 0.0, 480.0); glOrtho() : specifies the coordinate system in three dimension Example : glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glFlush() : ensures that the drawing commands are actually executed and sent to the display.

Rectangle on a Black Background (3- Dimension co-ordinates) #include "stdafx.h" #include "gl/glut.h" #include void Display(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON); glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0); glEnd(); glFlush(); }

int main (int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640,480); glutCreateWindow("Intro"); glClearColor(0.0,0.0,0.0,0.0); glutDisplayFunc(Display); glutMainLoop(); return 0; }

Drawing three dimensional objects & Drawing three dimensional scenes OpenGL has separate transformation matrices for different graphics features glMatrixMode(GLenum mode), where mode is one of: GL_MODELVIEW - for manipulating model in scene GL_PROJECTION - perspective orientation GL_TEXTURE - texture map orientation glLoadIdentity(): loads a 4-by-4 identity matrix into the current matrix glPushMatrix() : push current matrix stack glPopMatrix() : pop the current matrix stack

glMultMatrix () : multiply the current matrix with the specified matrix glViewport() : set the viewport Example : glViewport(0, 0, width, height); gluPerspective() : function sets up a perspective projection matrix. Format : gluPerspective(angle, asratio, ZMIN, ZMAX); Example : gluPerspective(60.0, width/height, 0.1, 100.0); glutSwapBuffers () : glutSwapBuffers swaps the buffers of the current window if double buffered.

// Triangle glBegin( GL_TRIANGLES ); glVertex3f( -0.5f, -0.5f, ); glVertex3f( 0.5f, -0.5f, ); glVertex3f( 0.0f, 0.5f, ); glEnd(); // Quads in different colours glBegin(GL_QUADS); glColor3f(1,0,0); //red glVertex3f(-0.5, -0.5, 0.0); glColor3f(0,1,0); //green glVertex3f(-0.5, 0.5, 0.0); glColor3f(0,0,1); //blue glVertex3f(0.5, 0.5, 0.0); glColor3f(1,1,1); //white glVertex3f(0.5, -0.5, 0.0); glEnd();

GLUT includes several routines for drawing these three-dimensional objects: cone icosahedron teapot cube octahedron tetrahedron sphere torus

OpenGL Functions for drawing the 3D Objects glutWireCube(double size); glutSolidCube(double size); glutWireCone(double radius, double height, int slices, int stacks); glutSolidCone(double radius, double height, int slices, int); glutWireTeapot(double size); glutSolidTeapot(double size);

glutWireTorus(double inner_radius, double outer_radius, int sides, int rings); glutSolidTorus(double inner_radius, double outer_radius, int sides, int rings);