Asked by:
Problem with direct3d

Question
-
Hi,
I wanted to create a cube in NxNxN fomat such that a bigger cube contains NxNxN smaller cubes of size say MxMxM and all cubes are seperated by some equal distance. I achieved that successfully by downloading some code from the internet. I can also rotate the cube with mouse.
Now the problem is that I wanted to color each cube on a mouse click. The cube on which I click should change the color of that cube say from Red to Green. I am not able to achieve this please help...
private void button2_Click(object sender, EventArgs e) { InitializeGraphics(); while(this.Created) { mtrx.Render(); Application.DoEvents(); } } // The Direct3D device. private Device m_Device; // Capabilities. private int m_MaxPrimitives; // The texture. // private Texture m_Texture; // The material. // private Material m_Material; // Box building constants. private const float XMIN = -4; private const float XMAX = -XMIN - 1; private const float XWID = 1; private const float XSTEP = 3; private const float ZMIN = XMIN; private const float ZMAX = -ZMIN - 1; private const float ZWID = XWID; private const float ZSTEP = XSTEP; private const float YMIN = XMIN; private const float YMAX = -YMIN - 1; private const float YWID = XWID; private const float YSTEP = XSTEP; #region "D3D Setup Code" // Data variables. private int m_NumTriangles; private int m_NumPoints; private List<CustomVertex.PositionNormalTextured> m_Points; // The vertex buffer that holds drawing data. private VertexBuffer m_VertexBuffer = null; // Initialize the graphics device. Return True if successful. public Device InitializeGraphics(PictureBox pictureBox1) { PresentParameters parms = new PresentParameters(); parms.Windowed = true; parms.SwapEffect = SwapEffect.Discard; parms.EnableAutoDepthStencil = true; // Depth stencil on. parms.AutoDepthStencilFormat = DepthFormat.D16; // Best: Hardware device and hardware vertex processing. try { m_Device = new Device(0, DeviceType.Hardware, pictureBox1, CreateFlags.HardwareVertexProcessing, parms); Debug.WriteLine("Hardware, HardwareVertexProcessing"); } catch { } // Good: Hardware device and software vertex processing. if (m_Device == null) { try { m_Device = new Device(0, DeviceType.Hardware, pictureBox1, CreateFlags.SoftwareVertexProcessing, parms); Debug.WriteLine("Hardware, SoftwareVertexProcessing"); } catch { } } // Adequate?: Software device and software vertex processing. if (m_Device == null) { try { m_Device = new Device(0, DeviceType.Reference, pictureBox1, CreateFlags.SoftwareVertexProcessing, parms); Debug.WriteLine("Reference, SoftwareVertexProcessing"); } catch (Exception ex) { // If we still can't make a device, give up. MessageBox.Show("Error initializing Direct3D\n\n" + ex.Message, "Direct3D Error", MessageBoxButtons.OK); return m_Device; } } // Get the maximum number of primitives we can render in one call to DrawPrimitives. Caps c = m_Device.DeviceCaps; m_MaxPrimitives = c.MaxPrimitiveCount; Debug.WriteLine("MaxPrimitives: " + m_MaxPrimitives); // This seems to work better than using the // actual value, at least in program d3dGasket. m_MaxPrimitives = 10000; // Turn on D3D lighting. m_Device.RenderState.Lighting = true; // Turn on the Z-buffer. m_Device.RenderState.ZBufferEnable = true; // Cull triangles that are oriented counter clockwise. m_Device.RenderState.CullMode = Cull.CounterClockwise; // Make points bigger so they're easy to see. m_Device.RenderState.PointSize = 4; // Create the vertex data. CreateVertexBuffer(); // Make the material. // SetupMaterial(); // Make the lights. //SetupLights(); // We succeeded. return m_Device; } // Create a vertex buffer for the device. public void CreateVertexBuffer() { // Create the points. m_Points = new List<CustomVertex.PositionNormalTextured>(); // Make boxes. for (float y = YMIN; y <= YMAX; y = y + YSTEP) { for (float x = XMIN; x <= XMAX; x = x + XSTEP) { for (float z = ZMIN; z <= ZMAX; z = z + ZSTEP) { MakeBoxPNT(x, y, z, x + XWID, y+ZWID, z + ZWID, 0, 0, 0, 1, 1, 1); // Create a buffer. m_NumPoints = m_Points.Count; m_NumTriangles = (int)(m_NumPoints / 3); m_VertexBuffer = new VertexBuffer( typeof(CustomVertex.PositionNormalTextured), m_NumPoints, m_Device, 0, CustomVertex.PositionNormalTextured.Format, Pool.Default); // Lock the vertex buffer. // Lock returns an array of PositionNormalTextured objects. CustomVertex.PositionNormalTextured[] vertices = (CustomVertex.PositionNormalTextured[])m_VertexBuffer.Lock(0, 0); // Copy the vertexes into the buffer. for (int i = 0; i <= m_NumPoints - 1; i++) { vertices[i] = m_Points[i]; } m_VertexBuffer.Unlock(); } } } } // Make a box. private void MakeBoxPNT(float xmin, float ymin, float zmin, float xmax, float ymax, float zmax, float umin, float vmin, float wmin, float umax, float vmax, float wmax) { // Top. MakeRectanglePNT( xmin, ymax, zmin, umin, wmin, xmin, ymax, zmax, umin, wmax, xmax, ymax, zmax, umax, wmax, xmax, ymax, zmin, umax, wmin); // Bottom. MakeRectanglePNT( xmax, ymin, zmin, umax, wmin, xmax, ymin, zmax, umax, wmax, xmin, ymin, zmax, umin, wmax, xmin, ymin, zmin, umin, wmin); // Front. MakeRectanglePNT( xmin, ymin, zmin, umin, vmin, xmin, ymax, zmin, umin, vmax, xmax, ymax, zmin, umax, vmax, xmax, ymin, zmin, umax, vmin); // Back. MakeRectanglePNT( xmax, ymin, zmax, umax, vmin, xmax, ymax, zmax, umax, vmax, xmin, ymax, zmax, umin, vmax, xmin, ymin, zmax, umin, vmin); // Left. MakeRectanglePNT( xmin, ymin, zmax, wmax, vmin, xmin, ymax, zmax, wmax, vmax, xmin, ymax, zmin, wmin, vmax, xmin, ymin, zmin, wmin, vmin); // Right. MakeRectanglePNT( xmax, ymin, zmin, wmin, vmin, xmax, ymax, zmin, wmin, vmax, xmax, ymax, zmax, wmax, vmax, xmax, ymin, zmax, wmax, vmin); } Vector3 vec0, vec1,n; // Add two triangles to make a rectangle to the vertex buffer. private void MakeRectanglePNT( float x0, float y0, float z0, float u0, float v0, float x1, float y1, float z1, float u1, float v1, float x2, float y2, float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3) { vec0 = new Vector3(x1 - x0, y1 - y0, z1 - z0); vec1 = new Vector3(x2 - x1, y2 - y1, z2 - z1); n = Vector3.Cross(vec0, vec1); n.Normalize(); m_Points.Add(new CustomVertex.PositionNormalTextured(x0, y0, z0, n.X, n.Y, n.Z, u0, v0)); m_Points.Add(new CustomVertex.PositionNormalTextured(x1, y1, z1, n.X, n.Y, n.Z, u1, v1)); m_Points.Add(new CustomVertex.PositionNormalTextured(x2, y2, z2, n.X, n.Y, n.Z, u2, v2)); m_Points.Add(new CustomVertex.PositionNormalTextured(x0, y0, z0, n.X, n.Y, n.Z, u0, v0)); m_Points.Add(new CustomVertex.PositionNormalTextured(x2, y2, z2, n.X, n.Y, n.Z, u2, v2)); m_Points.Add(new CustomVertex.PositionNormalTextured(x3, y3, z3, n.X, n.Y, n.Z, u3, v3)); // SetupMaterial(Color.Red); } #endregion // D3D Setup Code #region "D3D Drawing Code" // Draw. public void Render() { // Clear the back buffer. m_Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Green, 1, 0); // Make a scene. m_Device.BeginScene(); // Draw stuff here... // Setup the world, view, and projection matrices. SetupMatrices(); // Tell the device the format of the vertices. m_Device.VertexFormat = CustomVertex.PositionNormalTextured.Format; // Set the device's data stream source (the vertex buffer). m_Device.SetStreamSource(0, m_VertexBuffer, 0); // Draw in groups. int first_triangle = 0; int num_triangles = m_MaxPrimitives; while (first_triangle < m_NumTriangles) { if (first_triangle + num_triangles > m_NumTriangles) { num_triangles = m_NumTriangles - first_triangle; } m_Device.DrawPrimitives(PrimitiveType.TriangleList, first_triangle * 3, num_triangles); first_triangle += num_triangles; } // End the scene and display. m_Device.EndScene(); m_Device.Present(); } // Setup the world, view, and projection matrices. // here we are rotating the cubes private void SetupMatrices() { // View Matrix: m_Device.Transform.View = Microsoft.DirectX.Matrix.LookAtLH( new Vector3(-20, 10, -20), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); m_Device.Transform.Projection = Microsoft.DirectX.Matrix.PerspectiveFovLH((float)(Math.PI / 4), 1, 1, 100); } #endregion // D3D Drawing Code
- Edited by hello_Jatin Monday, January 17, 2011 6:23 AM
- Moved by Paul Zhou Thursday, January 20, 2011 7:32 AM not supported (From:Visual C# General)
Saturday, January 15, 2011 8:33 AM
All replies
-
Hi,
However you may get answer here but you'll have more chance at
DirectX Community (Select your desired forum from left side)
Sincerely,
Yasser.
LEARN HOW TO USE WINDOWS API DURING A QUICK, SIMPLE AND PRACTICAL HOW TO:
How To: Changing TextBox blinking caret using Windows APISaturday, January 15, 2011 9:10 AM -
Hi Jatin,
Welcome to the MSDN forum.
According to your description, the problem you are experiencing is related to Direct3D which is not support here. This is a forum to discuss with questions about C# general. So I would like to move this thread to off-topic. I suggest that you can post in the forum: http://social.msdn.microsoft.com/Forums/en/direct3d/threads
There are more related experts in that forum so that you can get more useful information.
Thank you for your posting and understanding.
Paul Zhou [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Thursday, January 20, 2011 7:31 AM