CreateCube with 6 surfaces

BlitzMax Forums/MiniB3D Module/CreateCube with 6 surfaces

Hezkore(Posted 2008) [#1]
Hey, I'm having a hard time with surfaces on a simple box. -_-
I'm still really new to 3D and creating a cube seems really messy (Looking at MiniB3D code), I need some help with my own "CreateCube" function, I want one that creates a cube but with 6 surfaces instead of just one.
Any code or just simple tips would be really great, thanks!


Warner(Posted 2008) [#2]
Do you have B3D installed ? Because in the "castle" demo, there is a function that does that. Otherwise, you might want to search for the function on this site. It is called "LoadSkyBox".


Hezkore(Posted 2008) [#3]
Yeah cheers mate, the BirdDemo had the skybox function.
Only one problem... the textures are rotate the wrong way.

Here's my code:
Function defaultTile:TMesh(Top:Int=Null,Down:Int=Null,Left:Int=Null,Right:Int=Null)
		
	Local m:TMesh=createMesh()
	Local s:TSurface
	
	s=CreateSurface( m )
	If Top Then
		addVertex s,+1,+1,+1,0,0;addVertex s,-1,+1,+1,1,0
		AddVertex s,-1,-1,+1,1,1;AddVertex s,+1,-1,+1,0,1
		AddTriangle s,0,1,2;AddTriangle s,0,2,3
	EndIf
	
	s=CreateSurface( m )
	If Down Then
		addVertex s,-1,+1,+1,0,1;addVertex s,+1,+1,+1,0,0
		AddVertex s,+1,+1,-1,1,0;AddVertex s,-1,+1,-1,1,1
		AddTriangle s,0,1,2;AddTriangle s,0,2,3
	EndIf
	
	s=CreateSurface( m )
	If Left Then
		addVertex s,-1,+1,+1,0,0;addVertex s,-1,+1,-1,1,0
		AddVertex s,-1,-1,-1,1,1;AddVertex s,-1,-1,+1,0,1
		AddTriangle s,0,1,2;AddTriangle s,0,2,3
	EndIf
		
	s=CreateSurface( m )
	If Right Then
		addVertex s,+1,+1,-1,0,0;addVertex s,+1,+1,+1,1,0
		AddVertex s,+1,-1,+1,1,1;AddVertex s,+1,-1,-1,0,1
		AddTriangle s,0,1,2;AddTriangle s,0,2,3
	EndIf
	
	Return m
End Function



plash(Posted 2008) [#4]
Perhaps it has something to do with uv positions?

note: [code ][/code ] (remove spaces) for code.


Hezkore(Posted 2008) [#5]
Heh thanks...
Yeah the UVs were messed up, after randomly trying I got the textures right.


Hezkore(Posted 2008) [#6]
Uh oh... still a problem...
Seems like the textures don't blend correctly on my box, the shades are visible without any texture but as soon as I apply a texture the shades disappear.
The weird thing is that if I use the same texture on a box created using the CreateBox function, the shades works perfectly fine.


Warner(Posted 2008) [#7]
Have you tried calling UpdateNormals() ?


Hezkore(Posted 2008) [#8]
Yeah, the shading DOES work...


But with textures applied they aren't visible :S


And the weird part is that if I use the CreateCube function and apply the same grass texture, it works perfectly fine!


The reason I'm not using the CreateCube function is because I need to be able to paint each face a different texture.
So how can I can get shades working with textures on my cube?


Warner(Posted 2008) [#9]
Maybe the difference is, that the corners of the cube contain only one vertex, but the cubes generated with defaulttile function have 3 vertices on each corner ? You could then average the normals of each vertex that shares the same corner.


Hezkore(Posted 2008) [#10]
Yeah that is most likely it...
Could you help me out with the code though?
I think it's really really confusing placing all the vertex and junk. -_-
The DefaultTile function is in my second post.


Warner(Posted 2008) [#11]
It could be better to find the CreateCube function from minib3d. Do you have minib3d installed ? If you go to c:\program files\blitzmax\mod, and then to sidesign.mod, minib3d.mod, then to "inc", there is a file called TMesh.bmx
In this file, there is a function called CreateCube()


Hezkore(Posted 2008) [#12]
Yeah as I said, I can't use that function because I need separate faces.


Warner(Posted 2008) [#13]
Well, you can copy it into your own program, and then build in the surfaces. The normals are defined in the function itself.


Hezkore(Posted 2008) [#14]
How would I "build in the surfaces" then?


Warner(Posted 2008) [#15]
In this function, all vertices/normals/texcoords/triangles are grouped. Each group has 4 lines, except for the "addtriangle" lines, they have 2 lines each.
You'll need to re-arrange the groups, so that 1 group of "addvertex" is followed by 1 group of "vertexnormal", 1 group of "vertextexcoords" and 1 group of "addtriangle". This will represent one surface. In total there will be 6 surfaces, each with 3x4 lines (verts) and 1x2 lines (tris).
Each surface group should be preceeded with this line:
surf:TSurface=mesh.CreateSurface()
It will create a new surface before adding vertices to it.



Hezkore(Posted 2008) [#16]
But isn't that kind what my DefaultTile function is doing?


Warner(Posted 2008) [#17]
Exactly like in your own function, yes. The main difference would be offcourse, having the same normals and mesh buildup that CreateCube() gives. And it has 6 sides as well.


klepto2(Posted 2008) [#18]
Maybe I miss something, but this is not exactly what you do in your tile function if it is still the one from above.

In your function you calculate no normals at all. You have to call m.UpdateNormals() at the end of your function. I know this was said already before, but it seems noone has recognised.


Hezkore(Posted 2008) [#19]
I did try putting UpdateNormals() at then end of my function but it didn't seem to do anything at all.
I merge the cubes/boxes into my "WorldModel" and the WorldModel does UpdateNormals(), that's why (I think) I don't need the UpdateNormals() in my DefaultTile function.


Warner(Posted 2008) [#20]
That makes sense then. That final UpdateNormals will overrule any existing normals. The following function will scan through all surfaces. If vertices share the same position, their normals will be averaged. It doesn't generate normals itself. However, with the optional flag, you can first perform UpdateNormals on the mesh. You could probably replace the UpdateNormals call from the WorldModel with this function.



Hezkore(Posted 2008) [#21]
Yeah cheers Warner!!
That really worked, still some minor problems but I'll fix it sooner or later. :P
Thanks to everyone for helping me!