Solved

Problem with Texture and VertexBuffer

Posted on 2003-10-31
1
583 Views
Last Modified: 2013-12-26
I cannot get a background file to display properly in C# DirectX9 using a Vertex Buffer, Texture and TriangleList. This is supposed to be one of the simplest operations in DirectX and I have tried everything to get an image to display. It seems to be displaying only a very small portion of the image. What am I doing wrong?

Here is the code:
----------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

/// <summary>
/// Summary description for Form.
/// </summary>
public class Form : System.Windows.Forms.Form
{
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;
      
      Microsoft.DirectX.Direct3D.Device device = null;
            
      PresentParameters parameters = new PresentParameters();

      bool terminate;
      
      Texture texture = null;
                  
      VertexBuffer vb;

      public Form()
      {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
                        
            // everthing happens from here
            try
            {
                  this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                  this.MaximizeBox = false;
                  this.MinimizeBox = false;
                  this.ControlBox = false;

                  // declare useful variables
                  Microsoft.DirectX.Direct3D.Caps caps;
                  Microsoft.DirectX.Direct3D.CreateFlags flags;

                  parameters = new PresentParameters();
                  parameters.BackBufferCount = 1;
                  parameters.SwapEffect = SwapEffect.Copy;
                  parameters.PresentationInterval = PresentInterval.Immediate;
            
                  parameters.BackBufferWidth = 1024;
                  parameters.BackBufferHeight = 768;
                  parameters.BackBufferFormat = Format.X8R8G8B8;

                  // check for, and specify, depth buffer
                  if (Microsoft.DirectX.Direct3D.Manager.CheckDepthStencilMatch(0,
                        Microsoft.DirectX.Direct3D.DeviceType.Hardware,
                        parameters.BackBufferFormat, parameters.BackBufferFormat,
                        DepthFormat.D16))
                  {
                        parameters.AutoDepthStencilFormat = DepthFormat.D16;
                        parameters.EnableAutoDepthStencil = true;
                  }
                  else
                  {
                        throw new Exception("This application requires an adapter with a 16-bit depth buffer.");
                  }

                  // examine the device capabilities
                  caps = Microsoft.DirectX.Direct3D.Manager.GetDeviceCaps(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware);

                  flags = Microsoft.DirectX.Direct3D.CreateFlags.SoftwareVertexProcessing;
                  
                  // attempt to create the device interface
                  device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, flags, parameters);
                  if (device == null) throw new Exception("Device was not allocated.");

                  vb = new VertexBuffer(typeof(CustomVertex.TransformedTextured),
                        4,
                        device,
                        Usage.WriteOnly,
                        CustomVertex.TransformedTextured.Format,
                        Pool.Default);

                  texture = TextureLoader.FromFile(device, "menu.jpg");

                  CustomVertex.TransformedTextured[] data =
                        new CustomVertex.TransformedTextured[4];
                  data[0].X = 0.0f;
                  data[0].Y = 0.0f;
                  data[0].Z = 0.0f;
                  data[0].Tu = 0.0f;
                  data[0].Tv = 0.0f;
                  data[1].X = device.Viewport.Width;
                  data[1].Y = 0.0f;
                  data[1].Z = 0.0f;
                  data[1].Tu = 1.0f;
                  data[1].Tv = 0.0f;
                  data[2].X = 0.0f;
                  data[2].Y = device.Viewport.Height;
                  data[2].Z = 0.0f;
                  data[2].Tu = 0.0f;
                  data[2].Tv = 1.0f;
                  data[3].X = device.Viewport.Width;
                  data[3].Y = device.Viewport.Height;
                  data[3].Z = 0.0f;
                  data[3].Tu = 1.0f;
                  data[3].Tv = 1.0f;

                  vb.SetData(data, 0, 0);
                  
                  terminate = false;
                  while (!terminate)      
                  {
                        device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
                        device.BeginScene();
                        
                        device.SetTexture(0, texture);
                        device.VertexFormat = CustomVertex.TransformedTextured.Format;
                        device.SetStreamSource(0, vb, 0);
                        device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);

                        device.EndScene();
                        device.Present();
                        Application.DoEvents();
                  }
                  Application.Exit();
            }
            catch (Exception exception)
            {
                  System.Windows.Forms.MessageBox.Show(
                        exception.ToString(), "EXCEPTION: " + Application.ProductName,
                        System.Windows.Forms.MessageBoxButtons.OK);
            }
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
            if( disposing )
            {
                  if(components != null)
                  {
                        components.Dispose();
                  }
            }
            base.Dispose( disposing );
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
            //
            // Form
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(294, 268);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Name = "Form";
            this.Text = "Form";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.Form_Closing);

      }
      #endregion

      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main()
      {
            try
            {
                  Application.Run(new Form());
            }
            catch (Exception)
            {}
      }

      private void Form_Closing(object sender, System.ComponentModel.CancelEventArgs e)
      {
            terminate = true;
      }
}      
0
Comment
Question by:0paque
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
Imhotep earned 250 total points
ID: 9708427
Hi 0paque,

I think the problem is that you place your vertices at a z of 0.0, this is too close. It gets culled by the near clipping plane. try setting it to a higher value, maybe 0.1 will do. To be sure, set the clipping planes manually.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question