opengl’fundamentals - unibzrusso/ap/opengl_presentation.pdf · opengl fundamentals opengl...

79
1 78 / Radek Oslejsek, Bolzano 2015 OpenGL Fundamentals OpenGL Fundamentals Radek Ošlejšek Masaryk University, Faculty of Informa<cs Brno, Czech Republic oslejsek@fi.muni.cz hGp://www.fi.muni.cz/~oslejsek

Upload: phungcong

Post on 05-Jun-2018

264 views

Category:

Documents


0 download

TRANSCRIPT

1 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

OpenGL  Fundamentals

Radek  Ošlejšek

Masaryk  University,   Faculty  of  Informa<cs Brno,  Czech  Republic

[email protected]

hGp://www.fi.muni.cz/~oslejsek

2 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

1.  Introduc<on  to  OpenGL

3 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Literature

• OpenGL  Programming  Guide  (Red  Book)

– Basics  for  1.1  version

– hGp://www.glprogramming.com/red/

• OpenGL  Reference  Manual  (Blue  Book)  hGp://www.glprogramming.com/

blue/

• hGp://www.opengl.org/documenta<on/

4 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

What’s  OpenGL

• Applica<on  programming  interface  (API)  for  graphical  subsystems • Hardware  independent  –  the  only  required  part  is  the  framebuffer. • Independence  on  opera<ng  system,  on  graphic  drivers  and  window  managers • OpenGL  isn’t  „pixel  exact“.  Same  sequence  of  commands  can  produce  slightly  different  pictures  on  different  pla\orms

5 78 / Radek Oslejsek, Bolzano 2015 Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

What OpenGL can/can't do

Can  do: l rendering  of  triangles,  lines,  polygons  (3D) l picture  manipula<on  (2D) l local  illumina<on,  i.e.  ligh<ng,  shading,  textures l fog l visibility  detec<on l transforma<ons l ...

Can't do: l window creation and management l defining objects, scene representation l NURBS l global illumination (shadows, reflections) l voxels, ...

http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter09.html

6 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

OpenGL libraries

l OpenGL  U<lity  Library l Extension  of  basic  OpenGL

l Mapping  between  the  global  and  local  coordinate  system l Mip-­‐mapping l NURBS  objects l …

l All  func<ons  have  the  glu  prefix l Other  libraries:  hGp://www.opengl.org/resources/libraries/

l Window  Management:  Different  tools  for  different  programming  languages

l C/C++:  GLUT,  SDL l Java:  JOGL  +  Swing/AWT

7 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

OpenGL  Architecture:  Pipeline

http://www.ntu.edu.sg/home/ehchua/programming/opengl/cg_basicstheory.html

8 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

OpenGL  as  a  state  automat    

• Set  the  state,  ask  for  the  current  state,  …  

9 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Syntax  Rules

 

10 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Data  types

 

11 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Different  version  of  one  func<on

 

glVertex2i(int1,  int2); glVertex2d(double1,  double2); glVertex3f(float1,  float2,  float3); glVertex4f(float1,  float2,  float3,  float4);  

12 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

„v“  usage

  • Parameters  of  given  func<on  are  stored  in  an  array: GLfloat color[]={1.0, 0.0, 1.0}; glColor3fv(color);

• Without  array: GLfloat r=1.0, g=0.0, b=1.0; glColor3f(r, g, b);

• Both  types  are  leading  to  the  same  result,  the  first  op<on  is  mostly  faster

13 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

First  example

 

 

