Top Banner
Mohammad Shaker mohammadshaker.com @ZGTRShaker 2014 OpenGL Graphics L01 WarmUP& Primitives
82

OpenGL L01-Primitives

Jan 13, 2015

Download

Software

Mohammad Shaker

OpenGL L01-WarmUp+Primitives
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OpenGL L01-Primitives

Mohammad Shaker

mohammadshaker.com

@ZGTRShaker

2014

OpenGL Graphics

L01 –WarmUP& Primitives

Page 2: OpenGL L01-Primitives

You Are Going to Learn OpenGL

Page 3: OpenGL L01-Primitives

OpenGL

Page 4: OpenGL L01-Primitives

Open Graphics Library

Page 5: OpenGL L01-Primitives

OpenGLStandard, cross-language, cross-platform API

Page 6: OpenGL L01-Primitives

Game Engines

Page 7: OpenGL L01-Primitives

Game Engines

Unreal Engine

Page 8: OpenGL L01-Primitives

Game Engines

FROSTBITE

Page 9: OpenGL L01-Primitives

Battlefield 3

FROSTBITE

Page 10: OpenGL L01-Primitives

Game Engines

CRY ENGINE

Page 11: OpenGL L01-Primitives

Game Engines

CRY ENGINE

Page 12: OpenGL L01-Primitives
Page 13: OpenGL L01-Primitives

Unity3D

Page 14: OpenGL L01-Primitives

Unity3D

Page 15: OpenGL L01-Primitives

OpenGL is..

• Library, not a programming language.

