Texture on a cube

BlitzMax Forums/MiniB3D Module/Texture on a cube

Kistjes(Posted 2007) [#1]
This should be simple. Can anyone give me a short example of how to give every side of a cube a different (image) texture? Like drawing a die.
I'm searching the community for two days now but I can't find an example showing this. I'm still working on my rotating postcard (see: http://www.blitzbasic.com/Community/posts.php?topic=69542)


simonh(Posted 2007) [#2]
Create a cube yourself in code using CreateMesh->CreateSurface->AddVertex->AddTriangle. Make sure you have four vertices per side.

Now, create one texture with six square images packed inside. Now you just need to use VertexTexCoords to set the texture coordinates for each side so that the correct square from the texture is displayed.

Alternatively create a mesh with six surfaces (one side per surface), create six separate textures, and use PaintSurface to texture each side individually. This is less efficient than the above method though.


Kistjes(Posted 2007) [#3]
I was hoping to hear a easier solution :(
Anyway, I'll try both methods.
Thanx


ima747(Posted 2007) [#4]
I would model and texture a the cube and then load the mesh personaly, but then it becomes difficult to change the faces later if that's what you need to do..


Kistjes(Posted 2007) [#5]
ima747> My question was how to do this in runtime.

At the moment I have a nice demo for the project I'm working on. Since everything I do here is (strictly) confidential, I can't show the result as it is now. I'm thinking about making something that is not classified, cos I think this (+ the rotating thing) can be handy for others as well.


Kistjes(Posted 2007) [#6]
Here is an example of the second sugestion of simonh.
It works fine for the creation of the cube. I can also color each surface with a RGB brush. But I can't get an image on a surface. Am I missing something?




klepto2(Posted 2007) [#7]
Indeed you have missed something ;)

Your Surfaces have no UV Coords for your texture.
This should work:


Take a closer look to the additional parameters in the AddVertex function.

Theses describes the uv coordinates:

TopLeft-------------TopRight
--------o,o******1,0
-----------*------*
-----------*------*
-----------*------*
--------0,1******1,1
BottomLeft----------BottomRight


Kistjes(Posted 2007) [#8]
Thanks for the help.
Implemented and tested your uv coordinates. Had to rearrange the order a little (0,0 - 0,1 - 1,0 - 1,1 has become 0,0 - 0,1 - 1,1 - 1,0). Now it works fine. Have to look at this closer. I do not really understand this uv-stuff, but I'll have a closer look at it.
Anyway, I have this nice cube on which I can attach a texture on each side in run-time.

If I want to create a texture not by using loadTexture(url$) but by using an image (or a pixmap); something like createTexture(img:TImage) do I have to modify your TTexture.bmx file or should I make a child type like:
TmyTexture extends TTexture
and make my own creation method? Or is there another way?
For my project I already have the required images stored in a field, so it's not desirable to have to file-load these images again for the textures.


klepto2(Posted 2007) [#9]
Yes, there is another way ;)

Use my modified (partly 0.42 ext) TTexture.bmx and recompile
the module.

TTexture.bmx


And in func.bmx change the LoadTexture Function to this:
Function LoadTexture:TTexture(file:Object,flags=1)
	Return TTexture.LoadTexture(file,flags)
End Function


After rebuilding the module you should normally be able to load Textures which are incbin, TImage or TPixmap .


Kistjes(Posted 2007) [#10]
I got an compile error in TTexture at line 243:
TGlobal.Errorhandler.CheckError("TTextrue.bmx",204)

Seems there is no ErrorHandler in TGlobal.


Kistjes(Posted 2007) [#11]
For now I commented line 243 & 244 of TTexture to check if everything else works.
And it does!!!

Now this is what I call a nice solution.
1. I can now create a cube (or any other shape) with individual surfaces and textures. Yes!
2. I can combine this 3D object with my 2D interface. Great effect!
3. I can reuse my 2D images as texures for the 3D objects!
I'm in heaven ;-)

Thanks a lot for all the effort and support.