多次渲染不同的物体

Nehe教程17

字体是正交投影,物体是透视投影,绘制字体时关闭深度,入栈矩阵即可:



glBindTexture(GL_TEXTURE_2D, texture[0]);               
// Select Our Font Texture

Now we disable depth testing. The reason I do this is so that blending works nicely. If you don't disable depth testing, the text may end up going behind something, or blending may not look right. If you have no plan to blend the text onto the screen (so that black spaces do not show up around our letters) you can leave depth testing on.

?
1
glDisable(GL_DEPTH_TEST);                        // Disables Depth Testing

The next few lines are VERY important! We select our Projection Matrix. Right after that, we use a command called glPushMatrix(). glPushMatrix stores the current matrix (projection). Kind of like the memory button on a calculator.

?
1
2
glMatrixMode(GL_PROJECTION);                         // Select The Projection Matrix
glPushMatrix();                              // Store The Projection Matrix

Now that our projection matrix has been stored, we reset the matrix and set up our Ortho screen. The first and third numbers (0) represent the bottom left of the screen. We could make the left side of the screen equal -640 if we want, but why would we work with negatives if we don't need to. The second and fourth numbers represent the top right of the screen. It's wise to set these values to match the resolution you are currently in. There is no depth so we set the z values to -1 & 1.

?
1
2
glLoadIdentity();                            // Reset The Projection Matrix
glOrtho(0,640,0,480,-1,1);                       // Set Up An Ortho Screen

Now we select our modelview matrix, and store it's current settings using glPushMatrix(). We then reset the modelview matrix so we can work with it using our Ortho view.

?
1
2
3
glMatrixMode(GL_MODELVIEW);                      // Select The Modelview Matrix
glPushMatrix();                              // Store The Modelview Matrix
glLoadIdentity();                            // Reset The Modelview Matrix

With our perspective settings saved, and our Ortho screen set up, we can now draw our text. We start by translating to the position on the screen that we want to draw our text at. We use glTranslated() instead of glTranslatef() because we are working with actual pixels, so floating point values are not important. After all, you can't have half a pixel :)

?
1
glTranslated(x,y,0);                             // Position The Text (0,0 - Bottom Left)

The line below will select which font set we want to use. If we want to use the second font set we add 128 to the current base display list (128 is half of our 256 characters). By adding 128 we skip over the first 128 characters.

?
1
glListBase(base-32+(128*set));                       // Choose The Font Set (0 or 1)

Now all that's left for us to do is draw the letters to the screen. We do this exactly the same as we did in all the other font tutorials. We use glCallLists(). strlen(string) is the length of our string (how many characters we want to draw), GL_UNSIGNED_BYTE means that each character is represented by an unsigned byte (a byte is any value from 0 to 255). Finally, string holds the actual text we want to print to the screen.

?
1
glCallLists( strlen (string),GL_UNSIGNED_BYTE,string);             // Write The Text To The Screen

All we have to do now is restore our perspective view. We select the projection matrix and use glPopMatrix() to recall the settings we previously stored with glPushMatrix(). It's important to restore things in the opposite order you stored them in.

?
1
2
glMatrixMode(GL_PROJECTION);                         // Select The Projection Matrix
glPopMatrix();                               // Restore The Old Projection Matrix

Now we select the modelview matrix, and do the same thing. We use glPopMatrix() to restore our modelview matrix to what it was before we set up our Ortho display.

?
1
2
glMatrixMode(GL_MODELVIEW);                      // Select The Modelview Matrix
glPopMatrix();                               // Restore The Old Projection Matrix

Finally, we enable depth testing. If you didn't disable depth testing in the code above, you don't need this line.

?
1
2
     glEnable(GL_DEPTH_TEST);    

在Unity中,使用`Update`和`LateUpdate`函数可以实现在每一帧连续渲染不同数据。`Update`函数在每一帧都会被调用一次,它是处理输入和更新逻辑的好地方。而`LateUpdate`函数则在所有`Update`函数调用完毕后执行,这使得它适合在所有物理更新完成后执行某些操作,比如相机跟随。如果你想连续渲染不同数据,可以根据数据更新的时机在这些函数中做相应处理。 例如,如果你想渲染移动的物体,可以在`Update`函数中更新物体的位置,并调用渲染相关的函数。对于每个物体,每次调用渲染函数时传入不同的数据,就可以实现连续渲染不同的数据。 下面是一个使用`Update`函数连续渲染不同数据的基本示例代码: ```csharp void Update() { // 假设有一个数据列表,用于存储需要渲染的数据 List<Data> dataList = GetDataList(); // 遍历数据列表,对每个数据进行处理和渲染 for (int i = 0; i < dataList.Count; i++) { // 更新渲染数据 RenderData(dataList[i]); // 执行实际的渲染操作,这里假设Render函数用于渲染 Render(dataList[i].position, dataList[i].rotation); } } // 更新数据的函数(伪代码) void RenderData(Data data) { // 更新数据的逻辑... } // 渲染的函数(伪代码) void Render(Vector3 position, Quaternion rotation) { // 使用更新后的数据渲染物体 // 这里可能涉及到调用Unity的渲染接口或者自定义的渲染逻辑 } ``` 需要注意的是,在Unity中渲染通常是指游戏物体在场景中的绘制,而数据更新不一定直接导致渲染结果的变化。如果要渲染物体与数据有直接关联,那么在数据更新后进行渲染是合适的。此外,过度渲染或者在每一帧都进行复杂的渲染操作可能会导致性能问题,因此需要根据实际情况进行性能优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值