– Can be used with programming languages (C++, C#, Python, Java,.. Etc.)

• Standard. (What about XNA, DirectX?)

• Everything is a Primitive

Page 16: OpenGL L01-Primitives

What about OpenGL ES?

Page 17: OpenGL L01-Primitives

OpenGL Embedded Systems

Page 18: OpenGL L01-Primitives

OpenGL ESAndroid, iOS,.. etc

Page 19: OpenGL L01-Primitives

OpenGL2D and 3D

Page 20: OpenGL L01-Primitives

How can we see “3D”?

Page 21: OpenGL L01-Primitives

How can we see “3D”?Human Eyes

Page 22: OpenGL L01-Primitives

Setting Up OpenGL with VS 2008http://nehe.gamedev.net

Page 23: OpenGL L01-Primitives

OpenGL DevelopmentC++, C# with TAO, OpenTK, SharpGL, ..etc

Page 24: OpenGL L01-Primitives

We’ll visit OpenTK at the end of the courseTake a look at sample project here:

http://www.codeproject.com/Articles/138452/An-Analog-Clock-Design-Using-OpenTK-in-Csharphttp://www.codeproject.com/Articles/308167/A-Basic-D-Asteroid-Game-in-openGL-with-Csharp

Page 25: OpenGL L01-Primitives

Preliminaries

• Headers Files• #include <GL/gl.h>

• #include <GL/glu.h>

• #include <GL/glut.h>

• Libraries

• Enumerated Types

– OpenGL defines numerous types for compatibility

GLfloat, GLint, GLenum, etc.

Page 26: OpenGL L01-Primitives

Setting Up OpenGL on Your Machine (C++, VS 2008)

• You are given the following files:

– Headers:

• GL.h / GLU.h / GLUT.h / GLAUX.h

– Libraries:

• OpenGL32.lib / GLU32.lib / GLUT.lib / GLAUX.lib

– DLLs:

• OpenGL32.dll / GLU32.dll / GLUT.dll

Page 27: OpenGL L01-Primitives

Setting Up OpenGL on Your Machine (C++, VS 2008)

• All you need to do is scatter the files on your machine correctly and link them to your project.

– Copy .h files to

C:\Program Files\Microsoft Visual Studio 9.0\VC\include

– Copy .lib files to

C:\Program Files\Microsoft Visual Studio 9.0\VC\lib

– Copy .dll files to

C:\Windows\System32

– Project Name > Right Click > Properties > Linker > Input > Additional Dependencies

And write the following:

openGL32.lib glu32.lib glut32.lib glaux.lib

Page 28: OpenGL L01-Primitives

OpenGL DevelopmentWe are also going to use C# with OpenTK

Page 29: OpenGL L01-Primitives

OpenGL Pipeline

Page 30: OpenGL L01-Primitives

OpenGL Graphics Pipeline

Mathematical stages

Creates the color image from the geometric and

texture

Memory of the graphics

display device

Page 31: OpenGL L01-Primitives

Data Types

Page 32: OpenGL L01-Primitives

GLUA graphics library built on top of OpenGL that comes bundled with OpenGL and provides

higher-level modeling primitives (like: curves, surfaces,.. etc.)

Page 33: OpenGL L01-Primitives

3D World

Page 34: OpenGL L01-Primitives

3D World

Page 35: OpenGL L01-Primitives

3D World

Page 36: OpenGL L01-Primitives

N E W

RULE Remember, Remember

OpenGL programming is state-machine based

Page 37: OpenGL L01-Primitives

OpenGLCan Work in Each State Separately

Push and Pop, as we will see

Page 38: OpenGL L01-Primitives

Let’s Draw Something, Anything!

Page 39: OpenGL L01-Primitives

Function Naming

Page 40: OpenGL L01-Primitives

Drawing Something

glBegin(GL_TRIANGLES);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3i(0, 1, 0);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3i(-1, -1, 0);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3i(1, -1, 0);

glEnd();

Page 41: OpenGL L01-Primitives

Drawing Something

glBegin(GL_TRIANGLES);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3i(0, 1, 0);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3i(-1, -1, 0);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3i(1, -1, 0);

glEnd();

Draw between begin and end

Page 42: OpenGL L01-Primitives

Drawing Something

glBegin(GL_TRIANGLES);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3i(0, 1, 0);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3i(-1, -1, 0);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3i(1, -1, 0);

glEnd();

Draw GL_TRIANGLES between begin and end

Page 43: OpenGL L01-Primitives

Drawing Something

glBegin(GL_TRIANGLES);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3i(0, 1, 0);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3i(-1, -1, 0);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3i(1, -1, 0);

glEnd();

Page 44: OpenGL L01-Primitives

Drawing Something

glBegin(GL_TRIANGLES);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3i(0, 1, 0);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3i(-1, -1, 0);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3i(1, -1, 0);

glEnd();

Page 45: OpenGL L01-Primitives

Drawing Something

glBegin(GL_TRIANGLES);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3i(0, 1, 0);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3i(-1, -1, 0);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3i(1, -1, 0);

glEnd();

Page 46: OpenGL L01-Primitives

Drawing Something

glBegin(GL_TRIANGLES);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3i(0, 1, 0);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3i(-1, -1, 0);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3i(1, -1, 0);

glEnd();

Page 47: OpenGL L01-Primitives

Drawing Something

glBegin(GL_TRIANGLES);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3i(0, 1, 0);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3i(-1, -1, 0);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3i(1, -1, 0);

glEnd();

Page 48: OpenGL L01-Primitives

Drawing Something

glBegin(GL_TRIANGLES);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3i(0, 1, 0);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3i(-1, -1, 0);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3i(1, -1, 0);

glEnd();

Watch out. Draw primitives Counter-Clockwise

Page 49: OpenGL L01-Primitives

Drawing Something

glBegin(GL_TRIANGLES);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3i(0, 1, 0);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3i(-1, -1, 0);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3i(1, -1, 0);

glEnd();

You can change that:

glFrontFace(GL_CCW);CounterClockwise is front facing

glFrontFace(GL_CW);Clockwise is front facing

Page 50: OpenGL L01-Primitives

Drawing Primitives

glBegin(GL_LINES);

glVertex2f(x0,y0);

glVertex2f(x1,y1);

glEnd();

glBegin(GL_TRIANGLES);

glVertex2f(x0,y0);

glVertex2f(x1,y1);

glVertex2f(x2,y2);

glEnd();

glBegin(GL_QUADS);

glVertex2f(x0,y0);

glVertex2f(x1,y1);

glVertex2f(x2,y2);

glVertex2f(x3,y3);

glEnd();

Page 51: OpenGL L01-Primitives

Drawing Primitives

Page 52: OpenGL L01-Primitives

N E W

RULE Remember, Remember

OpenGL programming is state-machine based

Page 53: OpenGL L01-Primitives

Drawing Primitives - Coloring

glColor3f(0,0,1);

glBegin(GL_POLYGON);glVertex2f(-1,1);glVertex2f(-1,-1);glVertex2f(1,-1);

glEnd();

glColor3f(1,0,0);glBegin(GL_POLYGON);glVertex2f(-1,1);glVertex2f(-1,-1);glVertex2f(1,-1);

glEnd();

glColor3f(0,0,0);glBegin(GL_LINE_LOOP);glVertex2f(-1,1);glVertex2f(-1,-1);glVertex2f(1,-1);

glEnd();

glBegin(GL_POLYGON);glColor3f(0,1,0);glVertex2f(-1,1);

glColor3f(0,0,1);glVertex2f(-1,-1);

glColor3f(1,0,0);glVertex2f(1,-1);

glEnd();

Page 54: OpenGL L01-Primitives

Drawing Primitives - Coloring

glColor3f(0,0,1);

glBegin(GL_POLYGON);glVertex2f(-1,1);glVertex2f(-1,-1);glVertex2f(1,-1);

glEnd();

glColor3f(1,0,0);glBegin(GL_POLYGON);glVertex2f(-1,1);glVertex2f(-1,-1);glVertex2f(1,-1);

glEnd();

glColor3f(0,0,0);glBegin(GL_LINE_LOOP);glVertex2f(-1,1);glVertex2f(-1,-1);glVertex2f(1,-1);

glEnd();

glBegin(GL_POLYGON);glColor3f(0,1,0);glVertex2f(-1,1);

glColor3f(0,0,1);glVertex2f(-1,-1);

glColor3f(1,0,0);glVertex2f(1,-1);

glEnd();

OpenGL programming is state-machine based

Page 55: OpenGL L01-Primitives

Polygon

Page 56: OpenGL L01-Primitives

Polygon

• To draw a polygon, the vertices should hold:

– Do not intersect (convex)

– All in same plane

glEdgeFlag(FALSE);glVertex2f(30.0f, 0.0f);glEdgeFlag(TRUE);glVertex2f(0.0f, 20.0f);glVertex2f(0.0f, 0.0f);

Page 57: OpenGL L01-Primitives

Polygon

• To draw a polygon, the vertices should hold:

– Do not intersect (convex)

– All in same plane

glEdgeFlag(FALSE);glVertex2f(30.0f, 0.0f);glEdgeFlag(TRUE);glVertex2f(0.0f, 20.0f);glVertex2f(0.0f, 0.0f);

Page 58: OpenGL L01-Primitives

Wireframes Meshes

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glBegin(GL_TRIANGLES);

for (int i=0; i< n; i++) {

glVertex3fv(v[i++]);

glVertex3fv(v[i++]);

glVertex3fv(v[i]);

}

glEnd();

Page 59: OpenGL L01-Primitives

Wireframes Meshes

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glBegin(GL_TRIANGLES);

for (int i=0; i< n; i++) {

glVertex3fv(v[i++]);

glVertex3fv(v[i++]);

glVertex3fv(v[i]);

}

glEnd();

Page 60: OpenGL L01-Primitives

Wanna Play More With Primitives? Let’s Go!From CS418 Computer Graphics, John C. Hart

Page 61: OpenGL L01-Primitives

Points

glBegin(GL_POINTS);

glVertex2f(-.6,1.);

glVertex2f(-.6,.6);

glVertex2f(-.2,.6);

glVertex2f(-.2,-.6);

glVertex2f(-.6,-.6);

glVertex2f(-.6,-1.);

glVertex2f(.6,-1.);

glVertex2f(.6,-.6);

glVertex2f(.2,-.6);

glVertex2f(.2,.6);

glVertex2f(.6,.6);

glVertex2f(.6,1.);

glEnd();

(-1,-1) (1,-1)

(1,1)(-1,1)

Page 62: OpenGL L01-Primitives

Lines

glBegin(GL_LINES);

glVertex2f(-.6,1.);

glVertex2f(-.6,.6);

glVertex2f(-.2,.6);

glVertex2f(-.2,-.6);

glVertex2f(-.6,-.6);

glVertex2f(-.6,-1.);

glVertex2f(.6,-1.);

glVertex2f(.6,-.6);

glVertex2f(.2,-.6);

glVertex2f(.2,.6);

glVertex2f(.6,.6);

glVertex2f(.6,1.);

glEnd();

(-1,-1) (1,-1)

(1,1)(-1,1)

Page 63: OpenGL L01-Primitives

Line Strip

glBegin(GL_LINE_STRIP);

glVertex2f(-.6,1.);

glVertex2f(-.6,.6);

glVertex2f(-.2,.6);

glVertex2f(-.2,-.6);

glVertex2f(-.6,-.6);

glVertex2f(-.6,-1.);

glVertex2f(.6,-1.);

glVertex2f(.6,-.6);

glVertex2f(.2,-.6);

glVertex2f(.2,.6);

glVertex2f(.6,.6);

glVertex2f(.6,1.);

glEnd();

(-1,-1) (1,-1)

(1,1)(-1,1)

Page 64: OpenGL L01-Primitives

Line Loop

glBegin(GL_LINE_LOOP);

glVertex2f(-.6,1.);

glVertex2f(-.6,.6);

glVertex2f(-.2,.6);

glVertex2f(-.2,-.6);

glVertex2f(-.6,-.6);

glVertex2f(-.6,-1.);

glVertex2f(.6,-1.);

glVertex2f(.6,-.6);

glVertex2f(.2,-.6);

glVertex2f(.2,.6);

glVertex2f(.6,.6);

glVertex2f(.6,1.);

glEnd();

(-1,-1) (1,-1)

(1,1)(-1,1)

Page 65: OpenGL L01-Primitives

Polygon

glBegin(GL_POLYGON);

glVertex2f(-.6,1.);

glVertex2f(-.6,.6);

glVertex2f(-.2,.6);

glVertex2f(-.2,-.6);

glVertex2f(-.6,-.6);

glVertex2f(-.6,-1.);

glVertex2f(.6,-1.);

glVertex2f(.6,-.6);

glVertex2f(.2,-.6);

glVertex2f(.2,.6);

glVertex2f(.6,.6);

glVertex2f(.6,1.);

glEnd();

(-1,-1) (1,-1)

(1,1)(-1,1)

OpenGL only supports convex polygons(and really only triangles)

Page 66: OpenGL L01-Primitives

Quads

glBegin(GL_QUADS);

glVertex2f(-.6,1.);

glVertex2f(-.6,.6);

glVertex2f(-.2,.6);

glVertex2f(-.2,-.6);

glVertex2f(-.6,-.6);

glVertex2f(-.6,-1.);

glVertex2f(.6,-1.);

glVertex2f(.6,-.6);

glVertex2f(.2,-.6);

glVertex2f(.2,.6);

glVertex2f(.6,.6);

glVertex2f(.6,1.);

glEnd();

(-1,-1) (1,-1)

(1,1)(-1,1)

Page 67: OpenGL L01-Primitives

Quads

glBegin(GL_QUADS);

glVertex2f(-.6,1.);

glVertex2f(-.6,.6);

glVertex2f(-.6,-.6);

glVertex2f(-.6,-1.);

glVertex2f(.6,-1.);

glVertex2f(.6,-.6);

glEnd();

glVertex2f(-.2,.6);

glVertex2f(-.2,-.6);

glVertex2f(.2,-.6);

glVertex2f(.2,.6);

glVertex2f(.6,.6);

glVertex2f(.6,1.);

Page 68: OpenGL L01-Primitives

Quads

glBegin(GL_QUADS);

glVertex2f(-.6,1.);

glVertex2f(-.6,.6);

glVertex2f(.6,.6);

glVertex2f(.6,1.);

glVertex2f(-.6,-.6);

glVertex2f(-.6,-1.);

glVertex2f(.6,-1.);

glVertex2f(.6,-.6);

glVertex2f(-.2,.6);

glVertex2f(-.2,-.6);

glVertex2f(.2,-.6);

glVertex2f(.2,.6);

glEnd();

(-1,1)

(-1,-1) (1,-1)

(1,1)(-1,1)

Lines should never pass through a vertex.

Page 69: OpenGL L01-Primitives

Triangles

glBegin(GL_TRIANGLES);

glVertex2f(-.6,1.);

glVertex2f(-.6,.6);

glVertex2f(-.2,.6);

glVertex2f(-.2,-.6);

glVertex2f(-.6,-.6);

glVertex2f(-.6,-1.);

glVertex2f(.6,-1.);

glVertex2f(.6,-.6);

glVertex2f(.2,-.6);

glVertex2f(.2,.6);

glVertex2f(.6,.6);

glVertex2f(.6,1.);

glEnd();

(-1,-1) (1,-1)

(1,1)(-1,1)

Page 70: OpenGL L01-Primitives

Triangles

glBegin(GL_TRIANGLES);

glVertex2f(-.6,1.);

glVertex2f(-.6,.6);

glVertex2f(-.2,.6);

glVertex2f(-.6,1.);

glVertex2f(-.2,.6);

glVertex2f(.6,1.);

glVertex2f(-.2,.6);

glVertex2f(.2,.6);

glVertex2f(.6,1.);

glVertex2f(.2,.6);

glVertex2f(.6,.6);

glVertex2f(.6,1.);

glEnd(); (-1,-1) (1,-1)

(1,1)(-1,1)

Page 71: OpenGL L01-Primitives

Triangle Strip

glBegin(GL_TRIANGLE_STRIP);

glVertex2f(-.6,1.);

glVertex2f(-.6,.6);

glVertex2f(-.2,.6);

glVertex2f(-.2,-.6);

glVertex2f(-.6,-.6);

glVertex2f(-.6,-1.);

glVertex2f(.6,-1.);

glVertex2f(.6,-.6);

glVertex2f(.2,-.6);

glVertex2f(.2,.6);

glVertex2f(.6,.6);

glVertex2f(.6,1.);

glEnd();

(-1,-1) (1,-1)

(1,1)(-1,1)

Page 72: OpenGL L01-Primitives

Triangle Strip

glBegin(GL_TRIANGLE_STRIP);

glVertex2f(-.6,1.);

glVertex2f(.6,1.);

glVertex2f(-.2,.6);

glVertex2f(.2,.6);

glVertex2f(-.2,-.6);

glVertex2f(.2,-.6);

glVertex2f(.6,-1.);

glVertex2f(.6,-.6);

glEnd();

(-1,-1) (1,-1)

(1,1)(-1,1)

First two vertices prime the pump,then every new vertex creates a triangle

connecting it to the previous two vertices

Page 73: OpenGL L01-Primitives

Triangle Fan

glBegin(GL_TRIANGLE_FAN);

glVertex2f(-.2,.6);

glVertex2f(-.6,.6);

glVertex2f(-.6,1.);

glVertex2f(.6,1.);

glVertex2f(.2,.6);

glVertex2f(.2,-.6);

glVertex2f(-.2,-.6);

glEnd();

(-1,-1) (1,-1)

(1,1)(-1,1)

Page 74: OpenGL L01-Primitives

OpenGL Program Structure

Page 75: OpenGL L01-Primitives

OpenGL Program Structure

• Application Structure

– Configure and open window

– Initialize OpenGL state

– Register input callback functions

• render

• resize

• input: keyboard, mouse, etc.

– Enter event processing loop

Page 76: OpenGL L01-Primitives

OpenGL Program Structure

• Application Structure

– Configure and open window

– Initialize OpenGL state

– Register input callback functions

• render

• resize

• input: keyboard, mouse, etc.

– Enter event processing loop

void main( int argc, char** argv ){int mode = GLUT_RGB|GLUT_DOUBLE;

glutInitDisplayMode( mode );glutCreateWindow( argv[0] );

init();

glutDisplayFunc( display );glutReshapeFunc( resize ); glutKeyboardFunc( key );glutIdleFunc( idle );

glutMainLoop();}

Page 77: OpenGL L01-Primitives

Now Take a Breath, Have a KitKat

Page 78: OpenGL L01-Primitives

Resources

Page 79: OpenGL L01-Primitives

Resources

Advanced stuff for math geeks

Page 80: OpenGL L01-Primitives

And yet more resources

• The OpenGL Programming Guide, 7th Edition

• Interactive Computer Graphics: A Top-down Approach using OpenGL, 6th Edition

• The OpenGL Superbible, 5th Edition

• The OpenGL Shading Language Guide, 3rd Edition

• OpenGL and the X Window System

• OpenGL Programming for Mac OS X

• OpenGL ES 2.0

• WebGL

• The OpenGL Website: www.opengl.org• API specifications

• Reference pages and developer resources

• PDF of the OpenGL Reference Card

• Discussion forums

• The Khronos Website: www.khronos.org• Overview of all Khronos APIs

• Numerous presentations

Page 81: OpenGL L01-Primitives

Resources (www.nehe.gamedev.net)

Page 82: OpenGL L01-Primitives

Keep in touch and let’s connect

http://www.mohammadshaker.com

[email protected]

https://twitter.com/ZGTRShaker @ZGTRShaker

https://de.linkedin.com/pub/mohammad-shaker/30/122/128/

http://www.slideshare.net/ZGTRZGTR

https://www.goodreads.com/user/show/11193121-mohammad-shaker

https://plus.google.com/u/0/+MohammadShaker/

https://www.youtube.com/channel/UCvJUfadMoEaZNWdagdMyCRA

http://mohammadshakergtr.wordpress.com/