Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 1st, 2005, 05:29 AM
Registered User
Join Date: Jan 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default managed directx 9drawsubset problem?

I work on a little project that load's mesh data from a

custom txt file, in managed way; i also know the other

way to load from .x file.
but this code give me the access violation exception
in Mesh.drawsubset(0) line.
I am using directx 9.0c april 2005 and vs2005.
any ideas can help
I am sorry for throwing my problem to you
[source lang="c#"]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
//using Microsoft.DirectX.Direct3Dx
using System.IO;
namespace MeshDemo
    public partial class Meshes : Form
        private float xRotate = 0;
        private float yRotate = 0;
        private float zRotate = 0;
        private float h = 0;
        private Device device = null;
        //private VertexBuffer vertexBuffer = null;
        private Mesh MyMesh = null;
        private int NumberOfVertices;
        private int NumberofIndices;
        PresentParameters presentParams;
        public Int64 iter=0;
        Material mtrl;
        public Meshes()

        static void Main()

            using (Meshes frm = new Meshes())
                if (!frm.InitializeGraphics()) //

Initialize Direct3D
                    MessageBox.Show("Could not initialize

Direct3D. This tutorial will exit.");

                // While the form is still valid, render

and process messages
                while (frm.Created)

        public bool InitializeGraphics()

                device.DeviceReset += new

                OnDeviceReset(null, EventArgs.Empty);

// this.OnCreateDevice(device, null);

                return true;
            catch (DirectXException)
                return false;


        private void OnDeviceReset(object sender,

EventArgs e)
        private void SetupDevice()
            device.RenderState.Lighting = true;
            device.RenderState.Ambient =

            device.RenderState.ZBufferEnable = true;
            //device.RenderState.DiffuseMaterialSource =

            mtrl = new Material();
            Color c = Color.FromArgb(127, 255, 0, 0);

            mtrl.Diffuse = c;
            mtrl.Ambient = c;
            mtrl.Specular = c;
            mtrl.Emissive = c;

            device.Material = mtrl;
        private void InitDevice()
            // Now let's setup our D3D stuff
            presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect =

            presentParams.DeviceWindow =

            presentParams.AutoDepthStencilFormat =

            presentParams.EnableAutoDepthStencil = true;
            device = new Device(0, DeviceType.Hardware,

this, CreateFlags.SoftwareVertexProcessing,

        protected override void

OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
            if ((int)(byte)e.KeyChar ==

                this.Close(); // Esc was pressed

        protected override void

OnKeyDown(System.Windows.Forms.KeyEventArgs e)

        if ((int)(byte)e.KeyCode ==

                xRotate += 0.1F;
            if ((int)(byte)e.KeyCode ==

                xRotate -= 0.1f;
            if ((int)(byte)e.KeyCode ==

                yRotate += 0.1f;
            if ((int)(byte)e.KeyCode ==

                yRotate -= 0.1f;
            if ((int)(byte)e.KeyCode ==

                zRotate += 0.1f;
            if ((int)(byte)e.KeyCode ==

                zRotate -= 0.1f;
            if ((int)(byte)e.KeyCode ==

                h += 1;
            if ((int)(byte)e.KeyCode ==

                h -= 1;


        //protected override void

OnPaint(System.Windows.Forms.PaintEventArgs e)
        // //Application.DoEvents();
        // //this.Render(); // Render
        // //Application.DoEvents();

        private void Render()

            if (device == null)

            //Clear the backbuffer to a blue color (ARGB

= 000000ff)

System.Drawing.Color.WhiteSmoke, 1.0f, 0);
            //Begin the scene



MyMesh.VertexBuffer, 0);
            device.VertexFormat =


            iter += 1;
           // MyMesh.UnlockAttributeBuffer();
           // MyMesh.UnlockIndexBuffer();

//device.DrawPrimitives(PrimitiveType.TriangleList, 0,

            //End the scene

        private void SetupMatrices()
            // For our world matrix, we will just rotate

the object about the y-axis.

            // Set up the rotation matrix to generate 1

full rotation (2*PI radians)
            // every 1000 ms. To avoid the loss of

precision inherent in very high
            // floating point numbers, the system time is

modulated by the rotation
            // period before conversion to a radian

            //float fangle;
            //int iTime = Environment.TickCount % 1000;
            //fangle = iTime * (2.0f * (float)Math.PI) /

            //device.Transform.World =


            device.Transform.World =

            //device.Transform.World =

            //device.Transform.World =

            // Set up our view matrix. A view matrix can

be defined given an eye
            // point, a point to lookat, and a direction

for which way is up. Here,
            // we set the eye five units back along the

z-axis and up three units,
            // look at the origin, and define "up" to be

in the y-direction.

            device.Transform.View = Matrix.LookAtLH(
                new Vector3(0.0f, 3.0f, -5.0f),
                new Vector3(0.0f, 0.0f, 0.0f),
                new Vector3(0.0f, 1.0f, 0.0f));

            // For the projection matrix, we set up a

perspective transform (which
            // transforms geometry from 3D view space to

2D viewport space, with
            // a perspective divide making objects

smaller in the distance). To build
            // a perpsective transform, we need the field

of view (1/4 pi is common),
            // the aspect ratio, and the near and far

clipping planes (which define
            // at what distances geometry should be no

longer be rendered).
            device.Transform.Projection =

                (float)Math.PI / 4,

        private void Calculate()

                String line;

                int color,index1;
                float x, y, z;

                #region vertices creation

                using (StreamReader sr = new

                    if (MyMesh != null)
                        MyMesh = null;
                    NumberOfVertices =


                    MyMesh =new Mesh(NumberOfVertices/3,



CustomVertex.PositionColored.Format, device);

arrayVertices =


                    for (int arrayIndex = 0; arrayIndex <

NumberOfVertices; arrayIndex++)
                            line = sr.ReadLine();
                            x = float.Parse(line);
                            line = sr.ReadLine();
                            y = float.Parse(line);
                            line = sr.ReadLine();
                            z = float.Parse(line);
                            line = sr.ReadLine();
                            color = int.Parse(line);


vertex = new CustomVertex.PositionColored(x, y, z,


                            arrayVertices[arrayIndex] =



                    line = sr.ReadLine();
                    int[] arrayIndices = new

                    for (int arrayIndex = 0; arrayIndex <

NumberofIndices; arrayIndex++)
                        line = sr.ReadLine();
                        index1 = int.Parse(line);

                        arrayIndices[arrayIndex] =


                    AttributeRange attributeRange = new

                    // There is only one attribute value

for this mesh.
                    // By specifying an attribute range

the DrawSubset function
                    // does not have to scan the entire

mesh for all faces that are
                    // are marked with a particular

attribute id.
                    attributeRange.AttributeId = 0;
                    attributeRange.FaceStart = 0;
                    attributeRange.FaceCount =

arrayIndices.Length / 3;
                    attributeRange.VertexStart = 0;
                    attributeRange.VertexCount =


MyMesh.VertexBuffer.SetData(arrayVertices, 0,


MyMesh.IndexBuffer.SetData(arrayIndices, 0,



AttributeRange[] { attributeRange });


            catch (Direct3DXException e)
                MessageBox.Show("Error in vertex

creatioin:{0}", e.ToString());



Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
about directx Innaorouk C# 2 August 27th, 2007 08:39 PM
Calling managed code from un-managed code David_0223 C# 7 August 21st, 2007 09:32 AM
Help. How to play media files using DirectX dextergaisie Pro VB 6 0 May 15th, 2007 06:09 AM
How to speed up 10 times using directX? czw6296 C++ Programming 2 October 19th, 2005 10:47 PM
directx programing bcmaverik VB.NET 5 January 28th, 2004 07:07 AM

All times are GMT -4. The time now is 10:02 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.