SlideShare une entreprise Scribd logo
1  sur  55
openFrameworks
     openGL
openGL
OF uses openGL to draw things to your screen.
openGL is a standard which is implemented by
most big operating systems and video cards. It’s
heavily in use by game developers.

Besided openGL there exist another big player
called DirectX. This is not (yet) used by OF.

These slides give you a model of how openGL
roughly works. For in depth information you can
visit www.opengl.org
GL

  ofVBO           ofMaterial

  ofFBO           ofTexture

ofShader         ofGLRenderer

 ofLight           ofGLUtils

ofVboMesh
Overview GL
ofTexture

An ofTexture is an object which holds pixels and is
used to draw images to your screen. When you
want to draw an image onto a lets say a 3D cube,
you tell openGL what texture you want to use, the
size, how the pixels are stored in memory etc..
etc.. Nice thing: you don’t need to know the
details, ofTexture does it all for you.
Overview GL
ofLight

ofLight is used to tell openGL how to simulate
lights in your 3D scene. In 3D graphics there are a
couple of standard light models. As in the real
world not every light has the same effect on an
object; the same count for openGL. There are i.e.
directional and spot lights.
Overview GL
ofMaterial

Each object in the real world has a specific kind of
surface. Some are rough some are smooth. Using
ofMaterial you can define the what kind of material
your 3D object has. openGL uses the material
properties together with the light properties to
calculate the colors of your 3D object.
Overview GL
ofVbo

openGL has the concept of “buffers”. In relation
with VBOs you can think of these as arrays with
lots of data describing a 3D scene. For example
where the 3D vertices of a cube are located in your
world. You can send these arrays with information
to your graphic card. VBO is short for Vertex Buffer
Object. ofVbo helps you managing the data you
can store in these buffers.
Overview GL
ofFbo

Frame Buffer Objects (FBO) are used to render
things, which you normally render to your screen
to something else, often to another texture. FBOs
are mostly used to apply special effects to your
scenes. ofFbo helps you to setup and manage
FBOs.
Overview GL
In short...

                            Frame Buffer Object used to render
    FBO         ofFbo                   offscreen.


                             Vertex Buffer Object: used to store
    VBO         ofVbo            data about your 3D scene.


                           OpenGL uses light settings to calculate
   Light       ofLight        the colors of your 3D objects.


                            Light color values are affected by the
 Material     ofMaterial             material properties.
important
Deprecated

The current version of openFrameworks uses
deprecated openGL functions to simulate light and
materials. So if you’re interested in how it’s done,
make sure to look at the current state of openGL.
Though you’re probably safe to use ofLight and
ofMaterial

A very good, the best I’ve found, online and free
tutorial can be found here:
http://www.arcsynthesis.org/gltut/
ofLight




setup ofLight
light.enable();
light.setDirectional();
light.setPosition(0,0,150);

ofFloatColor ambient_color(1.0, 0.0, 0.0,1.0);
ofFloatColor diffuse_color(1.0, 1.0, 1.0);
ofFloatColor specular_color(0.0, 1.0, 0.0);

light.setAmbientColor(ambient_color);
light.setDiffuseColor(diffuse_color);
light.setSpecularColor(specular_color);
ofLight
Draw something with light

   void testApp::draw(){
   
 // use an easy cam!
   
 cam.begin();
   
   
 // rotate around origin of view space
   
 float xx = 0;
   
 float yy = sin(ofGetElapsedTimef()*0.4)*150;
   
 float zz = cos(ofGetElapsedTimef()*0.4)*150;

   
   light.enable();
   
   light.setPosition(xx,yy,zz);
   
   light.draw();

   
   // draw a box
   
   ofPushStyle();
   
   
    ofRotateY(45);
   
   
    ofSetColor(255,255,255);
   
   
    ofBox(0,0,10,220);
   
   ofPopStyle();
   }
ofVbo
A VBO is an openGL concept used to reduce the
number of openGL function calls. With one call you
can draw millions of vertices at once.

ofVbo sets up all things related to VBOs. You need
to provide the vertex data yourself.

ofMesh (see the 3D keynote), is an
openFrameworks container for 3D related data.
ofMesh and ofVbo are therefore very related and
used together.
ofVbo
So how does this work?

Simple, first create an ofMesh and fill it with some
vertices, then pass it to an ofVbo and you’re ready
to go!

               // our vbo/mesh
               ofVbo my_vbo;
               ofMesh my_mesh;
               
               // store position data
               my_mesh.addVertex(ofVec3f(-1,0,1));
               my_mesh.addVertex(ofVec3f(1,0,1));
               my_mesh.addVertex(ofVec3f(1,0,-1));
               my_mesh.addVertex(ofVec3f(-1,0,-1));
               
 
               // pass the mesh to the vbo
               my_vbo.setMesh(my_mesh, GL_STATIC_DRAW);
ofVbo
GL_STATIC_DRAW (?)

Here we use raw openGL. The VBO and related
vertex data is stored in high performance memory.
When you supply data to a VBO (remember, which
is just an array), you give openGL a hint on how
often this data is changed. openGL can apply
different performance tricks based on this hint.
ofVbo
Hints about VBO data
                  When vertices data are never or almost never
 GL_STATIC_DRAW                     updated

GL_DYNAMIC_DRA    When vertices data could be updated between
                                  each frames.
      W
                  When vertices data could be updated between
GL_STREAM_DRAW                   each rendering.
ofVbo
class My3DObject : public ofNode {
public:

 My3DObject() {

 
     // store position data

 
     my_mesh.addVertex(ofVec3f(-1,0,1));

 
     my_mesh.addVertex(ofVec3f(1,0,1));

 
     my_mesh.addVertex(ofVec3f(1,0,-1));

 
     my_mesh.addVertex(ofVec3f(-1,0,-1));

 

 
     // pass the mesh to the vbo

 
     my_vbo.setMesh(my_mesh, GL_STATIC_DRAW);

 }


 void customDraw() {

 

 
        setScale(100);
        glDisable(GL_CULL_FACE);                   We inherit from

 

 
        rotate(0.05, getXAxis());
        my_vbo.draw(GL_QUADS, 0, 4);
                                                   ofNode so we got all

 

 
     ofScale(1.1,1.1,1.1);
                                                   the functions like

 

 }
        my_vbo.draw(GL_LINE_LOOP, 0, 4);
          scaling, positioning,


 ofVbo my_vbo;
                                                   rotating etc..

 ofMesh my_mesh;
};
ofVbo
Note that when adding more information then
just the positions, like color, normals, texcoords,
you need to provide the same number of
elements. So 4 vertices mean 4 colors.



