u,v Coord Question

Blitz3D Forums/Blitz3D Programming/u,v Coord Question

Fry Crayola(Posted 2004) [#1]
According to the manual, the U,V coords of a texture are 0,0 at the top left, and 1,1 at the bottom right.

This isn't working out for me. If I have a 256x256 texture, 0,0 is the top right (255,0), with (1/256),0 being the top left (0,0). i.e. it has managed to shift itself slightly to the left and wrap round on itself.

Can this be fixed by something, or is it a quirk I'll have to get used to? It makes less sense this way, and if stuff makes sense to me I find it much easier to code and debug.


MSW(Posted 2004) [#2]
Look in your DX properties (start => run => enter "DXdiag.exe" or search for it)...I think it's in there that you can adjust the U,V texture cords so they default to the center of the pixel instead of the upper left edge.


Fry Crayola(Posted 2004) [#3]
Will do. Out of curiosity though, will this affect anything I make? If I have to switch that, won't some others have to do it to so that everything works as planned?


Fry Crayola(Posted 2004) [#4]
I see no way to change the defaults from there. Help file gives me no indication either.


skidracer(Posted 2004) [#5]
Sounds like you are doing something wrong, maybe post some code?

You are using Addvertex with 6 coordinates the last two being u#,v# right (emphasis on floating point nature of coordinates and no don't set w#).

If you use CreateCube and texture it using LoadBrush and then PaintEntity do you get the same effect?

Also, not sure which manual you are reading but under AddVertex it states:

the bottom left of an image has the uv coordinates 0,0. The bottom right has coordinates 0,1, top left 0,1 and top right 1,1.


Which I'm not sure I agree with so await your reply with interest...

Also I assume you are not using TextureScale etc. which could easily give the disfigured results you are experiencing and are not talking about Terrains from what I recall have a mapping system all to themselves.


Fry Crayola(Posted 2004) [#6]
Interesting.

I am using Addvertex, and the last two are the u#, v#, indeed floating point.

Here's the code for drawing a rectangle:

Function rectangle(x#, y#, width#, height#, red, green, blue, alpha#)
	v1 = AddVertex(mainSurface, x, y, 0, 0.0, 0.0)
	v2 = AddVertex(mainSurface, x+width-1, y, 0, 1/255.0, 0.0)
	v3 = AddVertex(mainSurface, x+width-1, y-height+1, 0, 1/255.0, 1/255.0)
	v4 = AddVertex(mainSurface, x, y-height+1, 0, 0.0, 1/255.0)
	
	t1 = AddTriangle(mainSurface, v1, v2, v3)
	t2 = AddTriangle(mainSurface, v1, v3, v4)
	
	VertexColor mainSurface, v1, red, green, blue, alpha
	VertexColor mainSurface, v2, red, green, blue, alpha
	VertexColor mainSurface, v3, red, green, blue, alpha
	VertexColor mainSurface, v4, red, green, blue, alpha
End Function


This adds a rectangle at x,y, of width*height size, with the rgba values as stated. The texture coordinates are taken from a 256*256 texture where in the top left there is a 2*2 white square (used for drawing this rectangle).

Also interestingly, this is an excerpt from the Blitz Command Reference, both online and the most recent docs downloaded.

The top left of an image has the uv coordinates 0,0.
The top right has coordinates 1,0
The bottom right is 1,1.
The bottom left 0,1

I'll get back to you on the CreateCube.


skidracer(Posted 2004) [#7]
If you want the top left 2x2 pixels of a 256x256 texture page on a quad I would have thought the UV coordinates would be

0,0, 2.0/256,0, 2.0/256,2.0/256, 0,2.0/256

And the manual should state 0,0 is the top left corner of the top left texel and 1,1 is the bottom right corner of the bottom right texel.


Fry Crayola(Posted 2004) [#8]
No. The top left 2x2 pixels would be the following

0,0
1,0
0,1
1,1

which would translate to coords as

0,0
1/255.0, 0
0, 1/255.0
1/255.0, 1/255.0

It would be 1/255.0 instead of 256.0 because the bottom right would be pixel 255,255, uv coord 1,1. Which means 255/255, 255/255.


skidracer(Posted 2004) [#9]
No, 1,1 means 256/256,256/256

You have to think of texel coordinates as defining an area. Draw a 4x4 box on some grid paper, reread my last post again, and then label the texel boundaries (5 on each edge) and you will hopefully understand.


Fry Crayola(Posted 2004) [#10]
I see what you're saying. On a horizontal scale, 0 is the exact left edge, 1 is the exact right. Ok, let me see how that goes with things.


Fry Crayola(Posted 2004) [#11]
Hmm. I've gone with the following code:

Function rectangle(x#, y#, width#, height#, red, green, blue, alpha#)
	v1 = AddVertex(mainSurface, x, y, 0, 0.0, 0.0)
	v2 = AddVertex(mainSurface, x+width-1, y, 0, 2/256.0, 0.0)
	v3 = AddVertex(mainSurface, x+width-1, y-height+1, 0, 2/256.0, 1/256.0)
	v4 = AddVertex(mainSurface, x, y-height+1, 0, 0.0, 2/256.0)
	
	t1 = AddTriangle(mainSurface, v1, v2, v3)
	t2 = AddTriangle(mainSurface, v1, v3, v4)
	
	VertexColor mainSurface, v1, red, green, blue, alpha
	VertexColor mainSurface, v2, red, green, blue, alpha
	VertexColor mainSurface, v3, red, green, blue, alpha
	VertexColor mainSurface, v4, red, green, blue, alpha
End Function


This should give me the 2x2 square... but it doesn't. It gives me a slightly larger area. I think what MSW is saying is significant, except it seems to be defaulting to the centre of the pixel. I used texture flags 16 and 32 to disable uv wrapping, which stopped it from accessing the top right of the texture. It still, however leaks into the pixel 3,0 and so forth. I need the large texture for a single surface system so I can't use one small texture for this.

Of course using a 3x3 pixel area solves this little problem for now. I wouldn't mind a more general solution but I can live with this as it is.


Fry Crayola(Posted 2004) [#12]
Even better - I've used a single pixel. Provided I have U V clamping on, it's fine...


skidracer(Posted 2004) [#13]
Ah, so your original problem was due to the filtering effect on non clamped textures, your description makes a lot more sense with hindsight.

For single color I would try setting all the coordinates to .5,.5.

This should give me the 2x2 square... but it doesn't. It gives me a slightly larger area.


Try positioning the quad half a pixel along and half down, the following illustrates a method of positioning sprites to acheive perfect 1:1 ratio of pixels to texels for zero filtering with 2D work :

http://www.blitzbasic.com/codearcs/codearcs.php?code=773