Storing Data on the GPU
- I am looking at HW5
- Start with Program.js
- This is the "main" for this example.
- Look at the points defined by CP1... CP8
- These are then turned into a series of lines when
defining cube.
- But notice that this cube is just a series of lines...
- Draw this by hand.
- This data is stored on the CPU in CUBE
- We then construct a "widget"
- Widget is a generic term for a thing.
- You could probably call this something better but ...
- It is responsible for maintaining a connection to data over on the GPU.
- It will initially store the data in the GPU
- It will make the data active in the GPU when it needs to.
- It will draw the object in the GPU when requested.
- The call to SetupVBO in the constructor.
- We first create a buffer.
- gl.createBuffer()
- This is equivalent to declaring a pointer on the GPU.
- It returns an index to a buffer.
- In GLES this is called GenBuffers
- Next we need to bind to the buffer.
- gl.BindBuffer(GLenum target, GLuint buffer);
- The target describes what we plan on storing in the buffer.
- gl.ARRAY_BUFFER are vertex attributes.
- gl.ELEMENT_ARRAY_BUFFER are index
- There are many others.
- The target is the buffer we just generated.
- If this is the first time we "bind" to the buffer, it allocates the right type of space based on the target.
- This makes the buffer "active"
- Ie it is "the" buffer.
- All buffer actions occur to this buffer until another is bound.
- We finally need to move the data over
- void gl.bufferData(target, ArrayBufferView srcData, usage);
- The array type.
- The actual data.
- How we plan to use this.
- This is not hard and fast.
- But it gives the GPU a clue how to organize the data for best use.
- If the use is STATIC_DRAW
- We don't plan to change this data.
- DYNAMIC_DRAW - we plan to change the data frequently.
- There are many others here as well.
- Later we will need to connect the VBO to a variable in a program
- Display
- To display an object we need to make it active by making a call to gl.bindBuffer with it's buffer.
- We need to map this data to the position in the shader.
- void gl.vertexAttribPointer(index, size, type, normalized, stride, offset);
- This takes the index of the variable in the shader program.
- The number of components at each point (ie x,y,z = 3).
- The type of the points we want to draw.
- If we want the data to be normalized.
- This is ignored for float.
- The stride
- The starting point.
- We can store data in several ways.
- In this case we stored data as x0,y0,z0,x1,y1,z1...xn,yn.zn
- So the stride is 0
- And the offset is 0.
- We could have stored the data as x0,y0,z0,r0,g0,b0,x1,y1,z1,r1 ...
- The stride would have been 6*sizeof(float).
- We would have specified another attribute for color
- And the offset would have been 3*sizeof(float)
- If we only wanted to draw the back face, we would have skipped the first few items.
- The offset would have been n*sizeof(float).
- See HW5B
- See HW5C