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);
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
Indicesproperty 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
Modelclass (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 aTexturemember that you can use to handle texturing.