8 Replies - 1214 Views - Last Post: 20 April 2013 - 03:22 PM

#1 VillageRaper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Alpha Channel Viewing Problem

Posted 18 April 2013 - 03:25 PM

Posted Image

I have a problem with Alpha Channel settings for my model in XNA 4.0.
Maybe someone could help me?
Is This A Good Question/Topic? 0
  • +

Replies To: Alpha Channel Viewing Problem

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Alpha Channel Viewing Problem

Posted 18 April 2013 - 03:27 PM

Without seeing the code that handles drawing the model, it's going to be very difficult to diagnose the issue.
Was This Post Helpful? 0
  • +
  • -

#3 VillageRaper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Re: Alpha Channel Viewing Problem

Posted 18 April 2013 - 03:37 PM

So far I've written this:

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;

namespace Aplikacja3D2
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Model rogatywka;
        Vector3 modelPosition = Vector3.Zero;
        Vector3 cameraPosition = new Vector3(500f, 250.0f, -350.0f);
        float modelRotation = 0.0f;
        float aspectRatio;

        MouseState state;
      //  int hold;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";


           // hold = 0;
        }
        protected override void Initialize()
        {

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            rogatywka = Content.Load<Model>("Models\\fiat");

            aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
            this.graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width;
            this.graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height;
            this.graphics.IsFullScreen = true;
            this.graphics.ApplyChanges();
            
        }
        protected override void UnloadContent()
        {
        }
       protected override void Update(GameTime gameTime)
{
    if (Keyboard.GetState().IsKeyDown(Keys.Escape))
        this.Exit();

    if (Mouse.GetState().ScrollWheelValue > state.ScrollWheelValue)
    {
        cameraPosition += new Vector3(0.0f, -5.0f, 0.0f);
    }
    if (Mouse.GetState().ScrollWheelValue < state.ScrollWheelValue)
    {
        cameraPosition += new Vector3(0.0f, 5.0f, 0.0f);
    }

    if (Keyboard.GetState().IsKeyDown(Keys.A))
        this.modelPosition += new Vector3(0.0f, 0.0f, 5.0f);
    if (Keyboard.GetState().IsKeyDown(Keys.D))
        this.modelPosition += new Vector3(0.0f, 0.0f, -5.0f);   


    if (Keyboard.GetState().IsKeyDown(Keys.Down))
        this.cameraPosition += new Vector3(5.0f, 0.0f, 0.0f);
    if (Keyboard.GetState().IsKeyDown(Keys.Up))
        this.cameraPosition += new Vector3(-5.0f, 0.0f, 0.0f);

           /// modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds *
           /// MathHelper.ToRadians(0.1f);
           /// 
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
    
    GraphicsDevice.BlendState = BlendState.Opaque;
   
    GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
    GraphicsDevice.DepthStencilState = DepthStencilState.Default;
    GraphicsDevice.BlendState = BlendState.Additive;
    GraphicsDevice.BlendState = BlendState.AlphaBlend;

            Matrix[] transforms = new Matrix[rogatywka.Bones.Count];
            rogatywka.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in rogatywka.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();

                   // effect.Alpha = 1f;
                   // effect.FogEnabled = true;
                    effect.TextureEnabled = true;
                    effect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation) * Matrix.CreateTranslation(modelPosition);
                    effect.View = Matrix.CreateLookAt(cameraPosition, modelPosition, Vector3.Up);
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio,1.0f, 10000.0f);   
                }
                
                mesh.Draw();
            }

            state = Mouse.GetState();

            base.Update(gameTime);

        }
    }
}


This post has been edited by Kilorn: 18 April 2013 - 04:14 PM
Reason for edit:: Added code tags.

Was This Post Helpful? 0
  • +
  • -

#4 VillageRaper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Re: Alpha Channel Viewing Problem

Posted 19 April 2013 - 04:54 AM

I believe that normally model should be shown behind textures cut in alpha channel, right?
Was This Post Helpful? 0
  • +
  • -

#5 VillageRaper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Re: Alpha Channel Viewing Problem

Posted 19 April 2013 - 05:17 PM

When I change this:
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.BlendState = BlendState.Additive;
GraphicsDevice.BlendState = BlendState.AlphaBlend;

into this:
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
GraphicsDevice.BlendState = BlendState.AlphaBlend;);

I've got this
Posted Image
Was This Post Helpful? 0
  • +
  • -

#6 VillageRaper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Re: Alpha Channel Viewing Problem

Posted 19 April 2013 - 05:32 PM

I'm trying to achieve this in XNA 4.0:
Posted Image
Was This Post Helpful? 0
  • +
  • -

#7 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 535
  • View blog
  • Posts: 1,188
  • Joined: 24-April 12

Re: Alpha Channel Viewing Problem

