OpenGL Tutorial Jason Lawrence COS426 Precept Notes 2/16/04.

Slides:



Advertisements
Similar presentations
Chapter 2: Graphics Programming
Advertisements

Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive.
David Luebke5/16/2015 Administrivia l Back on track: canceling OpenGL lecture 2 l Assignment 1 –Greg Yukl found an alternate XForms site:
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
#4: OpenGL Implementation & Project 2 CSE167: Computer Graphics TAs: Alex Kozlowski & Cameron Chrisman UCSD, Winter 2006.
Shading in OpenGL Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
OpenGL and Projections
CS 248 OpenGL Help Session CS248 Presented by Sean Walker
Computer Graphics (Spring 2008) COMS 4160, Lecture 14: OpenGL 3
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Models and Architectures Ed Angel Professor of Computer Science, Electrical and Computer.
Lighting & Material. Example 1/5 #include ” glut.h ” void display(); void reshape(int, int); void lighting(); int main(int argc, char** argv) { glutInit(&argc,
Illumination and Shading. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
Development of Interactive 3D Virtual World Applications
3D Rendering with JOGL Introduction to Java OpenGL Graphic Library By Ricardo Veguilla
CSE 872 Dr. Charles B. Owen Advanced Computer Graphics1 CSE 872 Advanced Computer Graphics Charles B. Owen (Instructor) – 1138 E. B., MW 12:30-1:50pm.
1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III.
CAP4730: Computational Structures in Computer Graphics Introduction to OpenGL.
3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!
1 OpenGL Basics A Graphics Standard ©Mel Slater, Anthony Steed
Shading in OpenGL.
Graphics Architectures & OpenGL API Introduction Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
Computer Graphics I, Fall 2010 Shading in OpenGL.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
CSC 461: Lecture 3 1 CSC461 Lecture 3: Models and Architectures  Objectives –Learn the basic design of a graphics system –Introduce pipeline architecture.
Introduction to OpenGL 1. 2 OpenGL A Graphics rendering API introduced in 1992 by Silicon Graphics Inc Provide the low-level functions to access graphics.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1Computer Graphics Lecture 4 - Models and Architectures John Shearer Culture Lab – space 2
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Computer Science Term 1, 2006 Tutorial 2 Assignment 3 – The Virtual World.
Intro to OpenGL Transformations CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003.
OpenGL Color and Lighting 2003 Spring Keng Shih-Ling.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Programming with OpenGL Review.
OpenGL Lighting Jian-Liang Lin 2002 Hidden-Surface Removal -1 Original Code: while (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT);
Project 5 Lamp Shader Fri, Nov 7, 2003 Due Mon, Nov 17, 2003.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
NoufNaief.net TA: Nouf Al-harbi.
David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke
Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall.
1 Angel: Interactive Computer Graphics5E © Addison- Wesley 2009 Image Formation Fundamental imaging notions Fundamental imaging notions Physical basis.
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.
CS559: Computer Graphics Lecture 12: OpenGL: ModelView Li Zhang Spring 2010.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL Based on GL (graphics library) by Silicon Graphics Inc. (SGI) Advantages: Runs on everything, including.
CS380 LAB II OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
CSC Graphic Programming Lecture 2 OpenGL Lightning.
CSC Graphics Programming
Administrivia Back on track: canceling OpenGL lecture 2 Assignment 1
Jason Lawrence COS426 Precept Notes 2/16/04
3D Computer Graphics (3080/GV10) Week 5-6 Tutorial 3
Reference1. [OpenGL course slides by Rasmus Stenholt]
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Lighting and Shading Lab 8:.
CSC461: Lecture 24 Lighting and Shading in OpenGL
Introduction to OpenGL
Lighting – Material Properties
Computer Graphics 4Practical Lesson
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Computer Graphics 5Practical Lesson
Lighting and Shading Lab 8:.
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

OpenGL Tutorial Jason Lawrence COS426 Precept Notes 2/16/04

OpenGL IS an API OpenGL IS nothing more than a set of functions you call from your program (think of as collection of.h file(s)). Hides the details of the display adapter, operating system, etc. Comprises several libraries with varying levels of abstraction: GL, GLU, and GLUT

OpenGL Hierarchy Several levels of abstraction are provided GL Lowest level: vertex, matrix manipulation glVertex3f(point.x, point.y, point.z) GLU Helper functions for shapes, transformations gluPerspective( fovy, aspect, near, far ) GLUT Highest level: Window and interface management glutSwapBuffers()

OpenGL Implementations OpenGL IS an API (think of as collection of.h files): #include Windows, Linux, UNIX, etc. all provide a platform specific implementation. Windows: opengl32.lib glu32.lib glut32.lib Linux: -l GL -l GLU –l GLUT

OpenGL API As a programmer, you need to do the following things: Specify the location/parameters of camera. Specify the geometry (and appearance). Specify the lights (optional). OpenGL will compute the resulting 2D image!

OpenGL Conventions Many functions have multiple forms: glVertex2f, glVertex3i, glVertex4dv, etc. Number indicates number of arguments Letters indicate type f: float, d: double, ub: unsigned byte, etc. ‘v’ (if present) indicates a single pointer argument glVertex3f(point.x, point.y, point.z) glVertex3fv(point)

OpenGL: Camera Two things to specify: Physical location of camera in the scene (MODELVIEW matrix in OpenGL).  Where is the camera?  Which direction is it pointing?  What is the orientation of the camera? Projection properties of the camera (PROJECTION matrix in OpenGL):  Depth of field?  Field of view in the x and y directions?

OpenGL: Camera Coordinate-systems are represented as matrices in OpenGL. Think of camera as implying a coordinate-system centered at its image plane. Therefore, specifying this matrix implies specifying the physical properties of the camera.

