This topic is now archived and is closed to further replies.


Mesh texture, blending colors when shouldnt

Recommended Posts

I am loading a .X file and reading in the textures and materials for the object. the problem i am having is the texture is taking the many colors of the jpf texture image and blending them together, to form one color, instead of a patterned texture. Any ideas why this would be happening. i looked at the MSDN examples for loading a mesh and nothing is different between the apps except the .X file i am loading in, as far as i can see. Any help would be great. here is my code Imports System Imports System.Drawing Imports System.ComponentModel Imports System.Windows.Forms Imports System.IO Imports Microsoft.DirectX Imports Microsoft.DirectX.Direct3D Imports Direct3D = Microsoft.DirectX.Direct3D Namespace MeshesTutorial Public Class Meshes Inherits Form Private Rendevice As Device = Nothing '' Our rendering device Private mesh As mesh = Nothing '' Our mesh object in sysmem Private meshMaterials() As Direct3D.Material '' Materials for our mesh Private meshTextures() As Texture '' Textures for our mesh Private bumpTextures() As Texture '' Textures for our mesh Private presentParams As New PresentParameters Private pause As Boolean = False Private Shared rotateflag As Boolean Private Shared renderflag As Boolean Private Shared zoompower As Integer Private Shared rotatespeed As Integer Private Shared lightData As Light '' Description of the D3D light Private Shared lighttype As String Protected appTime As Single '' Current time in seconds Private Shared vertbuf As Direct3D.VertexBuffer Private Shared indbuff As Direct3D.IndexBuffer Private Shared att As System.Array Private Shared angle As Single = 0 Private Shared angle2 As Single = 0 Private Shared autoangle As Single = 0 Private Shared leftbool As Boolean Private Shared rightbool As Boolean Private Shared upbool As Boolean Private Shared downbool As Boolean Public Sub New() InitializeComponent() End Sub ''New Function InitializeGraphics() As Boolean '' Get the current desktop display mode, so we can set up a back '' buffer of the same format Try '' Set up the structure used to create the D3DDevice. Since we are now '' using more complex geometry, we will create a device with a zbuffer. presentParams.Windowed = True presentParams.SwapEffect = SwapEffect.Discard presentParams.EnableAutoDepthStencil = True presentParams.AutoDepthStencilFormat = DepthFormat.D16 '' Create the D3DDevice Rendevice = New Device(0, DeviceType.Hardware, PictureBox1, CreateFlags.SoftwareVertexProcessing, presentParams) AddHandler Rendevice.DeviceReset, AddressOf Me.OnResetDevice Me.OnResetDevice(Rendevice, Nothing) pause = False Catch e As DirectXException Return False End Try Return True End Function ''InitializeGraphics Public Sub OnResetDevice(ByVal sender As Object, ByVal e As EventArgs) Dim materials As ExtendedMaterial() = Nothing Dim dev As Device = CType(sender, Device) Dim adj As Direct3D.GraphicsStream Dim tempmesh As Direct3D.Mesh '' Set the directory up two to load the right data (since the default build location is bin\debug or bin\release Directory.SetCurrentDirectory((Application.StartupPath + "\..\")) '' Turn on the zbuffer dev.RenderState.ZBufferEnable = True dev.RenderState.CullMode = Cull.None '' Turn on ambient lighting ''dev.RenderState.Ambient = System.Drawing.Color.White dev.RenderState.Ambient = System.Drawing.Color.FromArgb(64, 64, 64) mesh = mesh.FromFile("C:\DXSDK\Samples\Media\teapot.X", MeshFlags.SystemMemory, Rendevice, adj, materials) If meshTextures Is Nothing Then '' We need to extract the material properties and texture names meshTextures = New Texture(materials.Length) {} bumpTextures = New Texture(materials.Length) {} meshMaterials = New Direct3D.Material(materials.Length) {} Dim i As Integer For i = 0 To materials.Length - 1 meshMaterials(i) = materials(i).Material3D '' Set the ambient color for the material (D3DX does not do this) meshMaterials(i).Ambient = meshMaterials(i).Diffuse '' Create the texture If materials(i).TextureFilename <> "" Then meshTextures(i) = TextureLoader.FromFile(dev, materials(i).TextureFilename) End If Next i End If '' Set light #0 to be a simple, faint grey directional light so '' the walls and floor are slightly different shades of grey dev.Lights(0).Type = Microsoft.DirectX.Direct3D.LightType.Directional dev.Lights(0).Diffuse = System.Drawing.Color.Gray ''dev.Lights(0).Direction = New Vector3(CSng(Math.Cos((Environment.TickCount / 250.0F))), 1.0F, CSng(Math.Sin((Environment.TickCount / 250.0F)))) dev.Lights(0).Direction = New Vector3(-3, -1, 3) dev.Lights(0).Commit() ''let d3d know about the light '' Set light #1 to be a simple, bright directional light to use dev.Lights(1).Type = Microsoft.DirectX.Direct3D.LightType.Point dev.Lights(1).Position = New Vector3(0, 20, -10) dev.Lights(1).Range = 10000 dev.Lights(1).Diffuse = System.Drawing.Color.White dev.Lights(1).Commit() dev.Lights(2).Type = Microsoft.DirectX.Direct3D.LightType.Spot dev.Lights(2).Position = New Vector3(2.0F, 10.0F, 2.0F) dev.Lights(2).Direction = New Vector3(-10, 10, 50) dev.Lights(2).InnerConeAngle = 0.5F dev.Lights(2).OuterConeAngle = 1.0F dev.Lights(2).Falloff = 1.0F dev.Lights(2).Attenuation0 = 1.0F End Sub ''OnResetDevice Sub SetupMatrices() Dim d3dmatrix As Matrix '' For our world matrix, we will just leave it as the identity If rotateflag = True Then Rendevice.Transform.World = Matrix.RotationY(Environment.TickCount / 3000.0F) Else If leftbool = True Or rightbool = True Then Rendevice.Transform.World = Matrix.RotationY((angle)) ElseIf upbool = True Or downbool = True Then Rendevice.Transform.World = Matrix.RotationX((angle2)) End If End If Matrix.Scaling(1.4, 1.4, 1.4) '' 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 100 units back along the z-axis and up three units, look at the '' origin, and define "up" to be in the y-direction. Rendevice.Transform.View = Matrix.LookAtLH(New Vector3(0.0F, 0.0F, zoompower), New Vector3(0.0F, 0.0F, 0.0F), New Vector3(0.0F, 1.0F, 0.0F)) ''Rendevice.Transform.View = Matrix.LookAtLH(New Vector3(0.0F, 0.0F, -10.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). Rendevice.Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI / 4), 1.0F, 1.0F, 1000.0F) End Sub ''SetupMatrices Private Sub Render() If Rendevice Is Nothing Then Return End If If pause Then Return End If ''Clear the backbuffer to a blue color Rendevice.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0F, 0) ''Begin the scene Rendevice.BeginScene() '' Setup the world, view, and projection matrices SetupMatrices() ''set up lights If lighttype = "directional" Then Rendevice.Lights(0).Enabled = True Rendevice.Lights(1).Enabled = False Rendevice.Lights(2).Enabled = False ElseIf lighttype = "point" Then Rendevice.Lights(0).Enabled = False Rendevice.Lights(1).Enabled = True Rendevice.Lights(2).Enabled = False ElseIf lighttype = "spot" Then Rendevice.Lights(0).Enabled = False Rendevice.Lights(1).Enabled = False Rendevice.Lights(2).Enabled = True End If '' Meshes are divided into subsets, one for each material. Render them in '' a loop ''Rendevice.RenderState.Lighting = False Dim i As Integer For i = 0 To meshMaterials.Length - 1 '' Set the material and texture for this subset Rendevice.Material = meshMaterials(i) Rendevice.SetTexture(0, meshTextures(i)) Rendevice.TextureState(0).ColorOperation = TextureOperation.Modulate Rendevice.TextureState(0).ColorArgument1 = TextureArgument.TextureColor Rendevice.TextureState(0).ColorArgument2 = TextureArgument.Diffuse Rendevice.TextureState(0).AlphaOperation = TextureOperation.Disable '' Draw the mesh subset mesh.DrawSubset(i) Next i ''End the scene Rendevice.EndScene() Rendevice.Present() End Sub ''Render Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Me.Render() '' Render on painting End Sub ''OnPaint Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs) If Asc(e.KeyChar) = CInt(System.Windows.Forms.Keys.Escape) Then Me.Dispose() '' Esc was pressed End If End Sub ''OnKeyPress Protected Overrides Sub OnResize(ByVal e As System.EventArgs) pause = (Me.WindowState = FormWindowState.Minimized Or Not Me.Visible) End Sub ''OnResize ''/ ''/ The main entry point for the application. ''/ Shared Sub Main() Dim frm As New Meshes If Not frm.InitializeGraphics() Then '' Initialize Direct3D MessageBox.Show("Could not initialize Direct3D. This tutorial will exit.") Return End If frm.Show() While frm.Created If renderflag = True Or rotateflag = True Then frm.Render() renderflag = True End If System.Windows.Forms.Application.DoEvents() End While End Sub ''Main Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents HScrollBar1 As System.Windows.Forms.HScrollBar Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton Private Sub InitializeComponent() Me.PictureBox1 = New System.Windows.Forms.PictureBox Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Label1 = New System.Windows.Forms.Label Me.HScrollBar1 = New System.Windows.Forms.HScrollBar Me.Label2 = New System.Windows.Forms.Label Me.RadioButton1 = New System.Windows.Forms.RadioButton Me.RadioButton2 = New System.Windows.Forms.RadioButton Me.RadioButton3 = New System.Windows.Forms.RadioButton Me.SuspendLayout() '' ''PictureBox1 '' Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.PictureBox1.Location = New System.Drawing.Point(8, 8) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(440, 408) Me.PictureBox1.TabIndex = 0 Me.PictureBox1.TabStop = False '' ''Button1 '' Me.Button1.Location = New System.Drawing.Point(464, 16) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(96, 24) Me.Button1.TabIndex = 1 Me.Button1.Text = "Rotate Stopped" '' ''Button2 '' Me.Button2.Location = New System.Drawing.Point(464, 96) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(96, 24) Me.Button2.TabIndex = 2 Me.Button2.Text = "Zoom In" '' ''Button3 '' Me.Button3.Location = New System.Drawing.Point(648, 96) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(96, 24) Me.Button3.TabIndex = 3 Me.Button3.Text = "Zoom Out" '' ''Label1 '' Me.Label1.Location = New System.Drawing.Point(464, 128) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(272, 24) Me.Label1.TabIndex = 4 Me.Label1.Text = "Label1" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter '' ''HScrollBar1 '' Me.HScrollBar1.LargeChange = 100 Me.HScrollBar1.Location = New System.Drawing.Point(464, 48) Me.HScrollBar1.Maximum = 10000 Me.HScrollBar1.Minimum = 100 Me.HScrollBar1.Name = "HScrollBar1" Me.HScrollBar1.Size = New System.Drawing.Size(280, 16) Me.HScrollBar1.SmallChange = 100 Me.HScrollBar1.TabIndex = 5 Me.HScrollBar1.Value = 1000 '' ''Label2 '' Me.Label2.Location = New System.Drawing.Point(576, 16) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(168, 24) Me.Label2.TabIndex = 6 Me.Label2.Text = "Rotate Speed" '' ''RadioButton1 '' Me.RadioButton1.Location = New System.Drawing.Point(464, 208) Me.RadioButton1.Name = "RadioButton1" Me.RadioButton1.Size = New System.Drawing.Size(152, 24) Me.RadioButton1.TabIndex = 7 Me.RadioButton1.Text = "Directional" '' ''RadioButton2 '' Me.RadioButton2.Location = New System.Drawing.Point(464, 248) Me.RadioButton2.Name = "RadioButton2" Me.RadioButton2.Size = New System.Drawing.Size(152, 24) Me.RadioButton2.TabIndex = 8 Me.RadioButton2.Text = "Point" '' ''RadioButton3 '' Me.RadioButton3.Location = New System.Drawing.Point(464, 288) Me.RadioButton3.Name = "RadioButton3" Me.RadioButton3.Size = New System.Drawing.Size(152, 32) Me.RadioButton3.TabIndex = 9 Me.RadioButton3.Text = "Spot" '' ''Meshes '' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(824, 421) Me.Controls.Add(Me.RadioButton3) Me.Controls.Add(Me.RadioButton2) Me.Controls.Add(Me.RadioButton1) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.HScrollBar1) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.PictureBox1) Me.Name = "Meshes" Me.Text = "Direct3D Tutorial 6 - Meshes" Me.ResumeLayout(False) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If rotateflag = True Then rotateflag = False Button1.Text = "Rotate Stopped" autoangle = 0 Else rotateflag = True Button1.Text = "Rotate Started" End If End Sub Private Sub Meshes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load rotateflag = False renderflag = True zoompower = -100.0F rotatespeed = 1000.0F lighttype = "directional" RadioButton1.Select() Label2.Text = "Rotate Speed Is: " & rotatespeed Label1.Text = "Zoom Power is: " & zoompower End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ''ZOOM IN '' If zoompower < -20.0F Then zoompower = zoompower + 10 Label1.Text = "Zoom Power is: " & zoompower renderflag = True System.Windows.Forms.Application.DoEvents() ''End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click ''ZOOM OUT If zoompower > -250 Then zoompower = zoompower - 10 Label1.Text = "Zoom Power is: " & zoompower renderflag = True System.Windows.Forms.Application.DoEvents() End If End Sub Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged lighttype = "directional" renderflag = True End Sub Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged lighttype = "point" renderflag = True End Sub Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged lighttype = "spot" renderflag = True End Sub Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean Select Case keyData.ToString Case "Left" leftbool = True rightbool = False downbool = False upbool = False angle = angle - 1000 Case "Right" downbool = False upbool = False leftbool = False rightbool = True angle = angle + 1000 Case "Up" downbool = False upbool = True leftbool = False rightbool = False angle2 = angle2 + 1000 Case "Down" downbool = True upbool = False leftbool = False rightbool = False angle2 = angle2 - 1000 End Select End Function End Class ''Meshes End Namespace ''MeshesTutorial

Share this post

Link to post
Share on other sites