Draw primitives

Status
Niet open voor verdere reacties.

wicherh

Gebruiker
Lid geworden
3 dec 2008
Berichten
188
Ik ben bezig met een heel simpele 3d engine aan het maken, maar mijn code drawt niks. Terwijl hij wel iets zou moeten drawen.
Code:
Code:
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace FPSengine
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GraphicsDevice device;
        BasicEffect effect;
        VertexPositionTexture[] vertices;
        int[] indices;

        Matrix viewMatrix;
        Matrix projectionMatrix;

        Texture2D texture;

        VertexBuffer vertexBuffer;
        IndexBuffer indexBuffer;

        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 = "BasicFPS";
            effect = new BasicEffect(GraphicsDevice, null);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            device = graphics.GraphicsDevice;
            spriteBatch = new SpriteBatch(GraphicsDevice);
            SetUpVertices();
            SetUpCamera();
            texture = Content.Load<Texture2D>("Brick");
            SetUpIndices();
        }

        private void SetUpVertices()
        {
          
            vertices = new VertexPositionTexture[4];

            vertices[0] = new VertexPositionTexture(new Vector3(0f, 0f, 0f), new Vector2(0, 0));
            vertices[1] = new VertexPositionTexture(new Vector3(5f, 0f, 0f), new Vector2(1, 0));
            vertices[2] = new VertexPositionTexture(new Vector3(0f, 0f, -5f), new Vector2(0, 1));
            vertices[3] = new VertexPositionTexture(new Vector3(5f, 0f, -5f), new Vector2(1, 1));

            vertexBuffer = new VertexBuffer(device, 4 * VertexPositionTexture.SizeInBytes, BufferUsage.WriteOnly);
            vertexBuffer.SetData<VertexPositionTexture>(vertices);


        }

        private void SetUpIndices()
        {
            indices = new int[6];

            indices[0] = 0;
            indices[1] = 2;
            indices[2] = 1;
            indices[3] = 1;
            indices[4] = 2;
            indices[5] = 3;
            indexBuffer = new IndexBuffer(device, 6, BufferUsage.WriteOnly, IndexElementSize.ThirtyTwoBits);
        }

        private void SetUpCamera()
        {
            viewMatrix = Matrix.CreateLookAt(new Vector3(0, 50, 0), new Vector3(0, 0, 0), new Vector3(0, 0, -1));
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.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)
                this.Exit();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            device.Clear(Color.DarkSlateBlue);

            effect.View = viewMatrix;
            effect.Projection = projectionMatrix;
            effect.World = Matrix.Identity;
            effect.Texture = texture;
            effect.TextureEnabled = true;
            device.VertexDeclaration = new VertexDeclaration(device, VertexPositionTexture.VertexElements);
            device.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionTexture.SizeInBytes);
            device.Indices = indexBuffer;
            effect.Begin();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();
                device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 1, 0, 4, 0, 4);
                pass.End();
            }
            effect.End();

            base.Draw(gameTime);
        }
    }
}
 
om te tekenen heb je een spriteBatch nodig, die heb je ook wel, maar je moet'm ook gebruiken; probeer eens in je Draw methode in de foreach lus
[CPP]spriteBatch.Begin()
//hier komt je eigenlijke Draw code
...
spriteBatch.End()[/CPP].
 
Dat is alleen voor 2D maar 3D stuff gebruik je geen spritebatch.

Maar heb het al opgelost
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan