U/V positions

Blitz3D Forums/Blitz3D Programming/U/V positions

Spike314(Posted 2016) [#1]
Hi Im trying to add U V positions to a geosphere with no luck and wondering if someone can help.

Here is the code I'm using:

Function GEOcreate( Parent , Detail )

	Mesh = CreateMesh( Parent )
	S = CreateSurface( Mesh )
	a#=2.0/(1.0+Sqr(5.0))					
	b#=1.0/Sqr((3.0+Sqr(5.0)) / (1.0+Sqr(5.0)))
	
	v01=AddVertex(S,  0,  a,  b)
	v02=AddVertex(S,  0,  a, -b)
	v03=AddVertex(S,  0, -a,  b)
	v04=AddVertex(S,  0, -a, -b)
	v05=AddVertex(S,  a,  b,  0)
	v06=AddVertex(S,  a, -b,  0)
	v07=AddVertex(S, -a,  b,  0)
	v08=AddVertex(S, -a, -b,  0)
	v09=AddVertex(S,  b,  0,  a)
	v10=AddVertex(S,  b,  0, -a)
	v11=AddVertex(S, -b,  0,  a)
	v12=AddVertex(S, -b,  0, -a)
	GEOsub( S, v02,v05,v07,Detail )
	GEOsub( S, v01,v07,v05,Detail )
	GEOsub( S, v01,v03,v11,Detail )
	GEOsub( S, v01,v09,v03,Detail )
	GEOsub( S, v02,v04,v10,Detail )
	GEOsub( S, v02,v12,v04,Detail )
	GEOsub( S, v03,v06,v08,Detail )
	GEOsub( S, v04,v08,v06,Detail )
	GEOsub( S, v07,v11,v12,Detail )
	GEOsub( S, v08,v12,v11,Detail )
	GEOsub( S, v05,v10,v09,Detail )
	GEOsub( S, v06,v09,v10,Detail )
	GEOsub( S, v01,v11,v07,Detail )
	GEOsub( S, v01,v05,v09,Detail )
	GEOsub( S, v02,v07,v12,Detail )
	GEOsub( S, v02,v10,v05,Detail )
	GEOsub( S, v04,v12,v08,Detail )
	GEOsub( S, v04,v06,v10,Detail )
	GEOsub( S, v03,v08,v11,Detail )
	GEOsub( S, v03,v09,v06,Detail )

	Return mesh

End Function

;======================================
;======================================
;======================================

Function GEOsub( S, v1 , v2 , v3 , Detail )
	
	If Detail > 0 
		nx#=(VertexX(S,v1)+VertexX(S,v2))/2
		ny#=(VertexY(S,v1)+VertexY(S,v2))/2
		nz#=(VertexZ(S,v1)+VertexZ(S,v2))/2
		l#=Sqr(nx#*nx#+ny#*ny#+nz#*nz#)
		n1=AddVertex(S,nx#/l,ny#/l,nz#/l)
		nx#=(VertexX(S,v2)+VertexX(S,v3))/2
		ny#=(VertexY(S,v2)+VertexY(S,v3))/2
		nz#=(VertexZ(S,v2)+VertexZ(S,v3))/2
		l#=Sqr(nx#*nx#+ny#*ny#+nz#*nz#)
		n2=AddVertex(S,nx#/l,ny#/l,nz#/l)
		nx#=(VertexX(S,v3)+VertexX(S,v1))/2
		ny#=(VertexY(S,v3)+VertexY(S,v1))/2
		nz#=(VertexZ(S,v3)+VertexZ(S,v1))/2
		l#=Sqr(nx#*nx#+ny#*ny#+nz#*nz#)
		n3=AddVertex(S,nx#/l,ny#/l,nz#/l)
		GEOsub( S, v1,n3,n1,Detail-1)
		GEOsub( S, v2,n1,n2,Detail-1)
		GEOsub( S, v3,n2,n3,Detail-1)
		GEOsub( S, n3,n2,n1,Detail-1)
	Else
		AddTriangle(S,v2,v1,v3)
	End If

End Function



RemiD(Posted 2016) [#2]
it depends on how you want to uvmap the shape (we can't decide for you)

to calculate the u,v coords of a vertex it works like this :
let's say that a texture is 128width*128height
let's say that you want to uvmap a triangle so that the vertices positions on the texture are :
AX% = 0
AY% = 128-1
BX% = 64
BY% = 0
CX% = 128-1
CY% = 128-1

vertexA will have these u,v coordinates : float(0)/128,float(128-1)/128
vertexB will have these u,v coordinates : float(64)/128,float(0)/128
vertexC will have these u,v coordinates : float(128-1)/128,float(128-1)/128

to set the u,v coords you can use
vertextexcoords(surface,vertexindex%,u#,v#)

this may give you some ideas...


Bobysait(Posted 2016) [#3]
@RemiD : There is no reason to be always that condescendant you know ^^
By the way, did you read what he's looking for ? Computing UVs for a geo sphere is not easy at all, and this one use a recursive algorithm ...
All you give as help is "this is how generic UV works"

LOL, good luck with that :D


@Spike314 :
The problem with the geosphere is that it's a recursive algorithm that create vertices that are not aligned with a UV map, so, you can eventually rotate the UV to fit the sphere but it's hard
Also, as the vertices are welded, the seams will be visible and UVs will be stretch.

ps : your geosphere seems to be reversed, am I wrong ? v2,v1,v3 -> that's not the good sens for triangle (should be v1,v2,v3 according to the coords you pass)

I'm going to look at your algo and see what can be done, but I won't spend too much time on it, so don't expect a result :)


Bobysait(Posted 2016) [#4]



That's the best I can do (but it won't fix the UV on the seams)

ps : fixed the triangle, it was fliped depending on the detail count.
Now, it's always counterclockwise.
Also, take care of the aguments of the function, I've swaped parent and details (because, blitz primitives always put the parent in last position -> so, it's consistant with blitz stuff)


RemiD(Posted 2016) [#5]
@Bobysait>>my post was informative about the basics (since i did not see any u,v in his/her code), not "condescending", i don't know the level of the guy/gal asking the question... Please focus on answering your way instead of teaching me life... Thanks


Bobysait(Posted 2016) [#6]
1 - If this guy just wanted to know how UVs work, he'd post in the beginner section
2 - Have you ever seen a recursive function that creates vertex ? knowing what UV is will not help at all, it's mostyle a matter of understanding the algorithm and what a geo sphere is ... but if you had read the code, you'll understand that
3 - geo spheres are very specific shapes which are supposed to use specific UV sets, and if you use basic spherical UVs (like I did in previous post) it's horrible because there are seams that are not aligned with the triangles because it's not the way it's supposed to be unwraped ... you would understand that if you had compiled the code
4 - most of the time, you can't keep on emitting things like "we can't decide for you" or other stuff like this ... is it really that relevant to say it ?
Seriously, anytime you post, I just feel like you hate almost everybody ... you maybe don't realize it but yes, you're "condescending" very often, and it becomes very hard to noticed if you're or not.

And i don't f****g care about teaching you life, but I care to see some respect to anyone, just because we're here to help or get helped. Not for being bashed.


RemiD(Posted 2016) [#7]

Seriously, anytime you post, I just feel like you hate almost everybody ... you maybe don't realize it but yes, you're "condescending" very often,


that's your perception, not my intent, wait until you meet RGR, i am sure that you will like him :)


Not for being bashed.


where in my post do you see that ? My post was neutral...


Bobysait(Posted 2016) [#8]
@RemiD : Everything is already said, I have no time to loose explaining trivial things like behaviors and we both know you won't accept it or you'll think I'm wrong, so let's skip this and go on for real stuff ^^.


Here is what I got for now :
- I replaced the GeoSphere function (lot of tweaking using a type to split the main vertices, so they are no more welded)
- the function create a pivot with some meshes attached to it (it allows to get more triangles than using a single mesh that seems to crash even with lots of surfaces)
- the demo will show you a random planet generator with export of the heightmap and colormap.

It can make Spore-like planet which are not that bad to be true.
(I had to use quaternion to update the normals ... sorry for that, but dealing with heightmap on a planet is never an easy task)

The UVs works pretty well, but are not generic, so it can be hard to find resources if you don't create them by yourself.
This sheet, shows how UVs are dispatched on the texture (the texture is not a square, it's composed of 4*2 frames).








* I've add the code to the code archive, so it 'll be a bit more accurate to find it.


RemiD(Posted 2016) [#9]

so let's skip this and go on for real stuff


i agree, let's focus on what we are here for : discuss about programming with blitzplus/blitz3d/blitzmax !!!


nice demo


Bobysait(Posted 2016) [#10]
I forgot to mention, the pixels of the edges (for the top and bottom parts) are stretched to meet their neighboors on the texture (so there is no hole, every pixels are filled)
It allows to remove some artifacts on the edges so we don't see aliasing of the texture.
it also allows to compute normals easily, but I still had to make a (bad) check to "weld" the normals on, the edge (it's the slowest part of the creation and would need some optimization)