(XNA) possible to change 3D model textures during gameplay?

Started by
11 comments, last by cNoob 15 years, 8 months ago
Quote:Original post by soarah
Ok so now your more or less saying you cant change the texture of a 3D model while your playing the game, because once you call your drawModel method that's it drawn and you cant go back and change things about it during run time.


For that frame, sure. Once you've drawn the model, changing textures isn't going to do jack shit unless you draw it again.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Advertisement
Quote:Original post by superpig
Quote:Original post by soarah
Ok so now your more or less saying you cant change the texture of a 3D model while your playing the game, because once you call your drawModel method that's it drawn and you cant go back and change things about it during run time.


For that frame, sure. Once you've drawn the model, changing textures isn't going to do jack shit unless you draw it again.


question answered; thanks. Now back to the drawing board.
You MUST render the model again in order to see any changes such as switching textures.

using System;using System.Collections.Generic;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.Net;using Microsoft.Xna.Framework.Storage;namespace SwitchTexture{    public class SwitchTextureGame : Microsoft.Xna.Framework.Game    {        GraphicsDeviceManager graphics;        // Create an array for holding 2 textures which we will switch between        Texture2D[] textures = new Texture2D[2];        // The index of the texture to use        int currentTexture = 0;        // The model we will be rendering with the current texture        Model model = null;        public SwitchTextureGame()        {            graphics = new GraphicsDeviceManager(this);            Content.RootDirectory = "Content";        }        protected override void LoadContent()        {            // Load both textures            textures[0] = Content.Load<Texture2D>("textures\\gold");            textures[1] = Content.Load<Texture2D>("textures\\silver");            // And the model we want to use            model = Content.Load<Model>("models\\sphere");        }        protected override void Update(GameTime gameTime)        {            // Allow switching the texture            if (Keyboard.GetState().IsKeyDown(Keys.Space))                currentTexture = 1; // SPACE DOWN: use silver texture            else                currentTexture = 0; // SPACE UP: use gold texture            base.Update(gameTime);        }        protected override void Draw(GameTime gameTime)        {            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);            // Draw the model            foreach (ModelMesh mesh in model.Meshes)            {                foreach (BasicEffect effect in mesh.Effects)                {                    // Make sure lighting and texturing is enabled so we can see the result                    effect.EnableDefaultLighting();                    effect.TextureEnabled = true;                    // Begin to render with the specified texture                    effect.Texture = textures[currentTexture];                                        effect.World = Matrix.Identity;                    effect.View = Matrix.CreateLookAt(new Vector3(0.0f, 2.0f, 5.0f), Vector3.Zero, Vector3.Up);                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 800.0f / 600.0f, 1.0f, 100.0f);                }                // Draw the mesh                mesh.Draw();            }            base.Draw(gameTime);        }    }    public class Application    {        public static void Main(string[] args)        {            SwitchTextureGame game = new SwitchTextureGame();            game.Run();        }    }}

This topic is closed to new replies.

Advertisement