Lighting a 2D mesh in a 3d environnement - c#

I'm making a 2.5D game with Monogame. I have 2D meshes in a 3D space, and I want them to shine. But if I activate default lighting, they're pitch black.
Here the render code:
BasicEffect effect = new BasicEffect(graphicsDevice);
VertexPositionTexture[] vertices =
{
new VertexPositionTexture(new Vector3(-.5f + Position.X, 0.5f + Position.Y, 0.0f), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3(-.5f + Position.X, -.5f + Position.Y, 0.0f), new Vector2(0, 1)),
new VertexPositionTexture(new Vector3(0.5f + Position.X, 0.5f + Position.Y, 0.0f), new Vector2(1, 0)),
new VertexPositionTexture(new Vector3(0.5f + Position.X, -.5f + Position.Y, 0.0f), new Vector2(1, 1)),
};
graphicsDevice.BlendState = BlendState.AlphaBlend;
effect.EnableDefaultLighting();
effect.LightingEnabled = true;
//effect.AmbientLightColor = new Vector3(.75f, .75f, .75f);
effect.DirectionalLight0.DiffuseColor = new Vector3(.75f, .75f, .75f);
effect.DirectionalLight0.Direction = new Vector3(0, 0, -1);
effect.DirectionalLight0.SpecularColor = new Vector3(.75f, .60f, .60f);
effect.TextureEnabled = true;
effect.Texture = Subtexture;
effect.Projection = camera.Fov;
effect.View = camera.ViewMatrix;
effect.World = camera.WorldMatrix;
VertexBuffer buffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionTexture), vertices.Length, BufferUsage.WriteOnly);
buffer.SetData(vertices);
graphicsDevice.SetVertexBuffer(buffer);
graphicsDevice.RasterizerState = RasterizerState.CullNone;
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, vertices, 0, 2);
}

Related

Assigning different materials to different meshes on the same object

I initially had a function that created a rectangular mesh given a specific height/width:
void BoxMesh(float width, float height)
{
MeshFilter mf = GetComponent<MeshFilter>();
Mesh mesh = new Mesh();
mf.mesh = mesh;
//Verticies
Vector3[] verticies = new Vector3[4]
{
new Vector3(0,0,0), new Vector3(0, height, 0), new Vector3(width, height, 0), new Vector3(width, 0, 0)
};
//Triangles
int[] tri = new int[6];
tri[0] = 0;
tri[1] = 1;
tri[2] = 3;
tri[3] = 1;
tri[4] = 2;
tri[5] = 3;
//normals
Vector3[] normals = new Vector3[4];
normals[0] = -Vector3.forward;
normals[1] = -Vector3.forward;
normals[2] = -Vector3.forward;
normals[3] = -Vector3.forward;
//UVs
Vector2[] uv = new Vector2[4];
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(0, 1);
uv[2] = new Vector2(1, 1);
uv[3] = new Vector2(1, 0);
//initialise
mesh.vertices = verticies;
mesh.triangles = tri;
mesh.normals = normals;
mesh.uv = uv;
//setting up collider
polyCollider.pathCount = 1;
Vector2[] path = new Vector2[4]
{
new Vector2(0,0), new Vector2(0, height), new Vector2(width, height), new Vector2(width, 0)
};
polyCollider.SetPath(0, path);
}
I then decided I wanted this mesh to have an outline so I edited the function so it is now:
void BoxMesh(float width, float height, float OLwidth)
{
MeshFilter mf = GetComponent<MeshFilter>();
Mesh mesh = new Mesh();
mf.mesh = mesh;
MeshFilter mfOL = GetComponent<MeshFilter>();
Mesh meshOL = new Mesh();
mfOL.mesh = meshOL;
//Verticies
Vector3[] verticies = new Vector3[4]
{
new Vector3(0,0,0), new Vector3(0, height, 0), new Vector3(width, height, 0), new Vector3(width, 0, 0)
};
//Verticies Outline
Vector3[] verticiesOL = new Vector3[4]
{
new Vector3(-OLwidth,-OLwidth,0), new Vector3(-OLwidth, height + OLwidth, 0), new Vector3(width + OLwidth, height + OLwidth, 0), new Vector3(width + OLwidth, -OLwidth, 0)
};
//Triangles
int[] tri = new int[6];
tri[0] = 0;
tri[1] = 1;
tri[2] = 3;
tri[3] = 1;
tri[4] = 2;
tri[5] = 3;
//normals
Vector3[] normals = new Vector3[4];
normals[0] = -Vector3.forward;
normals[1] = -Vector3.forward;
normals[2] = -Vector3.forward;
normals[3] = -Vector3.forward;
//UVs
Vector2[] uv = new Vector2[4];
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(0, 1);
uv[2] = new Vector2(1, 1);
uv[3] = new Vector2(1, 0);
//initialise
mesh.vertices = verticies;
mesh.triangles = tri;
mesh.normals = normals;
mesh.uv = uv;
meshOL.vertices = verticiesOL;
meshOL.triangles = tri;
meshOL.normals = normals;
meshOL.uv = uv;
//setting up collider
polyCollider.pathCount = 1;
Vector2[] path = new Vector2[4]
{
new Vector3(-OLwidth,-OLwidth,0), new Vector3(-OLwidth, height + OLwidth, 0), new Vector3(width + OLwidth, height + OLwidth, 0), new Vector3(width + OLwidth, -OLwidth, 0)
};
polyCollider.SetPath(0, path);
}
So i now have two meshes one outside the other. However I want the one on the outside to have a different material: so it looks like an outline.
I have these two materials in my mesh renderer, so how would I go about assigning one material to the outer mesh, and another to the inner mesh.
The concept you might be missing here is "SubMesh", each submesh of a mesh will be rendered using a material from the list at a given index.
To create a submesh you can call
mesh.SetTriangles(int[] triangles, int submesh);
You can re-use your vertexes, by specifying different faces basing on the same list of points. If you specify more submeshes than materials, remaining submeshes will be rendered solid magenta.

