Simple for loop issue
I have three for loops that are used to loop through an array of cubes and set the World matrix to a position. The problem is that all the cubes are being drawn on top of each other. Am i missing something? I was able to do this before somehow. Would the order of the loops matter? My goal here is to create a 10x10 grid of cubes.
http://pastebin.com/LcvSCLV8
Im on my phone so i cant post any code so sorry for the inconvenience :S
http://pastebin.com/LcvSCLV8
Im on my phone so i cant post any code so sorry for the inconvenience :S
The particular problem you're having has nothing to do with the for loops.
You're trying to use the world matrix as a vector... or something. It's hard to describe.
Your WVP matrix is the multiplication of "World", "View", and "Projection"; currently you're just multiplying VP together.
Likewise you probably shouldn't be setting your vertex declaration to your world matrix. I think this is XNA, and if it is, that doesn't seem right.
EDIT: Does this help?
Im using the world matrix as the cubes position, translating it with a vector3.
http://www.float4x4.net/index.php/2011/07/hardware-instancing-for-pc-in-xna-4-with-textures
This is what im using. It works fine with random(). When i try to make a grid, theyre all drawn on top of reach other
http://www.float4x4.net/index.php/2011/07/hardware-instancing-for-pc-in-xna-4-with-textures
This is what im using. It works fine with random(). When i try to make a grid, theyre all drawn on top of reach other
Looking at the code there, these cubes are 2x2x2, so you want to space by that much. However, seeing as how you're spacing them on 1x1 intervals, they shouldn't be completely on top of each other.
Each cube would have a vector position of (x + 1, 0, z + 1). So i think they're being spread far enough apart
Oh i got it. Removed the first loop and used x * z for the index. But now they're drawn sporadically almost. Strange. I did shrink the sizes down too
Oh i got it. Removed the first loop and used x * z for the index
This is wrong. If you want a linear index for a "2D array", it's x * stride + z (or z*stride + x). Your stride is probably count here, but note that you also allocate too small an array. It should be rather Cube[] cubes = new Cube[count*count].
Your original problem was that you set count*count cubes to the same index i. I advise to not use an array to collect cubes, but List<Cube> and Add them as you generate them. Then finally use ToArray(). No hassling with manual indexing needed.
@SeraphLance: The code is a bit confusing (View*Projection used for the WVP uniform) but I think it's ok: You use View*Projection since the world transformation comes from the instances.
As an aside: If you're not going to rotate and scale your cubes, a simple offset can be used instead of a full world transformation. Much less memory footprint
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement