My VirtualGL lib again - showcase&cry for help

Blitz3D Forums/Blitz3D Programming/My VirtualGL lib again - showcase&cry for help

hi!

as you see in the pic below, the textures are drawn linear, and not perspectively correct. i read in a forum thread about how it works. the link ain't working no more. anyway, i just dont get it! how is it working?



download: http://stuff.dev-ch.de/index.php?article=stuff_virtualgl

please help me where you can :)thanks for the help in the other threads, too!!
bye

That model is where you're problem lies. The organization of polies defining the bonnet and windscreen are particularly horrid.

Re-model it using some nice symetrical quads, along the lines of the pic below (doesn't necessarily have to be this hi-poly), and it should texture fine.


i think you didnt understand my problem.
when you use this many polys, it will be drawn all right, but with low poly models, the whole render looks bad. what i want to do is bring some perspective in it like that:

example:
==>

Assuming I'm understanding you correctly, that can't be done.

It may be possible to do that type of thing with shaders (I don't know about such things), but with simple UV mapping? No.

What you have to do is compute u/z and v/z (call these s and t) at the corners and interpolate those instead of u and v. Also interpolate 1/z (call that w) instead of z.
Then, at each pixel you need to get z, and this is where the divide comes in.
At each pixel
z = 1/w
u = s*z
v = t*z

If you're doing this in fixed point, be very, very careful about number precision and ranges. 1/z is difficult to compute in fixed point as well as having a good range and precision.

Jim

Bibo ergo sum

from here: http://p205.ezboard.com/very-fast-3d-duel-screen-demo/fyabasicprogrammingfrm25.showMessage?topicID=23.topic

^^ that guy explained it, but i didnt manage to implement it.

also here are some examples:
http://p205.ezboard.com/BlitzBasic/fyabasicprogrammingfrm22
http://p205.ezboard.com/3D-Engine-Source/fyabasicprogrammingfrm22.showMessage?topicID=167.topic
(i didn't manage to learn from them)

i sound like a noob right now, but believe me, it is harder than you think. also i dont want to copy&paste the whole source code.

This might help you, it also has some code examples and optimisations.

http://www.gamedev.net/reference/articles/article331.asp

and here
http://www.lysator.liu.se/~mikaelk/doc/perspectivetexture/

I had a very quick look. Your uv's are loaded as float but passed to ( and referenced ) in alot of functions as integers. Not sure if that's intentional or how much of a difference that this'll make but worth considering.

Stevie

i pass it further as integer using <SHL 16>
this is all right this way.
thanks for that references, i will take a look at it. i hope i get it working, but i expect, that it wont. i'll come back if it doesnt work in a while
bye:)

ok, i read and tried both. no success yet.
is here anybody, who could help me this far, that it works? - i mean, wheter, anybody could implement that piece of code into my program.

ps: dont take this the wrong way: i'm not bagging for source code!!! i'm just asking for advice:)

well, it's really imprtant for me, that the perspective correction works. is here anybody who can help me this far that it actually works? :(
==>

Try this code which I believe was originally by Sswift which I put a wrapper on.
My understanding (though I could be wrong) is that it removes shared vertices and gives each surface it's own.
;-------------------------------------------------------------------------------------------------------------------
; This function calculates and sets the normals for a mesh.
; Should probably update this so that it can recursively loop through all of an entities children as well.
;-------------------------------------------------------------------------------------------------------------------
Const swiftmax = 65535 ;32768
Const maxtris = 32*4
Dim Face_NX#(swiftmax)
Dim Face_NY#(swiftmax)
Dim Face_NZ#(swiftmax)
Dim Vertex_ConnectedTris(swiftmax)
Dim Vertex_TriList(swiftmax, maxtris)

;----------------------
Function JSmooth(model)
;----------------------
	cc = CountChildren(model)
	If cc > 0 
		For c = 1 To cc
			child = GetChild(model,c) 
			If EntityClass$(child)="Mesh"
				Calculate_Normals(child)
			EndIf
		Next
	Else
		If EntityClass$(model)="Mesh"
			Calculate_Normals(model)
		EndIf
	EndIf
End Function

;----------------------
Function Calculate_Normals(ThisMesh)
;----------------------
;	Stop

	; Loop through all surfaces of the mesh.
	Surfaces = CountSurfaces(ThisMesh)
	For LOOP_Surface = 1 To Surfaces

		Surface_Handle = GetSurface(ThisMesh, LOOP_Surface)

		; Reset the number of connected polygons for each vertex.
		For LoopV = 0 To 32767	
			Vertex_ConnectedTris(LoopV) = 0
		Next	
	
		; Loop through all triangles in this surface of the mesh.
		Tris = CountTriangles(Surface_Handle)
		For LOOP_Tris = 0 To Tris-1

			; Get the vertices that make up this triangle.
				Vertex_0 = TriangleVertex(Surface_Handle, LOOP_Tris, 0)
				Vertex_1 = TriangleVertex(Surface_Handle, LOOP_Tris, 1)
				Vertex_2 = TriangleVertex(Surface_Handle, LOOP_Tris, 2)
	
			; Adjust the number of triangles each vertex is connected to and
			; store this triangle in each vertex's list of triangles it is connected to.
				ConnectedTris = Vertex_ConnectedTris(Vertex_0)
				Vertex_TriList(Vertex_0, ConnectedTris) = LOOP_Tris
				Vertex_ConnectedTris(Vertex_0) = ConnectedTris + 1

				ConnectedTris = Vertex_ConnectedTris(Vertex_1)
				Vertex_TriList(Vertex_1, ConnectedTris) = LOOP_Tris
				Vertex_ConnectedTris(Vertex_1) = ConnectedTris + 1

				ConnectedTris = Vertex_ConnectedTris(Vertex_2)
				Vertex_TriList(Vertex_2, ConnectedTris) = LOOP_Tris
				Vertex_ConnectedTris(Vertex_2) = ConnectedTris + 1

			; Calculate the normal for this face.

				; Get the corners of this face:
				Ax# = VertexX#(Surface_Handle, Vertex_0)
				Ay# = VertexY#(Surface_Handle, Vertex_0)
				Az# = VertexZ#(Surface_Handle, Vertex_0)

				Bx# = VertexX#(Surface_Handle, Vertex_1)
				By# = VertexY#(Surface_Handle, Vertex_1)
				Bz# = VertexZ#(Surface_Handle, Vertex_1)

				Cx# = VertexX#(Surface_Handle, Vertex_2)
				Cy# = VertexY#(Surface_Handle, Vertex_2)
				Cz# = VertexZ#(Surface_Handle, Vertex_2)

				; Triangle 1
				; Get the vectors for two edges of the triangle.
				Px# = Ax#-Bx#
				Py# = Ay#-By#
				Pz# = Az#-Bz#

				Qx# = Bx#-Cx#
				Qy# = By#-Cy#
				Qz# = Bz#-Cz#

				; Compute their cross product.
				Nx# = Py#*Qz# - Pz#*Qy#
				Ny# = Pz#*Qx# - Px#*Qz#
				Nz# = Px#*Qy# - Py#*Qx#

				; Store the face normal.
				Face_NX#(LOOP_Tris) = Nx#
				Face_NY#(LOOP_Tris) = Ny#
				Face_NZ#(LOOP_Tris) = Nz#

		Next

		; Now that all the face normals for this surface have been calculated, calculate the vertex normals.
		Vertices = CountVertices(Surface_Handle)
		For LOOP_Vertices = 0 To Vertices-1

			; Reset this normal.
			Nx# = 0
			Ny# = 0
			Nz# = 0

			; Add the normals of all polygons which are connected to this vertex.
			Polys = Vertex_ConnectedTris(LOOP_Vertices)
				
			For LOOP_Polys = 0 To Polys-1

				ThisPoly = Vertex_TriList(LOOP_Vertices, LOOP_Polys)

				Nx# = Nx# + Face_NX#(ThisPoly)
				Ny# = Ny# + Face_NY#(ThisPoly)
				Nz# = Nz# + Face_NZ#(ThisPoly)			
				
			Next	
				
			; Normalize the new vertex normal.
			; (Normalizing is scaling the vertex normal down so that it's length = 1)

				Nl# = Sqr(Nx#^2 + Ny#^2 + Nz#^2)

				; Avoid a divide by zero error if by some freak accident, the vectors add up to 0.
				; If Nl# = 0 Then Nl# = 0.1

				Nx# = Nx# / Nl#
				Ny# = Ny# / Nl#
				Nz# = Nz# / Nl#

			; Set the vertex normal.
			
				VertexNormal Surface_Handle, LOOP_Vertices, Nx#, Ny#, Nz#
				;VertexColor Surface_Handle, LOOP_Vertices, polys*127, polys*127, polys*127
		
		Next

	Next

End Function


this does not solve my problem.
i think the two pics with the "==>" between them are showing the exact problem.

You are coding this in Blitz3D, right (this is a B3D forum afterall)? So, I don't understand how you expect to get such low-level control over texturing.

dont know what you mean...

http://p205.ezboard.com/fyabasicprogrammingfrm22.showMessage?topicID=164.topic
this link shows perspective corrected texture mapping. but i didt get it. how does it work? i still dont understand :(
am i too stupid?

Oh, this is for a software renderer? I see.

yes. as you see in the link i posted above, it certainly is possible. but i still didnt get it :(

i would really appreciate it if someone would help me.
remember that i've done a lot for this community and not only for this)

ok. i found help in an other forum. thank you guys anyway.


hi all,

i have changed the your function renderline.

[code]
Function vgliFillTriLine(p1x, p1d, p1u, p1v, p2x, p2d, p2u, p2v, y)

If p1x > p2x Then
tx = p1x : td = p1d : tu = p1u : tv = p1v
p1x = p2x : p1d = p2d : p1u = p2u : p1v = p2v
p2x = tx : p2d = td : p2u = tu : p2v = tv
EndIf

p1x = p1x+vglViewportX
p2x = p2x+vglViewportX
Local dif= (p2x - p1x)
Local fact=0
If p1x <vglViewportX Then
fact= -p1x
p1x = vglViewportX
EndIf
If p1x => p2x Then Return

Local id = (p2d - p1d) / dif
Local iu = (p2u - p1u) / dif
Local iv = (p2v - p1v) / dif

Local tw = (vglTex\w Shl vglShlUV)-1
Local th = (vglTex\h Shl vglShlUV)-1

Local d = p1d + (fact*id)
Local u = ((p1u + vglShlUV2) Mod tw) + (fact*iu)
Local v = ((p1v + vglShlUV2) Mod th) + (fact*iv)

tex.vglTexture = vglTex

If p2x >(vglViewportX+vglViewportW) Then p2x = (vglViewportX+vglViewportW )

y2 = y + vglViewportY
Local texw = tex\h
;texture 512 = shl 9
For x = p1x To p2x
;===>
If u > tw Then u = 0
If v > th Then v = 0
If u < 0 Then u = tw
If v < 0 Then v = th
;===>
If d < vglBuffer(x, y) Then
vglBuffer(x, y) = d
; WritePixelFast x, y2 , tex\texel[((v Shr vglShlUV) * shl 9 ) + (u Shr vglShlUV)]
WritePixelFast x, y2 , tex\texel[((v Shr vglShlUV) * texw ) + (u Shr vglShlUV)]
EndIf
d = d + id
u = u + iu
v = v + iv
Next
End Function


/[code]

thank you for these tips. some of them made me think. also i couldnt take it this way because i changed a lot since the last version published here.
but i will take a very sharp look at these

here is the new version
http://stuff.dev-ch.de/index.php?article=stuff_virtualgl
(now supporting a z-buffer instead of a w-buffer)


btw:
:)

Software renderers rule. I love this!