官术网_书友最值得收藏!

Vertex and UV data

Before we look at a large chunk of scary WebGL JavaScript code, I want to briefly discuss data buffers and how we are going to pass the geometry and texture coordinate data into the shaders. We will be passing in 32-bit floating point data in a large buffer that will contain a combination of the X and Y coordinates for the vertex and UV texture coordinates for that same vertex. UV mapping is the method by which your GPU maps 2D texture coordinates onto 3D geometry:

WebGL and OpenGL accomplish this by assigning a U and V coordinate to every vertex. A UV coordinate of (0,0) assigned to a vertex means that the vertex will be colored based on the color in the texture in the top left corner. A UV coordinate of (1,1) would imply that it would be painted based on what color is in the texture on the bottom right. As we interpolate between the points in our 3D object, we also interpolate between the different UV coordinates inside of the texture. Those UV coordinates can be sampled in our fragment shader using the texture2D built-in function by passing in the texture and the current UV coordinates.

Let's take a look at the vertex and texture data array that we are using inside of this WebGL app:

var vertex_texture_data = new Float32Array([
// X, Y, U, V
0.16, 0.213, 1.0, 1.0,
-0.16, 0.213, 0.0, 1.0,
0.16, -0.213, 1.0, 0.0,
-0.16, -0.213, 0.0, 0.0,
-0.16, 0.213, 0.0, 1.0,
0.16, -0.213, 1.0, 0.0
]);

This data has been typed out in rows and columns. Even though this is a linear array of data, the formatting allows you to see that we have four floating-point values that will be passed in for each vertex. There is a comment above the data showing what each column represents. The first two data values are the X and Y coordinates of the geometry. The second two values are the U and V coordinates that map the texture to the X and Y coordinates in the geometry. There are six rows here, even though we are rendering a rectangle. The reason we need six points instead of just four is that the geometry used by WebGL typically consists of triangles. Because of this, we will need to repeat two of the vertices.

You may be wondering, why triangles? Well, there was a time when computer graphics used geometry that was not decomposed into triangles. But a problem arises when you have a quad, and not all the points are coplanar (in the same plane). This is the same problem I have whenever I go to a bar that uses four-legged stools. I am pretty sure the existence of the four-legged stool is some sort of Illuminati plot to keep me off balance, but I digress. Because three points define a plane, a triangle is, by definition, always coplanar, just like a three-legged stool will never wobble.
主站蜘蛛池模板: 贵定县| 日照市| 杂多县| 凉城县| 桃园县| 盐亭县| 辰溪县| 龙州县| 青田县| 霍城县| 自贡市| 定远县| 三原县| 大宁县| 樟树市| 湾仔区| 余姚市| 雅安市| 德钦县| 民丰县| 呼伦贝尔市| 华宁县| 阿合奇县| 军事| 临泉县| 鹰潭市| 衢州市| 台安县| 大连市| 尼勒克县| 且末县| 盐亭县| 泌阳县| 峡江县| 巴林左旗| 屏边| 黔西县| 陆河县| 泗阳县| 伊吾县| 徐闻县|