| Topic: Texture is colouring
|
|
 | Texture is colouring | |  |
| Poster | : SmallDeadGuy | | Posts | : 2 | | Country | : United Kingdom | | City | : Cambridge |
| | | | Posted by SmallDeadGuy on 26/06/2012 at 04:05:57
| | I am trying to render squares where the middle is blended to a colour representing a face (rubik's cube). The texture is a black square with an inner white square. The cube itself renders in the right place but the entire square is always black (unless i turn texture off, at which point the entire square is the desired colour).
Here is the code I use:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Rubik_Teacher {
public class RubikTeacher : Microsoft.Xna.Framework.Game {
public GraphicsDeviceManager graphics;
public SpriteBatch spriteBatch;
public BasicEffect effect;
public Cube cube;
public float angle = 0.0F;
public Matrix viewMatrix;
public Matrix projectionMatrix;
public Texture2D texture;
public RubikTeacher() {
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize() {
base.Initialize();
}
protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice);
texture = Content.Load<Texture2D>("side");
RasterizerState rs = new RasterizerState();
rs.CullMode = CullMode.None;
GraphicsDevice.RasterizerState = rs;
effect = new BasicEffect(GraphicsDevice);
effect.VertexColorEnabled = true;
effect.TextureEnabled = true;
effect.Texture = texture;
effect.LightingEnabled = false;
cube = new Cube();
viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 7), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1.0f, 300.0f);
}
protected override void UnloadContent() {}
protected override void Update(GameTime gameTime) {
if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) Exit();
angle += 0.005F;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix worldMatrix = Matrix.CreateRotationY(3 * angle) * Matrix.CreateRotationZ(angle) * Matrix.CreateRotationX(2 * angle);
effect.World = worldMatrix;
effect.View = viewMatrix;
effect.Projection = projectionMatrix;
foreach(EffectPass p in effect.CurrentTechnique.Passes) {
p.Apply();
cube.renderCube();
}
base.Draw(gameTime);
}
}
}
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace Rubik_Teacher {
public enum FaceID {Top, Bottom, Front, Back, Left, Right};
public class Cube {
public FaceID[,,] faceColours = new FaceID[6, 3, 3];
public Color[] colourIDs = {Color.Orange, Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.White};
public Cube() {
for(int i = 0; i < 6; i++)
for(int x = 0; x < 3; x++) for(int y = 0; y < 3; y++)
faceColours[i, x, y] = (FaceID) i;
}
public void renderCube() {
for(int i = 0; i < faceColours.GetLength(0); i++) {
VertexPositionColorTexture[] vertices = generateFaceVertices((FaceID) i);
Program.game.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3, VertexPositionColorTexture.VertexDeclaration);
}
}
public VertexPositionColorTexture[] generateFaceVertices(FaceID face) {
VertexPositionColorTexture[] vertices = new VertexPositionColorTexture[54];
switch(face) {
case FaceID.Top:
for(int x = 0; x < 3; x++) for(int z = 0; z < 3; z++) {
Color c = colourIDs[(int) faceColours[(int) face, x, z]];
vertices[x * 6 + z * 18 + 0] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, 1.51F, (float) z - 1.4F), c, Vector2.Zero);
vertices[x * 6 + z * 18 + 1] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, 1.51F, (float) z - 1.4F), c, Vector2.Zero);
vertices[x * 6 + z * 18 + 2] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, 1.51F, (float) z - 0.6F), c, Vector2.Zero);
vertices[x * 6 + z * 18 + 3] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, 1.51F, (float) z - 1.4F), c, Vector2.Zero);
vertices[x * 6 + z * 18 + 4] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, 1.51F, (float) z - 0.6F), c, Vector2.Zero);
vertices[x * 6 + z * 18 + 5] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, 1.51F, (float) z - 0.6F), c, Vector2.Zero);
}
break;
case FaceID.Bottom:
for(int x = 0; x < 3; x++) for(int z = 0; z < 3; z++) {
Color c = colourIDs[(int) faceColours[(int) face, x, z]];
vertices[x * 6 + z * 18 + 0] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, -1.51F, (float) z - 1.4F), c, Vector2.Zero);
vertices[x * 6 + z * 18 + 1] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, -1.51F, (float) z - 1.4F), c, Vector2.Zero);
vertices[x * 6 + z * 18 + 2] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, -1.51F, (float) z - 0.6F), c, Vector2.Zero);
vertices[x * 6 + z * 18 + 3] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, -1.51F, (float) z - 1.4F), c, Vector2.Zero);
vertices[x * 6 + z * 18 + 4] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, -1.51F, (float) z - 0.6F), c, Vector2.Zero);
vertices[x * 6 + z * 18 + 5] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, -1.51F, (float) z - 0.6F), c, Vector2.Zero);
}
break;
case FaceID.Front:
for(int x = 0; x < 3; x++) for(int y = 0; y < 3; y++) {
Color c = colourIDs[(int) faceColours[(int) face, x, y]];
vertices[x * 6 + y * 18 + 0] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, (float) y - 1.4F, 1.51F), c, Vector2.Zero);
vertices[x * 6 + y * 18 + 1] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, (float) y - 1.4F, 1.51F), c, Vector2.Zero);
vertices[x * 6 + y * 18 + 2] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, (float) y - 0.6F, 1.51F), c, Vector2.Zero);
vertices[x * 6 + y * 18 + 3] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, (float) y - 1.4F, 1.51F), c, Vector2.Zero);
vertices[x * 6 + y * 18 + 4] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, (float) y - 0.6F, 1.51F), c, Vector2.Zero);
vertices[x * 6 + y * 18 + 5] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, (float) y - 0.6F, 1.51F), c, Vector2.Zero);
}
break;
case FaceID.Back:
for(int x = 0; x < 3; x++) for(int y = 0; y < 3; y++) {
Color c = colourIDs[(int) faceColours[(int) face, x, y]];
vertices[x * 6 + y * 18 + 0] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, (float) y - 1.4F, -1.51F), c, Vector2.Zero);
vertices[x * 6 + y * 18 + 1] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, (float) y - 1.4F, -1.51F), c, Vector2.Zero);
vertices[x * 6 + y * 18 + 2] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, (float) y - 0.6F, -1.51F), c, Vector2.Zero);
vertices[x * 6 + y * 18 + 3] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, (float) y - 1.4F, -1.51F), c, Vector2.Zero);
vertices[x * 6 + y * 18 + 4] = new VertexPositionColorTexture(new Vector3((float) x - 0.6F, (float) y - 0.6F, -1.51F), c, Vector2.Zero);
vertices[x * 6 + y * 18 + 5] = new VertexPositionColorTexture(new Vector3((float) x - 1.4F, (float) y - 0.6F, -1.51F), c, Vector2.Zero);
}
break;
case FaceID.Left:
for(int y = 0; y < 3; y++) for(int z = 0; z < 3; z++) {
Color c = colourIDs[(int) faceColours[(int) face, y, z]];
vertices[y * 6 + z * 18 + 0] = new VertexPositionColorTexture(new Vector3(-1.51F, (float) y - 1.4F, (float) z - 1.4F), c, Vector2.Zero);
vertices[y * 6 + z * 18 + 1] = new VertexPositionColorTexture(new Vector3(-1.51F, (float) y - 0.6F, (float) z - 1.4F), c, Vector2.Zero);
vertices[y * 6 + z * 18 + 2] = new VertexPositionColorTexture(new Vector3(-1.51F, (float) y - 0.6F, (float) z - 0.6F), c, Vector2.Zero);
vertices[y * 6 + z * 18 + 3] = new VertexPositionColorTexture(new Vector3(-1.51F, (float) y - 1.4F, (float) z - 1.4F), c, Vector2.Zero);
vertices[y * 6 + z * 18 + 4] = new VertexPositionColorTexture(new Vector3(-1.51F, (float) y - 0.6F, (float) z - 0.6F), c, Vector2.Zero);
vertices[y * 6 + z * 18 + 5] = new VertexPositionColorTexture(new Vector3(-1.51F, (float) y - 1.4F, (float) z - 0.6F), c, Vector2.Zero);
}
break;
case FaceID.Right:
for(int y = 0; y < 3; y++) for(int z = 0; z < 3; z++) {
Color c = colourIDs[(int) faceColours[(int) face, y, z]];
vertices[y * 6 + z * 18 + 0] = new VertexPositionColorTexture(new Vector3(1.51F, (float) y - 1.4F, (float) z - 1.4F), c, Vector2.Zero);
vertices[y * 6 + z * 18 + 1] = new VertexPositionColorTexture(new Vector3(1.51F, (float) y - 0.6F, (float) z - 1.4F), c, Vector2.Zero);
vertices[y * 6 + z * 18 + 2] = new VertexPositionColorTexture(new Vector3(1.51F, (float) y - 0.6F, (float) z - 0.6F), c, Vector2.Zero);
vertices[y * 6 + z * 18 + 3] = new VertexPositionColorTexture(new Vector3(1.51F, (float) y - 1.4F, (float) z - 1.4F), c, Vector2.Zero);
vertices[y * 6 + z * 18 + 4] = new VertexPositionColorTexture(new Vector3(1.51F, (float) y - 0.6F, (float) z - 0.6F), c, Vector2.Zero);
vertices[y * 6 + z * 18 + 5] = new VertexPositionColorTexture(new Vector3(1.51F, (float) y - 1.4F, (float) z - 0.6F), c, Vector2.Zero);
}
break;
}
return vertices;
}
}
}
|
| |
|
| | | | | | Poster | : Rufi | | Posts | : 1 | | Country | : | | City | : |
| | | | Posted by Rufi on 26/06/2012 at 06:56:52
| | Well, you have set the Cube's vertices' texture coordinates to Vector2.Zero! ;) To solve the problem just put in the correct coordinates.
For example, the TOP face becomes (notice the last parameter):
vertices[x * 6 + z * 18 + 0] = new VertexPositionColorTexture(new Vector3((float)x - 1.4F, 1.51F, (float)z - 1.4F), c, new Vector2(0, 0));
vertices[x * 6 + z * 18 + 1] = new VertexPositionColorTexture(new Vector3((float)x - 0.6F, 1.51F, (float)z - 1.4F), c, new Vector2(1, 0));
vertices[x * 6 + z * 18 + 2] = new VertexPositionColorTexture(new Vector3((float)x - 0.6F, 1.51F, (float)z - 0.6F), c, new Vector2(1, 1));
vertices[x * 6 + z * 18 + 3] = new VertexPositionColorTexture(new Vector3((float)x - 1.4F, 1.51F, (float)z - 1.4F), c, new Vector2(0, 0));
vertices[x * 6 + z * 18 + 4] = new VertexPositionColorTexture(new Vector3((float)x - 0.6F, 1.51F, (float)z - 0.6F), c, new Vector2(1, 1));
vertices[x * 6 + z * 18 + 5] = new VertexPositionColorTexture(new Vector3((float)x - 1.4F, 1.51F, (float)z - 0.6F), c, new Vector2(0, 1));
|
I tested the code, and I guess that is the effect you want. If you create the other faces' vertices in the same order, then you should be able to just copy the texture coordinates from the top face in the same order. | |
|
| | | | | | Poster | : SmallDeadGuy | | Posts | : 2 | | Country | : United Kingdom | | City | : Cambridge |
| | | | Posted by SmallDeadGuy on 26/06/2012 at 10:38:22
| | | Thank you! It seems so simple now that you've posted that. I made Vector2.Zero for the top left corner then copy-pasted for the rest forgetting to change it. Thanks for noticing :) | |
|
|
 | | |  |
|
|
|
If you appreciate the amount of time I spend creating and updating these pages, feel free to donate -- any amount is welcome !
|
- Website design & DirectX code : Riemer Grootjans - ©2006 Riemer Grootjans
|
|