// store position data
my_mesh.addVertex(ofVec3f(-1,0,1));
my_mesh.addVertex(ofVec3f(1,0,1));
my_mesh.addVertex(ofVec3f(1,0,-1));
my_mesh.addVertex(ofVec3f(-1,0,-1));

 
// store colors
my_mesh.addColor(ofFloatColor(1.0, 0.0,   0.0,   1.0));
my_mesh.addColor(ofFloatColor(0.0, 1.0,   0.0,   1.0));
my_mesh.addColor(ofFloatColor(0.0, 0.0,   1.0,   1.0));
my_mesh.addColor(ofFloatColor(0.0, 1.0,   1.0,   1.0));
ofVbo
Mesh
void setMesh(const ofMesh& mesh, int usage)

void updateMesh(const ofMesh& mesh)




Vertex Data
void setVertexData(const ofVec3f* verts, int total, int usage)

void setVertexData(const ofVec2f* verts, int total, int usage)

void updateVertexData(const ofVec3f* verts, int total)

void updateVertexData(const ofVe2f* verts, int total)

GLuint getVertId()
ofVbo

TexCoord data
void setTexCoordData(const ofVec2f* texcoords, int total, int usage)

void updateTexCoordData(const ofFloatColor* colors, int total)

GLuint getTexCoordId()



Normal data
void updateNormalData(const ofFloatColor* colors, int total)

void setNormalData(const ofVec3f* normals, int total, int usage)

GLuint getNormalId()
ofVbo

Color data
void setColorData(const ofFloatColor* colors, int total, int usage)

void updateColorData(const ofFloatColor* colors, int total)

GLuint getColorId()



Index data
void setIndexData(const ofIndexType* indices, int total, int usage)

void updateIndexData(const oofIndexType* indices, int total)

GLuint getIndexId()
ofFbo
A FBO, frame buffer object, is mainly used to
render something to a offscreen “thing”. This thing
can be a texture which you then can apply on
something else.


Note: although a FBO has the word buffer in it, it
does not allocate any memory itself. You hookup
other objects that have memory storage such as
render buffers or textures.
ofFbo
    How to use an ofFbo
    Create a member for the ofFbo
    •Allocate the size of the FBO
     •Call begin()
     •[DRAW]
     •call end()
    •Use the fbo texture to draw the capture [DRAW].

     Pseude code (!)

    my_fbo.allocate(320,240);

    ...

    my_fbo.begin();

    
   my_cam.begin();

    
   my_3d_object.draw();

    
   my_cam.end();

    my_fbo.end();

    my_fbo.draw(0,0);
ofFbo example
Custom 3D object using ofVbo and ofMesh                            testApp.h (standard functions hidden)
class My3DObject : public ofNode {
public:                                                            class testApp : public ofBaseApp{

   My3DObject() {

   
     // store position data                                   
    public:
   

   
     my_mesh.addVertex(ofVec3f(-1,0,1));                      
    
     ofEasyCam cam;

   
     my_mesh.addVertex(ofVec3f(1,0,1));                       
    
     My3DObject my_3d_object;

   
     my_mesh.addVertex(ofVec3f(1,0,-1));                      
    
     ofFbo my_fbo;

   
     my_mesh.addVertex(ofVec3f(-1,0,-1));                     };

   

   
     my_mesh.addColor(ofFloatColor(1.0, 1.0, 0.0, 1.0));

   
     my_mesh.addColor(ofFloatColor(1.0, 1.0, 1.0, 1.0));

   
     my_mesh.addColor(ofFloatColor(1.0, 0.0, 1.0, 1.0));

   
     my_mesh.addColor(ofFloatColor(0.0, 1.0, 1.0, 1.0));

   

   
     // pass the mesh to the vbo

   
     my_vbo.setMesh(my_mesh, GL_STATIC_DRAW);

   }


   void customDraw() {

   
     setScale(100);

   
     glDisable(GL_CULL_FACE); // we want to see both sides.

   
     rotate(0.05, getXAxis());

   
     my_vbo.draw(GL_QUADS, 0, 4);

   

   
     ofScale(1.1,1.1,1.1);

   
     my_vbo.draw(GL_LINE_LOOP, 0, 4);

   }


   ofVbo my_vbo;

   ofMesh my_mesh;
};
ofFbo example
                               Drawing the VBO (not into the FBO...yet)




void testApp::setup(){

 ofBackground(33,33,33);

 my_fbo.allocate(ofGetWidth(),ofGetHeight());

}

void testApp::draw(){

 cam.begin();

 my_3d_object.draw();
}
ofFbo example
                                        Drawing into the FBO




void testApp::setup(){

 ofBackground(33,33,33);

 my_fbo.allocate(ofGetWidth(),ofGetHeight());

}

void testApp::draw(){

 my_fbo.begin();

 
      cam.begin();

 
      
  my_3d_object.draw();

 
      cam.end();

 my_fbo.end();

 my_fbo.draw(0,0,320,240);

}
ofFbo example
                            Drawing the FBO texture more then once




void testApp::draw(){

 cam.begin();

 my_fbo.begin();

 
      cam.begin();

 
      
    my_3d_object.draw();

 
      cam.end();

 my_fbo.end();


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

 
      for(int j = 0; j < 2; ++j) {

 
      
    my_fbo.draw(i*320,j*240,320,240);

 
      }

 }
}
ofFbo
void allocate(int width, int height, int internalformat = GL_RGBA, int numSamples = 0)

void allocateForShadow(int width, int height)

void draw(float x, float y)

void draw(float x, float y, float width, float height)

float getWidth()

float getHeight()

void begin()

void end()

ofTexture& getTexture(int attachmnetPoint = 0)


For more information see ofFbo.h
ofShader
Shaders let you create amazing effects. A shader is
a small program which runs on your graphics card.
There are several kinds of programs you can create
and the programs run in parallel on your graphics
card.

You can create a shader for vertices and i.e.
change the position of these. Such a shader is
called a vertex shader. Another type of shader is
called a fragment shader which operates on one
pixel.
ofShader
                Vertex and fragment shaders




So each shader has a specific purpose. Vertex and
fragment shaders form a “group”. Often with a
“shader” we mean both a vertex and fragment
shader.

There are more kinds of shaders like geometry
shaders which let you generate vertices on the
graphics card. Mac OSX 10.6.x does not support
these. 10.7 will!
ofShader
For a shader you need to write the code for the
shader-program. The language is called openGL
Shader Language (GLSL) and looks a lot like C.

