【Cherno的OpenGL视频】Batch rendering - Colors

1、

So let’s continue this batch rendering party, last time we took a look at what batch rendering actually was, we did a bit of theory and put together a basic example of just rendering 2 squares, today we’re going to continue on with filling in the blanks of what we could do with traditional rendering which we can’t do with batch rendering, and bring all of those features back into it, so last time we did just draw 2 white squares and that was fine obviously, but eventually you’re probably gonna want to do other things, for example draw quads of different colors, or draw textured quads, or dynamically change the quads that are being drawn, all those thing and that’s exactly what this miniseries is about, we’re kind of going through everything step by step breaking it down doing it in a really simple way and then filling in everything that we would want to actually achieve with batch rendering.

Today is going to be all about color, how do we draw 2 quads of different colors or in fact any number of quads with different colors? Traditionally when we want to draw a colored quad, the simplest way is probably quite subjective, something that might be simple to me might not be simple to you, you might think something else is simple, but when I think of “let’s just render a flat colored quad in the most basic way possible” is simply filling in a vertex buffer with the vertex positions, and then rendering that quad, and then in the fragment shader passing in some kind of uniform variable which contains the color, of course we could just hardcode the fragment shader to be a particular color, but that woundn’t really be useful if we have 2 different quads we’d have to write 2 different shaders or something like that, insteat of that what we would do is just render our quad twice using 2 different draw calls, but then have a uniform like vec4 inside our fragment shader which we would set the output color to be, and then that would actually be kind of setup before the draw call happens, so I can just use like glUniform3f() or glUniform4f() and actually set those variables before drawing my quad, before actually calling glDrawElements(). That’s kind of to me what would be the simplest way of rendering a quad, however we can’t do that anymore, the reason we can’t do that anymore is because we just have 1 draw call, we can’t call glUniform4f() then glDrawElements() and then call glUniform4f() again and then glDrawElements() again, because we only have 1 glDrawElements() call, so we can’t set the uniform to be 2 different values between draws, what do we do?

Now there are a number of solutions to this, you could have like an array of color values and you could kind of pass in the index…there’s a lot of different things you could do, some of them better than others, but the way that we’re gonna solve it in this video is what I think is probably the best way to solve it is by using vertex colors, now you may have heard of vertex colors before, so let’s just talk a little bit about what that actually means.

Currently our vertex simply contains a position, so every vertex just has 3 floats X Y Z, and that defines the position of that vertex, but positions are not the only things that vertices are capable of containing, and in fact vertex and vertex position is often used interchangeably which isn’t exactly correct, for example someone might refer to the vertices of an object and really be talking about the vertex positions of an object, however the word vertex and what it actually means in graphics programming is simply a point which can contain as much data as you really want it to do, you could store absolutely anything inside of vertex and then access that data inside your vertex shader, so that’s what we’re going to do today, every single vertex that we actually have inside our vertex buffer is going to contain a color, and then we’re going to access that color within the vertex buffer inside our vertex shader then pass it to the fragment shader and then set that as our output color, so since each vertex now contains a color, obviously every quad has the ability to be any color we set it to, in fact it’s even finer grain than that, we can actually control the color of every vertex thus creating a gradient or some

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值