PDA

View Full Version : Texturing Points Cloud



primo
01-01-2016, 21:13
i was trying to cast a texture to a 3D curve points cloud. so every point have a corresponding color from the texture. ie i don't want to make quads or triangles or whatever to connect the points , just points cloud, and it seems the only way the texture appears real is to make the points thicker, and the number of points more and more. i have used a mixture of opengl and TBGL since the opengl example was ready, i only have added the easy texturing from a TBGL example. in fact i seek Petr comments and suggestions about my approach, Thanks
Happy New Year for All

Uses "tbgl", "MATH"

#INCLUDE "%app_includepath%\thinbasic_gl.inc"
#INCLUDE "%app_includepath%\thinbasic_glu.inc"

Type Point3D
x As Single
y As Single
z As Single
red As Single
green As Single
blue As Single
tu As Single
tv As Single
End Type

Dim hwnd As DWord

Global NbX As Integer = 200
Global NbZ As Integer = 200

Global Vertex(NbX, NbX) As Point3D
'msgbox 0, SizeOf(Point3D))

hwnd = TBGL_CreateWindowEx("glDrawArrays example - esc to exit", 600, 600, 32, 0)
TBGL_ShowWindow
TBGL_ResetKeyState()

glPointSize( 2 ) ' just to show the curve in bold
TBGL_LoadTexture APP_Path+"SampleScripts\TBGL\Sprites\Textures\ship3.tga", 1, %TBGL_TEX_MIPMAP
TBGL_UseTexturing(%TRUE)
TBGL_BindTexture(1)

FillArray ' fill Vertex(...) with position data and color data

While TBGL_IsWindow(hwnd)
TBGL_ClearFrame
'TBGL_Camera(0, 3, 4, 0, 0, 0)
display
TBGL_DrawFrame
If TBGL_GetWindowKeyState( hwnd, %VK_ESCAPE) Then Exit While
Wend

TBGL_DestroyWindow

'=================================================================================

Sub display()

glMatrixMode(%GL_PROJECTION)
glLoadIdentity()
gluPerspective(90.0, 800/600, 1.0, 60.0)
glMatrixMode(%GL_MODELVIEW)
glTranslatef(0, 0, -20)
glShadeModel(%GL_SMOOTH)
glEnable(%GL_DEPTH_TEST)

gluLookAt( 0, 1.5, 1,
0, 1, 0,
0, 1, 0 )
glclear(%gl_color_buffer_bit)

TBGL_Rotate GetTickCount/30,0,1,0


glClear(%GL_COLOR_BUFFER_BIT Or %GL_DEPTH_BUFFER_BIT)
glClearColor(1, 1, 1, 1)

glEnableClientState(%GL_VERTEX_ARRAY )
glEnableClientState(%GL_COLOR_ARRAY)
glEnableClientState(%GL_TEXTURE_COORD_ARRAY)
glVertexPointer(3, %GL_FLOAT,SizeOf(Point3D),VarPtr(Vertex(1,1).x))
glColorPointer(3, %GL_FLOAT, SizeOf(Point3D), VarPtr(Vertex(1,1).red))
glTexCoordPointer(2, %GL_FLOAT, SizeOf(Point3D), VarPtr(Vertex(1,1).tu))

glDrawArrays(%GL_POINTS, 1, NbX*NbX )'40000 points

glDisableClientState(%GL_COLOR_ARRAY)
glDisableClientState(%GL_VERTEX_ARRAY)
'**************************************************

End Sub

Sub FillArray()

Single x, y, z, tu, tv, xMin, yMin, zMin, xMax, yMax, zMax, range, step1
'**********************************
xMin = -12 : yMin = -12: zMin = -12 : xMax = 12: yMax = 12 : zMax = 12
range = xMax - xMin
step1 = range / NbX
x = xMin: z = zMin : y = yMin
Integer b, a
For b=1 To NbZ

For a=1 To NbX