void render() { glDisable(GL_LIGHTING); // turn off the lights glBegin(GL_TRIANGLES); // drawing triangles glShadeModel(GL_SMOOTH); // Gouraud shading // drawing a triangle with red, blue and green vertices glColor3f(1.0,0.0,0.0); glVertex3f(0.0, 0.0, 0.0); glColor3f(0.0,0.0,1.0); glVertex3f(1.0, 0.0, 0.0); glColor3f(0.0,1.0,0.0); glVertex3f(1.0, 1.0, 0.0); glEnd(); // rendering finished }

14 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

2.  Camera  and  view-­‐port  sekngs

15 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Camera position

l Camera  can  be  posi<oned  in  two  manners: l Object  manipula<on  (model  transforma<ons) l Camera  manipula<on  (view  transforma<ons) l Shifting the camera = shifting all objects in opposite direction l Rotating the camera CW = rotating objects CCW

l Initial position: [0,0,0] l Initial viewing direction: (0,0,-1) l How to set initial camera position and orientation: l  glMatrixMode(GL_PROJECTION); l  glLoadIdentity();

16 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Camera  posi<on  (cont.)

• Positioning the camera in 3D space is too complex • It can be enhanced by using function of the glu library

gluLookAt( GLdouble eyeX,GLdouble eyeY,GLdouble eyeZ, GLdouble posX,GLdouble posY,GLdouble posZ, GLdouble upX,GLdouble upY,GLdouble upZ)

17 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Projec<ons

Orthographic Perspective

“focal length” of the camera

18 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Orthographic  projec<on

void glOrtho(GLdouble left,GLdouble right, GLdouble bottom,GLdouble top, GLdouble near, GLdouble far)

19 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Perspec<ve  projec<on

void glFrustum(GLdouble left,GLdouble right, GLdouble bottom,GLdouble top, GLdouble near, GLdouble far)

20 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Perspec<ve  projec<on

void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)

21 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Viewport  sekngs

By  default,  the  viewport  is  set  to  cover  the  en<re  applica<on  window.  We  can  use  the  glViewport()  func<on  to  choose  a  smaller  area  (e.g.,  for  split-­‐screen  or  mul<-­‐screen  applica<on) • x  and  y  determine  the  lower  lel  viewport  corner • w  and  h  determine  the  size  of  the  viewport  rectangle

void glViewport(GLint x,GLint y, GLsizei w, GLsizei h)

22 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Example  –  rendering  into  2  viewports

int w = 600, h = 300; // window size // ... gluPerspective(60.0, w/(2.0*h), 1, 10.0);//!!! // ... glViewport(0,0,w/2,h); glutSolidTeapot(0.8); glViewport(w/2,0,w/2,h); glRotatef(60, 1, 1, 0); glutSolidTeapot(0.8);

23 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Transformation matrices

l In  general,  they  enable  to  express  the  linear  transforma<ons  –  transla<on,  rota<on,  scaling. l We  can  combine  the  linear  transforma<ons  –  by  mul<plying  the  matrices l ModelView  matrix  –  affects  loca<ons  of  objects  in  the  scene  as  well  as  the  posi<on  of  the  camera.  Func<ons  affec<ng  transforma<on  matrix:

l gluLookAt l glRotate,  glTranslate,  etc.  –  will  be  described  later

l Projec=on  matrix  –  affects  the  projec<on  of  the  camera.  Func<ons  affec<ng  transforma<on  matrix:

l glOrtho,  glFrustum,  gluPerspec9ve l Texture  matrix  –  maps  textures  of  object  surface  –  out  of  the  scope  of  this  lecture.

24 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Sekng  the  current  transforma<on  matrix

We  determine  the  transforma<on  matrix  to  be  modified:      parameter  mode:          GL_MODELVIEW          GL_PROJECTION          GL_TEXTURE …  because  gluLookAt()  can  easily  modify  projec<on  matrix,  for  instance. Advice:  Keep  the  modelview  matrix  always  on,  i.e.  a  code  modifying  the  projec<on,  for  instance,  should  switch  to  projec<on  matrix  only  temporarily  and  then  switch  back  by  glMatrixMode(GL_MODELVIEW).

glMatrixMode(GLenum mode)

25 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Clearing  transforma<on  matrix

Matrix  opera<ons  are  “cumula<ve”.  To  set  the  current  transforma<on  matrix  do  default  value: Advice:  Always  call  glLoadIden9ty()  before  sekng  the  camera  posi<on  or  projec<on,  otherwise  the  new  computed  matrix  is  added  (mul<plied  with)  the  current  matrix

glLoadIdentity()

26 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Exercise: Goal 1

27 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

3.  Geometry  and  colors

28 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Drawing  primi<ves

• Any  arbitrary  complex  scene  is  composed  of  ver<ces  (triangles). • Vertex  is  a  basic  part  of  all  primi<ves • Next  commands  are  equivalent

void glVertex{234}{sifd}[v](TYPE coords)

GLfloat foo[]={3.0, 2.0, 0.0}; glVertex3fv(foo); glVertex2i(3,2); glVertex2f(3.0f,2.0f); glVertex3i(3,2,0);

29 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Drawing  state

