CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2010
    Posts
    10

    DirectX Basic Triangle Transparency

    Hi. Im new to direct x, i only need simple functionality for my project. it will just be a load of cubes in an n*n*n grid where each block can be colorized and have its transparency altered. Im haveing a problem setting transparency which is to me more important that color. Hear is my test application. I create a cube but in the "device.DrawUserPrimitives(PrimitiveType.TriangleList,(vir.Length/3)-6,vir);" i only show half the sides so that i can test transparency.
    I have looked hard and long to find the solution but with no joy. If anyone can help i would be a very happy man. the color is applied in the initVertecies() method and the scene is in draw() Method. Thankyou.

    Code:
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using Microsoft.DirectX.Direct3D;
    using Microsoft.DirectX;
    
    using Microsoft.DirectX.PrivateImplementationDetails;
    
    namespace DirectX_9_Test
    {
    
    	public partial class ViewPort : UserControl
    	{
    		private Point_3 CamPos=new Point_3();
    		Collection<Point_3> p3VerteciesList;
    		public bool isDeviceReady=false;
    		public Microsoft.DirectX.Direct3D.Device device;
    		CustomVertex.PositionColored[] vir;
    		public ViewPort()
    		{
    			//
    			// The InitializeComponent() call is required for Windows Forms designer support.
    			//
    			InitializeComponent();
    			InitDirectX();
    			
    			//
    			// TODO: Add constructor code after the InitializeComponent() call.
    			//
    		}
    		
    		private void InitDirectX()
    		{
    			PresentParameters presentParams=new PresentParameters();
    			presentParams.Windowed=true;
    			presentParams.SwapEffect=SwapEffect.Discard;
    			device=new Microsoft.DirectX.Direct3D.Device(0,Microsoft.DirectX.Direct3D.DeviceType.Hardware,this,CreateFlags.HardwareVertexProcessing,presentParams);
    			device.RenderState.SourceBlend = Blend.SourceAlpha;
    			device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
    		}
    		
    		public void initCamra()
    		{
    			device.Transform.Projection=Matrix.PerspectiveFovLH((float)(Math.PI/4),(float)(Width/Height),1f,500f);
    			device.Transform.View=Matrix.LookAtLH(new Vector3((float)CamPos.x,(float)CamPos.y,(float)CamPos.z),new Vector3(),new Vector3(0f,1f,0f));
    			device.RenderState.Lighting=false;
    			device.RenderState.CullMode=Cull.None;
    		}
    		private void initVertecies()
    		{
    			p3VerteciesList=Block.StandardBlock();
    			vir=new CustomVertex.PositionColored[p3VerteciesList.Count];
    			int n=0;
    			foreach(Point_3 p in p3VerteciesList)
    			{
    				vir[n].Position=new  Vector3((float)p.x*50,(float)p.y*50,(float)p.z*50);
    				vir[n].Color=(new ColorValue(255,2,0,255/2).ToArgb());
    				n++;
    			}
    		}
    		private void Draw()
    		{
    			device.VertexFormat=CustomVertex.PositionColored.Format;
    			device.DrawUserPrimitives(PrimitiveType.TriangleList,(vir.Length/3)-6,vir);
    			
    		}
    		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    		{
    			initVertecies();
    			initCamra();
    			device.Clear(ClearFlags.Target,Color.White,1.0f,0);
    			device.BeginScene();
    			Draw();
    			device.EndScene();
    			device.Present();
    		}
    		
    		public void ViewPortKeyPress(object sender, KeyPressEventArgs e)
    		{
    			char c=e.KeyChar;
    			switch(c)
    			{
    				case ' ':
    					CamPos.y++;
    					this.Invalidate();
    					break;
    				case 'x':
    					CamPos.y--;
    					this.Invalidate();
    					break;
    				case 'w':
    					CamPos.z++;
    					this.Invalidate();
    					break;
    				case 's':
    					CamPos.z--;
    					this.Invalidate();
    					break;
    				case 'a':
    					CamPos.x++;
    					this.Invalidate();
    					break;
    					
    				case 'd':
    					CamPos.x--;
    					this.Invalidate();
    					break;
    			}
    			
    		}
    	}
    	
    	public class Block
    	{
    		public static Collection<Point_3> StandardBlock()
    		{Point_3 a,b,c,d,e,f,g,h;
    			a=new Point_3(-0.5f,0.5f,-0.5f);
    			b=new Point_3(0.5f,0.5f,-0.5f);
    			c=new Point_3(0.5f,0.5f,0.5f);
    			d=new Point_3(-0.5f,0.5f,0.5f);
    			e=new Point_3(0.5f,-0.5f,-0.5f);
    			f=new Point_3(-0.5f,-0.5f,-0.5f);
    			g=new Point_3(0.5f,-0.5f,0.5f);
    			h=new Point_3(-0.5f,-0.5f,0.5f);
    			Collection<Point_3> block=new Collection<Point_3>();
    			block.Add(d);block.Add(g);block.Add(h);
    			block.Add(d);block.Add(c);block.Add(g);
    			block.Add(c);block.Add(e);block.Add(g);
    			block.Add(c);block.Add(b);block.Add(e);
    			block.Add(b);block.Add(f);block.Add(e);
    			block.Add(b);block.Add(a);block.Add(f);
    			block.Add(a);block.Add(h);block.Add(f);
    			block.Add(a);block.Add(d);block.Add(h);
    			block.Add(a);block.Add(c);block.Add(d);
    			block.Add(a);block.Add(b);block.Add(c);
    			block.Add(h);block.Add(e);block.Add(f);
    			block.Add(h);block.Add(g);block.Add(e);
    			return block;
    		}
    	}
    	
    	public class Point_3
    	{
    		public double x=0,y=0,z=0;
    		public Point_3()
    		{
    			
    		}
    		public Point_3(double x,double y,double z)
    		{
    			this.x=x;this.y=y;this.z=z;
    		}
    	}
    }

  2. #2
    Join Date
    Oct 2010
    Posts
    10

    Angry Re: DirectX Basic Triangle Transparency

    I have enabled the ZBuffer, as i guess we cant sort the transparency without it. I have also added another cube so i can show all sides, however when i use the ClearFlags.ZBuffer when i clear, i get no more output on the screen. if i do not use this flag i get no output in the world space where a polygon has already been. So im haveing no luck. Please help. once i get past this i can get into some real cool 3d fractal code and have lots of fun. I dont want to have to create my own renderer because it will be to slow. use too much memory or disk I/O operations and it will take me a few weeks to compleat. Plus i want to make good use of my Graphics cards. Thankyou

  3. #3
    Join Date
    Jan 2009
    Posts
    596

    Re: DirectX Basic Triangle Transparency

    The z-buffer approach won't work for transparent surfaces. This is because transparent surfaces affect the colour behind them, so what is behind them must have already been drawn.

    Using a z-buffer you have to:
    1) Draw non-transparent surfaces (any order will do) using z-buffer
    2) Draw transparent surfaces in back-to-front order, using same z-buffer.

    It is also sensible to draw the non-transparent surfaces in front-to-back order (as much as possible) as this cuts down the amount of processing needed for calculating shading/texturing.

  4. #4
    Join Date
    Oct 2010
    Posts
    10

    Re: DirectX Basic Triangle Transparency

    Thankyou Peter_B, Ill give that a try.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured