views:

77

answers:

2

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);
+2  A: 

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.

Andrew Russell
A: 

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;
Empyrean