Good day everyone.
I hope you can help me with my question.
Is there a way to transform or directly render sprite like so?
Without using 3d. I know it can be easily done in 3d, but the project I'm working on doesn't use 3d at all, so I don't really want to also include 3d just because of that small thing...
(example image is from some random game)
So basically what I need:
Take a sprite as a rectangle, and then transform it in a free way, meaning that I can set points of that sprite to any coorditates, not just as rectangle.
Thanks in advance.
The technique used in that image is raycasting 2d. that was used first time with wolfenstein 3d, and implements a fake 3d over 2D.
here you can find a tutotial http://lodev.org/cgtutor/
though this is not what you want.
the best way to achieve what you want is define two triangles and use GraphicsDevice.DrawUserPrimitives with a basiceffect to draw it.
// Init Triangles with four points A,B,C and D
VertexPOsitionTexture[] Vertex = new VertexPositionTexture[6];
Vertex[0].Position = (A.X,A.Y,0);
Vertex[1].Position = (B.X,B.Y,0);
Vertex[2].Position = (C.X,C.Y,0);
Vertex[3].Position = (C.X,C.Y,0);
Vertex[4].Position = (B.X,B.Y,0);
Vertex[5].Position = (D.X,D.Y,0);
Vertex[0].Texture= (0,0);
Vertex[1].Texture= (1,0);
Vertex[2].Texture= (0,1);
Vertex[3].Texture= (0,1);
Vertex[4].Texture= (1,0);
Vertex[5].Texture= (1,1);
// Init Effect from http://blogs.msdn.com/b/shawnhar/archive/2010/04/05/spritebatch-and-custom-shaders-in-xna-game-studio-4-0.aspx
Matrix projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
BasicEffect effect = new BasicEffect();
effect.Texture = your texture;
effect.TextureEnabled = true;
effect.World = Matrix.Identity;
effect.View = Matrix.Identity;
effect.Projection = halfPixelOffset * projection;
// Draw Triangles
effect.Apply():
GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(vertex, TriangleList, ...,...);
this code have to be understand as pseudocode, it's not tested but it shows the relevant actions to be done.
Related
I'm developing a 3D application in OpenGL and I want to rotate the objects with the mouse like in AutoCad, SolidEdge or another CAD program. By the way, I'm totally new in 3D programing and OpenGl.
I've tried with GL.Rotate but as far as I know its complicated and not very attratctive for the user because one rotation around one axis affect the others. So I've been searching and I found the quaternions. I thought I understood how they work but when I run the app the GLControl that Im using is totally white. Also In the debugging mode I've seen that the Matrix "Mat" is full with NaN and I think that that's the problem. The AngX and AngY are the angles of rotation that I change in the mousemove
This is what I've done:
The main loop:
Mat = Matrix4d.Identity; //Global
Vector3d vect1 = new Vector3d(0, -1, 0);
Vector3d vect2 = new Vector3d(-1, 0, 0);
QuatRot1 = new Quaterniond(vect1, AngX);//Global
QuatRot2 = new Quaterniond(vect2, AngY);//Global
QuatRot1.Normalize();
QuatRot2.Normalize();
Quaterniond.Multiply(QuatAcc, QuatRot1);
Quaterniond.Multiply(QuatAcc, QuatRot2);
Matrix4d.CreateFromQuaternion(ref QuatAcc, out Mat);// I think the problem is here
AngX = 0;
AngY = 0;
GL.MultMatrix(ref Mat);
The mousemove:
if (WheelPress)
{ //mouse1 previous mouse position
//mouse0 current mouse position
AngY = 0.1f * (mouse1.X - mouse0.X);
AngX = 0.1f * (mouse1.Y - mouse0.Y);
Cursor.Current = Cursors.Hand;
glControl1.Refresh();
}
PS.I don't know if it's important but im working with OpenTK
I just got a problem with drawing two 3D cubes standing in line. In my code, I made a cube class, and in the game1 class, I built two cubes, A on the right side, B on the left side. I also setup an FPS camera in the 3D world. The problem is if I draw cube B first(Blue), and move the camera to the left side to cube B, A(Red) is still standing in front of B, which is apparently wrong.
I guess some pics can make much sense.
Then, I move the camera to the other side, the situation is like:
This is wrong....
From this view, the red cube, A should be behind the blue one, B....
Could somebody give me help please?
This is the draw in the Cube class
Matrix center = Matrix.CreateTranslation(
new Vector3(-0.5f, -0.5f, -0.5f));
Matrix scale = Matrix.CreateScale(0.5f);
Matrix translate = Matrix.CreateTranslation(location);
effect.World = center * scale * translate;
effect.View = camera.View;
effect.Projection = camera.Projection;
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.SetVertexBuffer(cubeBuffer);
RasterizerState rs = new RasterizerState();
rs.CullMode = CullMode.None;
rs.FillMode = FillMode.Solid;
device.RasterizerState = rs;
device.DrawPrimitives(
PrimitiveType.TriangleList,
0,
cubeBuffer.VertexCount / 3);
}
This is the Draw method in game1
A.Draw(camera, effect); B.Draw(camera, effect);
It seems like your depth buffer does not work. Have you turned it off? Make sure to use a correct DepthStencilState and that you have a bound depth buffer (both should be set up correctly by default).
Another possible reason is the effect not returning reasonable depth information. However, this seems unlikely.
I have a project due very soon and I'm having lots of issues trying to load a model I made in 3D Studio Max. The model I made is what I want as my terrain in my XNA game. Here is what I've done so far:
Methods:
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Model terrainModel;
Vector3 terrainPosition = Vector3.Zero;
Vector3 cameraPosition = new Vector3(0.0f, 60.0f, 160.0f);
Vector3 cameraLookAt = new Vector3(0.0f, 60.0f, 160.0f);
Matrix cameraProjectionMatrix;
Matrix cameraViewMatrix;
LoadContent()
spriteBatch = new SpriteBatch(GraphicsDevice);
cameraViewMatrix = Matrix.CreateLookAt(
cameraPosition,
Vector3.Zero,
Vector3.Up);
cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(80.0f),
graphics.GraphicsDevice.Viewport.AspectRatio,
1.0f,
1000.0f);
terrainModel = Content.Load<Model>("alphastreet");
Draw(GameTime gameTime)
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawModel(terrainModel, terrainPosition);
// TODO: Add your drawing code here
base.Draw(gameTime);
And then I want to Draw:
void DrawModel(Model model, Vector3 modelPosition)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
effect.World = Matrix.CreateTranslation(modelPosition);
effect.Projection = cameraProjectionMatrix;
effect.View = cameraViewMatrix;
}
mesh.Draw();
}
}
Everything else is just as an XNA file should look. The model it's self looks like a fairly straightforward street. However, upon XNA loading the model, it's just a big block in the window that loads.
I don't know what I'm doing that's making the model load like this, but it's making me pull my hair out. Any help would be appreciated.
Also, would anybody be able to direct me to walk-through/guide or a class that creates a First Person Shooter camera, since that is the game I'm going. I probably shouldn't have picked that, but it's way too late to change now, so if you could help there too you would really be saving my skin!
Thanks, Jake!
Is there more than one mesh in the model, and if so, have you tried rendering each one separately?
Have you tried using identity matrices for world and view to make sure these aren't the issue?
I assume you haven't made any other state changes to xna/directx, if so try disabling these.
For a FPS camera, you can just track the x and y rotation in float variables and update these each frame based upon how far the mouse has moved in x and y. From these you can generate a camera view matrix; something like the following:
var mx = Matrix.CreateRotationX(xangle);
var my = Matrix.CreateRotationY(yangle);
var pos = Matrix.CreateTranslation(position);
var view = mx * my * pos; // These might be in the wrong order, not sure off the top of my head
view.Invert();
The camera is always inverted because moving left moves the world right, turning left turns the world right etc.
I am trying to set up an extremely simple XNA game with a 3d terrain and some 2d GUI objects on top. I chose Nuclex since that seems to be one of the few 2d GUIs that's currently active.
My problem: adding a Nuclex "screen" class to my game results in the 3d terrain and lines drawn on top of it to screw up -- I have three layers of 3d objects: terrain, a wireframe outline of the terrain and some "routes" that hug the terrain. With the screen added, the routes that are partially-submerged into the terrain appear entirely on top of the terrain, and the wireframe grid appears thicker.
This is the tail-end of my Game.Initialize method, as made by following the sample on the Nuclex GuiManager page:
InputManager im = new InputManager();
IGraphicsDeviceService igds = Nuclex.Graphics.GraphicsDeviceServiceHelper.MakeDummyGraphicsDeviceService(GraphicsDevice);
gui = new GuiManager(igds, im);
gui.Initialize();
Viewport vp = GraphicsDevice.Viewport;
Screen main_screen = new Screen(vp.Width, vp.Height);
//main_screen.
this.gui.Screen = main_screen;
main_screen.Desktop.Bounds = new UniRectangle(
new UniScalar(0.1f, 0.0f), new UniScalar(0.1f, 0.0f), // x and y
new UniScalar(0.8f, 0.0f), new UniScalar(0.8f, 0.0f) // width and height
);
LabelControl text = new LabelControl("hello");
text.Bounds = new UniRectangle(10, 10, 200, 30);
main_screen.Desktop.Children.Add(text);
Components.Add(gui);
base.Initialize();
So, any ideas as to what I'm doing wrong?
Thanks!
Right, so thanks to catflier who pointed me in the direction of render states, I fixed the problem:
* First, "RenderState" is an XNA3 term; XNA4 simply allows a few state flags directly on the GraphicsDevice object.
* The problem is that Nuclex is secretly setting the GraphicsDevice.DepthStencilState and leaving it instead of restoring it to its previous value after use (naughty!!).
* So the solution is, at the top of your Draw method, to add a line like so:
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
Hello I'm trying to make a terrain engine similar to that of Minecraft.
I was able to get a chunk loaded. It is very laggy and when there is more than one chunk loaded at once it becomes unplayable.
This is my render code:
public static void renderNormalBlock(GraphicsDevice g, Chunk chunk,Texture2D texture, int x, int y, int z)
{
float tileSize = 0.5F;
Vector3 blockPosition = new Vector3(x / tileSize, y / tileSize, z / tileSize);
Model blockModel = Main.defaultBlockModel;
ModelMesh mesh = blockModel.Meshes[0];
g.SamplerStates[0] = SamplerState.PointWrap;
BasicEffect effect = (BasicEffect)mesh.Effects[0];
effect.TextureEnabled = true;
effect.Texture = texture;
effect.View = Main.camera;
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), g.DisplayMode.AspectRatio, 1, 128);
effect.World = Matrix.CreateWorld(blockPosition, Vector3.Forward, Vector3.Up);
mesh.Draw();
}
As You can see I am not using for-each or for loops because as it's only a cube; It is not required.
I did some research and the best answer I found was that I need to hide the cube's faces that are not visible. So say if there's 2 cubes next to each other, I don't want to render the in between faces.
This is where I get stuck, Most people are using cubes that were drawn in XNA, and I'm using a model.
I'm new to XNA and I don't understand too much of the Math involved in manually drawing a cube since I'm currently in grade 9, so I used a model.
So how would I go about rendering only the faces that are visible?
your starting to develop a game before learning the basics. you wont get too far this way. First grab a book about XNA development and go through it. This is a basic subject that will be covered there. In addition, go visit techCraft http://techcraft.codeplex.com/ and download their implementation which comes with all the code. you will learn alot from that alone.