I'm trying to draw a triangle using this code in XNA:
VertexPositionColor[] vertices = new VertexPositionColor[3];
vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
vertices[0].Color = Color.Red;
vertices[1].Position = new Vector3(0, 0.5f, 0f);
vertices[1].Color = Color.Green;
vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);
vertices[2].Color = Color.Yellow;
GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, vertices, 0, 1);
However, as soon as I run it, application closes, and InvalidOperationException is thrown. It's WP7 application. Am I missing something? Thanks for your help in advance.
The documentation says that DrawUserPrimitives throws InvalidOperationException when:
A valid vertex shader and pixel shader was not set before calling DrawUserPrimitives. Both a valid vertex shader and pixel shader (or valid effect) must be set on the device before any draw operations may be performed.
(It also says it will throw if your vertices are invalid - but they look ok to me.)
You need to set an Effect on the graphics device. Specifically you need to call EffectPass.Apply before you call DrawUserPrimitives. An easy way to start is with BasicEffect. Here is some code, suitable to put in the Draw method, to illustrate this:
// These three lines are required if you use SpriteBatch, to reset the states that it sets
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
// Transform your model to place it somewhere in the world
basicEffect.World = Matrix.CreateRotationZ(MathHelper.PiOver4) * Matrix.CreateTranslation(0.5f, 0, 0); // for sake of example
//basicEffect.World = Matrix.Identity; // Use this to leave your model at the origin
// Transform the entire world around (effectively: place the camera)
basicEffect.View = Matrix.CreateLookAt(new Vector3(0, 0, -3), Vector3.Zero, Vector3.Up);
// Specify how 3D points are projected/transformed onto the 2D screen
basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45),
(float)GraphicsDevice.Viewport.Width / (float)GraphicsDevice.Viewport.Height, 1.0f, 100.0f);
// Tell BasicEffect to make use of your vertex colors
basicEffect.VertexColorEnabled = true;
// I'm setting this so that *both* sides of your triangle are drawn
// (so it won't be back-face culled if you move it, or the camera around behind it)
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
// Render with a BasicEffect that was created in LoadContent
// (BasicEffect only has one pass - but effects in general can have many rendering passes)
foreach(EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
// This is the all-important line that sets the effect, and all of its settings, on the graphics device
pass.Apply();
// Here's your code:
VertexPositionColor[] vertices = new VertexPositionColor[3];
vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
vertices[0].Color = Color.Red;
vertices[1].Position = new Vector3(0, 0.5f, 0f);
vertices[1].Color = Color.Green;
vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);
vertices[2].Color = Color.Yellow;
GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, vertices, 0, 1);
}
That exception (InvalidOperationException) is typically thrown when a component finds it's in an unexpected state. So in your case, make sure GraphicsDevice does not need some other properties set before you invoke DrawUserPrimitives.
Related
I drew a line parallel to the Z axis in sharpgl. I set the camera above the Z axis, so I thought I would see 1 point, but I don't see it. Later I want to draw a large number of these lines side by side, now I'm trying on one.
Camera settings:
gl.Ortho (0, 1595, 0, 1000, -30, 80);
gl.LookAt(0.0f, 0.0f, 90.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
Line Drawing:
gl.LineWidth (100.0f);
gl.Begin (OpenGL.GL_LINES);
gl.Color (0, 0, 0);
gl.Vertex (600, 600, 30);
gl.Vertex (600, 600, 50);
gl.End ();
gl.Flush ();
When I draw a line at a different angle, you can see it:
gl.Vertex (180, 15, 20);
gl.Vertex (800, 800, 20);
When I draw only a point, I also see it:
gl.PointSize (100.0f);
gl.Begin (OpenGL.GL_POINTS);
gl.Color (0, 0, 0);
gl.Vertex (800, 800, 20);
gl.End ();
I need to see the line. Do I need to add a point to the beginning and end of a line, or is it just something to set the line to be visible?
I have a class called Cube in which I render a basic cube to the screen. I've got it working as far as rendering a basic cube of any scale at origin 0, 0, 0. However, if I want to move the cube, scale any of its axes, or rotate the cube; I have no idea where to begin since I have to achieve this within a Vertex object.
struct Vertex {
Vector3 Position;
Vector3 Normal;
Vector2 TextureUV;
Color Color;
}
To render a cube of size 1000000 I simply fill the Position properties with that value (with regards to negative and positive values to generate the faces).
new Vertex(new Vector3(-1000000, -1000000, -1000000), new Vector3(-1, -1, -1), Color.White);
Cube has four properties:
Position (Vector3).
Scale (Vector3).
Rotation (Vector3).
Color (SharpDX.Color).
My question is, as I am specifying Vertex locations, how can I calculate the new position when rotation and position are applied?
I attempted basic subtraction and addition which simply scales the cube differently and the origin remains at 0, 0, 0 (please don't mind the shorthand).
new Vertex(new Vector3(P.X - S.X, P.Y - S.Y, P.Z - S.Z), new Vector3(1, 1, 1), Color.White);
The above isn't the exact code as it is on a different computer currently.
If you need additional details, please let me know and I will clarify.
The Question
How can I set the position of the vertices based on the supplied position, scale, and rotation vectors?
Test Case
The following test cases are for position only and are to show you how I will test the answers given.
// Renders at origin.
Cube myCube = new Cube(new Vector3(0, 0, 0), new Vector3(100, 100, 100), Color.White);
// Renders to the left.
Cube myCubeLeft = new Cube(new Vector3(-250, 0, 0), new Vector3(100, 100, 100), Color.White);
// Renders to the right.
Cube myCubeRight = new Cube(new Vector3(250, 0, 0), new Vector3(100, 100, 100), Color.White);
// Renders above.
Cube myCubeUp = new Cube(new Vector3(0, -250, 0), new Vector3(100, 100, 100), Color.White);
// Renders below.
Cube myCubeDown = new Cube(new Vector3(0, 250, 0), new Vector3(100, 100, 100), Color.White);
I will add rotation and scaling test cases later if needed.
I am drawing two wireframe spheres that I would like to follow the player around. When the player moves the two gizmos follow, however, when I rotate only one of the gizmos rotates.
The broken gizmo code looks like this, it should have an offset of 45:
void OnDrawGizmosSelected() {
Gizmos.color = new Color(1, 0, 0);
Gizmos.matrix = Matrix4x4.TRS(transform.position, Quaternion.Euler(transform.rotation.x, transform.rotation.y + 45, transform.rotation.z), Vector3.one);
Gizmos.DrawWireSphere(Vector3.zero, 5f);
}
For reference here is the whole block with both gizmos:
void OnDrawGizmosSelected() {
Gizmos.color = new Color(1, 0, 0);
// This one works
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
Gizmos.DrawWireSphere(Vector3.zero, 5f);
// This one does not work
Gizmos.matrix = Matrix4x4.TRS(transform.position, Quaternion.Euler(transform.rotation.x, transform.rotation.y + 45, transform.rotation.z), Vector3.one);
Gizmos.DrawWireSphere(Vector3.zero, 5f);
}
Default with no rotation (How I want it to stay when rotating)
Rotation around the Y Axis
Quaternion has 4 components, x,y,z and w.
Just putting x,y and z into Quaternion.Euler will not give you the expected results.
Instead, use transform.rotation.eulerAngles
void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1, 0, 0);
// This one works
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
Gizmos.DrawWireSphere(Vector3.zero, 5f);
// This one works now :)
Gizmos.matrix = Matrix4x4.TRS(transform.position, Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 45, transform.rotation.eulerAngles.z), Vector3.one);
Gizmos.DrawWireSphere(Vector3.zero, 5f);
}
EDIT:
Okay, that fixes the Y value, but X and Z are still broken. They move but not in the proper direction.
Then try
// This works even better
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one) * Matrix4x4.Rotate(Quaternion.Euler(0, 45, 0));
When I create particle effects, they all have the same pattern. They are rotated, but they all have the same pattern and same colored particles. See picture:
This is how a new ParticleEffect gets created:
ParticleEffect p = new ParticleEffect(textures, Vector2.Zero, destination, speed);
Where textures is aTexture2D list, VectorZero is the starting location, and so on.
Whenever a new ParticleEffect gets created it gets added to the ParticleList list, which later loops through all the items and calls update and draw for each effect inside.
Here is where the particles are randomised:
private Particle GenerateNewParticle()
{
Random random = new Random();
Texture2D texture = textures[random.Next(textures.Count)];
Vector2 position = EmitterLocation;
Vector2 velocity = new Vector2(
1f * (float)(random.NextDouble() * 2 - 1),
1f * (float)(random.NextDouble() * 2 - 1));
float angle = 0;
float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
Color color = new Color(
(float)random.NextDouble(),
(float)random.NextDouble(),
(float)random.NextDouble());
float size = (float)random.NextDouble();
int ttl = 20 + random.Next(40);
return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl);
}
A bunch of randoms there, but each effect still comes out the same.
Comment if you want to see more code.
Edit:
Here is how a particle gets drawn:
public void Draw(SpriteBatch spriteBatch)
{
Rectangle sourceRectangle = new Rectangle(0, 0, Texture.Width, Texture.Height);
Vector2 origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
spriteBatch.Draw(Texture, Position, sourceRectangle, Color,
Angle, origin, Size, SpriteEffects.None, 0f);
}
By default, Random instances are initiated with the current time as seed, that means the same sequence of numbers will be re-generated if you create instances at the same time - do not create new instances, reuse an existing instance to get more "randomized" behavior (in your case e.g. use a static Random instance).
I have a problem just with rendering a simple rotating cube with SlimDX (DirectX 11). Cube is rotating and these are the images i get:
I use this vertex shader:
float4x4 WorldViewProj : register(c0);
float4 VShader(float4 position : POSITION) : SV_POSITION
{
return mul(float4(position.xyz, 1.0), WorldViewProj);
}
And this is how i prepare WorldViewProj:
ProjectionMatrix = Matrix.PerspectiveFovLH((float)(Math.PI / 4.0), 1f, -1, 20);
ViewMatrix = Matrix.LookAtLH(new Vector3(0f, 0, ypos), new Vector3(0f, 0, 10), new Vector3(0f, 1f, 0f));
Matrix positionMatrix = Matrix.Translation(position);
Matrix rotationMatrix = Matrix.RotationYawPitchRoll(rotation.X, rotation.Y, rotation.Z);
Matrix scaleMatrix = Matrix.Scaling(scale);
Matrix worldMatrix = rotationMatrix * scaleMatrix * positionMatrix;
worldMatrix = Matrix.Transpose(worldMatrix);
Matrix WorldViewProj = ViewMatrix * ProjectionMatrix * worldMatrix;
First of all i saw this question: Incorrect Clipping and 3D Projection when using SlimDX
And, probably, there's something wrong with matrices (since i don't transpose neither view nor projection but transpose world - in opposite case nothing is rendered). But even if i set both view and projection matrices to identity matrix the "hole" is still there.
Please help and thanks in advance!
The problem is found. It was just a wrong order of vertices in an index buffer. Shame on me:) Thanks everybody for your help!