'y =(1 - x*x -z*z) * Exp(-1/2 * (x*x + z*z)) ' Hat
'y = Sin(10*(x^2+z^2))/5
y = Sin(Sqr(x*x+ z*z)) / Sqr(x*x + z*z)

Vertex(a,b).x = x*1
Vertex(a,b).y = y*5
Vertex(a,b).z = z*1

Vertex(a,b).red = 1 :Vertex(a,b).green = 1 :Vertex(a,b).blue = 1

Vertex(a,b).tu = a/NbX
Vertex(a,b).tv = b/NbZ

x + step1

Next a

x = xMin
z + step1
Next b

'**********************************

End Sub

Petr Schreiber
01-01-2016, 21:39
Hi Primo,

thanks for interesting example. I made it 30 lines shorter and using just TBGL, check it out:


Uses "TBGL"

Function TBMain()

DWord hwnd = TBGL_CreateWindowEx("GBuffers example - esc to exit", 600, 600, 32, 0)
TBGL_ShowWindow

TBGL_PointSize 2 ' just to show the curve in bold
TBGL_LoadTexture APP_Path+"SampleScripts\TBGL\Sprites\Textures\ship3.tga", 1, %TBGL_TEX_MIPMAP
TBGL_UseTexturing(%TRUE)
TBGL_BindTexture(1)
TBGL_BackColor 255, 255, 255

Long gBuffer = PrepareGBufferObject(200, 200)

TBGL_ResetKeyState()
While TBGL_IsWindow(hwnd)
TBGL_ClearFrame
TBGL_Camera(0.0, 45, 45,
0.0, 0.0, 0.0)

TBGL_Rotate GetTickCount/30, 0, 1, 0
TBGL_GBufferRender(gBuffer)

TBGL_DrawFrame
If TBGL_GetWindowKeyState( hwnd, %VK_ESCAPE) Then Exit While
Wend

TBGL_DestroyWindow

End Function

Function PrepareGBufferObject(gridX As Long, gridZ As Long) As Long

Single axisMin = -12
Single axisMax = 12

Single x, y, z = axisMin ' -- Assigns value to all
Single range, step1, distance

range = axisMax - axisMin
step1 = range / gridX

Dim VertexPosition(gridX, gridZ) As TBGL_TVECTOR3F
Dim VertexColor(gridX, gridZ) As TBGL_TRGB
Dim VertexNormal(gridX, gridZ) As TBGL_TVECTOR3F
Dim VertexUV(gridX, gridZ) As TBGL_TVECTOR2F

Long a, b

For b = 1 To gridZ
For a = 1 To gridX
distance = Dist(0, 0, x, z)
y = Sin(distance) / distance

With VertexPosition(a,b)
.x = x * 1
.y = y * 5
.z = z * 1
End With

With VertexColor(a,b)
.r = 255
.g = 255
.b = 255
End With

With VertexUV(a, b)
.x = a / gridX
.y = b / gridZ
End With

x += step1
Next

x = axisMin
z += step1
Next

Long gBuffer = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)
TBGL_GBufferDefineFromArray(gBuffer, %TBGL_STATIC, gridX*gridZ, VertexPosition(1,1), VertexColor(1,1), VertexNormal(1,1), VertexUV(1,1))

Return gBuffer

End Function


Let me know if you can follow the changes!


Petr

primo
02-01-2016, 10:29
i really always appreciate your versions Petr since it always contains some golden eggs. and it is much shorter than my example,
i have noticed you have added normals to the vertices. and replacing Sqr(x*x+ z*z) by Dist(0, 0, x, z)
using this scheme

With VertexPosition(a,b)
.x = x * 1
.y = y * 5
.z = z * 1
End With
is a neat feature

recently i have read in a superficial way about point based graphics, but my interest are not so big once noticed the project is frozen around 2006. seems there is no more enthusiasm. don't know. in paper https://www.graphics.rwth-aachen.de/media/papers/points1.pdf page 11 i see small points and big points cover a body so may be this is the way they represent the models. there is even the https://graphics.ethz.ch/pointshop3d/ .
https://graphics.ethz.ch/pointshop3d/downloadPS3D20.html
i have loaded a high density model (skull.sfl) https://graphics.ethz.ch/pointshop3d/Download/Models/skull.zip with this utility and it was very slow and causing the program to freeze on my system winxp. i will try it later in win7

