XNA Game Development Forums
2012/05/18 05:55:33 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Need some *more* help please :-)  (Read 1215 times)
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« on: 2007/02/15 13:46:25 »

Hi again folks.

Firstly a big thanks to everyone who's helped me so far. 

My own game engine is now growing steadily thanks to the help and support I've received here.

I have a little issue I can't seem to get my head around and was hoping someone might be able to point me in the right direction.

I've recenty added Point Sprites to my engine so I can do laser bolts, explosions, smoke, etc.  It's working well, with the exception that the point sprites are *always* in front of everything else (although they do scale correctly).

I wanted to use this effect for engine exhaust flares on spaceships, but if I turn the ship to face the camera, you can see the point sprite "through" the ship....there must be a way to determine that the point sprite is obscured from the camera's view - but I haven't got a clue how to do it.

Anyone got any ideas?  Any help would be appreciated.

Here's my PointSprite Class:

#region Using Directives

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

using XNACore.Graphics;
using XNACore.Helpers;

#endregion



namespace XNACore.Graphics
{
    class XNAPointSprite
    {
        #region Structures

        private struct pointSpriteVertexFormat
        {
            private Vector3 position;
            private float pointSize;

            public pointSpriteVertexFormat(Vector3 position, float pointSize)
            {
                this.position = position;
                this.pointSize = pointSize;
            }

            public static VertexElement[] Elements =
             {
                 new VertexElement(0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0),
                 new VertexElement(0, sizeof(float)*3, VertexElementFormat.Single, VertexElementMethod.Default, VertexElementUsage.PointSize, 0),
             };

            public static int SizeInBytes = sizeof(float) * (3 + 1);
        }

        #endregion

        #region Variables

        private GraphicsDevice objDevice = null;
        private XNAViewPort objViewPort = null;
        private Effect objEffect;

        #endregion

        #region Constructor

        public XNAPointSprite(ref XNAGame XNAGame, ref XNAViewPort XNAViewPort)
        {
            objDevice = XNAGame.Device;
            objViewPort = XNAViewPort;
            objEffect = XNAGame.Content.Load<Effect>(FolderHelper.ShadersFolder() + "PointSprite");
        }

        #endregion

        #region Public Methods

        public void RenderSprite(Texture2D texture, Vector3 position, float scale)
        {
            pointSpriteVertexFormat[] objPoint = new pointSpriteVertexFormat[1];

            objPoint[0] = new pointSpriteVertexFormat(position, scale);

            objEffect.CurrentTechnique = objEffect.Techniques["PointSprites"];
            Matrix worldMatrix = Matrix.Identity;
            objEffect.Parameters["xWorld"].SetValue(worldMatrix);
            objEffect.Parameters["xView"].SetValue(objViewPort.MatrixView);
            objEffect.Parameters["xProjection"].SetValue(objViewPort.MatrixProjection);
            objEffect.Parameters["xTexture"].SetValue(texture);

            objDevice.RenderState.AlphaBlendEnable = true;
            objDevice.RenderState.SourceBlend = Blend.One;
            objDevice.RenderState.DestinationBlend = Blend.One;

            objEffect.Begin();
            foreach (EffectPass pass in objEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                objDevice.VertexDeclaration = new VertexDeclaration(objDevice, pointSpriteVertexFormat.Elements);
                objDevice.DrawUserPrimitives(PrimitiveType.PointList, objPoint, 0, 1);
                pass.End();
            }
            objEffect.End();

            objDevice.RenderState.AlphaBlendEnable = false;               
        }


        #endregion



    }
}


and here's the shader I'm using:

//------- Technique: PointSprites --------

struct SpritesVertexToPixel
{
    float4 Position      : POSITION;
    float4 Color       : COLOR0;
    float1 Size       : PSIZE;
};

SpritesVertexToPixel PointSpritesVS (float4 Position : POSITION, float4 Color : COLOR0, float1 Size : PSIZE)
{
    SpritesVertexToPixel Output = (SpritesVertexToPixel)0;
     
    float4x4 preViewProjection = mul (xView, xProjection);
   float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);
    Output.Position = mul(Position, preWorldViewProjection);   
    Output.Color = Color;
    Output.Size = 1/(pow(Output.Position.z,2)+1   ) * Size;
   
    return Output;   
}

PixelToFrame PointSpritesPS(SpritesVertexToPixel PSIn, float2 TexCoords  : TEXCOORD0)
{
    PixelToFrame Output = (PixelToFrame)0;

    Output.Color = tex2D(TextureSampler, TexCoords);
   
    return Output;
}

technique PointSprites
{
   pass Pass0
    {   
       PointSpriteEnable = true;
       VertexShader = compile vs_1_1 PointSpritesVS();
        PixelShader  = compile ps_1_1 PointSpritesPS();
    }
}
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #1 on: 2007/02/15 14:06:24 »

DaphydTheBard,

How are your shaders coming along?

You could try setting the depth buffering in the shader, do this by setting ZEnable to true after the PointSprieEnable. You may need to set the ZBufferWriteEnable also, depending on your renderstate.

Let me know if this helps.
Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #2 on: 2007/02/15 14:18:39 »

HI Nemo

I must be half asleep :-)

The point sprites have been working all along - it was down to the SCALE of my models in the scene, and their placement....basically the sprite WAS in front of the ship but I hadn't look at my vector coordinates properly... Grin

so panic over....this is actually working.

Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.143 seconds with 18 queries.