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(绘制第一个三角形)
本日的翻译就到这里,明天见,拜拜~~

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

东汉书院,等你来玩哦

相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...