• Switching  to  drawing  state  using    glBegin(primi=ve  type) • Finishing  the  drawing  state  using  glEnd() • Example: glBegin(GL_TRIANGLES); // setting the OpenGL state glVertex3f(0.0, 0.0, 0.0); glVertex3f(1.0, 0.0, 0.0); glVertex3f(1.0, 1.0, 0.0); glEnd(); //end of rendering • The  following  command  doesn't  exist!  It's  because  of  using  OpenGL  pipeline. glTriangle(x1,y1,z1, x2,y2,z2, x3,y3,z3);

30 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

OpenGL  primi<ves

• Basic  primi<ves: – glBegin(GL_POINTS) – glBegin(GL_LINES) – glBegin(GL_LINE_STRIP) – glBegin(GL_LINE_LOOP) – glBegin(GL_TRIANGLES) – glBegin(GL_TRIANGLE_FAN) – glBegin(GL_TRIANGLE_STRIP) – glBegin(GL_QUADS) – glBegin(GL_QUAD_STRIP) – glBegin(GL_POLYGON)

http://www.informit.com/articles/article.aspx?p=1377833&seqNum=2

Surfaces must be always planar and convex (triangles are always planar and convex), otherwise:

31 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Changing  aGributes  of  points

• Color:  glColor*(); • Texture  mapping:  glTexCoord*(); • Size:  glPointSize(Glfloat size); • Example: glBegin(GL_TRIANGLES); glColor3i(1, 0, 0); glTexCoord2f(0.1, 0.1); glVertex3f(-2.0, -1.0, 0.0); glTexCoord2f(0.0, 0.9); glVertex3f(-2.0, 1.0, 0.0); glTexCoord2f(1.1, 0.8); glVertex3f(0.0, 1.0, 0.0); glEnd();

32 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Example  1  –  which  color  is  used?

glBegin(GL_TRIANGLES); glColor3f(1.0,0.0,0.0); glVertex2i(0, 0); glVertex2i(0, 10); glVertex2i(5, 0); glColor3f(0.0,1.0,0.0); glVertex2i(5, 10); glColor3f(0.0,0.0,1.0); glVertex2i(10, 0); glColor3f(0.0,0.0,0.0); // black glVertex2i(10, 10); glEnd();

1

2

3

4

5

6

33 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Example  2  –  which  color  is  used?

glBegin(GL_TRIANGLE_STRIP); glColor3f(1.0,0.0,0.0); glVertex2i(0, 0); glVertex2i(0, 10); glVertex2i(5, 0); glColor3f(0.0,1.0,0.0); glVertex2i(5, 10); glColor3f(0.0,0.0,1.0); glVertex2i(10, 0); GlColor3f(0.0,0.0,0.0); // black glVertex2i(10, 10); glEnd();

2 4 6

1 3 5

34 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Changing  the  drawing  mode

• While  the  glBegin()  affects  the  defini<on  of  the  geometry,  glPolygonMode()  affects  visualiza<on  of  the  geometry. • glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); • glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); • glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

35 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Color  of  polygons

glShadeModel(GL_SMOOTH) glShadeModel(GL_FLAT)

36 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

4.  Object  transforma<ons

37 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Modeling  transforma<ons

• Basic  affine  transforma<ons  are  predefined  in  OpenGL  transla=on,  rota=on,  scaling • Using  these  predefined  transforma<on  is  much  faster  than  mul<plying  matrices  (it  has  the  hardware  support) • Note:  we  are  working  with  modelview  matrix,  recall  the  glMatrixMode(GL_MODELVIEW)

38 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

glTranslate()

Shiling  by  vector  [x,  y,  z]

void glTranslate{fd}(TYPE x,TYPE y,TYPE z)

39 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

glScale()

• Magnifica<on/minifica<on  in  all  three  coordinates    x,  y,  z  determines  the  size  change  in  axes  x,  y,  z • Example glLoadIdentity(); DrawObject(); glScalef(0.8, -2.0, 1.3); DrawObject();

void glScale{fd}(TYPE x,TYPE y,TYPE z)

40 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

glRotate()

• Rota<ng  by  angle  around  the  axis  passing  through  the  (x,  y,  z)  origin      angle  is  in  <0,  360>      x,  y,  z  determines  the  axis  of      rota<on

void glRotate{fd}(TYPE angle,TYPE x,TYPE y,TYPE z)

41 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

glRotate()

• Example glLoadIdentity(); DrawObject(); glRotatef(45,-1,-1.5,0.5); DrawObject();

42 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Order  of  transforma<ons

