ПроЭктировщик
Регистрация: 14.01.2012
Адрес: Зеленоград
Сообщений: 192
Написано 5 полезных сообщений (для 9 пользователей)
|
Ответ: Работа с треугольниками
Получилось, спасибо)
Global player
Global cam
Global sphere
Global lamp
Graphics3D 800,600,32,2
SetBuffer BackBuffer()
cam = CreateCamera()
player = CreateSphere()
EntityParent cam,player
piv = CreatePivot()
lamp = CreateSphere()
ScaleEntity lamp,0.1,0.1,0.1
PositionEntity lamp,2,1,-5
sphere = CreateSphere()
PositionEntity sphere,0,0,5
EntityFX sphere,35
While Not KeyHit(1)
CreateLightmap()
If KeyDown(200)
MoveEntity lamp,0,0.1,0
ElseIf KeyDown(208)
MoveEntity lamp,0,-0.1,0
EndIf
If KeyDown(203)
MoveEntity lamp,-0.1,0,0
ElseIf KeyDown(205)
MoveEntity lamp,0.1,0,0
EndIf
TurnEntity cam,MouseYSpeed()/5,0,0
TurnEntity player,0,-MouseXSpeed()/5,0
MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
RotateEntity cam,EntityPitch(cam),0,0,0
If KeyDown(17)
TranslateEntity player,(-0.5 * Cos(EntityPitch(cam))) * Sin(EntityYaw(player)),-0.5 * Sin(EntityPitch(cam)),(0.5 * Cos(EntityPitch(cam))) * Cos(EntityYaw(player))
ElseIf KeyDown(31)
TranslateEntity player,(0.5 * Cos(EntityPitch(cam))) * Sin(EntityYaw(player)),0.5 * Sin(EntityPitch(cam)),(-0.5 * Cos(EntityPitch(cam))) * Cos(EntityYaw(player))
EndIf
If KeyDown(30)
MoveEntity player,-0.5,0,0
ElseIf KeyDown(32)
MoveEntity player,0.5,0,0
EndIf
UpdateWorld()
RenderWorld()
Text 10,10,("Use arrows to move the lamp")
Text 10,20,("Use WASD to move the observer")
Text 10,30,("Free look is available")
Flip
Wend
End
;----------Functions----------;
Function CreateLightmap()
For m = 1 To CountSurfaces(sphere)
surf = GetSurface(sphere,m)
For n = 1 To CountTriangles(surf)
v0 = TriangleVertex(surf,n,0)
v1 = TriangleVertex(surf,n,-1)
v2 = TriangleVertex(surf,n,-2)
v0v1X# = VertexX(surf,v1) - VertexX(surf,v0)
v0v1Y# = VertexY(surf,v1) - VertexY(surf,v0)
v0v1Z# = VertexZ(surf,v1) - VertexZ(surf,v0)
v0v2X# = VertexX(surf,v2) - VertexX(surf,v0)
v0v2Y# = VertexY(surf,v2) - VertexY(surf,v0)
v0v2Z# = VertexZ(surf,v2) - VertexZ(surf,v0)
i# = (v0v1Y * v0v2Z - v0v2Y * v0v1Z)
j# = (v0v1Z * v0v2X - v0v2Z * v0v1X)
k# = (v0v1X * v0v2Y - v0v2X * v0v1Y)
x# = EntityX(lamp) - ((VertexX(surf,v0) + VertexX(surf,v1) + VertexX(surf,v2)) / 3)
y# = EntityY(lamp) - ((VertexY(surf,v0) + VertexY(surf,v1) + VertexY(surf,v2)) / 3)
z# = EntityZ(lamp) - ((VertexZ(surf,v0) + VertexZ(surf,v1) + VertexZ(surf,v2)) / 3)
CosA# = (x * i + y * j + z * k) / (Sqr(x^2 + y^2 + z^2) * Sqr(i^2 + j^2 + k^2))
If CosA >= 0
VertexColor(surf,v0,0,0,0)
VertexColor(surf,v1,0,0,0)
VertexColor(surf,v2,0,0,0)
Else
VertexColor(surf,v0,255 * -CosA,255 * -CosA,255 * -CosA)
VertexColor(surf,v1,255 * -CosA,255 * -CosA,255 * -CosA)
VertexColor(surf,v2,255 * -CosA,255 * -CosA,255 * -CosA)
EndIf
Next
Next
End Function
__________________
|