Petr Schreiber
02-01-2016, 12:47
Hi Primo,

I also like the With/EndWith syntax, it makes the code easy to follow.

Regarding normals - you can use them in creative way to highlight the shape, when used along with lighting. Here little example:


Uses "TBGL"

Function TBMain()

DWord hwnd = TBGL_CreateWindowEx("GBuffers example - esc to exit", 600, 600, 32, 0)
TBGL_ShowWindow

' -- Environment setup
TBGL_BackColor 255, 255, 255

TBGL_UseLighting(TRUE)
TBGL_UseLightSource(%GL_LIGHT0, TRUE)
TBGL_SetLightParameter(%GL_LIGHT0, %TBGL_LIGHT_POSITION, 50, 1, 50, 1)
TBGL_SetLightParameter(%GL_LIGHT0, %TBGL_LIGHT_AMBIENT, 0.025, 0.05, 0.1, 1)
TBGL_SetLightParameter(%GL_LIGHT0, %TBGL_LIGHT_DIFFUSE, 1, 0.5, 0.25, 1)

TBGL_UseFog TRUE
TBGL_SetupFog 255, 255, 255, 255, 1

' -- Model setup
TBGL_LoadTexture APP_Path+"SampleScripts\TBGL\Sprites\Textures\ship3.tga", 1, %TBGL_TEX_MIPMAP
TBGL_UseTexturing(%TRUE)
TBGL_BindTexture(1)

TBGL_PointSize 4 ' just to show the curve in bold

Long gBuffer = PrepareGBufferObject(200, 200)

TBGL_ResetKeyState()
While TBGL_IsWindow(hwnd)

TBGL_ClearFrame
TBGL_Camera(0.0, 25, 45,
0.0, 0.0, 0.0)

TBGL_Rotate GetTickCount/30, 0, 1, 0
TBGL_GBufferRender(gBuffer)

TBGL_DrawFrame
If TBGL_GetWindowKeyState( hwnd, %VK_ESCAPE) Then Exit While
Wend

TBGL_DestroyWindow

End Function

Function PrepareGBufferObject(gridX As Long, gridZ As Long) As Long

Single axisMin = -12
Single axisMax = 12

Single x, y, z = axisMin ' -- Assigns value to all
Single range, step1, distance

range = axisMax - axisMin
step1 = range / gridX

Dim VertexPosition(gridX, gridZ) As TBGL_TVECTOR3F
Dim VertexColor(gridX, gridZ) As TBGL_TRGB
Dim VertexNormal(gridX, gridZ) As TBGL_TVECTOR3F
Dim VertexUV(gridX, gridZ) As TBGL_TVECTOR2F

Long a, b

For b = 1 To gridZ
For a = 1 To gridX
distance = Dist(0, 0, x, z)
y = Sin(distance) / distance

With VertexPosition(a,b)
.x = x * 1
.y = y * 5
.z = z * 1
End With

With VertexNormal(a,b)
.x = VertexPosition(a,b).x
.y = VertexPosition(a,b).y
.z = VertexPosition(a,b).z
End With

With VertexColor(a,b)
.r = 255
.g = 255
.b = 255
End With

With VertexUV(a, b)
.x = a / gridX
.y = b / gridZ
End With

x += step1
Next

x = axisMin
z += step1
Next

Long gBuffer = TBGL_GBufferCreate(%TBGL_POINTS, %TBGL_3D)
TBGL_GBufferDefineFromArray(gBuffer, %TBGL_STATIC, gridX*gridZ, VertexPosition(1,1), VertexColor(1,1), VertexNormal(1,1), VertexUV(1,1))

Return gBuffer

End Function


I found the read on points very interesting. It reminded me of similar papers focused on displaying volumes via "marching cubes":
http://paulbourke.net/geometry/polygonise/


Petr