Wierd texturing problem

Blitz3D Forums/Blitz3D Beginners Area/Wierd texturing problem

hockings(Posted 2006) [#1]
I can't figure this one out for the life of me.

I draw a shape from 6 triangles (it ends up 3 boxes joined together). 2 of the boxes texture correctly, the first one doesn't. Can someone please run the following code and see if they can figure out the problem? Use a 32x32 image of anything to see the problem. It's a problem with the u and v values for the texture, but I can't work out why the other boxes texture correctly and the first one doesn't.
Thanks for your help!!!
Shane
----------------------------------------------------------------

Global ScreenWidth = 800
Global ScreenHeight = 600
Global ScreenDepth = 16
Global ScreenMode = 2

Graphics3D ScreenWidth,ScreenHeight,ScreenDepth,ScreenMode
SetBuffer BackBuffer() ; Switch to Back Buffer for Double Buffered drawing.

; Setup the lighting...
AmbientLight 200,200,200

; Setup the texture
ShapeBrush=LoadBrush("c:\my_texture.png")
If ShapeBrush = 0
RuntimeError ("cannot open my_texture.png")
EndIf


; Setup the camera.
Global MainCamera = CreateCamera()
CameraViewport MainCamera,0,0,ScreenWidth,ScreenHeight
WireframeState=0

MoveEntity (MainCamera,10,5,-8)
TurnEntity (MainCamera,0,45,0)

;######################################################################

; Draw Shape
ShapeMesh = CreateMesh()
ShapeSurface = CreateSurface(ShapeMesh)

Restore ShapeData
For VertexLoop=1 To 18
Read TempX
Read TempY
Read TempZ
v4=AddVertex(ShapeSurface,TempX,TempY,TempZ)
VertexTexCoords ShapeSurface, v4,TempX,TempY
Next

For v0 = 0 To 15 Step 3
v1 = v0 + 1
v2 = v1 + 1
AddTriangle(ShapeSurface,v0,v1,v2)
AddTriangle(ShapeSurface,v2,v1,v0)
Next

PaintMesh (ShapeMesh,ShapeBrush)

;######################################################################
Repeat ; Main Game Loop
UpdateWorld
RenderWorld
ModifyCamera()

;
Flip
Until KeyDown(1) Or Terminate
End

;######################################################################

Function ModifyCamera()
If KeyDown(200) Then
MoveEntity MainCamera,0,0,1
EndIf

If KeyDown(208) Then
MoveEntity MainCamera,0,0,-1
EndIf

If KeyDown(203) Then
TurnEntity MainCamera,0,1.0,0
EndIf

If KeyDown(205) Then
TurnEntity MainCamera,0,-1.0,0
EndIf


If KeyDown(30) Then
TurnEntity MainCamera,1.0,0,0
EndIf

If KeyDown(44) Then
TurnEntity MainCamera,-1.0,0,0
EndIf
If KeyDown(44) Then
TurnEntity MainCamera,-1.0,0,0
EndIf

End Function

;######################################################################

.ShapeData ; x,y,z triplets for triangles
Data 7,1,5,7,0,5,6,0,5
Data 6,1,5,7,1,5,6,0,5
Data 5,1,6,6,1,5,6,0,5
Data 5,1,6,6,0,5,5,0,6
Data 5,1,7,5,1,6,5,0,6
Data 5,0,7,5,1,7,5,0,6


Floyd(Posted 2006) [#2]
A quick look at the code suggests these lines are the problem:

v4=AddVertex(ShapeSurface,TempX,TempY,TempZ)
VertexTexCoords ShapeSurface, v4,TempX,TempY

You are using the same values (TempX, TempY) for vertex positions and for texture coordinates.

Position values can be anything. But texture U,V values should generally be in the range of 0 to 1.

A (U,V) of (0,0) is the upper left corner of the texture and (1,1) is the lower right. Thus ( 0.1, 0.9 ) would be a little to the right and above the lower left corner.

Unless you specify otherwise the U,V values wrap 'modulo 1'. For example, U values of 0.3, 1.3, 2.3 etc. all denote the same location.


hockings(Posted 2006) [#3]
Floyd,
Thanks for the reply. As you noted, the values are modulo 1 which is what I want. The code draws 3 squares - I want all 3 to have the same texture. It works for 2 of them, but one it doesn't work for - all should work as far as I can tell though due to the modulo thing.


Floyd(Posted 2006) [#4]
Edit: Second attempted explanation deleted. It made no sense, although I had the right basic idea.

Okay, this time for sure! Look at the last two Data statements:

Data 5,1,7,5,1,6,5,0,6
Data 5,0,7,5,1,7,5,0,6

All X values are the same. This makes all U values the same. Both triangles are textured with a single vertical stripe from the image.


hockings(Posted 2006) [#5]
Ahhh! Of course! Thanks Floyd! That had me beat!
I had bram32 at coders workshop suggest the following solution for anyone interested.
http://www.codersworkshop.com/viewpost.php?id=63984#63984