Posted 20 April 2013 - 10:31 AM

I don't think this has anything to do with the alpha channel. I think it's more about the drawing order. I actually learned about this when I created this type of problem on my own models by drawing fonts on the screen. That resets some of the graphical settings and causes this type of problem. But it basically boils down to having the settings right. Here's the post where I talk about correcting it (actually I've posted this a couple times but can't find the others):


http://www.dreaminco...ing-when-drawn/

I think it's actually the depth stencil state that's causing that. It's interpreting the depth of the polygons incorrectly and drawing them in the wrong order.

This post has been edited by BBeck: 20 April 2013 - 10:33 AM

Was This Post Helpful? 0
  • +
  • -

#8 VillageRaper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Re: Alpha Channel Viewing Problem

Posted 20 April 2013 - 02:36 PM

BBeck why I've got this
http://imageshack.us...phablending.jpg

instead of this?
http://img854.images...6/problemlr.jpg

I need help.
Here is my current 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;

namespace Aplikacja3D2
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Model model;
        Vector3 modelPosition = Vector3.Zero;
        Vector3 cameraPosition = new Vector3(500f, 250.0f, -350.0f);
        float modelRotation = 0.0f;
        float aspectRatio;
        MouseState state;
        int fullscreen;
        int windowed;
        //  int hold;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            fullscreen = 1;
            windowed = 0;
            // hold = 0;
        }
        protected override void Initialize()
        {
            base.Initialize();

            spriteBatch = new SpriteBatch(GraphicsDevice);

            model = Content.Load<Model>("Models\\fiat");

            this.graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width;
            this.graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height;
            this.graphics.IsFullScreen = true;
            this.graphics.ApplyChanges();
            aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
            window.Title = "Aplikacja 3D";
        }
        protected override void LoadContent()
        {
        }
        protected override void UnloadContent()
        {
        }
        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            if (Mouse.GetState().ScrollWheelValue > state.ScrollWheelValue)
            {
                cameraPosition += new Vector3(0.0f, -5.0f, 0.0f);
            }
            if (Mouse.GetState().ScrollWheelValue < state.ScrollWheelValue)
            {
                cameraPosition += new Vector3(0.0f, 5.0f, 0.0f);
            }

            if (Keyboard.GetState().IsKeyDown(Keys.A))
                this.modelPosition += new Vector3(0.0f, 0.0f, 5.0f);
            if (Keyboard.GetState().IsKeyDown(Keys.D))
                this.modelPosition += new Vector3(0.0f, 0.0f, -5.0f);

            if (Keyboard.GetState().IsKeyDown(Keys.Down))
                this.cameraPosition += new Vector3(5.0f, 0.0f, 0.0f);
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
                this.cameraPosition += new Vector3(-5.0f, 0.0f, 0.0f);

            if ((Keyboard.GetState().IsKeyDown(Keys.W)) && (windowed == 0))
            {
                this.graphics.IsFullScreen = false;
                this.graphics.PreferredBackBufferWidth = 800;
                this.graphics.PreferredBackBufferHeight = 600;
                this.graphics.ApplyChanges();
                aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
                fullscreen = 0;
                windowed = 1;
            }
            if ((Keyboard.GetState().IsKeyDown(Keys.F)) && (fullscreen == 0))
            {
                this.graphics.IsFullScreen = true;
                this.graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width;
                this.graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height;
                this.graphics.ApplyChanges();
                aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
                fullscreen = 1;
                windowed = 0;
            }
            GraphicsDevice.Clear(Color.CornflowerBlue);
            GraphicsDevice.BlendState = BlendState.AlphaBlend;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
           
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                    effect.TextureEnabled = true;
                    effect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation) * Matrix.CreateTranslation(modelPosition);
                    effect.View = Matrix.CreateLookAt(cameraPosition, modelPosition, Vector3.Up);
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f);
                }

                mesh.Draw();
            }

            state = Mouse.GetState();

            base.Update(gameTime);

        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#9 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 535
  • View blog
  • Posts: 1,188
  • Joined: 24-April 12

Re: Alpha Channel Viewing Problem

Posted 20 April 2013 - 03:22 PM

Ok. Now that is an alpha problem. :-)

And honestly I'm not sure what to do about it. I had a similar problem when I tried to make trees in my terrain program. Because I was using two crossed bitmapped images of a tree, you had an alpha image over an alpha image. And it would do basically what you are seeing there.

I'm not sure I ever really understood the problem fully or how to fix it. I did fix it in my terrain example, but I always felt like I had the wrong solution even though it worked. It seems to me that part of my solution was drawing it twice in two different ways. I couldn't tell you much more then that.

The code for my terrain program is here if you care to download it.

Sorry I can't be of much more help than that.

This post has been edited by BBeck: 20 April 2013 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1