Just like any other programming language you
need to compile it. This is done by the openGL
driver. openGL provides functions to compile the
shaders.

ofShader sets up all necessary things like loading
the shader, compiling, linking, etc..
ofShader

Geometry Shader       Shaders are executed in
                      different, successive
                      stages.

 Vertex Shader




Fragment Shader
ofShader
Commonly used file extensions

      .vert              For vertex shaders



      .frag             For fragment shaders



     .geom              For geometry shaders
Vertex Shader
                             Basic vertex shader example



           void main() {
           
 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
           }



This example shows a very basic shader. It uses
(by openGL) globally defined variables.


          gl_Position                         The final position of the 3D point


  gl_ModelViewProjectionMatrix       Converts a point from object space to screen space



                                    The 3D position we’re working with. When you created
           gl_Vertex                 an ofMesh and added vertices to it, this variable will
                                             hold the value of the set vertices.
Fragment Shader
                        Basic fragment shader example




               void main() {
               
 gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
               }




A fragment shader is executed for each fragment
and is the final shader stage. It is executed after
the vertex shader.


       gl_FragColor                           The final color of a fragment
Recap shaders
So what you need to remember is that a shader is a
program which runs on GPU. The vertex shader is
used to set a position of a 3D point; the fragment
shader is used to set the color of a fragment.

There are several globally defined variables, i.e.
gl_Position, gl_ModelViewProjectionMatrix,
gl_Vertex, gl_FragData.

Note: some of these are deprecated
Shader programming
GLSL looks like C. There are several data types and
functions. Just like any other language the
variables have scope.

Some common data types are int, vec2, vec3, vec4,
mat3 and mat4. For boolean values you can use an
int. GLSL has some special variable identifiers
called varying and uniform.

varying variables will be interpolated between the
vertex and fragment shader. uniform variable will
stay... uniform (immutable)
Shader programming
Lets say you have two vertices and a varying
variable. When the value is 0.0 for the first vertex
and 1.0 for the second vertex it will be
interpolated between 0.0 and 1.0 between the
vertices.

      v0     0.1

                       0.5

                                   0.95


                                          v1
Shader programming
Uniform variables are used to “communicate”
between a shader and the host application (the OF
application in this case).

To change a value of an uniform variable your
application needs to know where in (GPU) memory
the location of the variable is stored. openGL uses
glGetUniformLocation(program, name) for this.
You don’t have to worry about this because
ofShader handles this for you.
Attributes
There is another type of variables I didn’t mention
yet. Attributes are a special kind of variables which
are the way to go when you want to start
programming shaders. They allow you to use
custom data for gl_Vertex, gl_FrontColor,
gl_TexCoord etc... For now we will focus on the
globally defined variables as this makes it a lot
easier to start creating shaders.
Using ofShader
To create a shader, follow these steps:
1. Lets assume we call the shader “my_shader”.
   Then create two files my_shader.vert and
   my_shader.frag in your data directory.

2. Create a member ofShader my_shader in your
   testApp.

3. Load the shaders

4. Create a simple ofVBO/ofMesh and draw while
   the shader is enabled.
1. create shader files



bin/data/my_shader.vert

void main() {

 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}




bin/data/my_shader.frag

void main() {

 gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
}
2. create ofShader member
  testApp.h (we add some more members at step 4)




       class testApp : public ofBaseApp{

       
   public:
       
   
    ofShader my_shader;
       }
3. Load the shaders
testApp.cpp (we add some more code at the next step)




            void testApp::setup(){
            
 my_shader.load("of");
            }
4a. Create simple ofVbo and ofMesh

         testApp.h

         class testApp : public ofBaseApp{
         
 public:
         
 
      ofShader my_shader;
         
 
      ofMesh my_mesh;
         
 
      ofVbo my_vbo;
         
 
      ofEasyCam cam;
         }


         testApp.cpp
         void testApp::setup(){
         
 // Load of.vert and of.frag
         
 my_shader.load("of");

         
   // Create a simple QUAD
         
   float s = 100;
         
   my_mesh.addVertex(ofVec3f(-s,s,0));
         
   my_mesh.addVertex(ofVec3f(s,s,0));
         
   my_mesh.addVertex(ofVec3f(s,-s,0));
         
   my_mesh.addVertex(ofVec3f(-s,-s,0));
         
   my_vbo.setMesh(my_mesh, GL_STATIC_DRAW);
         }
4b. Draw using shader




  testApp.cpp
  void testApp::draw(){
  
 cam.begin();
  
 
      my_shader.begin();
  
 
      
  my_vbo.draw(GL_QUADS,0,4);
  
 
      my_shader.end();
  
 cam.end();
  }
Using ofShader - all code


testApp.h                                                     testApp.cpp
                                                              void testApp::setup(){
class testApp : public ofBaseApp{                             
   ofBackground(33,33,33);

   public:

   
      ofShader my_shader;

   
      ofMesh my_mesh;                                    
   // Load of.vert and of.frag

   
      ofVbo my_vbo;                                      
   my_shader.load("of");

   
      ofEasyCam cam;
}                                                             
   // Create a simple QUAD
                                                              
   float s = 100;
                                                              
   my_mesh.addVertex(ofVec3f(-s,s,0));
                                                              
   my_mesh.addVertex(ofVec3f(s,s,0));
of.vert                                                       
   my_mesh.addVertex(ofVec3f(s,-s,0));
void main() {                                                 
   my_mesh.addVertex(ofVec3f(-s,-s,0));

   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;   
   my_vbo.setMesh(my_mesh, GL_STATIC_DRAW);
}                                                             }



of.frag                                                       void testApp::draw(){
                                                              
   cam.begin();
void main() {                                                 
   
     my_shader.begin();

   gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);                  
   
     
     my_vbo.draw(GL_QUADS,0,4);
}                                                             
   
     my_shader.end();
                                                              
   cam.end();
                                                              }
ofShader
Using uniforms
Uniforms allow you to change values of variables in a
shader. You can do this by defining a uniform
variable and using one of the setUniform**()
functions. The next example uses an uniform to
change the value of gl_FragData
ofShader
Using uniforms - vertex shader
uniform int u_color;
void main() {

 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}



Using uniforms - fragment shader
uniform int u_color;

