9
CS 4204 Computer Graphics OpenGL Basics OpenGL Basics Doug Bowman Doug Bowman (based on notes by Yong Cao) (based on notes by Yong Cao) References: References: 2001 Siggraph, “An Interactive Introduction to OpenGL Programming”, Dave Dave Shreiner Shreiner,Ed Angel, Vicki ,Ed Angel, Vicki Shreiner Shreiner Official Presentation from Text book Official Presentation from Text book “Computer Graphics using OpenGL Computer Graphics using OpenGL”, chapter 2 , chapter 2 OpenGL and GLUT Overview What is OpenGL & what can it do for me? What is OpenGL & what can it do for me? OpenGL in windowing systems OpenGL in windowing systems Why GLUT Why GLUT A GLUT program template A GLUT program template What is it for us? Open Graphics Standard Open Graphics Standard API API Library Library State Machine State Machine Pipeline Pipeline GL: Core GL: Core GLU: Higher level utilities GLU: Higher level utilities GLUT: Windowing and interaction GLUT: Windowing and interaction

Opengl Basics

Embed Size (px)

DESCRIPTION

Basic tutorials for OpenGL

Citation preview

Page 1: Opengl Basics

CS 4204 Computer Graphics

OpenGL BasicsOpenGL Basics

Doug BowmanDoug Bowman(based on notes by Yong Cao)(based on notes by Yong Cao)

References:References:2001 Siggraph, “An Interactive Introduction to OpenGL Programming”, Dave Dave ShreinerShreiner,Ed Angel, Vicki ,Ed Angel, Vicki ShreinerShreinerOfficial Presentation from Text book Official Presentation from Text book ““Computer Graphics using OpenGLComputer Graphics using OpenGL””, chapter 2, chapter 2

OpenGL and GLUT Overview

What is OpenGL & what can it do for me?What is OpenGL & what can it do for me?

OpenGL in windowing systemsOpenGL in windowing systems

Why GLUTWhy GLUT

A GLUT program templateA GLUT program template

What is it for us?Open Graphics StandardOpen Graphics Standard•• APIAPI

•• LibraryLibrary

•• State MachineState Machine

•• PipelinePipeline

GL: CoreGL: Core

GLU: Higher level utilitiesGLU: Higher level utilities

GLUT: Windowing and interactionGLUT: Windowing and interaction

Page 2: Opengl Basics

OpenGL and Related APIs

GLUT

GLU

GL

GLX, AGLor WGL

X, Win32, Mac O/S

software and/or hardware

application program

OpenGL Motifwidget or similar

PreliminariesHeaders:Headers:•• #include <#include <GL/glGL/gl.h>.h>

•• #include <#include <GL/gluGL/glu.h>.h>

•• #include "GL/glut.h#include "GL/glut.h““

Libraries:Libraries:•• glut32.lib,glut32.lib,

•• opengl32.lib,opengl32.lib,

•• glu32.libglu32.lib

Dynamic librariesDynamic libraries•• glut32.dllglut32.dll

Let’s set up anOpenGL project inXcode

