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


[.NET] 'Shared' mesh in class not working

Recommended Posts

I''m using VB.NET and DirectX9 and I''m working on an asteroids-style game just to learn some techniques with. Here''s my situation, I have an class called AsteroidObject. I have a dynamic list such that I can make a call to New AsteroidObject and have a new object dynamically created durring runtime. The problem I''m having currently is that when a new instance is created durring runtime, the game pauses while the object load the mesh, materials, etc. Currently I have my main routine creating 8 instances of AsteroidObject and storing them in the list (an ArrayList) so I can go through them and update/render them, etc. This works just fine. But, in order to try to save memory and stop the pausing while loading a new instance durring game play I tried setting up the mesh-related variables as Shared between each instance of the class.

Public Class AsteroidObject : Inherits BaseObject

    ''mesh info
    Private Shared mesh As Direct3D.Mesh = Nothing
    Private Shared meshMaterials() As Material = Nothing
    Private Shared meshTextures() As Texture = Nothing
    Private Shared materials As ExtendedMaterial()

    ... other class vars ...

 Public Sub New(ByRef theDevice As Device)

        ''loads mesh and texture data on creation of new asteroid
        ''default variable data setup as well

        If mesh Is Nothing Then
            mesh = mesh.FromFile("test.x", MeshFlags.Dynamic, theDevice, materials)
            meshMaterials = New Material(materials.Length) {}
            meshTextures = New Texture(materials.Length) {}
            Dim i As Integer
            For i = 0 To materials.Length - 1
                meshMaterials(i) = materials(i).Material3D
                meshMaterials(i).Ambient = meshMaterials(i).Diffuse
                meshTextures(i) = TextureLoader.FromFile(theDevice, materials(i).TextureFilename)
        End If

        .... code continues ....
The problem is that with the shared variables, even though I''m loading 8 instances to begin with, only 1 displays. I can hit my key that loads a new instance and it will load more really fast now, but even though it says that I have, say, 26 objects loaded, only 8 or so asteroids will appear. Any suggestion about how to fix this? Maybe just load the mesh and materials in the main program and pass them by reference to the class? Thanks.

Share this post

Link to post
Share on other sites
The problem seems to be with the textures. When I have the textures set as shared (or passed by reference) not all of the asteroids will be rendered on the screen (or maybe they are, just not textured?). But if it''s not shared then they all display properly. I''m stumped.

Share this post

Link to post
Share on other sites
Any takers?

When I have each object store it''s own data it works fine except it''s too slow to load objects durring gameplay.

I have it setup so that if I hit space, it will load a new asteroid on the screen durring play. Before play starts, I have 8 asteroids loaded into my list.

When I try to share the textures between the classes, or load them in the main routine and pass them to the class durring rendering, only 1 of the 8 asteroids will appear on the screen even though my status bar says that there are 8 created. When I hit space, it will load new asteroids, but only about 1/6th of what the status bar says exist.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
i think it is because you have them declared as shared, the class members are used throughout each instance of the class. so i think you are overwriting your previously loaded textures.

Share this post

Link to post
Share on other sites
As a test, I tried not storing any of the mesh info in the class itself, now I load the mesh and it's materials/textures when the program load and pass them to the Render function of an AsteroidObject when I want to draw it.

Public Sub Render(ByRef theDevice As Device, ByVal mesh As Direct3D.Mesh, ByVal meshMaterials() As Material, ByVal meshTextures() As Texture)

'renders asteroid to the screen

Dim myMatrix As Matrix
Dim originalMatrix As Matrix = theDevice.Transform.World

myMatrix = Matrix.RotationX(rotation.X)
myMatrix = Matrix.Multiply(myMatrix, Matrix.RotationY(rotation.Y))
myMatrix = Matrix.Multiply(myMatrix, Matrix.RotationZ(rotation.Z))
myMatrix = Matrix.Multiply(myMatrix, Matrix.Translation(position))

theDevice.Transform.World = myMatrix

'do the rendering
Dim i As Integer
For i = 0 To meshMaterials.Length - 1
theDevice.Material = meshMaterials(i)
theDevice.SetTexture(0, meshTextures(i))

'restore world matrix
theDevice.Transform.World = originalMatrix

End Sub

It still does the thing where, out of the initial 8 that I have declared, only 1 will render to the screen. If I add more at runtime and the number of Asteroid instances is 22, it will display 5 total out of the 22. I don't get it. If only the 1 would display, then I would say that I cannot have multiple objects using the same mesh textures in memory, but apparently that's not the case.

[edited by - FieroAddict on May 4, 2003 3:00:16 PM]

Share this post

Link to post
Share on other sites

Using this method, I was able to load the objects too fast for my random seed to start all the asteroids in different locations, so now I''m seeding random with the current count of asteroids so that it works!

What a pain.

Share this post

Link to post
Share on other sites