Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C# XNA] Rendering a Textured Sphere

Started by Zmurf Jul 1, 2007 at 9:04 PM 1 replies 9.3k views
Original Post
Zmurf
Zmurf
Hey, I want to generate a sphere and texture it with a texture of Saturn for the back drop in my game. How can I render the sphere pragmatically and texture it? I can't seem to find a tutorial on this, every time I search I only find sky boxes and nothing XNA specific.
If it's possible for us to conceive it, than it must be possible.
ViLiO
ViLiO
Easiest way would be to use a modelling application and save the inside-out sphere mesh to .X or .FBX and just use Model to load and render it.

Another option would be to generate the vertices yourself using something like this:
VertexPositionTexture[] vertices = new VertexPositionTexture[numThetaSides * 2 * (numPhiSides + 1)];int index = 0;for (int i = 0; i < numThetaSides; ++i){    float theta1 = ((float)i / (float)numThetaSides) * MathHelper.ToRadians(360f);    float theta2 = ((float)(i + 1) / (float)numThetaSides) * MathHelper.ToRadians(360f);    for (int j = 0; j <= numPhiSides; ++j)    {        float phi = ((float)j / (float)numPhiSides) * MathHelper.ToRadians(180f);        float x1 = (float)(Math.Sin(phi) * Math.Cos(theta1)) * this.horizontalRadius;        float z1 = (float)(Math.Sin(phi) * Math.Sin(theta1)) * this.horizontalRadius;        float x2 = (float)(Math.Sin(phi) * Math.Cos(theta2)) * this.horizontalRadius;        float z2 = (float)(Math.Sin(phi) * Math.Sin(theta2)) * this.horizontalRadius;        float y = (float)Math.Cos(phi) * this.verticalRadius;        Vector3 position = this.centre + new Vector3(x1, y, z1);        vertices[index] = new VertexPositionTexture(position,            new Vector2((float)i / (float)numThetaSides, (float)j / (float)numPhiSides));        index++;        position = this.centre + new Vector3(x2, y, z2);        vertices[index] = new VertexPositionNormalTexture(position,            new Vector2((float)(i + 1) / (float)numThetaSides, (float)j / (float)numPhiSides));        index++;    }}this.vertexBuffer = new VertexBuffer(device, typeof(VertexPositionTexture), vertices.Length,    ResourceUsage.WriteOnly, ResourceManagementMode.Automatic);this.vertexBuffer.SetData<VertexPositionTexture>(vertices, 0, vertices.Length, SetDataOptions.None);


All the best,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
Zmurf
Zmurf
Thanks, I got the sphere up!
If it's possible for us to conceive it, than it must be possible.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.