Drawing Our First Triangle(绘制第一个三角形)
admin
2023-02-11 09:20:03
0

周一到周五,每天一篇,北京时间早上7点准时更新~,中英文对照,一边学编程一边弹吉他,做一个奇葩码农!

请不要怀疑翻译是否有问题,我们的翻译工程师是蓝翔毕业的呢!

Drawing a single point is not really that impressive (even if it is really big!)(画个点还不是那么杀改(过瘾的意思,湘西土家族方言))—we already mentioned that OpenGL supports many different primitive types(俺们也说过了,OpenGL支持很多类型的图元), and that the most important are points, lines, and triangles(其中最重要滴就是点、线和三角形). In our toy example, we draw a single point by passing the token GL_POINTS to the glDrawArrays() function(在俺们的例子中,我们给glDrawArrays传了个GL_POINTS让OpenGL给画个点). What we really want to do is draw lines or triangles(要是俺想画线或者是三角形咋整). As you may have guessed, we could have passed GL_LINES or GL_TRIANGLES to glDrawArrays()(此时给glDrawArrays穿线和三角形不就完事了) but there’s one hitch:The vertex shader in Listing 2.3 places every vertex in the same place, right in the middle of clip space(但是你瞅瞅我们之前的shader,那代码里面把数据都写死了,怎么玩啊). For points, that’s fine: OpenGL assigns area to points for you(对于画点来说,还没啥,也就能画一个). But for lines and triangles, having two or more vertices in the exact same place produces a degenerate primitive(但如果是画别的图元,你把所有数据都写死在那一个位置,不还是个点吗,想象一下,你把三角形和线都挤在一个点的位置,不还是个点?), which is a line with zero length or a triangle with zero area. If we try to draw anything but points with this shader, we won’t get any output at all because all of the primitives will be degenerate(如果我们使用这个shader去画其他图元的话,我们什么也画不出来). To fix this, we need to modify our vertex shader to assign a different position to each vertex(为了修复这个问题,我们需要修改我们的vertex shader,为每个点指定不一样的坐标)

Fortunately, GLSL includes a special input to the vertex shader called gl_VertexID, which is the index of the vertex that is being processed at the time(幸运滴是有一个内置变量叫gl_VertexID,标记了当下被执行的这个点的索引index). The gl_VertexID input starts counting from the value given by the first parameter of glDrawArrays() and counts upward one vertex at a time for count vertices (the third parameter of glDrawArrays())(gl_VertexID会从你调用glDrawArrays时传入的起始位置的索引开始,一直迭代到那个位置的索引加上count为止). This input is one of the many built-in variables provided by GLSL, which represent data that is generated by OpenGL or that you should generate in your shader and give to OpenGL(这是GLSL的内置变量,它是由OpenGL直接生成的,你在代码里面直接用就是了,这些数据可以是OpenGL生成的也可以是你自己生成并提供给OpenGL的). (gl_Position, which we just covered,is another example of a built-in variable.(gl_Position是另外一个内置变量,我们已经在前面讲过了)) We can use this index to assign a different position to each vertex (我们可以用这个内置变量,去给每个顶点赋值不同的位置)(see Listing 2.8, which does exactly this)

新手看到这里肯定是一头雾水,什么鬼。这不是你的智商不行,是书本讲的不好,他简单的文字叙述,且没有讲原理。听过我们东汉书院的OpenGL进阶课程的人,肯定是轻松愉快的学过这一段的。

#version 450 core
void main(void)
{
// Declare a hard-coded array of positions
const vec4 vertices[3] = vec4[3](vec4(0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4(0.25, 0.25, 0.5, 1.0));
// Index into our array using gl_VertexID
gl_Position = vertices[gl_VertexID];
}
Listing 2.8: Producing multiple vertices in a vertex shader

By using the shader of Listing 2.8, we can assign a different position to each of the vertices based on its value of gl_VertexID(我们可以根据gl_VertexID给每个顶点赋值一个位置). The points in the array vertices form a triangle(那些点是以三角形的方式进行组织的), and if we modify our rendering function to pass GL_TRIANGLES to glDrawArrays() instead of GL_POINTS(并且我们修改glDrawArrays的参数,从GL_POINTS变成GL_TRIANGLES,如Listing2.9所示), as shown in Listing 2.9, then we obtain the image shown in Figure 2.4(这样一来,我们应该会得到如图2.4的运行结果).

// Our rendering function
void render(double currentTime)
{
const GLfloat color[] = { 0.0f, 0.2f, 0.0f, 1.0f };glClearBufferfv(GL_COLOR, 0, color);
// Use the program object we created earlier for rendering
glUseProgram(rendering_program);
// Draw one triangle
glDrawArrays(GL_TRIANGLES, 0, 3);
}
Listing 2.9: Rendering a single triangle
Drawing Our First Triangle(绘制第一个三角形)
本日的翻译就到这里,明天见,拜拜~~

第一时间获取最新桥段,请关注东汉书院以及图形之心公众号

东汉书院,等你来玩哦

相关内容

热门资讯

今日重大发现“老友十三水.究竟... 家人们!今天小编来为大家解答老友十三水透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买...
重磅!全球首款,2nm手机芯片... 每经编辑|程鹏 向江林 手机芯片,进入2nm时代! 据财联社12月21日报道,三星在全球移动半导体...
终于懂了“微乐四川麻将.开挂神... 网上科普关于“微乐四川麻将有没有挂”话题很是火热,小编也是针对微乐四川麻将作*弊开挂的方法以及开挂对...
终于明白“新毛豆炸金花.是不是... 终于明白“新毛豆炸金花.是不是有挂?”必胜开挂神器您好,新毛豆炸金花这个游戏其实有挂的,确实是有挂的...
重磅消息“白金岛游戏.开挂神器... 重磅消息“白金岛游戏.开挂神器?”确实真的有挂您好,白金岛游戏这个游戏其实有挂的,确实是有挂的,需要...
【第一资讯】“新畅游互娱.到底... 有 亲,根据资深记者爆料新畅游互娱是可以开挂的,确实有挂(咨询软件无需打...
最新引进“开心游戏.到底有挂吗... 有 亲,根据资深记者爆料开心游戏是可以开挂的,确实有挂(咨询软件无需打开...
终于懂了“乐禧棋牌.开挂神器?... 您好:乐禧棋牌这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款游戏...
今日重大消息“兴动竞赛.有挂吗... 您好:兴动竞赛这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款游戏...
今日重大发现“十胡卡.是不是有... 家人们!今天小编来为大家解答十胡卡透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买很多...