void main() {

 vec4 diffuse_color = vec4(1.0, 0.0, 0.0, 1.0);

 if(u_color == 1) {

 
     diffuse_color.rgb = vec3(1.0, 0.0, 0.0);

 }

 else if(u_color == 2) {

 
     diffuse_color.rgb = vec3(0.0, 1.0, 0.0);

 }

 else if(u_color == 3) {

 
     diffuse_color.rgb = vec3(0.0, 0.0, 1.0);

 }


 gl_FragColor = diffuse_color;
}
ofShader
Using uniforms
When you want to set an uniform value make sure
that you enable the shader first! You can do this by
calling the
begin() function.

        void testApp::draw(){
        
 cam.begin();
        
 
      my_shader.begin();
        
 
      
  my_shader.setUniform1i("u_color", use_color);
        
 
      
  my_vbo.draw(GL_QUADS,0,4);
        
 
      my_shader.end();
        
 cam.end();
        }
ofShader
Using uniforms




Press “1”         Press “2”   Press “3”
ofShader
  Simple light
  Here follows an example of a simple light shader. For more
  information on shaders you can visit this page which has lots of
  interesting information:
  http://www.arcsynthesis.org/gltut/

of.vert                                                          of.frag
varying vec3 v_light_dir;
varying vec3 v_normal;                                           varying vec3 v_light_dir;
uniform float time;                                              varying vec3 v_normal;
                                                                 uniform float time;
void   main() {

      gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

      
                                                         void   main() {

      // get view space position.                               
      vec4 diffuse_color = vec4(1.0, 0.0, 0.0, 1.0);

      vec4 position = gl_ModelViewMatrix * gl_Vertex;           

                                                                
      // IMPORTANT: you need to normalize the normal and light dir!

      // we create our light position in "view space".          
      float ndotl = max(dot(normalize(v_normal), normalize(v_light_dir)),0.0);

      vec3 light_position = vec3(1.0, sin(time)*130.0,1.0);     
      vec4 result_color = diffuse_color * ndotl;

      v_light_dir = (position.xyz - light_position);            
      gl_FragColor = result_color;

                                                                }

      // get normal in eye coordinates.

      v_normal = (gl_NormalMatrix * gl_Normal);
}
ofShader
General
void load(string shaderName)

void load(string vertName, string fragName, string geomName = “”)

void begin()

void end()


Uniforms: integers
void setUniformTexture(const char* name, ofBaseHasTexture& img, int textureLocation)

void setUniformTexture(const char* name, ofTexture& img, int textureLocation)

void setUniform1i(const char* name, int v1)

void setUniform2i(const char* name, int v1, int v2)

void setUniform3i(const char* name, int v1, int v2, int v3)

void setUniform4i(const char* name, int v1, int v2, int v3, int v4)
ofShader
Uniforms: floats
void setUniform1f(const char* name, float v1)

void setUniform2f(const char* name, float v1, float v2)

void setUniform3f(const char* name, float v1, float v2, float v3)

void setUniform4f(const char* name, float v1, float v2, float v3, float v4)


Uniforms: floats arrays
void setUniform1f(const char* name, float* v, int count = 1)

void setUniform2f(const char* name, float* v, int count = 1)

void setUniform3f(const char* name, float* v, int count = 1)

void setUniform4f(const char* name, float* v, int count = 1)

For more information see ofShader.h
roxlu
www.roxlu.com

Contenu connexe

Tendances

Python Collections
Python CollectionsPython Collections
Python Collectionssachingarg0
 
Pendugaan dan-pengujian-hipotesis
Pendugaan dan-pengujian-hipotesis Pendugaan dan-pengujian-hipotesis
Pendugaan dan-pengujian-hipotesis Wisma Morgans
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#foreverredpb
 
Scaling and shearing
Scaling and shearingScaling and shearing
Scaling and shearingMani Kanth
 
Pendapatan nasional
Pendapatan nasionalPendapatan nasional
Pendapatan nasionalKuny Raint
 
Introduction to Computer graphics
Introduction to Computer graphicsIntroduction to Computer graphics
Introduction to Computer graphicsLOKESH KUMAR
 
Bab 3 nilai_waktu_uang
Bab 3 nilai_waktu_uangBab 3 nilai_waktu_uang
Bab 3 nilai_waktu_uangInal Ypyn
 
Perspective projection
Perspective projectionPerspective projection
Perspective projectionPranjalDas25
 
Diferensial fungsi sederhana.pptx
Diferensial fungsi sederhana.pptxDiferensial fungsi sederhana.pptx
Diferensial fungsi sederhana.pptxJohan Sampoerno
 
Bab 12 uji anova stata dan spss
Bab 12 uji anova stata dan    spssBab 12 uji anova stata dan    spss
Bab 12 uji anova stata dan spssNajMah Usman
 
KOMPONEN PENDAPATAN NASIONAL
KOMPONEN PENDAPATAN NASIONALKOMPONEN PENDAPATAN NASIONAL
KOMPONEN PENDAPATAN NASIONALheckaathaya
 
Parallel projection
Parallel projectionParallel projection
Parallel projectionPrince Shahu
 

Tendances (17)

Python Collections
Python CollectionsPython Collections
Python Collections
 
3D Transformation
3D Transformation 3D Transformation
3D Transformation
 
Pendugaan dan-pengujian-hipotesis
Pendugaan dan-pengujian-hipotesis Pendugaan dan-pengujian-hipotesis
Pendugaan dan-pengujian-hipotesis
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Shading in OpenGL
Shading in OpenGLShading in OpenGL
Shading in OpenGL
 
Uji kolmogorov 2
Uji kolmogorov 2Uji kolmogorov 2
Uji kolmogorov 2
 
Scaling and shearing
Scaling and shearingScaling and shearing
Scaling and shearing
 
Pendapatan nasional
Pendapatan nasionalPendapatan nasional
Pendapatan nasional
 
Kemungkinan
KemungkinanKemungkinan
Kemungkinan
 
Introduction to Computer graphics
Introduction to Computer graphicsIntroduction to Computer graphics
Introduction to Computer graphics
 
Bab 3 nilai_waktu_uang
Bab 3 nilai_waktu_uangBab 3 nilai_waktu_uang
Bab 3 nilai_waktu_uang
 
Perspective projection
Perspective projectionPerspective projection
Perspective projection
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - Shading
 
Diferensial fungsi sederhana.pptx
Diferensial fungsi sederhana.pptxDiferensial fungsi sederhana.pptx
Diferensial fungsi sederhana.pptx
 
Bab 12 uji anova stata dan spss
Bab 12 uji anova stata dan    spssBab 12 uji anova stata dan    spss
Bab 12 uji anova stata dan spss
 
