Stratos: Punto de Encuentro de Desarrolladores

¡Bienvenido a Stratos!

Acceder

Foros





Dibujar un cubo

Iniciado por DarkGDK, 17 de Junio de 2010, 10:45:39 PM

« anterior - próximo »

DarkGDK

Hola soy principiante en XNA, estoy con este código para dibujar un cubo pero no me compila:


1        public override void Draw(GameTime gameTime)
2        {
3            GraphicsDevice.RenderState.CullMode = CullMode.None;
4
5            GraphicsDevice.VertexDeclaration = new VertexDeclaration(GraphicsDevice,
6                VertexPositionColor.VertexElements);
7
8            basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
9                MathHelper.ToRadians(45.0f),
10                GraphicsDevice.Viewport.Width / GraphicsDevice.Viewport.Height,
11                1.0f, 10000.0f);
12
13            basicEffect.View = Matrix.CreateLookAt(new Vector3(0.0f, -50.0f, 1.0f),
14                Vector3.Zero, Vector3.Up);
15            
16            basicEffect.Begin();
17            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
18            {
19                pass.Begin();
20
21                DrawCube(Color.Red);
22
23                pass.End();
24            }
25            basicEffect.End();
26            
27            base.Draw(gameTime);
28        }
29
30        void DrawCube(Color color)
31        {
32            VertexPositionColor[ vertices = new VertexPositionColor[8];
33
34            vertices[0] = new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), color);
35            vertices[1] = new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), color);
36            vertices[2] = new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), color);
37            vertices[3] = new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), color);
38            vertices[4] = new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), color);
39            vertices[5] = new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), color);
40            vertices[6] = new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), color);
41            vertices[7] = new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), color);
42
43            short[ indexes = new short[14];
44
45            indexes[0] = 1;
46            indexes[0] = 2;
47            indexes[0] = 0;
48            indexes[0] = 3;
49            indexes[0] = 7;
50            indexes[0] = 6;
51            indexes[0] = 2;
52            indexes[0] = 5;
53            indexes[0] = 1;
54            indexes[0] = 4;
55            indexes[0] = 0;
56            indexes[0] = 7;
57            indexes[0] = 5;
58            indexes[0] = 6;
59
60            GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip,
61                vertices, 0, vertices.Length, indexes, 0, indexes.Length - 2);
62        }
63
64        protected override void LoadContent()
65        {
66            basicEffect = new BasicEffect(GraphicsDevice, null);
67
68            base.LoadContent();
69        }


Me dice que El nombre 'basicEffect' no existe en el contexto actual, entre otros errores.

Otra cosa, esto ¿está bien declarado? VertexPositionColor[ vertices = new VertexPositionColor[8];

El código lo he sacado de aquí.

¿Me echáis una mano?

Muchas gracias.

blau

Código (csharp) [Seleccionar]

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            basicEffect = new BasicEffect(GraphicsDevice, null);
            basicEffect.VertexColorEnabled = true;

            // TODO: use this.Content to load your game content here
        }


        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            GraphicsDevice.RenderState.CullMode = CullMode.None;

            GraphicsDevice.VertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionColor.VertexElements);
            basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
                         MathHelper.ToRadians(45.0f),
                         GraphicsDevice.Viewport.Width / GraphicsDevice.Viewport.Height,
                         1.0f, 10000.0f);

            basicEffect.View = Matrix.CreateLookAt(new Vector3(0.0f, -50.0f, 1.0f), Vector3.Zero, Vector3.Up);

            basicEffect.Begin();
            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Begin();

                DrawCube(Color.Red);

                pass.End();
            }
            basicEffect.End();

            base.Draw(gameTime);

        }

        void DrawCube(Color color)
        {
            VertexPositionColor[] vertices = new VertexPositionColor[8];

            vertices[0] = new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), color);
            vertices[1] = new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), color);
            vertices[2] = new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), color);
            vertices[3] = new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), color);
            vertices[4] = new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), color);
            vertices[5] = new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), color);
            vertices[6] = new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), color);
            vertices[7] = new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), color);

            short[] indexes = new short[] { 1, 2, 0, 3, 7, 6, 2, 5, 1, 4, 0, 7, 5 };

            GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip,
                vertices, 0, vertices.Length, indexes, 0, indexes.Length - 2);
        }


Makaimura

vaya, todo eso para un cubo mira que sois raros los programadores en Maya haces un cubo en 1 segundo  :D

blau

y el maya ese, funciona en la xbox?   :P

DarkGDK

Gracias blau, ahora me compila, pero no veo el cubo por ninguna parte cuando ejecuto.

¿Alguna sugerencia?

Saludos.

blau

Pues yo he copiado el codigo tal cual y funciona.

asi que como no sea algo de tu tarjeta grafica, al estilo de que no soporta shader 2.0 o que le sienta algo mal.  ???

Porque no pruebas alguno de los ejemplos de creators.xna.com

Hay ejemplos del nivel que quieras y muy variados.

DarkGDK

#6
Ahora sí que se dibuja el cubo, pero no lo veo rojo, lo veo blanco. Por mi tarjeta gráfica no hay problema pues tengo una ATI Radeon X1650.