Primitive made with Vertices deformed - C#

Hey im making a game out of primitive objects. To make these objects im using the Vertex Buffer. The problem im having is that when i am rendering a cube it looks perfect... But when i scale it bigger the cube deforms leaving holes and jagged ends. Ive tried numerous ways to make the cube and i cant figure it out. The idea of scaling the cube bigger is to make a Skybox.
Here is some images:
Deformed Cube:
http://i.imgur.com/5aGu7mF.png
Perfect small cube:
http://i.imgur.com/FmMBb4X.png
My code:
Path Data for the Skybox using vertices.
private void loadSkyboxData(Color color)
{
VertexPositionColorTexture[] verts = new VertexPositionColorTexture[25];
//Top Left
verts[0] = new VertexPositionColorTexture(new Vector3(-500, 500, -500), color, new Vector2(0, 0));
verts[1] = new VertexPositionColorTexture(new Vector3(500, 500, -500), color, new Vector2(0, 0));
verts[2] = new VertexPositionColorTexture(new Vector3(-500, -500, -500), color, new Vector2(0, 0));
verts[3] = new VertexPositionColorTexture(new Vector3(500, -500, -500), color, new Vector2(0, 0));
//Top Left
verts[4] = new VertexPositionColorTexture(new Vector3(500, 500, -500), color, new Vector2(0, 0));
verts[5] = new VertexPositionColorTexture(new Vector3(500, 500, 500), color, new Vector2(0, 0));
verts[6] = new VertexPositionColorTexture(new Vector3(500, -500, -500), color, new Vector2(0, 0));
verts[7] = new VertexPositionColorTexture(new Vector3(500, -500, 500), color, new Vector2(0, 0));
//Top Left
verts[8] = new VertexPositionColorTexture(new Vector3(-500, 500, 500), color, new Vector2(0, 0));
verts[9] = new VertexPositionColorTexture(new Vector3(500, 500, 500), color, new Vector2(0, 0));
verts[10] = new VertexPositionColorTexture(new Vector3(-500, 500, 500), color, new Vector2(0, 0));
verts[11] = new VertexPositionColorTexture(new Vector3(500, 500, -500), color, new Vector2(0, 0));
//Top Left
verts[12] = new VertexPositionColorTexture(new Vector3(500, 500, 500), color, new Vector2(0, 0));
verts[13] = new VertexPositionColorTexture(new Vector3(-500, 500, 500), color, new Vector2(0, 0));
verts[14] = new VertexPositionColorTexture(new Vector3(500, -500, 500), color, new Vector2(0, 0));
verts[15] = new VertexPositionColorTexture(new Vector3(-500, -500, 500), color, new Vector2(0, 0));
//Top Left
verts[16] = new VertexPositionColorTexture(new Vector3(-500, 500, 500), color, new Vector2(0, 0));
verts[17] = new VertexPositionColorTexture(new Vector3(-500, 500, -500), color, new Vector2(0, 0));
verts[18] = new VertexPositionColorTexture(new Vector3(-500, -500, 500), color, new Vector2(0, 0));
verts[19] = new VertexPositionColorTexture(new Vector3(500, -500, -500), color, new Vector2(0, 0));
//Top Left
verts[20] = new VertexPositionColorTexture(new Vector3(-500, -500 ,-500), color, new Vector2(0, 0));
verts[21] = new VertexPositionColorTexture(new Vector3(500, -500, -500), color, new Vector2(0, 0));
verts[23] = new VertexPositionColorTexture(new Vector3(-500, -500, 500), color, new Vector2(0, 0));
verts[24] = new VertexPositionColorTexture(new Vector3(500, -500, 500), color, new Vector2(0, 0));
vertexDictionary.Add("Skybox", new BufferedVertexTextureData(verts, PrimitiveType.TriangleStrip, 21));
BufferedVertexTextureData.cs
public class BufferedVertexTextureData : VertexTextureData
{
protected VertexBuffer vertsBuffer;
public BufferedVertexTextureData(VertexPositionColorTexture[] verts, PrimitiveType primitiveType, int primitiveCount)
: base(verts, primitiveType, primitiveCount)
{
this.vertsBuffer = new VertexBuffer(Object3D.Game.GraphicsDevice, typeof(VertexPositionColorTexture), verts.Length, BufferUsage.WriteOnly);
this.vertsBuffer.SetData(verts, 0, verts.Length);
}
public override void Draw(BasicEffect effect)
{
//apply all changes
effect.CurrentTechnique.Passes[0].Apply();
//instruct gfx card to use vertsbuffer
Game.GraphicsDevice.SetVertexBuffer(vertsBuffer);
//draw
Game.GraphicsDevice.DrawPrimitives(PrimitiveType, 0, PrimitiveCount);
}
}
Thanks for the help in advance. Its greaty appreciated.

WP7 Lagging when drawing lots of primitive cubes

I'm currently working on a 3D Arkanoid clone, and I'm drawing the cubes using primitives.
I have 6 rows, each with 49 blocks, so 294 blocks total.
Normally I'm not to bothered about optomisation, but the game is ridiculously slower on lower end phones. I know the issue is with the drawing of the blocks, because the game runs fine with that code removed.
My code for setting up the primitives is
VertexPositionNormalTexture[] vertices1 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices2 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices3 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices4 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices5 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices6 = new VertexPositionNormalTexture[4];
public void SetupVertices(){
vertices1[0].Position = new Vector3(position.X, position.Y, position.Z);
vertices1[0].TextureCoordinate = new Vector2(1, 1);
vertices1[1].Position = new Vector3(position.X, position.Y + size.Y, position.Z);
vertices1[1].TextureCoordinate = new Vector2(1, 0);
vertices1[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
vertices1[2].TextureCoordinate = new Vector2(0, 0);
vertices1[3].Position = new Vector3(position.X + size.X, position.Y, position.Z);
vertices1[3].TextureCoordinate = new Vector2(0, 1);
//Another side
vertices2[0].Position = new Vector3(position.X, position.Y, position.Z);
vertices2[0].TextureCoordinate = new Vector2(0, 1);
vertices2[1].Position = new Vector3(position.X, position.Y + size.Y, position.Z);
vertices2[1].TextureCoordinate = new Vector2(0, 0);
vertices2[2].Position = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
vertices2[2].TextureCoordinate = new Vector2(1, 0);
vertices2[3].Position = new Vector3(position.X, position.Y, position.Z + size.Z);
vertices2[3].TextureCoordinate = new Vector2(1, 1);
//ANOTHER SIDE
vertices3[0].Position = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
vertices3[0].TextureCoordinate = new Vector2(0, 1);
vertices3[1].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
vertices3[1].TextureCoordinate = new Vector2(0, 0);
vertices3[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
vertices3[2].TextureCoordinate = new Vector2(1, 0);
vertices3[3].Position = new Vector3(position.X + size.X, position.Y, position.Z);
vertices3[3].TextureCoordinate = new Vector2(1, 1);
vertices4[0].Position = new Vector3(position.X, position.Y, position.Z + size.Z);
vertices4[0].TextureCoordinate = new Vector2(0, 1);
vertices4[1].Position = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
vertices4[1].TextureCoordinate = new Vector2(0, 0);
vertices4[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
vertices4[2].TextureCoordinate = new Vector2(1, 0);
vertices4[3].Position = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
vertices4[3].TextureCoordinate = new Vector2(1, 1);
vertices5[0].Position = new Vector3(position.X, position.Y + size.Y, position.Z);
vertices5[0].TextureCoordinate = new Vector2(0, 0);
vertices5[1].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
vertices5[1].TextureCoordinate = new Vector2(1, 0);
vertices5[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
vertices5[2].TextureCoordinate = new Vector2(1, 1);
vertices5[3].Position = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
vertices5[3].TextureCoordinate = new Vector2(0, 1);
//bottom
vertices6[0].Position = new Vector3(position.X, position.Y, position.Z);
vertices6[0].TextureCoordinate = new Vector2(0, 0);
vertices6[1].Position = new Vector3(position.X + size.X, position.Y, position.Z);
vertices6[1].TextureCoordinate = new Vector2(1, 0);
vertices6[2].Position = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
vertices6[2].TextureCoordinate = new Vector2(1, 1);
vertices6[3].Position = new Vector3(position.X, position.Y, position.Z + size.Z);
vertices6[3].TextureCoordinate = new Vector2(0, 1);
numTriangles = 4;
indices = new short[numTriangles + 2];
int i = 0;
indices[i++] = 0;
indices[i++] = 1;
indices[i++] = 3;
indices[i++] = 2;
}
My code for drawing the blocks is basic, but I have no idea how to optomise it.
//Game1.CS Code
GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp; // need to do this on reach devices to allow non 2^n textures
#region GamePlaying
if (gameState == State.BREAKOUT || gameState == State.GAMEOVERBREAKOUT)
{
foreach (Block block in lastBlocks)
{
block.Draw(camera);
}
foreach (Block block in fourthBlocks)
{
block.Draw(camera);
}
foreach (Block block in thirdBlocks)
{
block.Draw(camera);
}
foreach (Block block in secondBlocks)
{
block.Draw(camera);
}
foreach (Block block in firstBlocks)
{
block.Draw(camera);
}
foreach (Block block in fifthBlocks)
{
block.Draw(camera);
}
}
Block.CS Draw Code
public void Draw(Camera camera)
{
effect.View = camera.View;
effect.Projection = camera.Projection;
effect.World = rotationMatrix;
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices1, 0, 4, indices, 0, numTriangles);
graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices2, 0, 4, indices, 0, numTriangles);
graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices3, 0, 4, indices, 0, numTriangles);
graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices4, 0, 4, indices, 0, numTriangles);
graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices5, 0, 4, indices, 0, numTriangles);
graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices6, 0, 4, indices, 0, numTriangles);
}
}
Calling DrawUserIndexedPrimitives per block is the problem. The idea behind this kind of draw call is to let you draw a large number of primitives with few calls, not make many calls that draw a little.
You have two options:
Use SpriteBatch to draw the quads (this internally performs batching for you so you don't have to handle it).
Instead of calling DrawUserIndexedPrimitives directly inside each Draw call, accumulate the vertices (and re-numbered indices) in a temporary buffer and submit this once (per Effect for a set of indices) when all objects have been accumulated.
Hope this helps.

How to prevent triangles in OpenGL from rendering on top of triangles in front of themselves

I've been working with OpenGL using the OpenTK library for .NET, writing my own engine. I placed 3 different objects, one spinning cube and 2 adjacent cubes. Everything seemed to work fine until I changed the color of the quad on top of the objects.
I'm rendering cubes with a green top, on the left the block on the back is being rendered over the block in the front. I can't seem to find out where I'm going wrong with this, when the camera is set to look from the other side it renders correctly.
The following is the related code in classes with irrelevant or unrelated methods, properties and attributes omitted:
GameState.cs
class GameState : State
{
// TEMP: Test Block
SimpleBlock block;
int i = 0;
public override void Render()
{
base.Render();
// Set OpenGL Settings
GL.Viewport(0, 0, 1024, 768);
GL.Enable(EnableCap.CullFace);
// Reset the Matrices
Matrices.ClearMatrices();
// Set Camera Settings (Field of view in radians)
Matrices.ProjectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 2, (1024.0f / 768.0f), 1, 1000);
// Create the Camera
// this has to be in reverse
Matrix4 viewMatrix = Matrix4.CreateRotationX((float)Math.PI/8);
viewMatrix = viewMatrix.Translate(0, -2, -4);
// Multiply it with the ModelView (Which at this point is set to a value that we can just use = and it has the same result)
Matrices.ModelViewMatrix = viewMatrix;
// Render the Block
Matrices.Push();
Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Translate(2, 0, 0);
Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Translate(0.5f, 0, 0.5f);
Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Rotate(0, i / 40.0f, 0);
block.Render();
Matrices.Pop();
// Render the Block Again Twice
Matrices.Push();
Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Translate(-2, 0, 0);
Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Translate(0.5f, 0, 0.5f);
block.Render();
Matrices.ModelViewMatrix = Matrices.ModelViewMatrix.Translate(0, 0, -1);
block.Render();
Matrices.Pop();
// Increment Rotation Test Variable
i++;
}
}
SimpleBlock.cs
class SimpleBlock : IBlock
{
public void Render()
{
// Send the Shader Parameters to the GPU
Shader.Bind();
Shader.SendMatrices();
// Begin Rendering the Polys
GL.Begin(BeginMode.Triangles);
// Front Quad
Shader.SetColor(Color4.SaddleBrown);
GL.Normal3(0, 0, 1);
GLUtils.QuadVertices(
new Vector3(-0.5f, 1, 0.5f),
new Vector3(-0.5f, 0, 0.5f),
new Vector3( 0.5f, 1, 0.5f),
new Vector3( 0.5f, 0, 0.5f));
// Right Quad
GL.Normal3(1, 0, 0);
GLUtils.QuadVertices(
new Vector3(0.5f, 1, 0.5f),
new Vector3(0.5f, 0, 0.5f),
new Vector3(0.5f, 1, -0.5f),
new Vector3(0.5f, 0, -0.5f));
// Back Quad
GL.Normal3(0, 0, -1);
GLUtils.QuadVertices(
new Vector3( 0.5f, 1, -0.5f),
new Vector3( 0.5f, 0, -0.5f),
new Vector3(-0.5f, 1, -0.5f),
new Vector3(-0.5f, 0, -0.5f));
// Left Quad
GL.Normal3(-1, 0, 0);
GLUtils.QuadVertices(
new Vector3(-0.5f, 1, -0.5f),
new Vector3(-0.5f, 0, -0.5f),
new Vector3(-0.5f, 1, 0.5f),
new Vector3(-0.5f, 0, 0.5f));
// Bottom Quad
GL.Normal3(0, -1, 0);
GLUtils.QuadVertices(
new Vector3(-0.5f, 0, 0.5f),
new Vector3(-0.5f, 0, -0.5f),
new Vector3( 0.5f, 0, 0.5f),
new Vector3( 0.5f, 0, -0.5f));
// Top Quad
Shader.SetColor(Color4.Green);
GL.Normal3(0, 1, 0);
GLUtils.QuadVertices(
new Vector3(-0.5f, 1, -0.5f),
new Vector3(-0.5f, 1, 0.5f),
new Vector3(0.5f, 1, -0.5f),
new Vector3(0.5f, 1, 0.5f));
// Done!
GL.End();
}
}
BasicFragment.glfs
#version 130
// MultiColor Attribute
in vec4 multiColor;
// Output color
out vec4 gl_FragColor;
void main()
{
// Set fragment
gl_FragColor = multiColor;
}
BasicVertex.glvs
#version 130
// Transformation Matrices
uniform mat4 ProjectionMatrix;
uniform mat4 ModelViewMatrix;
// Vertex Position Attribute
in vec3 VertexPos;
// MultiColor Attributes
in vec4 MultiColor;
out vec4 multiColor;
void main()
{
// Process Colors
multiColor = MultiColor;
// Process Vertex
gl_Position = ProjectionMatrix * ModelViewMatrix * vec4(VertexPos.x, VertexPos.y, VertexPos.z, 1);
}
MainWindow.cs
// Extends OpenTK's GameWindow Class
class MainWindow : GameWindow
{
public MainWindow()
: base(1024, 768, new GraphicsMode(32, 0, 0, 4))
{
this.Title = "Trench Wars";
this.WindowBorder = WindowBorder.Fixed;
this.ClientSize = new Size(1024, 768);
// Set VSync On
this.VSync = VSyncMode.Adaptive;
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
// Clear Screen
GL.ClearColor(Color4.CornflowerBlue);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
// Do State-Specific Rendering
StateEngine.Render();
// Pull a Wicked Bluffing move in Poker
GL.Flush();
// Swap Buffers
this.SwapBuffers();
}
}
It seems like you did forget to enable depth testing. glEnable(GL_DEPTH_TEST) before rendering the geometry is your friend (or given the language bindings you're using GL.Enable(EnableCap.DepthTest);).

Texture2D: SetData doesn't work

I'd like to have a sort of floor with a programmaically created texture on it. I already created the vertices and the indexes needed for the work:
private VertexPositionNormalTexture[] verticiBase;
private short[] indici;
...
verticiBase = new VertexPositionNormalTexture[4];
verticiBase[0] = new VertexPositionNormalTexture(new Vector3(0.0f, 0.0f, 0.0f), Vector3.Up, new Vector2(0, 0));
verticiBase[1] = new VertexPositionNormalTexture(new Vector3(dimensioneVera.X, 0.0f, 0.0f), Vector3.Up, new Vector2(1, 0));
verticiBase[2] = new VertexPositionNormalTexture(new Vector3(0.0f, 0.0f, dimensioneVera.Y), Vector3.Up, new Vector2(0, 1));
verticiBase[3] = new VertexPositionNormalTexture(new Vector3(dimensioneVera.X, 0.0f, dimensioneVera.Y), Vector3.Up, new Vector2(1, 1));
graphics.GraphicsDevice.VertexDeclaration = new
VertexDeclaration(graphics.GraphicsDevice,
VertexPositionNormalTexture.VertexElements);
indici = new short[6];
indici[0] = 0;
indici[1] = 1;
indici[2] = 2;
indici[3] = 1;
indici[4] = 3;
indici[5] = 2;
Then I created the texture and the data which I'd like to show:
private Texture2D texture;
private Color[] textureData;
...
texture = new Texture2D(Game.GraphicsDevice, (int)dimensioneVera.X, (int)dimensioneVera.Y);
textureData = new Color[(int)dimensioneVera.X * (int)dimensioneVera.Y];
for (int x = 0; x < textureData.Length; x++)
textureData[x] = Color.Red;
texture.SetData(textureData);
And this is the code I used to draw:
private BasicEffect effetti;
...
effetti = new BasicEffect(graphics.GraphicsDevice, null);
...
public override void Draw(GameTime gameTime)
{
effetti.World = Matrix.Identity;
effetti.View = camera.view;
effetti.Projection = camera.projection;
effetti.TextureEnabled = true;
effetti.Texture = texture;
effetti.Begin();
effetti.EnableDefaultLighting();
effetti.CurrentTechnique.Passes[0].Begin();
graphics.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList,
verticiBase, 0, verticiBase.Length, indici, 0, indici.Length / 3);
effetti.CurrentTechnique.Passes[0].End();
effetti.End();
base.Draw(gameTime);
}
The floor is displayed but the texture is all black. What's wrong?
Thanks for your time.
I suspect the problem is not that you've created the texture incorrectly. The problem is your lighting; you've called EnableDefaultLighting() but not provided any lights, so everything is completely dark (black). Try setting effetti.AmbientLightColor = Color.White and see if that helps.

Categories