Forum
Contact





DirectX using C#
DirectX using C++
DirectX using Visual Basic



Latest Forum posts

 Tutorial 3 for Windows Phone 7
  Posted by: Anonymous
  When: 20/05/2013 at 02:30:13

 No download link for 2d series: shooter
  Posted by: zaboleq
  When: 07/05/2013 at 15:46:28

 Collision Class?
  Posted by: Anonymous
  When: 05/05/2013 at 19:03:59

 stack overflow
  Posted by: cityguy
  When: 07/04/2013 at 01:58:38

 Meshes looks strange.
  Posted by: ab_saratov
  When: 01/04/2013 at 04:31:08

 Lamppost Not loaded
  Posted by: Anonymous
  When: 22/03/2013 at 06:43:52

 Collision Class?
  Posted by: Da_Boom
  When: 21/03/2013 at 01:23:09

 Math boggles me
  Posted by: cityguy
  When: 17/03/2013 at 03:44:48

 Collision Class?
  Posted by: Da_Boom
  When: 16/03/2013 at 03:44:42

 Tree update
  Posted by: Anonymous
  When: 15/03/2013 at 21:11:22

 XNA 4.0
  Posted by: Anonymous
  When: 15/03/2013 at 19:43:57

 Error when I try to run.
  Posted by: Anonymous
  When: 15/03/2013 at 19:21:06

 Error With the Effect File
  Posted by: Anonymous
  When: 15/03/2013 at 18:21:01

 Can only get shadowmap
  Posted by: Anonymous
  When: 15/03/2013 at 15:48:52

 Vertex and Pixel Shader Versions?
  Posted by: Anonymous
  When: 15/03/2013 at 15:07:16

 Unsupported properties
  Posted by: Anonymous
  When: 15/03/2013 at 14:23:00

 Problem Loading Skybox
  Posted by: Rana
  When: 15/03/2013 at 10:34:45

 Black Screen Of Death - Help!
  Posted by: Anonymous
  When: 15/03/2013 at 03:43:43

 2.0 anyone?
  Posted by: Anonymous
  When: 15/03/2013 at 02:19:48

 Defitinition of tha rotation axis
  Posted by: Anonymous
  When: 15/03/2013 at 00:55:14




Topic: Texture is colouring



  
Goto parent category
  
Create a new user account


   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 :)

  
Post a new reply
 





Google
 
Web www.riemers.net
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


News
Home
Forum
XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Downloads
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
XNA 4.0 using C# (89)
DirectX using C# (54)
Series 1:Terrain (14)
Opening a window
Linking to the Device
Drawing a triangle
Camera
Rotation - Translation
Indices
Terrain creation
Terrain from file
DirectInput
Importing bmp files
Colored vertices
DirectX Light basics
Mesh creation
Mesh lighting
Series 2: Flightsim (19)
Series 3: HLSL (19)
Short Tuts (2)
Resizing problem
Checking Device caps
DirectX using C++ (15)
DirectX using VB (2)