Setting up a GLUT Windowint int main(main(int argcint argc, char** , char** argvargv)){{ glutInitDisplayMode glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowPosition glutInitWindowPosition (0, 0);(0, 0); glutInitWindowSize(640,640); glutInitWindowSize(640,640); glutCreateWindow(argv[0]); glutCreateWindow(argv[0]);

// register callbacks// register callbacks glutDisplayFuncglutDisplayFunc(display);(display);

myinitmyinit() ; () ; // initialize// initialize glutMainLoopglutMainLoop();(); // start the main loop// start the main loop return 0; return 0; // never reached// never reached}}

Page 3: Opengl Basics

Display functionvoid display(void) {void display(void) {

glMatrixModeglMatrixMode(GL_PROJECTION) ;(GL_PROJECTION) ;

glLoadIdentityglLoadIdentity() ;() ;

glMatrixModeglMatrixMode(GL_MODELVIEW) ;(GL_MODELVIEW) ;

glLoadIdentityglLoadIdentity();();

glClearColor(0.0f,0.0f,0.0f,0.0f); glClearColor(0.0f,0.0f,0.0f,0.0f); // set the background // set the background colourcolour

// OK, now clear the screen with the background // OK, now clear the screen with the background colourcolour

glClearglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f(0.5,0,0) ;glColor3f(0.5,0,0) ; // set the current color// set the current color

glutWireSphereglutWireSphere( 1.0, 10, 10 ) ;( 1.0, 10, 10 ) ; // draw a sphere// draw a sphere

glutSwapBuffersglutSwapBuffers();(); // swap the buffers (show the image)// swap the buffers (show the image)

}}

Elements of a scene in OpenGLGeometric PrimitivesGeometric Primitives

Material propertiesMaterial properties

Light sourcesLight sources

Copyright Pixar

Primitives in OpenGL

PointsPoints

LinesLines

Curves (piece-wise linear approximation)Curves (piece-wise linear approximation)

PolygonsPolygons

Surfaces ( polygonal approximation)Surfaces ( polygonal approximation)

Page 4: Opengl Basics

OpenGL Geometric Primitives

All geometric primitives are specified byAll geometric primitives are specified byverticesvertices

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP

GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTSGL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_QUADSGL_QUADS

OpenGL Geometric Primitives(Continued)

Simple Example

void drawRhombus( GLfloat color[] )void drawRhombus( GLfloat color[] )

{{

glBegin( GL_QUADS ); glBegin( GL_QUADS );

glColor3fv( color ); glColor3fv( color );

glVertex2f( 0.0, 0.0 ); glVertex2f( 0.0, 0.0 );

glVertex2f( 1.0, 0.0 ); glVertex2f( 1.0, 0.0 );

glVertex2f( 1.5, 1.118 ); glVertex2f( 1.5, 1.118 );

glVertex2f( 0.5, 1.118 ); glVertex2f( 0.5, 1.118 );

glEnd(); glEnd();

}}

Page 5: Opengl Basics

OpenGL Command Formats

glVertex3fv( glVertex3fv( vv ) )

Number ofNumber ofcomponentscomponents

2 - (x,y) 2 - (x,y) 3 - (x,y,z)3 - (x,y,z)4 - (x,y,z,w)4 - (x,y,z,w)

Data TypeData Typeb - byteb - byteub - unsigned byteub - unsigned bytes - shorts - shortus - unsigned shortus - unsigned shorti - inti - intui - unsigned intui - unsigned intf - floatf - floatd - doubled - double

VectorVector

omit omit ““vv”” for forscalar formscalar form

glVertex2f( x, y )glVertex2f( x, y )

Specifying Geometric Primitives

Primitives are specified usingPrimitives are specified using

glBegin( glBegin( primType primType ););

glEnd();glEnd();

•• primTypeprimType determines how vertices are combined determines how vertices are combined

GLfloat red, green, blue;GLfloat red, green, blue;Glfloat coords[3];Glfloat coords[3];glBegin( glBegin( primType primType ););for ( i = 0; i < nVerts; ++i ) { for ( i = 0; i < nVerts; ++i ) { glColor3f( red, green, blue ); glColor3f( red, green, blue ); glVertex3fv( coords ); glVertex3fv( coords );}}glEnd();glEnd();

Types

GLintGLint

GLfloatGLfloat

GLdoubleGLdouble

Page 6: Opengl Basics

Points

glBeginglBegin(GL_POINTS)(GL_POINTS)glVertex3f(GLfloat x, glVertex3f(GLfloat x, GLfloat GLfloat y, y, GLfloat GLfloat z) ;z) ;

glVertex2i(GLint x, glVertex2i(GLint x, GLint GLint y) ;y) ;

glVertex3dv(GLdouble p[3] ) ;glVertex3dv(GLdouble p[3] ) ;

glEndglEnd() ;() ;

Point details

glPointSizeglPointSize(float size) ;(float size) ;

glColor3f(GLfloat r, glColor3f(GLfloat r, GLfloat GLfloat g, g, Glfloat Glfloat b) ;b) ;

Lines

glBeginglBegin(GL_LINES)(GL_LINES)glVertex2i(x1,y1) ;glVertex2i(x1,y1) ;

glVertex2i(x2,y2) ;glVertex2i(x2,y2) ;

glVertex2i(x3,y3) ;glVertex2i(x3,y3) ;

glVertex2i(x4,y4) ;glVertex2i(x4,y4) ;

glEndglEnd()()

Page 7: Opengl Basics

Line strip

glBeginglBegin(GL_LINE_STRIP)(GL_LINE_STRIP)glVertex2i(x1,y1) ;glVertex2i(x1,y1) ;

glVertex2i(x2,y2) ;glVertex2i(x2,y2) ;

glVertex2i(x3,y3) ;glVertex2i(x3,y3) ;

glVertex2i(x4,y4) ;glVertex2i(x4,y4) ;

glEndglEnd()()

Line loop

glBeginglBegin(GL_LINE_LOOP)(GL_LINE_LOOP)glVertex2i(x1,y1) ;glVertex2i(x1,y1) ;

glVertex2i(x2,y2) ;glVertex2i(x2,y2) ;

glVertex2i(x3,y3) ;glVertex2i(x3,y3) ;

glVertex2i(x4,y4) ;glVertex2i(x4,y4) ;

glEndglEnd()()

Line detailsglLineWidthglLineWidth((GLfloat GLfloat w) ;w) ;

glColor3f(GLfloat r,glColor3f(GLfloat r,GLfloat GLfloat g,g,GLfloat GLfloat b) ;b) ;

glLineStippleglLineStipple(Glint factor, (Glint factor, GLushort GLushort pattern) ;pattern) ;

glEnableglEnable(GL_LINE_STIPPLE) ;(GL_LINE_STIPPLE) ;

Page 8: Opengl Basics

Polygons in OpenGLglPolygonModeglPolygonMode(GL_FRONT,GL_FILL) ;(GL_FRONT,GL_FILL) ;

glPolygonModeglPolygonMode(GL_BACK,GL_LINE) ;(GL_BACK,GL_LINE) ;

glColor3f(red,green,blue) ;glColor3f(red,green,blue) ;

glBeginglBegin(GL_POLYGON)(GL_POLYGON)

glNormal3f(v1,v2,v3) ;

glVertex3f(x1,y1,z1) ;

glNormal3f(v1n,v2n,v3n) ;

glVertex3f(xn,yn,zn) ;

glEndglEnd() ;() ;

Higher Primitives in GLUT

glutSolidShereglutSolidShere() ;() ;

glutSolidCubeglutSolidCube();();

glutSolidConeglutSolidCone() ;() ;

glutSolidTeapotglutSolidTeapot() ;() ;

Shapes Tutorial

Page 9: Opengl Basics

Other callbacks in GLUT

glutReshapeFunc glutReshapeFunc ((myReshapeCBmyReshapeCB);); glutKeyboardFuncglutKeyboardFunc((myKeyboardCB myKeyboardCB );); glutMouseFuncglutMouseFunc((myMouseCBmyMouseCB) ;) ; glutMotionFuncglutMotionFunc((myMotionCBmyMotionCB) ;) ;

Mouse callbacksvoid void myMouseCBmyMouseCB((int int button, button, int int state, state, int int x, x, int int y) { y) { // // start or end interactionstart or end interaction

if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {

printfprintf("Left button down\n") ;("Left button down\n") ;

}}

if( button == GLUT_LEFT_BUTTON && state == GLUT_UP ) {if( button == GLUT_LEFT_BUTTON && state == GLUT_UP ) {

printfprintf("Left button up\n") ;("Left button up\n") ;

}}

glutPostRedisplayglutPostRedisplay() ; () ; // // Tell the system to redraw the windowTell the system to redraw the window

}}

void void myMotionCBmyMotionCB((int int x, x, int int y) {y) { // // interaction (mouse motion)interaction (mouse motion)

printfprintf("Moving the mouse\n") ;("Moving the mouse\n") ;

glutPostRedisplayglutPostRedisplay() ;() ;

}}

Keyboard callbackvoid void myKeyboardCBmyKeyboardCB(unsigned char key, (unsigned char key, int int x, x, int int y) {y) {

switch (key) {switch (key) {

case 'q':case 'q':

case 27:case 27:

exit(0);exit(0);

break;break;

}}

}}