KOMPONEN PENDAPATAN NASIONAL
KOMPONEN PENDAPATAN NASIONALKOMPONEN PENDAPATAN NASIONAL
KOMPONEN PENDAPATAN NASIONAL
 
Parallel projection
Parallel projectionParallel projection
Parallel projection
 

En vedette

openFrameworks 007 - video
openFrameworks 007 - videoopenFrameworks 007 - video
openFrameworks 007 - videoroxlu
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utilsroxlu
 
openFrameworks 007 - events
openFrameworks 007 - eventsopenFrameworks 007 - events
openFrameworks 007 - eventsroxlu
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3Droxlu
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphicsroxlu
 
openFrameworks 007 - sound
openFrameworks 007 - soundopenFrameworks 007 - sound
openFrameworks 007 - soundroxlu
 

En vedette (6)

openFrameworks 007 - video
openFrameworks 007 - videoopenFrameworks 007 - video
openFrameworks 007 - video
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utils
 
openFrameworks 007 - events
openFrameworks 007 - eventsopenFrameworks 007 - events
openFrameworks 007 - events
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3D
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphics
 
openFrameworks 007 - sound
openFrameworks 007 - soundopenFrameworks 007 - sound
openFrameworks 007 - sound
 

Similaire à openFrameworks 007 - GL

Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeksBeginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeksJinTaek Seo
 
Introduction to open gl in android droidcon - slides
Introduction to open gl in android   droidcon - slidesIntroduction to open gl in android   droidcon - slides
Introduction to open gl in android droidcon - slidestamillarasan
 
Richard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL ModuleRichard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL ModuleAxway Appcelerator
 
GL Shading Language Document by OpenGL.pdf
GL Shading Language Document by OpenGL.pdfGL Shading Language Document by OpenGL.pdf
GL Shading Language Document by OpenGL.pdfshaikhshehzad024
 
Js info vis_toolkit
Js info vis_toolkitJs info vis_toolkit
Js info vis_toolkitnikhilyagnic
 
WebGL - 3D in your Browser
WebGL - 3D in your BrowserWebGL - 3D in your Browser
WebGL - 3D in your BrowserPhil Reither
 
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...ICS
 
Unconventional webapps with gwt:elemental & html5
Unconventional webapps with gwt:elemental & html5Unconventional webapps with gwt:elemental & html5
Unconventional webapps with gwt:elemental & html5firenze-gtug
 
ShaderX³: Geometry Manipulation - Morphing between two different objects
ShaderX³: Geometry Manipulation - Morphing between two different objectsShaderX³: Geometry Manipulation - Morphing between two different objects
ShaderX³: Geometry Manipulation - Morphing between two different objectsRonny Burkersroda
 
World wind java sdk in progess
World wind java sdk in progessWorld wind java sdk in progess
World wind java sdk in progessRaffaele de Amicis
 
Developing Web Graphics with WebGL
Developing Web Graphics with WebGLDeveloping Web Graphics with WebGL
Developing Web Graphics with WebGLTony Parisi
 
A Novice's Guide to WebGL
A Novice's Guide to WebGLA Novice's Guide to WebGL
A Novice's Guide to WebGLKrzysztof Kula
 
iOS Visual F/X Using GLSL
iOS Visual F/X Using GLSLiOS Visual F/X Using GLSL
iOS Visual F/X Using GLSLDouglass Turner
 
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014Verold
 
I need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdfI need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdfConint29
 

Similaire à openFrameworks 007 - GL (20)

Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeksBeginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
 
Introduction to open gl in android droidcon - slides
Introduction to open gl in android   droidcon - slidesIntroduction to open gl in android   droidcon - slides
Introduction to open gl in android droidcon - slides
 
OpenGL L06-Performance
OpenGL L06-PerformanceOpenGL L06-Performance
OpenGL L06-Performance
 
Richard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL ModuleRichard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL Module
 
GL Shading Language Document by OpenGL.pdf
GL Shading Language Document by OpenGL.pdfGL Shading Language Document by OpenGL.pdf
GL Shading Language Document by OpenGL.pdf
 
Js info vis_toolkit
Js info vis_toolkitJs info vis_toolkit
Js info vis_toolkit
 
WebGL - 3D in your Browser
WebGL - 3D in your BrowserWebGL - 3D in your Browser
WebGL - 3D in your Browser
 
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
 
Unconventional webapps with gwt:elemental & html5
Unconventional webapps with gwt:elemental & html5Unconventional webapps with gwt:elemental & html5
Unconventional webapps with gwt:elemental & html5
 
ShaderX³: Geometry Manipulation - Morphing between two different objects
ShaderX³: Geometry Manipulation - Morphing between two different objectsShaderX³: Geometry Manipulation - Morphing between two different objects
ShaderX³: Geometry Manipulation - Morphing between two different objects
 
World wind java sdk in progess
World wind java sdk in progessWorld wind java sdk in progess
World wind java sdk in progess
 
Developing Web Graphics with WebGL
Developing Web Graphics with WebGLDeveloping Web Graphics with WebGL
Developing Web Graphics with WebGL
 
WebGL 2.0 Reference Guide
WebGL 2.0 Reference GuideWebGL 2.0 Reference Guide
WebGL 2.0 Reference Guide
 
A Novice's Guide to WebGL
A Novice's Guide to WebGLA Novice's Guide to WebGL
A Novice's Guide to WebGL
 
React native
React nativeReact native
React native
 
iOS Visual F/X Using GLSL
iOS Visual F/X Using GLSLiOS Visual F/X Using GLSL
iOS Visual F/X Using GLSL
 
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
From Hello World to the Interactive Web with Three.js: Workshop at FutureJS 2014
 
pastel
pastelpastel
pastel
 
pastel
pastelpastel
pastel
 
I need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdfI need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdf
 

Dernier

Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Dernier (20)

Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