OpenGL: MODELVIEW glMatrixMode(GL_MODELVIEW); // Specify matrix mode glLoadIdentity(); // Clear modelview matrix //Utility function provided by the GLU API (included with OpenGL) gluLookAt(eyex,eyey,eyez,centerx,centery,centerz,upx,upy,upz); World x y z eye up = y’ center = (0,0,0) Point p in world is: (MODELVIEW * p) in camera coord-sys!!! Camera p z’ x’

OpenGL: MODELVIEW World coord-sys:Camera coord-sys:

OpenGL: PROJECTION Intrinsic (optical) properties of camera: center near far fov z x y

OpenGL: PROJECTION glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(fovy, aspect, near, far); Camera viewport: x y z near far h w aspect = w/h

OpenGL: Setting Camera Assume window is width x height: void SetCamera() { glViewport(0, 0, width, height); /* Set camera position */ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(m_vEye[0], m_vEye[1], m_vEye[2], m_vRef[0], m_vRef[1], m_vRef[2], m_vUp[0], m_vUp[1], m_vUp[2]); /* Set projection frustrum */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(m_fYFOV, width / height, m_fNear, m_fFar); }

OpenGL: Geometry Specify geometry using primitives: triangles, quadrilaterals, lines, etc…

OpenGL: Geometry Specify geometry using primitives: triangles, quadrilaterals, lines, points, etc…

OpenGL: Geometry Specify geometry using primitives: triangles, quadrilaterals, lines, points, etc…

OpenGL: Geometry Specify geometry using primitives: triangles, quadrilaterals, lines, points, etc…

OpenGL: Triangle Mesh Represent the surface of an object by a collection of oriented triangles: Specify vertices ccwise around normal!!!! normal

OpenGL: glBegin()…glEnd() Geometry passed btwn glBegin(.), glEnd(.)!!! glBegin(GL_TRIANGLES); for (int i=0; i<ntris; i++) { glColor3f(tri[i].r0,tri[i].g0,tri[i].b0); // Color of vertex glNormal3f(tri[i].nx0,tri[i].ny0,tri[i].nz0); // Normal of vertex glVertex3f(tri[i].x0,tri[i].y0,tri[i].z0); // Position of vertex … glColor3f(tri[i].r2,tri[i].g2,tri[i].b2); glNormal3f(tri[i].nx2,tri[i].ny2,tri[i].nz2); glVertex3f(tri[i].x2,tri[i].y2,tri[i].z2); } glEnd(); // Sends all the vertices/normals to the OpenGL library

OpenGL: glBegin()…glEnd() OpenGL supports many primitives: glBegin(GL_LINES); glBegin(GL_QUADS); glBegin(GL_POLYGON); Use GL_LINES to specify a line in 3D space (such as a ray!!!). All vertices can have a normal (we will see why next!).

OpenGL: Lighting OpenGL computes the color at each vertex with a shading calculation: color=ambient; for (int i=0; i<nlights; i++) color += (diffuse + specular * dot(n,h) shine ) * cos(θ in ) * light_color[i]; camera n light θ in h h = norm(light+camera) argmax(dot(n,h)): h=n!

OpenGL: Lighting GLfloat mat_diffuse[4] = {0.75, 0.75, 0.75, 1.0}; GLfloat mat_specular[4] = {0.55, 0.55, 0.55, 1.0}; GLfloat mat_shininess[1] = {80}; glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); GLfloat light0_ambient[4] = { 0.0, 0.0, 0.0, 1.0}; GLfloat light0_color[4] = { 0.4, 0.4, 0.4, 1.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color); glLightfv(GL_LIGHT0, GL_SPECULAR, light0_color); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING);

OpenGL: Lighting GLfloat mat_diffuse[4] = {0.75, 0.75, 0.75, 1.0}; GLfloat mat_specular[4] = {0.55, 0.55, 0.55, 1.0}; GLfloat mat_shininess[1] = {80}; glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,mat_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); GLfloat light0_ambient[4] = { 0.0, 0.0, 0.0, 1.0}; GLfloat light0_color[4] = { 0.4, 0.4, 0.4, 1.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color); glLightfv(GL_LIGHT0, GL_SPECULAR, light0_color); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING);

OpenGL: Lighting There are two “shading modes” in OpenGL: Flat shading: entire face is the color computed at the 0 th vertex. Gouraud (“smooth”) shading: color computed at each vertex and interpolated across polygon.

OpenGL: Flat Shading glShadeModel(GL_FLAT);

OpenGL: Flat Shading glShadeModel(GL_SMOOTH);

OpenGL: Lighting is Optional! Lighting is an option! glEnable(GL_LIGHTING); glDisable(GL_LIGHTING); When disabled, the color of each vertex is directly glColor3f(r,g,b). Do not need lighting when debugging your ray tracer.

GLUT GLUT is a very simple API that supports creating GUI+OpenGL apps.

GLUT Call these functions in your from main: /* Initialize glut engine */ glutInit(&argc,argv); glutInitWindowPosition(100,100); glutInitWindowSize(window_width,window_height); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); main_window = glutCreateWindow(“Ray Viewer"); /* Register callbacks */ glutDisplayFunc(MainRedraw); // Main redraw function /* Run the interactive interface */ glutMainLoop();

GLUT: MainRedraw void MainRedraw() { glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SetCamera(); DrawGeometry(); glutSwapBuffers(); // Update the screen }

GLUT: Example Skeleton code for a simple mesh viewer can be found at: The program reads a “simple mesh” file format. The zip file includes several examples.

GLUT: Example

OpenGL: Resources Textbook! Nate Robbins OpenGL tutorials: