Related
I have a little problem with rendering in my SharpDX Direct11 App.
I had being tested rendering scene on a texture, and then draw this texture on backBuffer... but unfortunately renderTexture do not contains primitives which should be drawn. Texture is only filled by color.
Whole project on github: https://github.com/Kordi3112/SharpDXTest11
Main code part with rendering methods:
public override void Render()
{
//Camera
var proj = Matrix.OrthoLH(3 * Form.Bounds.Width / Form.Bounds.Height, 3, 0.01f, 100f);
var view = Matrix.LookAtLH(new Vector3(0, 0, -10), new Vector3(0, 0, 20), Vector3.UnitY);
var viewProj = Matrix.Multiply(view, proj);
var world = Matrix.Identity;
var worldViewProj = world * viewProj;
worldViewProj.Transpose();
//Update wvp matrix
Context.UpdateSubresource(ref worldViewProj, ContantBuffer);
DrawOnTexture();
//Set BackBuffer as render target
Context.OutputMerger.SetTargets(depthView, renderView);
// Clear views
Context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
Context.ClearRenderTargetView(renderView, Color.Pink);
//Set TextureColor Shader
Effect2.ApplyShader(Context);
//Set Buffers
Context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer2, Utilities.SizeOf<VertexPositionColorTexture>(), 0));
Context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
//Set Texture to Shader
Context.PixelShader.SetShaderResource(0, RenderTexture.ShaderResourceView);
//Draw
Context.DrawIndexed(6, 0, 0);
// Present!
SwapChain.Present(0, PresentFlags.None);
}
private void DrawOnTexture()
{
//Set Color Shader
Effect1.ApplyShader(Context);
//Set Buffers
Context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer, Utilities.SizeOf<VertexPositionColor>(), 0));
Context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
//Set Target
RenderTexture.SetRenderTarget(Context, depthView);
//Clear Targets - Green Bgound
RenderTexture.ClearRenderTarget(Context, depthView, 0, 1, 0, 1);
//Draw on RenderTarget
Context.DrawIndexed(6, 0, 0);
}
After call: Context.DrawIndexed(6, 0, 0); in private void DrawOnTexture() primitive should be drawn.
What this code above do
What i wanted to get
What's wrong with my code?
I'm sure the problem is not matrix or camera. When i will modify code to render primitive directly on backBuffer then its drawing normaly.
public override void Render()
{
//Camera
var proj = Matrix.OrthoLH(3 * Form.Bounds.Width / Form.Bounds.Height, 3, 0.01f, 100f);
var view = Matrix.LookAtLH(new Vector3(0, 0, -10), new Vector3(0, 0, 20), Vector3.UnitY);
var viewProj = Matrix.Multiply(view, proj);
var world = Matrix.Identity;
var worldViewProj = world * viewProj;
worldViewProj.Transpose();
//Update wvp matrix
Context.UpdateSubresource(ref worldViewProj, ContantBuffer);
//DrawOnTexture();
//Set BackBuffer as render target
Context.OutputMerger.SetTargets(depthView, renderView);
// Clear views
Context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
Context.ClearRenderTargetView(renderView, Color.Pink);
//Set Color Shader
Effect1.ApplyShader(Context);
//Set Buffers
Context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer, Utilities.SizeOf<VertexPositionColor>(), 0));
Context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
//Set Texture to Shader
//Context.PixelShader.SetShaderResource(0, RenderTexture.ShaderResourceView);
//Draw
Context.DrawIndexed(6, 0, 0);
// Present!
SwapChain.Present(0, PresentFlags.None);
}
output
Vertex Buffers declaration:
//Position Color
VertexBuffer = Buffer.Create(Device, BindFlags.VertexBuffer, new[] {
new VertexPositionColor(new Vector4(-1, -1, 0, 1), Color.Red.ToVector4()),
new VertexPositionColor(new Vector4(-1, 1, 0, 1), Color.Green.ToVector4()),
new VertexPositionColor(new Vector4(1, 1, 0, 1), Color.Blue.ToVector4()),
new VertexPositionColor(new Vector4(1, -1, 0, 1), Color.Yellow.ToVector4())
});
//Position Color Texture
VertexBuffer2 = Buffer.Create(Device, BindFlags.VertexBuffer, new[] {
new VertexPositionColorTexture(new Vector4(-1, -1, 0, 1), Color.White.ToVector4(), new Vector2(0,1)),
new VertexPositionColorTexture(new Vector4(-1, 1, 0, 1), Color.White.ToVector4(),new Vector2(0,0)),
new VertexPositionColorTexture(new Vector4(1, 1, 0, 1), Color.White.ToVector4(),new Vector2(1,0)),
new VertexPositionColorTexture(new Vector4(1, -1, 0, 1), Color.White.ToVector4(),new Vector2(1,1))
});
IndexBuffer = Buffer.Create(Device, BindFlags.IndexBuffer, new[] {
0,1,2,
0,2,3
});
I want to change the color of a few vertices under an event but I cannot seem to make it work.
How is the usual procedure done when you want to change a vertex color?
I have an initialization where I give instructions on how the cube is going to be made and a onRenderFrame(); where I draw the cube so the shape and color are already set but I cannot seem to manage to make a color change under an event (like an if-statement).
I will send the whole code. It also involves a connection between Arduino which will play part in the color change
using System;
using System.IO.Ports;
using Tao.FreeGlut;
using OpenGL;
namespace Arduino
{
class Program
{
private static int width = 1200, height = 720;
private static ShaderProgram program;
private static VBO<Vector3> squareColor;
private static VBO<Vector3> square;
private static VBO<int> squareElements;
private static System.Diagnostics.Stopwatch watch;
private static float angle;
private static bool exitWhile = false;
private static bool initOK = true;
static void Main(string[] args)
{
while(exitWhile == false)
{
if(initOK == true)
{
Init();
}
}
}
private static void Init()
{
Glut.glutInit();
Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH);
Glut.glutInitWindowSize(width, height);
Glut.glutCreateWindow("OpenGL Arduino");
SerialPort myArduino = new SerialPort();
myArduino.BaudRate = 9600;
myArduino.PortName = "COM3";
myArduino.Open();
myArduino.DataReceived += new SerialDataReceivedEventHandler(Comm);
Glut.glutIdleFunc(OnRenderFrame);
Glut.glutDisplayFunc(OnDisplay);
Glut.glutCloseFunc(OnClose);
Gl.Enable(EnableCap.DepthTest);
program = new ShaderProgram(VertexShader, Fragmentshader);
program.Use();
program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f));
program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.UnitY));
square = new VBO<Vector3>(new Vector3[] {
new Vector3(1, 1, -1), new Vector3(-1, 1, -1), new Vector3(-1, 1, 1), new Vector3(1, 1, 1),
new Vector3(1, -1, 1), new Vector3(-1, -1, 1), new Vector3(-1, -1, -1), new Vector3(1, -1, -1),
new Vector3(1, 1, 1), new Vector3(-1, 1, 1), new Vector3(-1, -1, 1), new Vector3(1, -1, 1),
new Vector3(1, -1, -1), new Vector3(-1, -1, -1), new Vector3(-1, 1, -1), new Vector3(1, 1, -1),
new Vector3(-1, 1, 1), new Vector3(-1, 1, -1), new Vector3(-1, -1, -1), new Vector3(-1, -1, 1),
new Vector3(1, 1, -1), new Vector3(1, 1, 1), new Vector3(1, -1, 1), new Vector3(1, -1, -1) });
squareColor = new VBO<Vector3>(new Vector3[] {
new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1) });
squareElements = new VBO<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, BufferTarget.ElementArrayBuffer);
watch = System.Diagnostics.Stopwatch.StartNew();
initOK = false;
Glut.glutMainLoop();
}
static void Comm(Object Sender, SerialDataReceivedEventArgs args)
{
SerialPort sp = (SerialPort)Sender;
string indata = sp.ReadExisting();
Console.Write(indata);
if (indata.Contains("1.10"))
{
Vector3[] vertexBuffer = (new Vector3[] {
new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 0, 0), new Vector3(1, 1, 1),
new Vector3(1, 1, 1), new Vector3(1, 0, 0), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
new Vector3(1, 1, 1), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 1),
new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1),
new Vector3(1, 0, 0), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 0, 0),
new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 1) });
squareColor.BufferSubData(vertexBuffer);
}
}
private static void OnClose()
{
square.Dispose();
squareColor.Dispose();
squareColorChange.Dispose();
squareElements.Dispose();
program.DisposeChildren = true;
program.Dispose();
exitWhile = true;
}
private static void OnDisplay()
{
}
private static void OnRenderFrame()
{
watch.Stop();
float deltaTime = watch.ElapsedMilliseconds / 1000f;
watch.Restart();
angle += deltaTime;
Gl.Viewport(0, 0, width, height);
Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
program.Use();
//Square draw
program["model_matrix"].SetValue(Matrix4.CreateRotationY(angle) * Matrix4.CreateTranslation(new Vector3(1.5f, 0, 0)));
Gl.BindBufferToShaderAttribute(square, program, "vertexPosition");
Gl.BindBufferToShaderAttribute(squareColor, program, "vertexColor");
Gl.BindBufferToShaderAttribute(squareColorChange, program, "vertexColor");
Gl.BindBuffer(squareElements);
Gl.DrawElements(BeginMode.Quads, squareElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
Glut.glutSwapBuffers();
}
public static string VertexShader = #"
in vec3 vertexPosition;
in vec3 vertexColor;
out vec3 color;
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
void main(void)
{
color = vertexColor;
gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertexPosition, 1);
}
";
public static string Fragmentshader = #"
in vec3 color;
out vec4 fragment;
void main(void)
{
fragment = vec4(color, 1);
}
";
}
It may look a little rough and that's because I am pretty new in both C# and asking questions on Stackoverflow.
I edited the code by adding a subBuffer to my event, although it does not render the new color anyway. Have I forgotten to do something?
Thanks in advance!
I created a texture (bitmap) that is used for drawing arbitrary numbers
The texture contains the text: 0123456789.-
Now I need the coordinates of each character.
TextRenderer.MeasureText(c.ToString(), m_font, new Size(1, 1), TextFormatFlags.NoPadding);
...returns is 16x17
g.MeasureString(c.ToString(), m_font);
...returns is 11.9x17.7
The actual size is 8x11
My workaround for now is a manual measurement of each character which yields the following table:
coords.Add('0', new RectangleF(1, 3, 8, 11));
coords.Add('1', new RectangleF(9, 3, 7, 11));
coords.Add('2', new RectangleF(15, 3, 8, 11));
coords.Add('3', new RectangleF(22, 3, 8, 11));
coords.Add('4', new RectangleF(29, 3, 8, 11));
coords.Add('5', new RectangleF(36, 3, 8, 11));
coords.Add('6', new RectangleF(43, 3, 8, 11));
coords.Add('7', new RectangleF(50, 3, 8, 11));
coords.Add('8', new RectangleF(57, 3, 8, 11));
coords.Add('9', new RectangleF(64, 3, 8, 11));
coords.Add('.', new RectangleF(72, 3, 3, 11));
coords.Add('-', new RectangleF(75, 3, 6, 11));
I would like to use a dynamic font, so a programmatic way to measure the size is required.
Tell the MeasureString method to work with the generic typographic StringFormat.
static void Main(string[] args)
{
var image = new Bitmap(1000, 500);
var g = Graphics.FromImage(image);
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, 1000, 500);
var stringFormat = new StringFormat(StringFormat.GenericTypographic) {
Alignment = StringAlignment.Near,
FormatFlags = System.Drawing.StringFormatFlags.LineLimit | System.Drawing.StringFormatFlags.NoClip | StringFormatFlags.DirectionRightToLeft
};
var font = new Font(new System.Drawing.FontFamily("Times New Roman"), 72.0f,FontStyle.Regular, GraphicsUnit.Point);
var point = new PointF { X = 10, Y = 10 };
SizeF[] outputs = new SizeF[7];
SizeF[] total = new SizeF[7];
outputs[0] = g.MeasureString("T", font, point, stringFormat);
outputs[1] = g.MeasureString("e", font, point, stringFormat);
outputs[2] = g.MeasureString("s", font, point, stringFormat);
outputs[3] = g.MeasureString("t", font, point, stringFormat);
outputs[4] = g.MeasureString("S", font, point, stringFormat);
outputs[5] = g.MeasureString("t", font, point, stringFormat);
outputs[6] = g.MeasureString("r", font, point, stringFormat);
total[0] = g.MeasureString("T", font, point, stringFormat);
total[1] = g.MeasureString("Te", font, point, stringFormat);
total[2] = g.MeasureString("Tes", font, point, stringFormat);
total[3] = g.MeasureString("Test", font, point, stringFormat);
total[4] = g.MeasureString("TestS", font, point, stringFormat);
total[5] = g.MeasureString("TestSt", font, point, stringFormat);
total[6] = g.MeasureString("TestStr", font, point, stringFormat);
stringFormat.FormatFlags = System.Drawing.StringFormatFlags.LineLimit | System.Drawing.StringFormatFlags.NoClip;
g.DrawString("TestStr", font, System.Drawing.Brushes.Red, new PointF { X = 10, Y = 10 }, stringFormat);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Brushes.Blue, 1.0f), GetOutputPositionX(total,outputs, 0), 10.0f, outputs[0].Width, outputs[0].Height);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Brushes.Blue, 1.0f), GetOutputPositionX(total,outputs, 1), 10.0f, outputs[1].Width, outputs[1].Height);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Brushes.Blue, 1.0f), GetOutputPositionX(total,outputs, 2), 10.0f, outputs[2].Width, outputs[2].Height);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Brushes.Blue, 1.0f), GetOutputPositionX(total,outputs, 3), 10.0f, outputs[3].Width, outputs[3].Height);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Brushes.Blue, 1.0f), GetOutputPositionX(total,outputs, 4), 10.0f, outputs[4].Width, outputs[4].Height);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Brushes.Blue, 1.0f), GetOutputPositionX(total,outputs, 5), 10.0f, outputs[5].Width, outputs[5].Height);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Brushes.Blue, 1.0f), GetOutputPositionX(total,outputs, 6), 10.0f, outputs[6].Width, outputs[6].Height);
image.Save(#"c:\Temp\bla.png");
}
private static float GetOutputPositionX(SizeF[] total, SizeF[] outputs, int p)
{
return 10.0f + total[p].Width - outputs[p].Width;
}
To get the Position exactly, because of these overlapping characters you have to calculate the width of "e" and the total width of "Te".
Plus there is obviously a bug in the MeasureString function. If you call MeasureString without the StringFormatFlags.DirectionRightToLeft the overlap of the characters "Te" is ignored.
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.
This time I have this cube made of triangles. My code assignes texture to each triangle. How to change the code so only one wall will be left as texture and other will be painted yellow?
public class Game1 : Microsoft.Xna.Framework.Game
{
private float angle = .9f;
private float SecondAngle = -1f;
//MyVertexFormat struct to go here
struct MyVertexFormat
{
private Vector3 position;
private Vector2 texCoord;
public MyVertexFormat(Vector3 position, Vector2 texCoord)
{
this.position = position;
this.texCoord = texCoord;
}
//Add VertexDeclaration here
public readonly static VertexDeclaration VertexDeclaration = new
VertexDeclaration(
new VertexElement(0, VertexElementFormat.Vector3,
VertexElementUsage.Position, 0),
new VertexElement(sizeof(float) * 3,
VertexElementFormat.Vector2,
VertexElementUsage.TextureCoordinate, 0));
}
GraphicsDeviceManager graphics;
GraphicsDevice device;
Effect effect;
Matrix viewMatrix;
Matrix projectionMatrix;
VertexBuffer vertexBuffer;
Vector3 cameraPos;
Texture2D wallTexture;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
graphics.PreferredBackBufferWidth = 500;
graphics.PreferredBackBufferHeight = 500;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
Window.Title = "HLSL Start";
base.Initialize();
}
protected override void LoadContent()
{
device = GraphicsDevice;
wallTexture = Content.Load<Texture2D>("wall");
effect = Content.Load<Effect>("Effect1");
SetUpVertices();
SetUpCamera();
}
private void SetUpVertices()
{
MyVertexFormat[] vertices = new MyVertexFormat[36];
//Back
vertices[0] = new MyVertexFormat(new Vector3(-1, -1, -1),
new Vector2(1.0f, 0.0f));
vertices[1] = new MyVertexFormat(new Vector3(1, 1, -1),
new Vector2(0.0f, 1.0f));
vertices[2] = new MyVertexFormat(new Vector3(-1, 1, -1),
new Vector2(1.0f, 1.0f));
vertices[3] = new MyVertexFormat(new Vector3(-1, -1, -1),
new Vector2(1.0f, 0.0f));
vertices[4] = new MyVertexFormat(new Vector3(1, -1, -1),
new Vector2(0.0f, 0.0f));
vertices[5] = new MyVertexFormat(new Vector3(1, 1, -1),
new Vector2(0.0f, 1.0f));
//Bottom
vertices[6] = new MyVertexFormat(new Vector3(1, -1, 1),
new Vector2(1.0f, 1.0f));
vertices[7] = new MyVertexFormat(new Vector3(1, -1, -1),
new Vector2(1.0f, 0.0f));
vertices[8] = new MyVertexFormat(new Vector3(-1, -1, -1),
new Vector2(0.0f, 0.0f));
vertices[9] = new MyVertexFormat(new Vector3(-1, -1, -1),
new Vector2(0.0f, 0.0f));
vertices[10] = new MyVertexFormat(new Vector3(-1, -1, 1),
new Vector2(0.0f, 1.0f));
vertices[11] = new MyVertexFormat(new Vector3(1, -1, 1),
new Vector2(1.0f, 1.0f));
//Top
vertices[12] = new MyVertexFormat(new Vector3(-1, 1, 1),
new Vector2(0.0f, 1.0f));
vertices[13] = new MyVertexFormat(new Vector3(-1, 1, -1),
new Vector2(1.0f, 1.0f));
vertices[14] = new MyVertexFormat(new Vector3(1, 1, 1),
new Vector2(0.0f, 0.0f));
vertices[15] = new MyVertexFormat(new Vector3(1, 1, 1),
new Vector2(0.0f, 0.0f));
vertices[16] = new MyVertexFormat(new Vector3(-1, 1, -1),
new Vector2(1.0f, 1.0f));
vertices[17] = new MyVertexFormat(new Vector3(1, 1, -1),
new Vector2(1.0f, 0.0f));
//Right side
vertices[18] = new MyVertexFormat(new Vector3(1, 1, 1),
new Vector2(1.0f, 1.0f));
vertices[19] = new MyVertexFormat(new Vector3(1, -1, -1),
new Vector2(0.0f, 0.0f));
vertices[20] = new MyVertexFormat(new Vector3(1, -1, 1),
new Vector2(1.0f, 0.0f));
vertices[21] = new MyVertexFormat(new Vector3(1, -1, -1),
new Vector2(0.0f, 0.0f));
vertices[22] = new MyVertexFormat(new Vector3(1, 1, 1),
new Vector2(1.0f, 1.0f));
vertices[23] = new MyVertexFormat(new Vector3(1, 1, -1),
new Vector2(0.0f, 1.0f));
//Left side
vertices[24] = new MyVertexFormat(new Vector3(-1, -1, 1),
new Vector2(0.0f, 0.0f));
vertices[25] = new MyVertexFormat(new Vector3(-1, -1, -1),
new Vector2(1.0f, 0.0f));
vertices[26] = new MyVertexFormat(new Vector3(-1, 1, 1),
new Vector2(0.0f, 1.0f));
vertices[29] = new MyVertexFormat(new Vector3(-1, 1, 1),
new Vector2(0.0f, 1.0f));
vertices[28] = new MyVertexFormat(new Vector3(-1, 1, -1),
new Vector2(1.0f, 1.0f));
vertices[27] = new MyVertexFormat(new Vector3(-1, -1, -1),
new Vector2(1.0f, 0.0f));
//Front
vertices[30] = new MyVertexFormat(new Vector3(-1, 1, 1),
new Vector2(1.0f, 1.0f));
vertices[31] = new MyVertexFormat(new Vector3(1, 1, 1),
new Vector2(0.0f, 1.0f));
vertices[32] = new MyVertexFormat(new Vector3(-1, -1, 1),
new Vector2(1.0f, 0.0f));
vertices[33] = new MyVertexFormat(new Vector3(1, -1, 1),
new Vector2(0.0f, 0.0f));
vertices[34] = new MyVertexFormat(new Vector3(-1, -1, 1),
new Vector2(1.0f, 0.0f));
vertices[35] = new MyVertexFormat(new Vector3(1, 1, 1),
new Vector2(0.0f, 1.0f));
vertexBuffer = new VertexBuffer(device,
MyVertexFormat.VertexDeclaration, vertices.Length,
BufferUsage.WriteOnly);
vertexBuffer.SetData(vertices);
}
private void SetUpCamera()
{
cameraPos = new Vector3(0, 5, 6);
viewMatrix = Matrix.CreateLookAt(cameraPos, new Vector3(0, 0, 1), new Vector3(0, 1, 0));
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 200.0f);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
viewMatrix = Matrix.CreateRotationY(MathHelper.ToRadians(angle)) *
Matrix.CreateRotationX(MathHelper.ToRadians(SecondAngle)) *
viewMatrix;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkSlateBlue, 1.0f, 0);
effect.CurrentTechnique = effect.Techniques["Shaded"];
effect.Parameters["View"].SetValue(viewMatrix);
effect.Parameters["Projection"].SetValue(projectionMatrix);
effect.Parameters["World"].SetValue(Matrix.Identity);
effect.Parameters["myTexture"].SetValue(wallTexture);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.SetVertexBuffer(vertexBuffer);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);
}
base.Draw(gameTime);
}
}
}
effect file:
float4x4 World;
float4x4 View;
float4x4 Projection;
Texture myTexture;
sampler TextureSampler = sampler_state {
texture = <myTexture>;
MinFilter = Anisotropic; // Minification Filter
MagFilter = Anisotropic; // Magnification Filter
MipFilter = Linear; // Mip-mapping
AddressU = Wrap; // Address Mode for U Coordinates
AddressV = Wrap; // Address Mode for V Coordinates
};
struct VertexShaderInput
{
float4 Position : POSITION0;
float2 UV: TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 UV: TEXCOORD0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.UV = input.UV;
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float3 output = float3(1, 1, 1);
output *= tex2D(TextureSampler, input.UV);
return float4(output, 1);
}
technique Shaded
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
Any idea anyone?
Add yellow to a small area of your texture, and set the (U,V) texture coords of the triangles you want to be yellow to map that area.
[EDIT]
Add a Colour channel to your vertex format struct:
Class
struct MyVertexFormat
{
private Vector3 position;
private Vector2 texCoord;
private Color color;
public MyVertexFormat(Vector3 position, Vector2 texCoord, Color color)
{
this.position = position;
this.texCoord = texCoord;
this.color = color;
}
//Add VertexDeclaration here
public readonly static VertexDeclaration VertexDeclaration =
new VertexDeclaration(
new VertexElement(0, VertexElementFormat.Vector3,
VertexElementUsage.Position, 0),
new VertexElement(sizeof(float) * 3,
VertexElementFormat.Vector2,
VertexElementUsage.TextureCoordinate, 0)),
new VertexElement(sizeof(float) * 7,
VertexElementFormat.Color,
VertexElementUsage.Color, 0));
}
Effect
struct VertexShaderInput
{
float4 Position : POSITION0;
float2 UV: TEXCOORD0;
float4 Color: Color0;
};
As a note, I think is better to inherit your vertex struct from IVertexType.