glMatrixMode(GL_MODELVIEW); 1: glLoadIdentity(); 2: glMultMatrix(translate); 3: glMultMatrix(rotate); 4: DrawObject();

x x x

y y y

z z z

43 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Order  of  transforma<ons

glMatrixMode(GL_MODELVIEW); 1: glLoadIdentity(); 2: glMultMatrix(rotate); 3: glMultMatrix(translate); 4: DrawObject();

x x x

y y y

z z z

44 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Matrix  stack

• OpenGL  enables  to  store  current  modelview  matrix  (and  other  matrices  as  well)  and  to  recover  this  matrix  later  on  (return  to  stored  transforma<on)

– Storing  the  currently  selected  matrix  to  the  top  of  the  stack  

– Removing  the  matrix  from  the  top  of  the  stack

void glPushMatrix()

void glPopMatrix()

45 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Matrix  stack

https://www.cosc.brocku.ca/Offerings/3P98/course/lectures/2d_3d_xforms/

http://what-when-how.com/opengl-programming-guide/manipulating-the-matrix-stacks-viewing-opengl-programming/

push pop

46 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Example  –  matrix  stack

void Tea(GLfloat i){ glPushMatrix(); if (i>0.01) Tea(i/2.0); glPopMatrix(); glPushMatrix(); glTranslatef(-i,-i,0);glutWireTeapot(i/2.0); glPopMatrix(); glPushMatrix(); glTranslatef(-i,i,0);glutWireTeapot(i/2.0); glPopMatrix(); glPushMatrix(); glTranslatef(i,-i,0);glutSolidTeapot(i/2.0); glPopMatrix(); glPushMatrix(); glTranslatef(i,i,0);glutSolidTeapot(i/2.0); glPopMatrix(); }

47 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Exercise: Goal 2

48 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

5.  Ligh<ng

49 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Shading  in  OpenGL

• OpenGL  supports  two  types  of  basic  shadings  ... – Constant  (flat)  shading:

                   glShadeModel(GL_FLAT) – Gouraud  shading  (color  interpola<on)

         glShadeModel(GL_SMO-OTH) • …  and  Phong  shading,  which  is  more  complicated.

http://148.204.81.206/matlab/visualize/selecting-a-lighting-method.html

50 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Phong  shading

• OpenGL  works  with  colors  (examples  so  far)  or  with  lights  (Phong  shading  model)

– Using  colors:  Specifying  color  of  each  vertex – Using  lights:  Color  of  each  vertex  is  computed  from

l incoming  light  –  we  have  to  define  the  light  proper<es l material  proper<es  of  objects

• Switching  between  these  modes: l glEnable(GL_LIG-HTING) l glDisa-ble(GL_LIGHTIN-G)

• Enabling/disabling  individual  lights: glEnable(GL_LIGHT?), glDisable(GL_LIG-HT?)

51 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Ambient  light

• Omnidirec<onal  light  –  independent  on  the  posi<on  and  orienta<on  of  sources  of  lights • Objects  are  flat  –  losing  the  spa<al  percep<on

52 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Diffuse  light

• Ligh<ng  the  object  surface  from  given  light  source • The  light  reflects  from  the  surface  to  all  direc<ons  with  the  same  intensity  –  opaque  materials

53 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Specular  light

• Ray  of  light  reflects  from  the  object  surface  according  to  the  law  of  reflec<on • Reflec<ng  is  not  ideal  –  we  have  to  take  into  account  the  coefficient  of  the  change  of  intensity  of  the  reflected  light

54 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Combina<on

55 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Sekng  light  proper<es

• where  light  is  GL_LIGHTn • p  is  one  of  the  parameters  (next  slide) • val  is  the  parameter  value

void glLight{if}[v](GLenum light, GLenum p, GLint val);

56 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Parameter  p

GL_AMBIENT (0.0, 0.0, 0.0, 1.0) ambient light intensity GL_DIFFUSE (1.0, 1.0, 1.0, 1.0) diffuse light intensity

or (0.0, 0.0, 0.0, 1.0) GL_SPECULAR (1.0 ,1.0 ,1.0, 1.0) specular light intensity

