
| |
L'archive contient le code source des 30 premières leçons de NeHe.
Pour vous donner un aperçu des codes, voici la première leçon.
Les commentaires sont en anglais, nous cherchons des volontaires pour proposer une traduction.
Le fichier OpenGL.pbi est inclu dans l'archive, et vous pouvez également le trouver dans votre répertoire PureBasic
/Examples/Sources - Advanced/OpenGL Cube
XIncludeFile "OpenGL.pbi"
#DM_BITSPERPEL=$40000
#DM_PELSWIDTH=$80000
#DM_PELSHEIGHT=$100000
#CDS_FULLSCREEN=4
#DISP_CHANGE_SUCCESSFUL=0
#SC_MONITORPOWER=$F170
Procedure.w LoWord(value.l)
ProcedureReturn (value & $FFFF)
EndProcedure
Procedure.w HiWord(value.l)
ProcedureReturn ((value >> 16) & $FFFF)
EndProcedure
Import "glu32.lib"
gluPerspective(fovy.d,aspect.d,zNear.d,zFar.d)
EndImport
Import "opengl32.lib"
glClearDepth(depth.d)
EndImport
Global hDC.l
Global hRC.l
Global hWnd.l
Global hInstance.l
Global Dim keys.b(256)
Global active.b=#True
Global fullscreen.b=#True
Declare.l WndProc(hWnd.l,uMsg.l,wParam.l,lParam.l)
Procedure ReSizeGLScene(width.l,height.l)
If height=0 : height=1 : EndIf
glViewport_(0,0,width,height)
glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_()
gluPerspective(45.0,Abs(width/height),0.1,100.0)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
EndProcedure
Procedure.l InitGL()
glShadeModel_(#GL_SMOOTH)
glClearColor_(0.0,0.0,0.0,0.5)
glClearDepth(1.0)
glEnable_(#GL_DEPTH_TEST)
glDepthFunc_(#GL_LEQUAL)
glHint_(#GL_PERSPECTIVE_CORRECTION_HINT,#GL_NICEST)
ProcedureReturn #True
EndProcedure
Procedure.l DrawGLScene()
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glLoadIdentity_()
ProcedureReturn #True
EndProcedure
Procedure KillGLWindow()
If fullscreen
ChangeDisplaySettings_(#Null,0)
ShowCursor_(#True)
EndIf
If hRC
If wglMakeCurrent_(#Null,#Null)=0
MessageBox_(#Null,"Release Of DC And RC Failed.","SHUTDOWN ERROR",#MB_OK | #MB_ICONINFORMATION)
EndIf
If wglDeleteContext_(hRC)=0
MessageBox_(#Null,"Release Rendering Context Failed.","SHUTDOWN ERROR",#MB_OK | #MB_ICONINFORMATION)
EndIf
hRC=#Null
EndIf
If hDC And ReleaseDC_(hWnd,hDC)=0
MessageBox_(#Null,"Release Device Context Failed.","SHUTDOWN ERROR",#MB_OK | #MB_ICONINFORMATION)
hDC=#Null
EndIf
If hWnd And DestroyWindow_(hWnd)=0
MessageBox_(#Null,"Could Not Release hWnd.","SHUTDOWN ERROR",#MB_OK | #MB_ICONINFORMATION)
hWnd=#Null
EndIf
If UnregisterClass_("OpenGL",hInstance)=0
MessageBox_(#Null,"Could Not Unregister Class.","SHUTDOWN ERROR",#MB_OK | #MB_ICONINFORMATION)
hInstance=#Null
EndIf
EndProcedure
Procedure.b CreateGLWindow(title.s,width.l,height.l,bits.l,fullscreenflag.b)
Protected PixelFormat.l
Protected wc.WNDCLASS
Protected dwExStyle.l
Protected dwStyle.l
Protected WindowRect.RECT
Protected wpos.POINT
WindowRect\left=0
WindowRect\right=width
WindowRect\top=0
WindowRect\bottom=height
fullscreen=fullscreenflag
hInstance=GetModuleHandle_(#Null)
wc\style=#CS_HREDRAW | #CS_VREDRAW | #CS_OWNDC
wc\lpfnWndProc=@WndProc()
wc\cbClsExtra=0
wc\cbWndExtra=0
wc\hInstance=hInstance
wc\hIcon=LoadIcon_(#Null,#IDI_WINLOGO)
wc\hCursor=LoadCursor_(#Null,#IDC_ARROW)
wc\hbrBackground=#Null
wc\lpszMenuName=#Null
wc\lpszClassName=@"OpenGL"
If RegisterClass_(wc)=0
MessageBox_(#Null,"Failed To Register The Window Class.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
ProcedureReturn #False
EndIf
If fullscreen
Protected dmScreenSettings.DEVMODE
dmScreenSettings\dmSize=SizeOf(DEVMODE)
dmScreenSettings\dmFields=#DM_BITSPERPEL | #DM_PELSWIDTH | #DM_PELSHEIGHT
dmScreenSettings\dmBitsPerPel=bits
dmScreenSettings\dmPelsWidth=width
dmScreenSettings\dmPelsHeight=height
If ChangeDisplaySettings_(dmScreenSettings,#CDS_FULLSCREEN)<>#DISP_CHANGE_SUCCESSFUL
If MessageBox_(#Null,"The Requested Fullscreen Mode Is Not Supported By"+Chr(10)+"Your Video Card. Use Windowed Mode Instead?","NeHe GL",#MB_YESNO | #MB_ICONEXCLAMATION)=#IDYES
fullscreen=#False
Else
MessageBox_(#Null,"Program Will Now Close.","ERROR",#MB_OK | #MB_ICONSTOP)
ProcedureReturn #False
EndIf
EndIf
EndIf
If fullscreen
dwExStyle=#WS_EX_APPWINDOW
dwStyle=#WS_POPUP
ShowCursor_(#False)
Else
dwExStyle=#WS_EX_APPWINDOW | #WS_EX_WINDOWEDGE
dwStyle=#WS_OVERLAPPEDWINDOW
EndIf
AdjustWindowRectEx_(WindowRect,dwStyle,#False,dwExStyle)
If fullscreen=0
wpos\x=(GetSystemMetrics_(#SM_CXSCREEN)/2)-((WindowRect\right-WindowRect\left)/2)
wpos\y=(GetSystemMetrics_(#SM_CYSCREEN)/2)-((WindowRect\bottom-WindowRect\top)/2)
EndIf
hWnd=CreateWindowEx_(dwExStyle,"OpenGL",title,dwStyle | #WS_CLIPSIBLINGS | #WS_CLIPCHILDREN,wpos\x,wpos\y,WindowRect\right-WindowRect\left,WindowRect\bottom-WindowRect\top,#Null,#Null,hInstance,#Null)
If hWnd=0
KillGLWindow()
MessageBox_(#Null,"Window Creation Error.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
ProcedureReturn #False
EndIf
Protected pfd.PIXELFORMATDESCRIPTOR
pfd\nSize=SizeOf(PIXELFORMATDESCRIPTOR)
pfd\nVersion=1
pfd\dwFlags=#PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW
pfd\iPixelType=#PFD_TYPE_RGBA
pfd\cColorBits=bits
pfd\cRedBits=0
pfd\cRedShift=0
pfd\cGreenBits=0
pfd\cGreenShift=0
pfd\cBlueBits=0
pfd\cBlueShift=0
pfd\cAlphaBits=0
pfd\cAlphaShift=0
pfd\cAccumBits=0
pfd\cAccumRedBits=0
pfd\cAccumGreenBits=0
pfd\cAccumBlueBits=0
pfd\cAccumAlphaBits=0
pfd\cDepthBits=16
pfd\cStencilBits=0
pfd\cAuxBuffers=0
pfd\iLayerType=#PFD_MAIN_PLANE
pfd\bReserved=0
pfd\dwLayerMask=0
pfd\dwVisibleMask=0
pfd\dwDamageMask=0
hDC=GetDC_(hWnd)
If hDC=0
KillGLWindow()
MessageBox_(#Null,"Can't Create A GL Device Context.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
ProcedureReturn #False
EndIf
PixelFormat=ChoosePixelFormat_(hDC,pfd)
If PixelFormat=0
KillGLWindow()
MessageBox_(#Null,"Can't Find A Suitable PixelFormat.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
ProcedureReturn #False
EndIf
If SetPixelFormat_(hDC,PixelFormat,pfd)=0
KillGLWindow()
MessageBox_(#Null,"Can't Set The PixelFormat.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
ProcedureReturn #False
EndIf
hRC=wglCreateContext_(hDC)
If hRC=0
KillGLWindow()
MessageBox_(#Null,"Can't Create A GL Rendering Context.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
ProcedureReturn #False
EndIf
If wglMakeCurrent_(hDC,hRC)=0
KillGLWindow()
MessageBox_(#Null,"Can't Activate The GL Rendering Context.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
ProcedureReturn #False
EndIf
ShowWindow_(hWnd,#SW_SHOW)
SetForegroundWindow_(hWnd)
SetFocus_(hWnd)
ReSizeGLScene(width,height)
If InitGL()=0
KillGLWindow()
MessageBox_(#Null,"Initialization Failed.","ERROR",#MB_OK | #MB_ICONEXCLAMATION)
ProcedureReturn #False
EndIf
ProcedureReturn #True
EndProcedure
Procedure.l WndProc(hWnd.l,uMsg.l,wParam.l,lParam.l)
Select uMsg
Case #WM_ACTIVATE
If HiWord(wParam)=0
active=#True
Else
active=#False
EndIf
ProcedureReturn 0
Case #WM_SYSCOMMAND
Select wParam
Case #SC_SCREENSAVE
ProcedureReturn 0
Case #SC_MONITORPOWER
ProcedureReturn 0
EndSelect
Case #WM_CLOSE
PostQuitMessage_(0)
ProcedureReturn 0
Case #WM_KEYDOWN
keys(wParam)=#True
ProcedureReturn 0
Case #WM_KEYUP
keys(wParam)=#False
ProcedureReturn 0
Case #WM_SIZE
ReSizeGLScene(LoWord(lParam),HiWord(lParam))
ProcedureReturn 0
EndSelect
ProcedureReturn DefWindowProc_(hWnd,uMsg,wParam,lParam)
EndProcedure
Procedure.l WinMain()
Protected msg.MSG
Protected done.b
If MessageBox_(#Null,"Would You Like To Run In Fullscreen Mode?","Start FullScreen?",#MB_YESNO | #MB_ICONQUESTION)=#IDNO
fullscreen=#False
EndIf
If CreateGLWindow("NeHe's OpenGL Framework",640,480,16,fullscreen)=0
ProcedureReturn 0
EndIf
While done=#False
If PeekMessage_(msg,#Null,0,0,#PM_REMOVE)
If msg\message=#WM_QUIT
done=#True
Else
TranslateMessage_(msg)
DispatchMessage_(msg)
EndIf
Else
If (active And DrawGLScene()=0) Or keys(#VK_ESCAPE)
done=#True
Else
SwapBuffers_(hDC)
EndIf
If keys(#VK_F1)
keys(#VK_F1)=#False
KillGLWindow()
fullscreen=~fullscreen & 1
If CreateGLWindow("NeHe's OpenGL Framework",640,480,16,fullscreen)=0
ProcedureReturn 0
EndIf
EndIf
EndIf
Wend
KillGLWindow()
End
EndProcedure
WinMain()
|
|
Auteur : Comtois
| | Version : 20/02/2008 | | |
| |
InitEngine3D()
InitSprite()
InitKeyboard()
ExamineDesktops()
OpenScreen(DesktopWidth(0), DesktopHeight(0), DesktopDepth(0), "Tutoriel 3D")
#Mesh = 0
CreateMesh(#Mesh, 200)
SetMeshData(#Mesh, #PB_Mesh_Vertex | #PB_Mesh_Color , ?SommetsTriangles, 3)
SetMeshData(#Mesh, #PB_Mesh_Face, ?IndexTriangles, 1)
#Texture = 0
CreateTexture(#Texture, 64, 64)
StartDrawing(TextureOutput(#Texture))
Box(0,0, TextureWidth(#Texture), TextureHeight(#Texture), RGB(255, 255, 255))
StopDrawing()
#Matiere = 0
CreateMaterial(#Matiere, TextureID(#Texture))
MaterialAmbientColor(#Matiere, #PB_Material_AmbientColors)
#Entity = 0
CreateEntity(#Entity, MeshID(#Mesh), MaterialID(#Matiere))
#Camera = 0
CreateCamera(#Camera, 25, 25, 50, 50)
CameraBackColor(#Camera, $FF0000)
CameraLocate(#Camera,0,0,500)
CameraLookAt(#Camera, EntityX(#Entity), EntityY(#Entity), EntityZ(#Entity))
Repeat
ClearScreen(0)
ExamineKeyboard()
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_All)
DataSection
SommetsTriangles:
Data.f 0.0,100.0,0.0
Data.l $FF0000
Data.f 200.0,-100.0,0.0
Data.l $00FF00
Data.f -200.0,-100.0,0.0
Data.l $0000FF
IndexTriangles:
Data.w 2,1,0
EndDataSection
|
|
Auteur : Comtois
| | Version : 20/02/2008 | | |
| |
InitEngine3D()
InitSprite()
InitKeyboard()
ExamineDesktops()
OpenScreen(DesktopWidth(0), DesktopHeight(0), DesktopDepth(0), "Tutoriel 3D")
Structure s_Sommet
Position.f[3]
Couleur.l
EndStructure
Structure s_Triangle
Index.w[3]
EndStructure
Dim Sommets.s_Sommet(2)
Dim Triangles.s_Triangle(0)
Sommets(0)\Position[0] = 0.0 : Sommets(0)\Position[1] = 100.0 : Sommets(0)\Position[2] = 0.0
Sommets(0)\Couleur = $FF0000
Sommets(1)\Position[0] = 200.0 : Sommets(1)\Position[1] = -100.0 : Sommets(1)\Position[2] = 0.0
Sommets(1)\Couleur = $00FF00
Sommets(2)\Position[0] = -200.0 : Sommets(2)\Position[1] = -100.0 : Sommets(2)\Position[2] = 0.0
Sommets(2)\Couleur = $0000FF
Triangles(0)\Index[0] = 2 : Triangles(0)\Index[1] = 1 : Triangles(0)\Index[2] = 0
#Mesh = 0
CreateMesh(#Mesh, 200)
SetMeshData(#Mesh, #PB_Mesh_Vertex | #PB_Mesh_Color , @Sommets(), 3)
SetMeshData(#Mesh, #PB_Mesh_Face, @Triangles(), 1)
#Texture = 0
CreateTexture(#Texture, 64, 64)
StartDrawing(TextureOutput(#Texture))
Box(0,0, TextureWidth(#Texture), TextureHeight(#Texture), RGB(255, 255, 255))
StopDrawing()
#Matiere = 0
CreateMaterial(#Matiere, TextureID(#Texture))
MaterialAmbientColor(#Matiere, #PB_Material_AmbientColors)
#Entity = 0
CreateEntity(#Entity, MeshID(#Mesh), MaterialID(#Matiere))
#Camera = 0
CreateCamera(#Camera, 25, 25, 50, 50)
CameraBackColor(#Camera, $FF0000)
CameraLocate(#Camera,0,0,500)
CameraLookAt(#Camera, EntityX(#Entity), EntityY(#Entity), EntityZ(#Entity))
Repeat
ClearScreen(0)
ExamineKeyboard()
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_All)
|
|
Auteur : Comtois
| | Version : 20/02/2008 | | |
| |
InitEngine3D()
InitSprite()
InitKeyboard()
OpenScreen(800, 600, 32, "Cube 3D")
Macro RGB_INVERSE(Rouge,Vert,Bleu)
Rouge << 16 + Vert << 8 + Bleu
EndMacro
Structure s_Sommet
px.f
py.f
pz.f
nx.f
ny.f
nz.f
co.l
u.f
v.f
EndStructure
Structure s_Triangle
f1.w
f2.w
f3.w
EndStructure
Structure s_Mesh
No.l
*VBuffer.s_Sommet
*Ibuffer.s_Triangle
EndStructure
Global Angle.f,Pas.f, CameraMode.l
Global *VBuffer,*IBuffer
Define.s_Mesh CubeMesh
Procedure CreateMeshCube(*Mesh.s_Mesh)
*Mesh\VBuffer=AllocateMemory(SizeOf(s_Sommet) * 24)
*Mesh\IBuffer=AllocateMemory(SizeOf(s_Triangle) * 12)
CopyMemory(?Sommets, *Mesh\VBuffer, SizeOf(s_Sommet) * 24)
CopyMemory(?Triangles, *Mesh\IBuffer, SizeOf(s_Triangle) * 12)
If CreateMesh(*Mesh\No, 100)
Options = #PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate | #PB_Mesh_Color
SetMeshData(*Mesh\No, Options , *Mesh\VBuffer, 24)
SetMeshData(*Mesh\No, #PB_Mesh_Face, *Mesh\IBuffer, 12)
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure UpColorCube(*Mesh.s_Mesh, Couleur)
*Mem.s_Sommet = *Mesh\VBuffer
For i = 0 To 3
*Mem\co=Couleur
*Mem + SizeOf(s_Sommet)
Next i
EndProcedure
Procedure DownColorCube(*Mesh.s_Mesh, Couleur)
*Mem.s_Sommet = *Mesh\VBuffer + 4 * SizeOf(s_Sommet)
For i = 0 To 3
*Mem\co=Couleur
*Mem + SizeOf(s_Sommet)
Next i
EndProcedure
Procedure FrontColorCube(*Mesh.s_Mesh, Couleur)
*Mem.s_Sommet = *Mesh\VBuffer + 8 * SizeOf(s_Sommet)
For i = 0 To 3
*Mem\co=Couleur
*Mem + SizeOf(s_Sommet)
Next i
EndProcedure
Procedure BackColorCube(*Mesh.s_Mesh, Couleur)
*Mem.s_Sommet = *Mesh\VBuffer + 12 * SizeOf(s_Sommet)
For i = 0 To 3
*Mem\co=Couleur
*Mem + SizeOf(s_Sommet)
Next i
EndProcedure
Procedure LeftColorCube(*Mesh.s_Mesh, Couleur)
*Mem.s_Sommet = *Mesh\VBuffer + 16 * SizeOf(s_Sommet)
For i = 0 To 3
*Mem\co=Couleur
*Mem + SizeOf(s_Sommet)
Next i
EndProcedure
Procedure RightColorCube(*Mesh.s_Mesh, Couleur)
*Mem.s_Sommet = *Mesh\VBuffer + 20 * SizeOf(s_Sommet)
For i = 0 To 3
*Mem\co=Couleur
*Mem + SizeOf(s_Sommet)
Next i
EndProcedure
Procedure UpDateCube(*Mesh.s_Mesh)
Flag = #PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate | #PB_Mesh_Color
SetMeshData(*Mesh\No, Flag , *Mesh\VBuffer, 24)
EndProcedure
#Mesh = 0
CubeMesh\No = #Mesh
CreateMeshCube(@CubeMesh)
UpColorCube(@CubeMesh, RGB_INVERSE(255,0,0))
DownColorCube(@CubeMesh, RGB_INVERSE(255,255,0))
FrontColorCube(@CubeMesh,RGB_INVERSE(0,255,0))
BackColorCube(@CubeMesh, RGB_INVERSE(0,0,255))
LeftColorCube(@CubeMesh, RGB_INVERSE(255,128,0))
RightColorCube(@CubeMesh,RGB_INVERSE(255,255,255))
UpDateCube(@CubeMesh)
#Texture = 0
CreateTexture(#Texture, 128, 128)
StartDrawing(TextureOutput(#Texture))
Box(0, 0, 128, 128, 0)
Box(1, 1, 126, 126, $FFFFFF)
StopDrawing()
#Matiere = 0
CreateMaterial(#Matiere, TextureID(#Texture))
MaterialAmbientColor(#Matiere, #PB_Material_AmbientColors)
#Entity = 0
CreateEntity(#Entity, MeshID(#Mesh), MaterialID(#Matiere))
ScaleEntity(#Entity, 90, 90, 90)
#Camera = 0
CreateCamera(#Camera, 0, 0 , 100 , 100)
MoveCamera(#Camera, 0, 0, -400)
CameraLookAt(#Camera, EntityX(#Entity), EntityY(#Entity), EntityZ(#Entity))
AmbientColor(RGB(255,255,255))
pas = 0.8
Repeat
Angle + Pas
RotateEntity(0,angle,angle/2,-Angle)
If ExamineKeyboard()
If KeyboardReleased(#PB_Key_F1)
CameraMode=1-CameraMode
CameraRenderMode(#Camera, CameraMode)
EndIf
EndIf
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
DataSection
Sommets:
Data.f -0.5,0.5,-0.5
Data.f 0,1,0
Data.l 0
Data.f 0,0
Data.f 0.5,0.5,-0.5
Data.f 0,1,0
Data.l 0
Data.f 0,1
Data.f 0.5,0.5,0.5
Data.f 0,1,0
Data.l 0
Data.f 1,1
Data.f -0.5,0.5,0.5
Data.f 0,1,0
Data.l 0
Data.f 1,0
Data.f -0.5,-0.5,0.5
Data.f 0,-1,0
Data.l 0
Data.f 0,0
Data.f 0.5,-0.5,0.5
Data.f 0,-1,0
Data.l 0
Data.f 0,1
Data.f 0.5,-0.5,-0.5
Data.f 0,-1,0
Data.l 0
Data.f 1,1
Data.f -0.5,-0.5,-0.5
Data.f 0,-1,0
Data.l 0
Data.f 1,0
Data.f -0.5,0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f 0,0
Data.f 0.5,0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f 0,1
Data.f 0.5,-0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f 1,1
Data.f -0.5,-0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f 1,0
Data.f 0.5,0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f 0,0
Data.f -0.5,0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f 0,1
Data.f -0.5,-0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f 1,1
Data.f 0.5,-0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f 1,0
Data.f -0.5,0.5,-0.5
Data.f -1,0,0
Data.l 0
Data.f 0,0
Data.f -0.5,0.5,0.5
Data.f -1,0,0
Data.l 0
Data.f 0,1
Data.f -0.5,-0.5,0.5
Data.f -1,0,0
Data.l 0
Data.f 1,1
Data.f -0.5,-0.5,-0.5
Data.f -1,0,0
Data.l 0
Data.f 1,0
Data.f 0.5,0.5,0.5
Data.f 1,0,0
Data.l 0
Data.f 0,0
Data.f 0.5,0.5,-0.5
Data.f 1,0,0
Data.l 0
Data.f 0,1
Data.f 0.5,-0.5,-0.5
Data.f 1,0,0
Data.l 0
Data.f 1,1
Data.f 0.5,-0.5,0.5
Data.f 1,0,0
Data.l 0
Data.f 1,0
Triangles:
Data.w 2,1,0
Data.w 0,3,2
Data.w 6,5,4
Data.w 4,7,6
Data.w 10,9,8
Data.w 8,11,10
Data.w 14,13,12
Data.w 12,15,14
Data.w 18,17,16
Data.w 16,19,18
Data.w 22,21,20
Data.w 20,23,22
EndDataSection
|
|
Auteur : Comtois
| | Version : 02/03/2008 | | |
| |
EnableExplicit
InitEngine3D()
InitSprite()
InitKeyboard()
UsePNGImageEncoder()
OpenScreen(800, 600, 32, "Dé 3D")
Global Angle.f,Pas.f, CameraMode.l
Define.l Options, i
Define.f Da, Da2
#Tiers = 1.0/3.0
#Mesh = 0
CreateMesh(#Mesh, 100)
Options = #PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate | #PB_Mesh_Color
SetMeshData(#Mesh, Options , ?Sommets, 24)
SetMeshData(#Mesh, #PB_Mesh_Face, ?Triangles, 12)
Macro Cercle(x, y, r)
DrawingMode(#PB_2DDrawing_Outlined)
Circle(x + 1, y + 1, r + 1, 0)
DrawingMode(#PB_2DDrawing_Default)
Circle(x, y, r, 0)
EndMacro
#Texture = 0
CreateTexture(#Texture, 256, 256)
StartDrawing(TextureOutput(#Texture))
Box(0, 0, TextureWidth(#Texture), TextureHeight(#Texture), $FFFFFF)
Da = TextureWidth(#Texture)/6.0
Da2 = Da/2.0
Cercle(3 * Da, Da, 7)
For i = -1 To 1 Step 2
Cercle(3 * Da + i * Da2, 3 * Da - i * Da2, 7)
Next i
For i = -1 To 1
Cercle(Da + i * Da2, 3 * Da - i * Da2, 7)
Next i
For i = -1 To 1 Step 2
Cercle(5 * Da + i * Da2, 3 * Da - Da2, 7)
Cercle(5 * Da + i * Da2, 3 * Da + Da2, 7)
Next i
For i = -1 To 1 Step 2
Cercle(Da + i * Da2, Da - Da2, 7)
Cercle(Da + i * Da2, Da + Da2, 7)
Next i
Cercle(Da, Da, 7)
For i = -1 To 1
Cercle((3 * Da + i * Da2), (5 * Da - Da2), 7)
Cercle((3 * Da + i * Da2), (5 * Da + Da2), 7)
Next i
StopDrawing()
#Matiere = 0
CreateMaterial(#Matiere, TextureID(#Texture))
#Entity = 0
CreateEntity(#Entity, MeshID(#Mesh), MaterialID(#Matiere))
ScaleEntity(#Entity, 90, 90, 90)
#Camera = 0
CreateCamera(#Camera, 0, 0 , 100 , 100)
MoveCamera(#Camera, 0, 0, -400)
CameraLookAt(#Camera, EntityX(#Entity), EntityY(#Entity), EntityZ(#Entity))
AmbientColor(RGB(55,55,55))
#LightRouge = 0 : CreateLight(#LightRouge,RGB(255, 255, 255), 0, 500, 0)
#LightBleue = 1 : CreateLight(#LightBleue,RGB(255, 255, 255), 0, -500, 0)
#LightVerte = 2 : CreateLight(#LightVerte,RGB(255, 255, 255), 500, 0, 0)
pas = 0.8
Repeat
Angle + Pas
RotateEntity(0,angle,angle/2,-Angle)
If ExamineKeyboard()
If KeyboardReleased(#PB_Key_F1)
CameraMode=1-CameraMode
CameraRenderMode(#Camera, CameraMode)
EndIf
If KeyboardReleased(#PB_Key_F10)
GrabSprite(0,0,0,800,600)
SaveSprite(0,"de3D.png",#PB_ImagePlugin_PNG)
EndIf
EndIf
RenderWorld()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
DataSection
Sommets:
Data.f -0.5,0.5,-0.5
Data.f 0,1,0
Data.l 0
Data.f 0, 0
Data.f 0.5,0.5,-0.5
Data.f 0,1,0
Data.l 0
Data.f #Tiers, 0
Data.f 0.5,0.5,0.5
Data.f 0,1,0
Data.l 0
Data.f #Tiers, #Tiers
Data.f -0.5,0.5,0.5
Data.f 0,1,0
Data.l 0
Data.f 0, #Tiers
Data.f -0.5,-0.5,0.5
Data.f 0,-1,0
Data.l 0
Data.f #Tiers, #Tiers
Data.f 0.5,-0.5,0.5
Data.f 0,-1,0
Data.l 0
Data.f #Tiers*2,#Tiers
Data.f 0.5,-0.5,-0.5
Data.f 0,-1,0
Data.l 0
Data.f #Tiers*2,#Tiers*2
Data.f -0.5,-0.5,-0.5
Data.f 0,-1,0
Data.l 0
Data.f #Tiers,#Tiers*2
Data.f -0.5,0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f #Tiers, 0
Data.f 0.5,0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f #Tiers*2,0
Data.f 0.5,-0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f #Tiers*2,#Tiers
Data.f -0.5,-0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f #Tiers,#Tiers
Data.f 0.5,0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f #Tiers,#Tiers*2
Data.f -0.5,0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f #Tiers*2,#Tiers*2
Data.f -0.5,-0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f #Tiers*2,1
Data.f 0.5,-0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f #Tiers,1
Data.f -0.5,0.5,-0.5
Data.f -1,0,0
Data.l 0
Data.f 0,#Tiers
Data.f -0.5,0.5,0.5
Data.f -1,0,0
Data.l 0
Data.f #Tiers,#Tiers
Data.f -0.5,-0.5,0.5
Data.f -1,0,0
Data.l 0
Data.f #Tiers,#Tiers*2
Data.f -0.5,-0.5,-0.5
Data.f -1,0,0
Data.l 0
Data.f 0,#Tiers*2
Data.f 0.5,0.5,0.5
Data.f 1,0,0
Data.l 0
Data.f #Tiers*2,#Tiers
Data.f 0.5,0.5,-0.5
Data.f 1,0,0
Data.l 0
Data.f 1,#Tiers
Data.f 0.5,-0.5,-0.5
Data.f 1,0,0
Data.l 0
Data.f 1,#Tiers*2
Data.f 0.5,-0.5,0.5
Data.f 1,0,0
Data.l 0
Data.f #Tiers*2,#Tiers*2
Triangles:
Data.w 2,1,0
Data.w 0,3,2
Data.w 6,5,4
Data.w 4,7,6
Data.w 10,9,8
Data.w 8,11,10
Data.w 14,13,12
Data.w 12,15,14
Data.w 18,17,16
Data.w 16,19,18
Data.w 22,21,20
Data.w 20,23,22
EndDataSection
|
|
Auteur : Comtois
| | Version : 20/02/2008 | | |
| |
EnableExplicit
InitEngine3D()
InitSprite()
InitKeyboard()
ExamineDesktops()
OpenScreen(DesktopWidth(0), DesktopHeight(0), DesktopDepth(0), "Cube 3D")
Enumeration
#VueDessus
#VueArriere
#VueCote
#VueAvant
EndEnumeration
Global Angle.f, Vitesse.f
Define.l Options, ModeCamera, i
Vitesse = 1
ModeCamera = #VueArriere
Macro NEW_X(x, Angle, Distance)
((x) + Cos((Angle) * 0.0174533) * (Distance))
EndMacro
Macro NEW_Z(z, Angle, Distance)
((z) - Sin((Angle) * 0.0174533) * (Distance))
EndMacro
Macro AFFICHE_AIDE()
StartDrawing(ScreenOutput())
DrawText(0,0,"Touches [F1] - [F2] - [F3] - [F4] pour changer la vue de la caméra", $FF0000, $00FFFF)
StopDrawing()
EndMacro
Declare.f CurveValue(actuelle.f, Cible.f, P.f)
Declare GestionCamera(Mode.l)
#Mesh = 0
CreateMesh(#Mesh, 100)
Options = #PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_Color | #PB_Mesh_UVCoordinate
SetMeshData(#Mesh, Options , ?Sommets, 24)
SetMeshData(#Mesh, #PB_Mesh_Face, ?Triangles, 12)
#Texture = 0
CreateTexture(#Texture, 64, 64)
StartDrawing(TextureOutput(#Texture))
Box(0, 0, TextureWidth(#Texture), TextureHeight(#Texture), $FFFFFF)
DrawingMode(#PB_2DDrawing_Outlined)
Box(0, 0, TextureWidth(#Texture), TextureHeight(#Texture), 0)
StopDrawing()
#TextureSol = 1
CreateTexture(#TextureSol, 128, 128)
StartDrawing(TextureOutput(#TextureSol))
Box(0, 0, TextureWidth(#TextureSol), TextureHeight(#TextureSol), $FFFFFF)
For i = 0 To 127 Step 10
Line(i, 0, 0, TextureHeight(#TextureSol), $0000FF)
Line(0, i, TextureWidth(#TextureSol), 0, $0000FF)
Next i
DrawingMode(#PB_2DDrawing_Outlined)
Box(0, 0, TextureWidth(#TextureSol), TextureHeight(#TextureSol), $000088)
StopDrawing()
#Matiere = 0
CreateMaterial(#Matiere, TextureID(#Texture))
#MatiereSol = 1
CreateMaterial(#MatiereSol, TextureID(#TextureSol))
#Entity = 0
CreateEntity(#Entity, MeshID(#Mesh), MaterialID(#Matiere))
ScaleEntity(#Entity, 10, 10, 10)
EntityLocate(#Entity, 500, 5, 500)
#EntitySol = 1
CreateEntity(#EntitySol, MeshID(#Mesh), MaterialID(#MatiereSol))
ScaleEntity(#EntitySol, 1000, 2, 1000)
EntityLocate(#EntitySol, 500, -5, 500)
#Camera = 0
CreateCamera(#Camera, 0, 0, 100, 100)
AmbientColor(RGB(55,55,55))
#LightRouge = 0 : CreateLight(#LightRouge,RGB(255, 0, 0), 0, 500, 0)
#LightBleue = 1 : CreateLight(#LightBleue,RGB(0, 0, 255), 0, 500, 1000)
#LightVerte = 2 : CreateLight(#LightVerte,RGB(0, 255, 0), 1000, 500, 1000)
Repeat
If ExamineKeyboard()
If KeyboardReleased(#PB_Key_F1)
ModeCamera = #VueDessus
ElseIf KeyboardReleased(#PB_Key_F2)
ModeCamera = #VueArriere
ElseIf KeyboardReleased(#PB_Key_F3)
ModeCamera = #VueCote
ElseIf KeyboardReleased(#PB_Key_F4)
ModeCamera = #VueAvant
EndIf
If KeyboardPushed(#PB_Key_Left)
Angle + 1
RotateEntity(#Entity, Angle, 0, 0)
ElseIf KeyboardPushed(#PB_Key_Right)
Angle - 1
RotateEntity(#Entity, Angle, 0, 0)
EndIf
If KeyboardPushed(#PB_Key_Up)
MoveEntity(#Entity, NEW_X(0, Angle , Vitesse), 0, NEW_Z(0, Angle, Vitesse))
ElseIf KeyboardPushed(#PB_Key_Down)
MoveEntity(#Entity, NEW_X(0, Angle , -Vitesse), 0, NEW_Z(0, Angle, -Vitesse))
EndIf
EndIf
GestionCamera(ModeCamera)
RenderWorld()
AFFICHE_AIDE()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Procedure.f CurveValue(actuelle.f, Cible.f, P.f)
Define.f Delta
Delta = Cible - actuelle
If P > 1000.0 : P = 1000.0 : EndIf
ProcedureReturn (actuelle + ( Delta * P / 1000.0))
EndProcedure
Procedure GestionCamera(Mode.l)
Define.f Px, Py, Pz, Pv
Static AngleCamera.f
Pv = 25
Select Mode
Case #VueDessus
AngleCamera = CurveValue(AngleCamera, Angle + 180, Pv)
Px = CurveValue(CameraX(#Camera), NEW_X(EntityX(#Entity), AngleCamera, 40), Pv)
Py = CurveValue(CameraY(#Camera), EntityY(#Entity) + 140, Pv)
Pz = CurveValue(CameraZ(#Camera), NEW_Z(EntityZ(#Entity), AngleCamera, 40), Pv)
Case #VueArriere
AngleCamera = CurveValue(AngleCamera, Angle + 180, Pv)
Px = CurveValue(CameraX(#Camera), NEW_X(EntityX(#Entity), AngleCamera, 80), Pv)
Py = CurveValue(CameraY(#Camera), EntityY(#Entity) + 40, Pv)
Pz = CurveValue(CameraZ(#Camera), NEW_Z(EntityZ(#Entity), AngleCamera, 80), Pv)
Case #VueCote
AngleCamera = CurveValue(AngleCamera, Angle + 120, Pv)
Px = CurveValue(CameraX(#Camera), NEW_X(EntityX(#Entity), AngleCamera, 80), Pv)
Py = CurveValue(CameraY(#Camera), EntityY(#Entity) + 40, Pv)
Pz = CurveValue(CameraZ(#Camera), NEW_Z(EntityZ(#Entity), AngleCamera, 80), Pv)
Case #VueAvant
AngleCamera = CurveValue(AngleCamera, Angle, Pv)
Px = CurveValue(CameraX(#Camera), NEW_X(EntityX(#Entity), AngleCamera, 80), Pv)
Py = CurveValue(CameraY(#Camera), EntityY(#Entity) + 40, Pv)
Pz = CurveValue(CameraZ(#Camera), NEW_Z(EntityZ(#Entity), AngleCamera, 80), Pv)
EndSelect
CameraLocate(#Camera, Px, Py, Pz)
CameraLookAt(#Camera, EntityX(#Entity), EntityY(#Entity), EntityZ(#Entity))
EndProcedure
DataSection
Sommets:
Data.f -0.5,0.5,-0.5
Data.f 0,1,0
Data.l 0
Data.f 0,0
Data.f 0.5,0.5,-0.5
Data.f 0,1,0
Data.l 0
Data.f 0,1
Data.f 0.5,0.5,0.5
Data.f 0,1,0
Data.l 0
Data.f 1,1
Data.f -0.5,0.5,0.5
Data.f 0,1,0
Data.l 0
Data.f 1,0
Data.f -0.5,-0.5,0.5
Data.f 0,-1,0
Data.l 0
Data.f 0,0
Data.f 0.5,-0.5,0.5
Data.f 0,-1,0
Data.l 0
Data.f 0,1
Data.f 0.5,-0.5,-0.5
Data.f 0,-1,0
Data.l 0
Data.f 1,1
Data.f -0.5,-0.5,-0.5
Data.f 0,-1,0
Data.l 0
Data.f 1,0
Data.f -0.5,0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f 0,0
Data.f 0.5,0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f 0,1
Data.f 0.5,-0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f 1,1
Data.f -0.5,-0.5,0.5
Data.f 0,0,1
Data.l 0
Data.f 1,0
Data.f 0.5,0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f 0,0
Data.f -0.5,0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f 0,1
Data.f -0.5,-0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f 1,1
Data.f 0.5,-0.5,-0.5
Data.f 0,0,-1
Data.l 0
Data.f 1,0
Data.f -0.5,0.5,-0.5
Data.f -1,0,0
Data.l 0
Data.f 0,0
Data.f -0.5,0.5,0.5
Data.f -1,0,0
Data.l 0
Data.f 0,1
Data.f -0.5,-0.5,0.5
Data.f -1,0,0
Data.l 0
Data.f 1,1
Data.f -0.5,-0.5,-0.5
Data.f -1,0,0
Data.l 0
Data.f 1,0
Data.f 0.5,0.5,0.5
Data.f 1,0,0
Data.l 0
Data.f 0,0
Data.f 0.5,0.5,-0.5
Data.f 1,0,0
Data.l 0
Data.f 0,1
Data.f 0.5,-0.5,-0.5
Data.f 1,0,0
Data.l 0
Data.f 1,1
Data.f 0.5,-0.5,0.5
Data.f 1,0,0
Data.l 0
Data.f 1,0
Triangles:
Data.w 2,1,0
Data.w 0,3,2
Data.w 6,5,4
Data.w 4,7,6
Data.w 10,9,8
Data.w 8,11,10
Data.w 14,13,12
Data.w 12,15,14
Data.w 18,17,16
Data.w 16,19,18
Data.w 22,21,20
Data.w 20,23,22
EndDataSection
|
|
Consultez les autres pages sources
Les sources présentés sur cette pages sont libre de droits,
et vous pouvez les utiliser à votre convenance. Par contre cette page de présentation de ces sources constitue une oeuvre intellectuelle protégée par les droits d'auteurs.
Copyright ©2008
Developpez LLC. Tout droits réservés Developpez LLC.
Aucune reproduction, même partielle, ne peut être faite de ce site et de
l'ensemble de son contenu : textes, documents et images sans l'autorisation
expresse de Developpez LLC. Sinon vous encourez selon la loi jusqu'à 3 ans
de prison et jusqu'à 300 000 E de dommages et intérets.
Cette page est déposée à la SACD.
|