OpenGL 被移植到 XNA
我开始将我的游戏从 C/OpenGL 代码库移植到 XNA。我现在刚刚开始了解渲染代码,我想知道从只需通过一次调用绑定纹理,然后将顶点缓冲区对象输出到 XNA 等效方法集的系统进行转换的最佳方法是什么?我可以看到如何传递顶点数据,但我不确定如何绑定纹理。这一切都必须在着色器中完成吗?或者 XNA 中有一个简单的程序吗?
我的模型主要渲染代码如下(我对小变量名称表示歉意)
glEnable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE);
glBindTexture(GL_TEXTURE_2D, obj->tx);
glColor4f(c.r, c.g, c.b, c.a);
glBindBuffer(GL_ARRAY_BUFFER, obj->iVBO);
glVertexPointer(3, GL_FLOAT, (sizeof(float) * 3) + (sizeof(float) * 2), 0);
glTexCoordPointer(2, GL_FLOAT, (sizeof(float) * 3) + (sizeof(float) * 2), (const GLvoid*)(4 * 3));
glDrawArrays(GL_TRIANGLES, 0, obj->iSize);
glBindBuffer(GL_ARRAY_BUFFER, 0);
I'm beginning to port my game over to XNA from a C/OpenGL codebase. I'm just now getting to the rendering code, and I'm wondering what the best methods would be for transitioning from a system where you simply bind a texture with one call, then output vertex buffers objects to an XNA equivalent set of methods? I can see how you pass vertex data, but I'm not exactly sure how you bind a texture. Must it all be done in shaders, or is there a simple procedure for this in XNA?
My main rendering code for models is as follows (I apologize for small variable names)
glEnable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE);
glBindTexture(GL_TEXTURE_2D, obj->tx);
glColor4f(c.r, c.g, c.b, c.a);
glBindBuffer(GL_ARRAY_BUFFER, obj->iVBO);
glVertexPointer(3, GL_FLOAT, (sizeof(float) * 3) + (sizeof(float) * 2), 0);
glTexCoordPointer(2, GL_FLOAT, (sizeof(float) * 3) + (sizeof(float) * 2), (const GLvoid*)(4 * 3));
glDrawArrays(GL_TRIANGLES, 0, obj->iSize);
glBindBuffer(GL_ARRAY_BUFFER, 0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自此处的 MSDN 文档,这是绑定纹理的方法(实际上是两个):
然后使用
Indices
属性和SetVertexBuffer( )
方法分别。然后使用GraphicsDevice.Draw*()
方法之一绘制它们。如果您只是渲染模型,那么您可能需要查看
Model
类(它将为您处理此类事情,包括通过内容管道导入模型)。此外,如果您使用的是
BasicEffect
,它还有一个Texture
成员,您可以使用它来处理纹理。From the MSDN documentation here, here is how you bind a texture (two, in fact):
And then you set your vertex and index buffers (as needed) to the device with the
Indices
property and theSetVertexBuffer()
method respectively. Then you draw them with one of theGraphicsDevice.Draw*()
methods.If you're simply rendering models, then you may want to look into the
Model
class (which will handle this kind of thing for you, including importing models through the content pipeline).Also, if you're using
BasicEffect
, it also has aTexture
member that you can use to handle texturing.通常使用 GraphicsDevice.Textures 属性将纹理绑定到设备。代码示例使用 XNA 4.0。代码的端口看起来像这样:
或者,如果您想使用 BasicEffect,则可以将纹理直接绑定到效果。
You normally bind the textures to the device using the GraphicsDevice.Textures property. Code sample is using XNA 4.0. The port of your code would look something like:
Or if you want to use a BasicEffect you bind the texture directly to the effect.