Texture Skewed When Applied

Blitz3D Forums/Blitz3D Programming/Texture Skewed When Applied

_PJ_(Posted 2015) [#1]
I think these are all the necessary functions.

When used in my program and, when extracted for testing separately, the following produces a skewed texture:

Graphics3D 1024,768,32
SetBuffer BackBuffer()

Global CAM=CreateCamera()
MoveEntity CAM,0,0,-5

Global RockTexture
Local Rock=BuildRocks()

RenderWorld
Flip

Function BuildRocks()
	Local Rock=CreateQuad(5,5)
	
	If (Not(RockTexture))
		RockTexture=RockPileTexture()
	End If
	
	EntityTexture Rock,RockTexture
	
	EntityFX Rock,16
	EntityBlend Rock,3
	
	Return Rock
End Function

Function RockPileTexture()
	Local Buffer=GraphicsBuffer()
	Local Texture=CreateTexture(128,128,1)
	Local TBuffer=TextureBuffer(Texture)
	Local X
	Local Y
	Local Layer
	Local Rock
	Local Radius=16
	Local Thickness=4
	
	SetBuffer TBuffer
	
	LockBuffer
	
	For Layer= 1 To 4
		X=80-(Layer*Radius)
		Y=(Layer*Radius*2)-(Radius+1)
		For Rock=0 To Layer-1
			ChalkCircle(X+(Rock*Radius*2),Y,Radius-1,Thickness)
		Next
	Next
	
	UnlockBuffer
	
	SetBuffer Buffer
	
	Return Texture
End Function

Function ChalkCircle(X,Y,Radius,Thickness)
	Local Angle#
	Local Radial
	Local RGB
	For Angle# = -180 To 180
		For Radial=Radius-Thickness To Radius
			RGB=GetRandomisedChalkRGB()
			WritePixelFast X+(Radial*Sin(Angle)),Y+(Radial*Cos(Angle)),RGB
		Next
	Next
End Function


Function GetRandomisedChalkRGB()
	Local R
	Local G
	Local B
	
				R=180
				G=180
				B=180
				
		
		Local Random=(Rand(100))
		
		If (Random>69)
			Random=70
		End If	
		
		R=R+Random
		G=G+Random
		B=B+Random
		
	
	Return ((R Shl 16) + (G Shl 8) + B)
	
End Function

Function CreateQuad(X#=1,Y#=1,Parent=0) 
	
	Local Mesh=CreateMesh(Parent)
	Local Surf=CreateSurface(Mesh)
	Local Vertex0=AddVertex (Surf,X#*0.5,0-(Y#*0.5),0,0,0)
	Local Vertex1=AddVertex (Surf,0-(X#*0.5),0-(Y#*0.5),0,1,0)
	Local Vertex2=AddVertex (Surf,0-(X#*0.5),Y#*0.5,0,1,1)
	Local Vertex3=AddVertex (Surf,X#*0.5,Y#*0.5,0,1,0)
	
	AddTriangle Surf,Vertex0,Vertex1,Vertex2
	AddTriangle Surf,Vertex0,Vertex2,Vertex3
	
	Return Mesh
End Function
















However, the base texture drawing function, when drawing direct to display, provides the intended appearance:

Graphics3D 1024,768,32

SetBuffer BackBuffer()

LockBuffer

Local X
Local Y
Local Layer
Local Rock
Local Radius=16
Local Thickness=4

For Layer= 1 To 4
	X=80-(Layer*Radius)
	Y=(Layer*Radius*2)-(Radius+1)
	For Rock=0 To Layer-1
		ChalkCircle(X+(Rock*Radius*2),Y,Radius-1,Thickness)
	Next
Next

UnlockBuffer

Flip

Function ChalkCircle(X,Y,Radius,Thickness)
	Local Angle#
	Local Radial
	Local RGB
	For Angle# = -180 To 180
		For Radial=Radius-Thickness To Radius
			RGB=GetRandomisedChalkRGB()
			WritePixelFast X+(Radial*Sin(Angle)),Y+(Radial*Cos(Angle)),RGB
		Next
	Next
End Function


Function GetRandomisedChalkRGB()
	Local R
	Local G
	Local B
	
	R=180
	G=180
	B=180
	
	
	Local Random=(Rand(100))
	
	If (Random>69)
		Random=70
	End If	
	
	R=R+Random
	G=G+Random
	B=B+Random
	
	
	Return ((R Shl 16) + (G Shl 8) + B)
	
End Function



RemiD(Posted 2015) [#2]
Not sure what you mean by "skewed", but if you mean that you want the texels of your texture to look like the pixels of an image (sharp, not blurred), take a look at this : http://www.blitzbasic.com/Community/posts.php?topic=103591 (#17)


Floyd(Posted 2015) [#3]
You need to fix the u,v coordinates in your quad.
Vertices 0,1,2,3 are being created so they appear on screen as

2 3
1 0

Load a familiar image with LoadTexture(). A picture of some text will do nicely as the orientation is obvious. Then apply that texture to the quad and observe what the current u,v mapping does.


Matty(Posted 2015) [#4]
Savebuffer texturebuffer (tex) to make sure you are really drawing what you think you are drawing...i think you will find it is not centred which seems deliberate but i dont know.


Floyd(Posted 2015) [#5]
It's really just the u,v that needs work. If you imagine the vertices on screen in the configuration

2 3
1 0

and then an image drawn with the corners at those vertices then the mapping is easy. The upper left corner of the image corresponds to vertex 2 which means u,v must be 0,0 for that vertex.


RemiD(Posted 2015) [#6]
A way to debug how a texture is drawn on a surface is to add one pixel of a different color in each corner.
This can help to calculate and set the uv coords, and to rotate the texture as you want...


Floyd(Posted 2015) [#7]
Here's something that might be handy when creating a mesh. I posted this eleven years ago. At the time UpdateNormals was doing something wrong. That got fixed so any comments about it no longer apply. That is also why it shows information for normals but not positions. I changed Graphics3D to windowed, otherwise it is just as it exists in the Bug Bin.

How to move around on a mesh and examine vertex info:

; Use left and right arrow keys to visit vertices.
; Vertex 60 has undefined normal.

Graphics3D 800,600,32,1
WireFrame True

Const KEY_LEFT  = 203
Const KEY_RIGHT = 205

cam = CreateCamera()
PositionEntity cam,0,4,-4
RotateEntity cam,45,0,0

light = CreateLight()
RotateEntity light,45,0,0

sphere = CreateSphere(16)	
EntityColor sphere,0,0,200

ScaleMesh sphere,5,1,5

UpdateNormals sphere  ; something wrong here

surface = GetSurface( sphere, 1 )
vMax = CountVertices( surface ) - 1

dot = CreateSphere() : ScaleEntity dot, .04, .04, .04
EntityColor dot, 255,255,0 : EntityFX dot, 1

v = 55

While Not KeyHit(1)

	x# = VertexX(surface,v)
	y# = VertexY(surface,v)
	z# = VertexZ(surface,v)
	PositionEntity dot, x, y, z
	
	If KeyDown( KEY_RIGHT )
		v = v + 1
		If v > vMax Then v = 0
		Delay 100
	End If
	If KeyDown( KEY_LEFT )
		v = v - 1
		If v < 0 Then v = vMax
		Delay 100
	End If

	RenderWorld
	ShowVertInfo( surface, v )

	Flip
Wend

End


Function ShowVertInfo( surf, v )
Local x#, y#, z#

	Text 300,10, "Vertex: " + v
	Text 300,30, "    NX: " + VertexNX( surf, v )
	Text 300,50, "    NY: " + VertexNY( surf, v )
	Text 300,70, "    NZ: " + VertexNZ( surf, v )
	
End Function



_PJ_(Posted 2015) [#8]
Thanks, Floyd you pointed me in the right direction!

The quad is drawn to that same orientation, only it's 1 unit dimensions are centred on 0,0 rather than with the origin at a vertex, and aligned with z=0 plane.

It still follows the same orientation, though of
23
10

You were right in that the UV were assigned incorrectly and that this was the offending line:
	Local Vertex3=AddVertex (Surf,X#*0.5,Y#*0.5,0,1,0)code]
I changed it to 
[code]	Local Vertex3=AddVertex (Surf,X#*0.5,Y#*0.5,0,0,1)


And it works perfectly, thanks!