or (0.0, 0.0 ,0.0 ,1.0) GL_POSITION (0.0 ,0.0, 1.0, 0.0) (x,y,z,w) light position note: position (x,y,z,1) means local light position (x,y,z,0) means infinity light, which is the directional light GL_SPOT_DIRECTION (0.0, 0.0, -1.0) (x,y,z) direction of the spot light GL_SPOT_EXPONENT 0.0 spot exponent GL_SPOT_CUTOFF 180.0 cutting angle for the spot light GL_CONSTANT_ATTENUATION 1.0 constant attenuation factor GL_LINEAR_ATTENUATION 0.0 linear attenuation factor GL_QUADRATIC_ATTENUATION 0.0 quadratic attenuation factor

57 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Example

GLfloat light_position[] = {0,0,-4,1.0}; // w=0:infinite GLfloat light_color[] = {1,1,1,1}; // set the lights glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); // set the spheres glTranslatef(0,0,-10); glutSolidSphere(0.2, 100, 100);//middle glLoadIdentity();glTranslatef(0.5,0,-5); glutSolidSphere(0.2, 100, 100); //left glLoadIdentity();glTranslatef(-0.5,0,-5); glutSolidSphere(0.2, 100, 100); //right

58 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Example  –  cont. GLfloat specular[] = {1, 1, 1, 1}; GLfloat diffuse[] = {1, 0, 0, 1}; glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, specular); GLfloat specular[] = {1, 1, 0, 1}; GLfloat diffuse[] = {1, 0, 0, 1}; GLfloat specular[] = {0, 0, 1, 1}; GLfloat diffuse[] = {0.5, 0.5, 0.5, 1}; GLfloat specular[] = {1, 0, 0, 1}; GLfloat diffuse[] = {1, 1, 1, 1};

59 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Point  lights

• Ligth  bulb  in  space  emikng  the  light  in  all  direc<ons  with  the  same  intensity

– Parameter  posi9on  –  pointer  to  the  array  of  4  values.  First  three  values  represent  the  x,  y,  z  posi<on  of  light,  the  last  value  is  1

glLightfv(GL_LIGHT?, GL_POSITION, position); glLightiv(GL_LIGHT?, GL_POSITION, position);

60 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Direc<onal  lights

• Light  it  emiGed  from  one  direc<on  independently  on  the  mutual  posi<on  of  objects  in  the  scene • E.g.  sun  –  sunrays  are  almost  parallel

glLightfv(GL_LIGHT?, GL_POSITION, direction); glLightiv(GL_LIGHT?, GL_POSITION, direction);

61 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Reflector  lights

• The  most  complex  light  in  OpenGL • Light  is  emmited  from  one  point  only  in  given  cone • We  have  to  define  more  parameters:

– Light  posi<ons – Direc<on  of  rays – Angle  in  which  the  light  intensity  decreases  (to  0)

62 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Reflector  lights

GL_SPOT_DIRECTION direction of refl. light (0,0,-1) GL_SPOT_CUTOFF angle cutoff for refl. Light 180.0 GL_SPOT_CUTOFF sekng  to  180o  means  non-­‐reflec<on  light

63 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Reflec<on  lights

• Changing  GL_SPOT_CUTOFF glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,7.0); glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spot_dir); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0);

64 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Lights  -­‐  aGenua<on

GL_CONSTANT ATTENUATION 1 GL_LINEAR_ATTENUATION 0 GL_QUADRATIC_ATTENUATION 0 • These  coefficients  are  combined  into  equa<on: • d  -­‐  distance • AGenua<on  is  not  implicitly  used

( )2321

1dcdcc

nattenuatio++

=

65 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Sekng  material  proper<es

face is GL_FRONT, GL_BACK or GL_FRONT_AND_BACK p one of the parameters (next slide) val parameter value

void glMaterial{if}[v](GLenum face, GLenum p, TYPE val);

66 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Sekng  material  proper<es

GL_AMBIENT ambient part of light (0.2,0.2,0.2,1) GL_DIFFUSE diffuse part of light (0.8,0.8,0.8,1) GL_AMBIENT_AND_DIFFUSE GL_SPECULAR specular part of light (0,0,0,1) GL_SHININESS exponent 0.0 [0.0 – 128.0] GL_EMISSION self-lighting (0,0,0,1)

67 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Material GL AMBIENT GL DIFFUSE GL SPECULAR GL SHININESS Brass 0.329412 0.780392 0.992157 27.8974

0.223529 0.568627 0.941176 0.027451 0.113725 0.807843 1.0 1.0 1.0

Bronze 0.2125 0.714 0.393548 25.6

0.1275 0.4284 0.271906 0.054 0.18144 0.166721 1.0 1.0 1.0

