OpenGL 被移植到 XNA

发布于 2024-09-24 09:59:18 字数 652 浏览 4 评论 0原文

我开始将我的游戏从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬 2024-10-01 09:59:18

来自此处的 MSDN 文档,这是绑定纹理的方法(实际上是两个):

graphics.GraphicsDevice.Textures[0] = firstTexture;
graphics.GraphicsDevice.Textures[1] = secondTexture;

然后使用 Indices 属性和 SetVertexBuffer( ) 方法分别。然后使用 GraphicsDevice.Draw*() 方法之一绘制它们。

如果您只是渲染模型,那么您可能需要查看 Model 类(它将为您处理此类事情,包括通过内容管道导入模型)。

此外,如果您使用的是 BasicEffect,它还有一个 Texture 成员,您可以使用它来处理纹理。

From the MSDN documentation here, here is how you bind a texture (two, in fact):

graphics.GraphicsDevice.Textures[0] = firstTexture;
graphics.GraphicsDevice.Textures[1] = secondTexture;

And then you set your vertex and index buffers (as needed) to the device with the Indices property and the SetVertexBuffer() method respectively. Then you draw them with one of the GraphicsDevice.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 a Texture member that you can use to handle texturing.

夜巴黎 2024-10-01 09:59:18

通常使用 GraphicsDevice.Textures 属性将纹理绑定到设备。代码示例使用 XNA 4.0。代码的端口看起来像这样:

        GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
        GraphicsDevice.Textures[0] = obj.tx;
        GraphicsDevice.SetVertexBuffer(obj.iVBO);

        effectColorParameter.SetValue(c.ToVector4());
        effect.CurrentTechnique.Passes[0].Apply();

        GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, obj.triangleCount);
        GraphicsDevice.SetVertexBuffer(null);

或者,如果您想使用 BasicEffect,则可以将纹理直接绑定到效果。

        basicEffect.Texture = obj.tx;
        basicEffect.TextureEnabled = true;

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:

        GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
        GraphicsDevice.Textures[0] = obj.tx;
        GraphicsDevice.SetVertexBuffer(obj.iVBO);

        effectColorParameter.SetValue(c.ToVector4());
        effect.CurrentTechnique.Passes[0].Apply();

        GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, obj.triangleCount);
        GraphicsDevice.SetVertexBuffer(null);

Or if you want to use a BasicEffect you bind the texture directly to the effect.

        basicEffect.Texture = obj.tx;
        basicEffect.TextureEnabled = true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文