Lecture 11 - Interactive 3D Computer Graphics
Interactive 3D Computer Graphics includes:
- Geometric Transforms (project 2)
- Shading (project 3)
- We here did just a local approximation of lighting
In our shading step we have objects with shapes/meshes with individual setColor
methods.
Let's review the Graphics Pipeline
The Graphics Pipeline
When we call draw
from the CPU, the vertex shader, which we write, does all of the geometric transforms and then draws on the screen:
glPosition = P * V * M * vertPos;
// ...
Notice that a fragment is just a pixel plus depth (ie: from P1, we're using depth z-buffer tests to draw certain front pixels).
Advancing the Pipeline
All of this is pretty standard, so let's add to it. Recall in the fragment shader that we have some:
outcolor = Kd + dot(N, L) + RGB
outcolor += // diffuse light
// ...
But the problem here is that
But we may want to have different fragments (different triangles, if you will) with different materials. This is called texture mapping:
Texture Mapping allows us to use a reference image, which we copy to our GPU, to map a 2D coordinate giving a pixel color from our texture, and coloring with that color.
Here a
Note that by using percentages of the width, the textures themselves don't even need to be square. Rather, the texture will get stretched and squished (or likely have overlap if values greater than 1 is used) and then mapped onto the surface in question:
v ...
vt 0.1 0.2 // v1's texture color
vt 0.2 0.4 // v2's texture color
...
f 0 17 27
face 0/1 17/16 27/117 // face has vertex 0 with v1's texture coordinate color
// for faces the full composition is v/vt/n
Benefits
Texture mapping can help make simple geometry have a lot more character, even though it's pretty simple geometry under the hood.
One are of study is texture synthesis, where you generate a larger texture, where needed, from a smaller repetitive texture. This is good as it uses less GPU VRAM.
Another thing is things like for mirrors, where you render a snapshot texture of one perspective, from where you are, and then map the texture to some rectangle which is your mirror surface.
This is where you turn a triangle from 3D space on the surface, and generate a 2D triangle on the texture itself. So for pixel writing between vertices, we can interpolate on the texture pixel for each of the corners and get a new color, not an interpolated color from the corners of the triangle.
More on Textures
There's a lot of texturing techniques. An overview of all of them can be seen in:
![[tex2.pdf]]
Some important notes:
- Texture aliasing is where you prevent too much stretching of our texture. OpenGL allows for
glTexParametri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
orglTexParametri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
to either clamp or repeat a texture over the texture coordinates to not have a stretched out ground texture.