Polished 0.25 0.4 0.774597 76.8 Bronze 0.148 0.2368 0.458561

0.06475 0.1036 0.200621 1.0 1.0 1.0

Chrome 0.25 0.4 0.774597 76.8

0.25 0.4 0.774597 0.25 0.4 0.774597 1.0 1.0 1.0

Copper 0.19125 0.7038 0.256777 12.8

0.0735 0.27048 0.137622 0.0225 0.0828 0.086014 1.0 1.0 1.0

68 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Material GL AMBIENT GL DIFFUSE GL SPECULAR GL SHININESS Polished 0.2295 0.5508 0.580594 51.2 Copper 0.08825 0.2118 0.223257

0.0275 0.066 0.0695701 1.0 1.0 1.0

Gold 0.24725 0.75164 0.628281 51.2

0.1995 0.60648 0.555802 0.0745 0.22648 0.366065 1.0 1.0 1.0

Polished 0.24725 0.34615 0.797357 83.2 Gold 0.2245 0.3143 0.723991

0.0645 0.0903 0.208006 1.0 1.0 1.0

Pewter 0.105882 0.427451 0.333333 9.84615

0.058824 0.470588 0.333333 0.113725 0.541176 0.521569 1.0 1.0 1.0

69 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

5.  Buffers

70 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Buffers

• Framebuffer  can  contain:

– Color  buffer  –  color  of  fragments  (pixels)   – Z-­‐buffer  –  depth  memory – Stencil  buffer  –  stencil  memory – Accumula=on  buffer  –  mo<on  blur,  an<aliasing

71 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Color  buffer

• Memory  for  storing  color  of  individual  fragments  (pixels) • Stereoskopic  displaying:

– min.  two  color  buffers • Double-­‐buffering:

– min.  two  color  buffers • Each  OpenGL  implemen<on  has  to  contain  at  least  one  color  buffer • You  can  u<lize  other  buffers  -­‐  auxiliary  color  buffers.  OpenGL  does  not  specify  their  func<on,  it  can  be  set  arbitrarily  (e.g.  storing  the  picture  which  will  be  used  repeatedly.  When  drawing,  the  picture  is  copied  to  the  color  buffer.) • Default  color  buffer  need  not  to  be  in<alized.

72 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Z-­‐buffer

• Storing  informa<on  about  the  distance  between  the  fragment  and  the  projec<on  plane. • Using  for  hidden  parts  removal • Depth  test  has  to  be  enabled,  it  is  called  only  once:  glEnable(GL_DEPTH_TEST) • Each  <me  when  used,  the  Z-­‐buffer  has  to  be  cleared:  glClear(GL_DEPTH_BUFFER_BIT)

http://bio.gsnu.ac.kr/~youknow/graphic/3DMAX_2.htm

https://lva.cg.tuwien.ac.at/ecg/wiki/doku.php?id=students:task4

73 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Stencil  buffer

• Memory  for  stencil • Determines  which  fragments  will  be  drawn

74 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Accumula<on  buffer

• Merging  more  scenes  or  more  views  to  one  scene • Enables  an<aliasing,  mo<on  blur,  blur  of  close  or  distant  objects

www.root.cz www.sgi.com

75 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Clearing  of  buffers

• Step  1:  specifying  values  which  should  be  used  aler  clearing void glClearColor(GLclampf r,GLclampf g, GLclampf b,GLclampf alpha) void glClearDepth(GLclampd depth) void glClearStencil(GLint s) void glClearAccum(GLfloat r, GLfloat g, GLfloat b,GLfloat alpha)

76 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Clearing  of  buffers

Step  2:  clearing  of  buffers where  mask  is  logical  OR  consis<ng  of: color          GL_COLOR_BUFFER_BIT depth          GL_DEPTH_BUFFER_BIT stencil        GL_STENCIL_BUFFER_BIT accumula<on    GL_ACCUM_BUFFER_BIT

void glClear(GLbitfield mask)

77 78 / Radek Oslejsek, Bolzano 2015 Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Example

glClearColor(0.0,0.0,0.0,0.0); glClearDepth(1.0); glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); This  sequence  of  commands  sets  the  color  of  window  to  black  and  the  value  in  the  Z-­‐buffer  to  1

78 78 / Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Exercise: Goals 3 and 4

79 78 / Radek Oslejsek, Bolzano 2015 Radek Oslejsek, Bolzano 2015 OpenGL

Fundamentals

Questions?