Drawing Our First Triangle(绘制第一个三角形)

发布时间:2020-10-23 18:51:32 作者:萌谷王
来源:网络 阅读:397

周一到周五,每天一篇,北京时间早上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(绘制第一个三角形)
本日的翻译就到这里,明天见,拜拜~~

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

东汉书院,等你来玩哦

推荐阅读:
  1. Android openGl 绘制简单图形的实现示例
  2. 使用OpenGL怎么实现一个Bezier曲线

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

opengl 游戏引擎 计算机图形学

上一篇:云服务器ecs快照有什么用

下一篇:python的下载方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》