openFrameworks 007 - GL

  • 1. openFrameworks openGL
  • 2. openGL OF uses openGL to draw things to your screen. openGL is a standard which is implemented by most big operating systems and video cards. It’s heavily in use by game developers. Besided openGL there exist another big player called DirectX. This is not (yet) used by OF. These slides give you a model of how openGL roughly works. For in depth information you can visit www.opengl.org
  • 3. GL ofVBO ofMaterial ofFBO ofTexture ofShader ofGLRenderer ofLight ofGLUtils ofVboMesh
  • 4. Overview GL ofTexture An ofTexture is an object which holds pixels and is used to draw images to your screen. When you want to draw an image onto a lets say a 3D cube, you tell openGL what texture you want to use, the size, how the pixels are stored in memory etc.. etc.. Nice thing: you don’t need to know the details, ofTexture does it all for you.
  • 5. Overview GL ofLight ofLight is used to tell openGL how to simulate lights in your 3D scene. In 3D graphics there are a couple of standard light models. As in the real world not every light has the same effect on an object; the same count for openGL. There are i.e. directional and spot lights.
  • 6. Overview GL ofMaterial Each object in the real world has a specific kind of surface. Some are rough some are smooth. Using ofMaterial you can define the what kind of material your 3D object has. openGL uses the material properties together with the light properties to calculate the colors of your 3D object.
  • 7. Overview GL ofVbo openGL has the concept of “buffers”. In relation with VBOs you can think of these as arrays with lots of data describing a 3D scene. For example where the 3D vertices of a cube are located in your world. You can send these arrays with information to your graphic card. VBO is short for Vertex Buffer Object. ofVbo helps you managing the data you can store in these buffers.
  • 8. Overview GL ofFbo Frame Buffer Objects (FBO) are used to render things, which you normally render to your screen to something else, often to another texture. FBOs are mostly used to apply special effects to your scenes. ofFbo helps you to setup and manage FBOs.
  • 9. Overview GL In short... Frame Buffer Object used to render FBO ofFbo offscreen. Vertex Buffer Object: used to store VBO ofVbo data about your 3D scene. OpenGL uses light settings to calculate Light ofLight the colors of your 3D objects. Light color values are affected by the Material ofMaterial material properties.
  • 10. important Deprecated The current version of openFrameworks uses deprecated openGL functions to simulate light and materials. So if you’re interested in how it’s done, make sure to look at the current state of openGL. Though you’re probably safe to use ofLight and ofMaterial A very good, the best I’ve found, online and free tutorial can be found here: http://www.arcsynthesis.org/gltut/
  • 11. ofLight setup ofLight light.enable(); light.setDirectional(); light.setPosition(0,0,150); ofFloatColor ambient_color(1.0, 0.0, 0.0,1.0); ofFloatColor diffuse_color(1.0, 1.0, 1.0); ofFloatColor specular_color(0.0, 1.0, 0.0); light.setAmbientColor(ambient_color); light.setDiffuseColor(diffuse_color); light.setSpecularColor(specular_color);
  • 12. ofLight Draw something with light void testApp::draw(){ // use an easy cam! cam.begin(); // rotate around origin of view space float xx = 0; float yy = sin(ofGetElapsedTimef()*0.4)*150; float zz = cos(ofGetElapsedTimef()*0.4)*150; light.enable(); light.setPosition(xx,yy,zz); light.draw(); // draw a box ofPushStyle(); ofRotateY(45); ofSetColor(255,255,255); ofBox(0,0,10,220); ofPopStyle(); }
  • 13. ofVbo A VBO is an openGL concept used to reduce the number of openGL function calls. With one call you can draw millions of vertices at once. ofVbo sets up all things related to VBOs. You need to provide the vertex data yourself. ofMesh (see the 3D keynote), is an openFrameworks container for 3D related data. ofMesh and ofVbo are therefore very related and used together.
  • 14. ofVbo So how does this work? Simple, first create an ofMesh and fill it with some vertices, then pass it to an ofVbo and you’re ready to go! // our vbo/mesh ofVbo my_vbo; ofMesh my_mesh; // store position data my_mesh.addVertex(ofVec3f(-1,0,1)); my_mesh.addVertex(ofVec3f(1,0,1)); my_mesh.addVertex(ofVec3f(1,0,-1)); my_mesh.addVertex(ofVec3f(-1,0,-1)); // pass the mesh to the vbo my_vbo.setMesh(my_mesh, GL_STATIC_DRAW);
  • 15. ofVbo GL_STATIC_DRAW (?) Here we use raw openGL. The VBO and related vertex data is stored in high performance memory. When you supply data to a VBO (remember, which is just an array), you give openGL a hint on how often this data is changed. openGL can apply different performance tricks based on this hint.
  • 16. ofVbo Hints about VBO data When vertices data are never or almost never GL_STATIC_DRAW updated GL_DYNAMIC_DRA When vertices data could be updated between each frames. W When vertices data could be updated between GL_STREAM_DRAW each rendering.
  • 17. ofVbo class My3DObject : public ofNode { public: My3DObject() { // store position data my_mesh.addVertex(ofVec3f(-1,0,1)); my_mesh.addVertex(ofVec3f(1,0,1)); my_mesh.addVertex(ofVec3f(1,0,-1)); my_mesh.addVertex(ofVec3f(-1,0,-1)); // pass the mesh to the vbo my_vbo.setMesh(my_mesh, GL_STATIC_DRAW); } void customDraw() { setScale(100); glDisable(GL_CULL_FACE); We inherit from rotate(0.05, getXAxis()); my_vbo.draw(GL_QUADS, 0, 4); ofNode so we got all ofScale(1.1,1.1,1.1); the functions like } my_vbo.draw(GL_LINE_LOOP, 0, 4); scaling, positioning, ofVbo my_vbo; rotating etc.. ofMesh my_mesh; };
  • 18. ofVbo Note that when adding more information then just the positions, like color, normals, texcoords, you need to provide the same number of elements. So 4 vertices mean 4 colors. // store position data my_mesh.addVertex(ofVec3f(-1,0,1)); my_mesh.addVertex(ofVec3f(1,0,1)); my_mesh.addVertex(ofVec3f(1,0,-1)); my_mesh.addVertex(ofVec3f(-1,0,-1)); // store colors my_mesh.addColor(ofFloatColor(1.0, 0.0, 0.0, 1.0)); my_mesh.addColor(ofFloatColor(0.0, 1.0, 0.0, 1.0)); my_mesh.addColor(ofFloatColor(0.0, 0.0, 1.0, 1.0)); my_mesh.addColor(ofFloatColor(0.0, 1.0, 1.0, 1.0));
  • 19. ofVbo Mesh void setMesh(const ofMesh& mesh, int usage) void updateMesh(const ofMesh& mesh) Vertex Data void setVertexData(const ofVec3f* verts, int total, int usage) void setVertexData(const ofVec2f* verts, int total, int usage) void updateVertexData(const ofVec3f* verts, int total) void updateVertexData(const ofVe2f* verts, int total) GLuint getVertId()
  • 20. ofVbo TexCoord data void setTexCoordData(const ofVec2f* texcoords, int total, int usage) void updateTexCoordData(const ofFloatColor* colors, int total) GLuint getTexCoordId() Normal data void updateNormalData(const ofFloatColor* colors, int total) void setNormalData(const ofVec3f* normals, int total, int usage) GLuint getNormalId()
  • 21. ofVbo Color data void setColorData(const ofFloatColor* colors, int total, int usage) void updateColorData(const ofFloatColor* colors, int total) GLuint getColorId() Index data void setIndexData(const ofIndexType* indices, int total, int usage) void updateIndexData(const oofIndexType* indices, int total) GLuint getIndexId()
  • 22. ofFbo A FBO, frame buffer object, is mainly used to render something to a offscreen “thing”. This thing can be a texture which you then can apply on something else. Note: although a FBO has the word buffer in it, it does not allocate any memory itself. You hookup other objects that have memory storage such as render buffers or textures.
  • 23. ofFbo How to use an ofFbo Create a member for the ofFbo •Allocate the size of the FBO •Call begin() •[DRAW] •call end() •Use the fbo texture to draw the capture [DRAW]. Pseude code (!) my_fbo.allocate(320,240); ... my_fbo.begin(); my_cam.begin(); my_3d_object.draw(); my_cam.end(); my_fbo.end(); my_fbo.draw(0,0);
  • 24. ofFbo example Custom 3D object using ofVbo and ofMesh testApp.h (standard functions hidden) class My3DObject : public ofNode { public: class testApp : public ofBaseApp{ My3DObject() { // store position data public: my_mesh.addVertex(ofVec3f(-1,0,1)); ofEasyCam cam; my_mesh.addVertex(ofVec3f(1,0,1)); My3DObject my_3d_object; my_mesh.addVertex(ofVec3f(1,0,-1)); ofFbo my_fbo; my_mesh.addVertex(ofVec3f(-1,0,-1)); }; my_mesh.addColor(ofFloatColor(1.0, 1.0, 0.0, 1.0)); my_mesh.addColor(ofFloatColor(1.0, 1.0, 1.0, 1.0)); my_mesh.addColor(ofFloatColor(1.0, 0.0, 1.0, 1.0)); my_mesh.addColor(ofFloatColor(0.0, 1.0, 1.0, 1.0)); // pass the mesh to the vbo my_vbo.setMesh(my_mesh, GL_STATIC_DRAW); } void customDraw() { setScale(100); glDisable(GL_CULL_FACE); // we want to see both sides. rotate(0.05, getXAxis()); my_vbo.draw(GL_QUADS, 0, 4); ofScale(1.1,1.1,1.1); my_vbo.draw(GL_LINE_LOOP, 0, 4); } ofVbo my_vbo; ofMesh my_mesh; };
  • 25. ofFbo example Drawing the VBO (not into the FBO...yet) void testApp::setup(){ ofBackground(33,33,33); my_fbo.allocate(ofGetWidth(),ofGetHeight()); } void testApp::draw(){ cam.begin(); my_3d_object.draw(); }
  • 26. ofFbo example Drawing into the FBO void testApp::setup(){ ofBackground(33,33,33); my_fbo.allocate(ofGetWidth(),ofGetHeight()); } void testApp::draw(){ my_fbo.begin(); cam.begin(); my_3d_object.draw(); cam.end(); my_fbo.end(); my_fbo.draw(0,0,320,240); }
  • 27. ofFbo example Drawing the FBO texture more then once void testApp::draw(){ cam.begin(); my_fbo.begin(); cam.begin(); my_3d_object.draw(); cam.end(); my_fbo.end(); for(int i = 0; i < 2; ++i) { for(int j = 0; j < 2; ++j) { my_fbo.draw(i*320,j*240,320,240); } } }
  • 28. ofFbo void allocate(int width, int height, int internalformat = GL_RGBA, int numSamples = 0) void allocateForShadow(int width, int height) void draw(float x, float y) void draw(float x, float y, float width, float height) float getWidth() float getHeight() void begin() void end() ofTexture& getTexture(int attachmnetPoint = 0) For more information see ofFbo.h
  • 29. ofShader Shaders let you create amazing effects. A shader is a small program which runs on your graphics card. There are several kinds of programs you can create and the programs run in parallel on your graphics card. You can create a shader for vertices and i.e. change the position of these. Such a shader is called a vertex shader. Another type of shader is called a fragment shader which operates on one pixel.
  • 30. ofShader Vertex and fragment shaders So each shader has a specific purpose. Vertex and fragment shaders form a “group”. Often with a “shader” we mean both a vertex and fragment shader. There are more kinds of shaders like geometry shaders which let you generate vertices on the graphics card. Mac OSX 10.6.x does not support these. 10.7 will!
  • 31. ofShader For a shader you need to write the code for the shader-program. The language is called openGL Shader Language (GLSL) and looks a lot like C. Just like any other programming language you need to compile it. This is done by the openGL driver. openGL provides functions to compile the shaders. ofShader sets up all necessary things like loading the shader, compiling, linking, etc..
  • 32. ofShader Geometry Shader Shaders are executed in different, successive stages. Vertex Shader Fragment Shader
  • 33. ofShader Commonly used file extensions .vert For vertex shaders .frag For fragment shaders .geom For geometry shaders
  • 34. Vertex Shader Basic vertex shader example void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } This example shows a very basic shader. It uses (by openGL) globally defined variables. gl_Position The final position of the 3D point gl_ModelViewProjectionMatrix Converts a point from object space to screen space The 3D position we’re working with. When you created gl_Vertex an ofMesh and added vertices to it, this variable will hold the value of the set vertices.
  • 35. Fragment Shader Basic fragment shader example void main() { gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0); } A fragment shader is executed for each fragment and is the final shader stage. It is executed after the vertex shader. gl_FragColor The final color of a fragment
  • 36. Recap shaders So what you need to remember is that a shader is a program which runs on GPU. The vertex shader is used to set a position of a 3D point; the fragment shader is used to set the color of a fragment. There are several globally defined variables, i.e. gl_Position, gl_ModelViewProjectionMatrix, gl_Vertex, gl_FragData. Note: some of these are deprecated
  • 37. Shader programming GLSL looks like C. There are several data types and functions. Just like any other language the variables have scope. Some common data types are int, vec2, vec3, vec4, mat3 and mat4. For boolean values you can use an int. GLSL has some special variable identifiers called varying and uniform. varying variables will be interpolated between the vertex and fragment shader. uniform variable will stay... uniform (immutable)
  • 38. Shader programming Lets say you have two vertices and a varying variable. When the value is 0.0 for the first vertex and 1.0 for the second vertex it will be interpolated between 0.0 and 1.0 between the vertices. v0 0.1 0.5 0.95 v1
  • 39. Shader programming Uniform variables are used to “communicate” between a shader and the host application (the OF application in this case). To change a value of an uniform variable your application needs to know where in (GPU) memory the location of the variable is stored. openGL uses glGetUniformLocation(program, name) for this. You don’t have to worry about this because ofShader handles this for you.
  • 40. Attributes There is another type of variables I didn’t mention yet. Attributes are a special kind of variables which are the way to go when you want to start programming shaders. They allow you to use custom data for gl_Vertex, gl_FrontColor, gl_TexCoord etc... For now we will focus on the globally defined variables as this makes it a lot easier to start creating shaders.
  • 41. Using ofShader To create a shader, follow these steps: 1. Lets assume we call the shader “my_shader”. Then create two files my_shader.vert and my_shader.frag in your data directory. 2. Create a member ofShader my_shader in your testApp. 3. Load the shaders 4. Create a simple ofVBO/ofMesh and draw while the shader is enabled.
  • 42. 1. create shader files bin/data/my_shader.vert void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } bin/data/my_shader.frag void main() { gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0); }
  • 43. 2. create ofShader member testApp.h (we add some more members at step 4) class testApp : public ofBaseApp{ public: ofShader my_shader; }
  • 44. 3. Load the shaders testApp.cpp (we add some more code at the next step) void testApp::setup(){ my_shader.load("of"); }
  • 45. 4a. Create simple ofVbo and ofMesh testApp.h class testApp : public ofBaseApp{ public: ofShader my_shader; ofMesh my_mesh; ofVbo my_vbo; ofEasyCam cam; } testApp.cpp void testApp::setup(){ // Load of.vert and of.frag my_shader.load("of"); // Create a simple QUAD float s = 100; my_mesh.addVertex(ofVec3f(-s,s,0)); my_mesh.addVertex(ofVec3f(s,s,0)); my_mesh.addVertex(ofVec3f(s,-s,0)); my_mesh.addVertex(ofVec3f(-s,-s,0)); my_vbo.setMesh(my_mesh, GL_STATIC_DRAW); }
  • 46. 4b. Draw using shader testApp.cpp void testApp::draw(){ cam.begin(); my_shader.begin(); my_vbo.draw(GL_QUADS,0,4); my_shader.end(); cam.end(); }
  • 47. Using ofShader - all code testApp.h testApp.cpp void testApp::setup(){ class testApp : public ofBaseApp{ ofBackground(33,33,33); public: ofShader my_shader; ofMesh my_mesh; // Load of.vert and of.frag ofVbo my_vbo; my_shader.load("of"); ofEasyCam cam; } // Create a simple QUAD float s = 100; my_mesh.addVertex(ofVec3f(-s,s,0)); my_mesh.addVertex(ofVec3f(s,s,0)); of.vert my_mesh.addVertex(ofVec3f(s,-s,0)); void main() { my_mesh.addVertex(ofVec3f(-s,-s,0)); gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; my_vbo.setMesh(my_mesh, GL_STATIC_DRAW); } } of.frag void testApp::draw(){ cam.begin(); void main() { my_shader.begin(); gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0); my_vbo.draw(GL_QUADS,0,4); } my_shader.end(); cam.end(); }
  • 48. ofShader Using uniforms Uniforms allow you to change values of variables in a shader. You can do this by defining a uniform variable and using one of the setUniform**() functions. The next example uses an uniform to change the value of gl_FragData
  • 49. ofShader Using uniforms - vertex shader uniform int u_color; void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; } Using uniforms - fragment shader uniform int u_color; void main() { vec4 diffuse_color = vec4(1.0, 0.0, 0.0, 1.0); if(u_color == 1) { diffuse_color.rgb = vec3(1.0, 0.0, 0.0); } else if(u_color == 2) { diffuse_color.rgb = vec3(0.0, 1.0, 0.0); } else if(u_color == 3) { diffuse_color.rgb = vec3(0.0, 0.0, 1.0); } gl_FragColor = diffuse_color; }
  • 50. ofShader Using uniforms When you want to set an uniform value make sure that you enable the shader first! You can do this by calling the begin() function. void testApp::draw(){ cam.begin(); my_shader.begin(); my_shader.setUniform1i("u_color", use_color); my_vbo.draw(GL_QUADS,0,4); my_shader.end(); cam.end(); }
  • 51. ofShader Using uniforms Press “1” Press “2” Press “3”
  • 52. ofShader Simple light Here follows an example of a simple light shader. For more information on shaders you can visit this page which has lots of interesting information: http://www.arcsynthesis.org/gltut/ of.vert of.frag varying vec3 v_light_dir; varying vec3 v_normal; varying vec3 v_light_dir; uniform float time; varying vec3 v_normal; uniform float time; void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; void main() { // get view space position. vec4 diffuse_color = vec4(1.0, 0.0, 0.0, 1.0); vec4 position = gl_ModelViewMatrix * gl_Vertex; // IMPORTANT: you need to normalize the normal and light dir! // we create our light position in "view space". float ndotl = max(dot(normalize(v_normal), normalize(v_light_dir)),0.0); vec3 light_position = vec3(1.0, sin(time)*130.0,1.0); vec4 result_color = diffuse_color * ndotl; v_light_dir = (position.xyz - light_position); gl_FragColor = result_color; } // get normal in eye coordinates. v_normal = (gl_NormalMatrix * gl_Normal); }
  • 53. ofShader General void load(string shaderName) void load(string vertName, string fragName, string geomName = “”) void begin() void end() Uniforms: integers void setUniformTexture(const char* name, ofBaseHasTexture& img, int textureLocation) void setUniformTexture(const char* name, ofTexture& img, int textureLocation) void setUniform1i(const char* name, int v1) void setUniform2i(const char* name, int v1, int v2) void setUniform3i(const char* name, int v1, int v2, int v3) void setUniform4i(const char* name, int v1, int v2, int v3, int v4)
  • 54. ofShader Uniforms: floats void setUniform1f(const char* name, float v1) void setUniform2f(const char* name, float v1, float v2) void setUniform3f(const char* name, float v1, float v2, float v3) void setUniform4f(const char* name, float v1, float v2, float v3, float v4) Uniforms: floats arrays void setUniform1f(const char* name, float* v, int count = 1) void setUniform2f(const char* name, float* v, int count = 1) void setUniform3f(const char* name, float* v, int count = 1) void setUniform4f(const char* name, float* v, int count = 1) For more information see ofShader.h

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n