Skip to main content

AccessTr.neT


(OPEN GL)-3D Oyun İçin Başlangıç

(OPEN GL)-3D Oyun İçin Başlangıç

#1
Programimiz icin oncelikle yapmaniz gerekenler ..:

1. Visual Basic Menusunden Project --> References Bolumunden --- VB OpenGL API 1.2(ANSI) Secenegini Activ Kiliniz.

2. Bir Form ismi Form1 bir Modul ismi Module1 ve bir de Timer ismi Timer1 ihtiyacimiz var.

'Forma Yazilacak Kisim Burdan itibaren Baslamaktadir....(Copy Paste yapsaniz yeterli olur.)
Kod:
Option Explicit

Dim xAngle As GLfloat
Dim yAngle As GLfloat
Dim zAngle As GLfloat
Dim YScale As Long
Dim XScale As Long

Private Sub Form_Load()
Dim hGLRC As Long
Dim fAspect As GLfloat
Call InitializeArrays

Form1.ScaleMode = 3

xAngle = 0
yAngle = 0
zAngle = 0

SetupPixelFormat hDC

hGLRC = wglCreateContext(hDC)
wglMakeCurrent hDC, hGLRC
glEnable GL_DEPTH_TEST
glEnable GL_DITHER
glDepthFunc GL_LESS
glClearDepth 1
glClearColor 0, 0, 0, 0
glMatrixMode GL_PROJECTION
glLoadIdentity
If Form1.ScaleHeight > 0 Then
fAspect = Form1.ScaleWidth / Form1.ScaleHeight
Else
fAspect = 0
End If

gluPerspective 60, fAspect, 1, 2000
glViewport 0, 0, Form1.ScaleWidth, Form1.ScaleHeight


glMatrixMode GL_MODELVIEW
glLoadIdentity

glLightfv GL_LIGHT0, GL_POSITION, LightPos(0)

glEnable GL_LIGHTING
glEnable GL_LIGHT0
glShadeModel GL_SMOOTH
glFrontFace GL_CCW

glMaterialfv GL_FRONT, GL_SPECULAR, SpecRef(0)
glMateriali GL_FRONT, GL_SHININESS, 50

BuildCube

Form_Paint

End Sub
Private Sub Form_Paint()
Dim i As Integer
Dim a As Integer
Dim b As Integer
Dim c As Integer

glLoadIdentity
gluLookAt m_Translate_X, m_Translate_Y, m_Translate_Z, m_Translate_X + (100# * (Cos(m_camera_radsFromEast))), m_Translate_Y + m_camera_direction_y, m_Translate_Z - (100# * Sin(m_camera_radsFromEast)), 0#, 1#, 0#


glClear GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT
glPushMatrix
glTranslatef 0, 0, -3
glRotatef xAngle, 0.1, 0, 0
glRotatef yAngle, 0, 0.1, 0
glRotatef zAngle, 0, 0, 1

glCallList m_Cube
glPopMatrix

glPushMatrix
glTranslatef 0, -2, 0

glPopMatrix

SwapBuffers hDC

End Sub
Private Sub Form_Resize()

glViewport 0, 0, Form1.ScaleWidth, Form1.ScaleHeight
Form_Paint
End Sub
Private Sub Form_Unload(Cancel As Integer)
If hGLRC <> 0 Then
wglMakeCurrent 0, 0
wglDeleteContext hGLRC
End If

If hPalette <> 0 Then
DeleteObject hPalette
End If

End Sub
Sub BuildCube()
Dim i As Integer
Dim a As Integer
Dim b As Integer
Dim c As Integer

m_Cube = glGenLists(1)
glNewList m_Cube, GL_COMPILE_AND_EXECUTE

For i = 0 To TRIANGLE_COUNT - 1
a = index(i, 0)
b = index(i, 1)
c = index(i, 2)
Call RenderTriangle(a, b, c)
Next

glEnd
glEndList

End Sub

Private Sub Timer1_Timer()
YScale = YScale + 1
XScale = XScale + 1
yAngle = YScale
xAngle = XScale
If YScale = 359 Then
YScale = 0
XScale = 0
End If

Form_Paint

End Sub

'Forma Yazilacak Kisim Burda Sona erdi (Umarim Copy Paste Yapmissinizdir.)

-------------------------------------------------------------------------------------------------------------------------------------


'Burasida Module Yazilacak Kisim (yine Hatirlatirim Copy Paste Yapiniz.)

Option Explicit
Private Type PALETTEENTRY
peRed As Byte
peGreen As Byte
peBlue As Byte
peFlags As Byte
End Type
Private Type LOGPALETTE
palVersion As Integer
palNumEntries As Integer
palPalEntry(0 To 255) As PALETTEENTRY
End Type
Private Type PIXELFORMATDESCRIPTOR
nSize As Integer
nVersion As Integer
dwFlags As Long
iPixelType As Byte
cColorBits As Byte
cRedBits As Byte
cRedShift As Byte
cGreenBits As Byte
cGreenShift As Byte
cBlueBits As Byte
cBlueShift As Byte
cAlphaBits As Byte
cAlphaShift As Byte
cAccumBits As Byte
cAccumRedBits As Byte
cAccumGreenBits As Byte
cAccumBlueBits As Byte
cAccumAlpgaBits As Byte
cDepthBits As Byte
cStencilBits As Byte
cAuxBuffers As Byte
iLayerType As Byte
bReserved As Byte
dwLayerMask As Long
dwVisibleMask As Long
dwDamageMask As Long
End Type

Const PFD_TYPE_RGBA = 0
Const PFD_TYPE_COLORINDEX = 1
Const PFD_MAIN_PLANE = 0
Const PFD_DOUBLEBUFFER = 1
Const PFD_DRAW_TO_WINDOW = &H4
Const PFD_SUPPORT_OPENGL = &H20
Const PFD_NEED_PALETTE = &H80

Private Declare Function ChoosePixelFormat Lib "gdi32" (ByVal hDC As Long, pfd As PIXELFORMATDESCRIPTOR) As Long
Private Declare Function CreatePalette Lib "gdi32" (pPal As LOGPALETTE) As Long
Private Declare Sub DeleteObject Lib "gdi32" (hObject As Long)
Private Declare Sub DescribePixelFormat Lib "gdi32" (ByVal hDC As Long, ByVal PixelFormat As Long, ByVal nBytes As Long, pfd As PIXELFORMATDESCRIPTOR)
Private Declare Function GetDC Lib "gdi32" (ByVal hWnd As Long) As Long
Private Declare Function GetPixelFormat Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Sub GetSystemPaletteEntries Lib "gdi32" (ByVal hDC As Long, ByVal start As Long, ByVal entries As Long, ByVal ptrEntries As Long)
Private Declare Sub RealizePalette Lib "gdi32" (ByVal hPalette As Long)
Private Declare Sub SelectPalette Lib "gdi32" (ByVal hDC As Long, ByVal hPalette As Long, ByVal bln As Long)
Private Declare Function SetPixelFormat Lib "gdi32" (ByVal hDC As Long, ByVal i As Long, pfd As PIXELFORMATDESCRIPTOR) As Boolean
Private Declare Sub SwapBuffers Lib "gdi32" (ByVal hDC As Long)
Private Declare Function wglCreateContext Lib "OpenGL32" (ByVal hDC As Long) As Long
Private Declare Sub wglDeleteContext Lib "OpenGL32" (ByVal hContext As Long)
Private Declare Sub wglMakeCurrent Lib "OpenGL32" (ByVal l1 As Long, ByVal l2 As Long)

Public hPalette As Long
Public hGLRC As Long

Public LightPos(3) As GLfloat
Public SpecRef(3) As GLfloat
Public Diffuse(3) As GLfloat

Public Const TRIANGLE_COUNT = 12

Public vdata(23, 2) As GLfloat
Public vcolor(23, 2) As GLfloat
Public index(TRIANGLE_COUNT, 3) As GLfloat
Public m_Grid As Integer
Public m_Cube As Integer
Public m_Translate_X As Integer
Public m_Translate_Y As Integer
Public m_Translate_Z As Integer
Public m_camera_radsFromEast As GLfloat
Public m_translationUnit As Double
Public m_camera_direction_y As Integer

Sub FatalError(ByVal strMessage As String)
MsgBox "Fatal Error: " & strMessage, vbCritical + vbApplicationModal + vbOKOnly + vbDefaultButton1, "Fatal Error In " & App.Title
Unload frmMain
Set frmMain = Nothing
End
End Sub
Sub SetupPixelFormat(ByVal hDC As Long)
Dim pfd As PIXELFORMATDESCRIPTOR
Dim PixelFormat As Integer
pfd.nSize = Len(pfd)
pfd.nVersion = 1
pfd.dwFlags = PFD_SUPPORT_OPENGL Or PFD_DRAW_TO_WINDOW Or PFD_DOUBLEBUFFER Or PFD_TYPE_RGBA
pfd.iPixelType = PFD_TYPE_RGBA
pfd.cColorBits = 24
pfd.cDepthBits = 24
pfd.iLayerType = PFD_MAIN_PLANE
PixelFormat = ChoosePixelFormat(hDC, pfd)
If PixelFormat = 0 Then FatalError "Could not retrieve pixel format!"
SetPixelFormat hDC, PixelFormat, pfd
End Sub
Sub SetupPalette(ByVal lhDC As Long)
Dim PixelFormat As Long
Dim pfd As PIXELFORMATDESCRIPTOR
Dim pPal As LOGPALETTE
Dim PaletteSize As Long
PixelFormat = GetPixelFormat(lhDC)
DescribePixelFormat lhDC, PixelFormat, Len(pfd), pfd
If (pfd.dwFlags And PFD_NEED_PALETTE) <> 0 Then
PaletteSize = 2 ^ pfd.cColorBits
Else
Exit Sub
End If

pPal.palVersion = &H300
pPal.palNumEntries = PaletteSize
Dim redMask As Long
Dim GreenMask As Long
Dim BlueMask As Long
Dim i As Long
redMask = 2 ^ pfd.cRedBits - 1
GreenMask = 2 ^ pfd.cGreenBits - 1
BlueMask = 2 ^ pfd.cBlueBits - 1
For i = 0 To PaletteSize - 1
With pPal.palPalEntry(i)
.peRed = i
.peGreen = i
.peBlue = i
.peFlags = 0
End With
Next
GetSystemPaletteEntries frmMain.hDC, 0, 256, VarPtr(pPal.palPalEntry(0))
hPalette = CreatePalette(pPal)
If hPalette <> 0 Then
SelectPalette lhDC, hPalette, False
RealizePalette lhDC
End If
End Sub

Public Sub InitializeArrays()
m_Translate_X = 0
m_Translate_Z = 5
m_translationUnit = 1
m_camera_direction_y = 0
m_camera_radsFromEast = 1.56

LightPos(0) = 0
LightPos(1) = 2
LightPos(2) = 2
LightPos(3) = 1

SpecRef(0) = 1#
SpecRef(1) = 0#
SpecRef(2) = 0#
SpecRef(3) = 1#


'Front (0-3)
vdata(0, 0) = 1
vdata(0, 1) = 1
vdata(0, 2) = 1
vdata(1, 0) = 1
vdata(1, 1) = -1
vdata(1, 2) = 1
vdata(2, 0) = -1
vdata(2, 1) = -1
vdata(2, 2) = 1
vdata(3, 0) = -1
vdata(3, 1) = 1
vdata(3, 2) = 1

'back (4-7)
vdata(4, 0) = 1#
vdata(4, 1) = 1#
vdata(4, 2) = -1#
vdata(5, 0) = 1#
vdata(5, 1) = -1#
vdata(5, 2) = -1#
vdata(6, 0) = -1#
vdata(6, 1) = -1#
vdata(6, 2) = -1#
vdata(7, 0) = -1#
vdata(7, 1) = 1#
vdata(7, 2) = -1#
'right (8-11)
vdata(8, 0) = 1#
vdata(8, 1) = 1#
vdata(8, 2) = 1#
vdata(9, 0) = 1#
vdata(9, 1) = 1#
vdata(9, 2) = -1#
vdata(10, 0) = 1#
vdata(10, 1) = -1#
vdata(10, 2) = -1#
vdata(11, 0) = 1#
vdata(11, 1) = -1#
vdata(11, 2) = 1#
'left (12-15)
vdata(12, 0) = -1#
vdata(12, 1) = 1#
vdata(12, 2) = 1#
vdata(13, 0) = -1#
vdata(13, 1) = 1#
vdata(13, 2) = -1#
vdata(14, 0) = -1#
vdata(14, 1) = -1#
vdata(14, 2) = -1#
vdata(15, 0) = -1#
vdata(15, 1) = -1#
vdata(15, 2) = 1#

'Top (16-20)
vdata(16, 0) = 1#
vdata(16, 1) = 1#
vdata(16, 2) = 1#
vdata(17, 0) = 1#
vdata(17, 1) = 1#
vdata(17, 2) = -1#
vdata(18, 0) = -1#
vdata(18, 1) = 1#
vdata(18, 2) = -1#
vdata(19, 0) = -1#
vdata(19, 1) = 1#
vdata(19, 2) = 1#

'Botton
vdata(20, 0) = 1#
vdata(20, 1) = -1#
vdata(20, 2) = 1#
vdata(21, 0) = 1#
vdata(21, 1) = -1#
vdata(21, 2) = -1#
vdata(22, 0) = -1#
vdata(22, 1) = -1#
vdata(22, 2) = -1#
vdata(23, 0) = -1#
vdata(23, 1) = -1#
vdata(23, 2) = 1#


'Index
'front
index(0, 0) = 0
index(0, 1) = 1
index(0, 2) = 2
index(1, 0) = 0
index(1, 1) = 2
index(1, 2) = 3
'Back
index(2, 0) = 4
index(2, 1) = 6
index(2, 2) = 5
index(3, 0) = 4
index(3, 1) = 7
index(3, 2) = 6
'Right
index(4, 0) = 8
index(4, 1) = 9
index(4, 2) = 10
index(5, 0) = 8
index(5, 1) = 10
index(5, 2) = 11
'Left
index(6, 0) = 12
index(6, 1) = 14
index(6, 2) = 13
index(7, 0) = 12
index(7, 1) = 15
index(7, 2) = 14
'Top
index(8, 0) = 16
index(8, 1) = 18
index(8, 2) = 17
index(9, 0) = 16
index(9, 1) = 19
index(9, 2) = 18
'Bottom
index(10, 0) = 20
index(10, 1) = 21
index(10, 2) = 22
index(11, 0) = 20
index(11, 1) = 22
index(11, 2) = 23

'Color
'front
vcolor(0, 0) = 1
vcolor(0, 1) = 1
vcolor(0, 2) = 1
vcolor(1, 0) = 1
vcolor(1, 1) = 0
vcolor(1, 2) = 1
vcolor(2, 0) = 0
vcolor(2, 1) = 0
vcolor(2, 2) = 1
vcolor(3, 0) = 0
vcolor(3, 1) = 1
vcolor(3, 2) = 1
'back
vcolor(4, 0) = 1#
vcolor(4, 1) = 1#
vcolor(4, 2) = 0#
vcolor(5, 0) = 1#
vcolor(5, 1) = 0#
vcolor(5, 2) = 0#
vcolor(6, 0) = 0#
vcolor(6, 1) = 0#
vcolor(6, 2) = 0#
vcolor(7, 0) = 0#
vcolor(7, 1) = 1#
vcolor(7, 2) = 0#
'right
vcolor(8, 0) = 1#
vcolor(8, 1) = 1#
vcolor(8, 2) = 1#
vcolor(9, 0) = 1#
vcolor(9, 1) = 1#
vcolor(9, 2) = 0#
vcolor(10, 0) = 1#
vcolor(10, 1) = 0#
vcolor(10, 2) = 0#
vcolor(11, 0) = 1#
vcolor(11, 1) = 0#
vcolor(11, 2) = 1#
'left
vcolor(12, 0) = 0#
vcolor(12, 1) = 0.1
vcolor(12, 2) = 1#
vcolor(13, 0) = 0#
vcolor(13, 1) = 1#
vcolor(13, 2) = 0#
vcolor(14, 0) = 0#
vcolor(14, 1) = 0#
vcolor(14, 2) = 0#
vcolor(15, 0) = 0#
vcolor(15, 1) = 0#
vcolor(15, 2) = 1#
'Top
vcolor(16, 0) = 1#
vcolor(16, 1) = 1#
vcolor(16, 2) = 1#
vcolor(17, 0) = 1#
vcolor(17, 1) = 1#
vcolor(17, 2) = 0#
vcolor(18, 0) = 0#
vcolor(18, 1) = 1#
vcolor(18, 2) = 0#
vcolor(19, 0) = 0#
vcolor(19, 1) = 1#
vcolor(19, 2) = 1#
'Bottom
vcolor(20, 0) = 1#
vcolor(20, 1) = 0#
vcolor(20, 2) = 1#
vcolor(21, 0) = 1#
vcolor(21, 1) = 0#
vcolor(21, 2) = 0#
vcolor(22, 0) = 0#
vcolor(22, 1) = 0#
vcolor(22, 2) = 0#
vcolor(23, 0) = 0#
vcolor(23, 1) = 0#
vcolor(23, 2) = 1#


End Sub

Public Sub RenderTriangle(a As Integer, b As Integer, c As Integer)
Dim x1 As GLfloat
Dim y1 As GLfloat
Dim z1 As GLfloat
Dim lRC As Long

Dim x2 As GLfloat
Dim y2 As GLfloat
Dim z2 As GLfloat

Dim x3 As GLfloat
Dim y3 As GLfloat
Dim z3 As GLfloat

Dim v1(3) As GLfloat
Dim v2(3) As GLfloat
Dim v3(3) As GLfloat

Dim v(3) As GLfloat
Dim w(3) As GLfloat
Dim out(3) As GLfloat

Dim r1 As GLfloat
Dim g1 As GLfloat
Dim b1 As GLfloat

Dim r2 As GLfloat
Dim g2 As GLfloat
Dim b2 As GLfloat

Dim r3 As GLfloat
Dim g3 As GLfloat
Dim b3 As GLfloat

b1 = vcolor(a, 1)
g1 = vcolor(a, 2)

r2 = vcolor(b, 0)
b2 = vcolor(b, 1)
g2 = vcolor(b, 2)

r3 = vcolor(c, 0)
b3 = vcolor(c, 1)
g3 = vcolor(c, 2)

v1(0) = vdata(a, 0)
v1(1) = vdata(a, 1)
v1(2) = vdata(a, 2)

x1 = vdata(a, 0)
y1 = vdata(a, 1)
z1 = vdata(a, 2)

v2(0) = vdata(b, 0)
v2(1) = vdata(b, 1)
v2(2) = vdata(b, 2)

x2 = vdata(b, 0)
y2 = vdata(b, 1)
z2 = vdata(b, 2)

v3(0) = vdata(c, 0)
v3(1) = vdata(c, 1)
v3(2) = vdata(c, 2)

x3 = vdata(c, 0)
y3 = vdata(c, 1)
z3 = vdata(c, 2)
'--------------------------------------------------------------------


v(0) = x2 - x1
v(1) = y2 - y1
v(2) = z2 - z1

w(0) = x3 - x2
w(1) = y3 - y2
w(2) = z3 - z2

Call normcrossprod(v, w, out)

glBegin (GL_TRIANGLES)
'Flip the normal
glNormal3f -1 * out(0), -1 * out(1), -1 * out(2)
Diffuse(0) = r1
Diffuse(1) = b1
Diffuse(2) = g1
Diffuse(3) = 1
glMaterialfv GL_FRONT, GL_AMBIENT_AND_DIFFUSE, Diffuse(0)
glVertex3f v1(0), v1(1), v1(2)
Diffuse(0) = r2
Diffuse(1) = b2
Diffuse(2) = g2
Diffuse(3) = 1
glMaterialfv GL_FRONT, GL_AMBIENT_AND_DIFFUSE, Diffuse(0)
glVertex3f v2(0), v2(1), v2(2)
Diffuse(0) = r3
Diffuse(1) = b3
Diffuse(2) = g3
Diffuse(3) = 1
glMaterialfv GL_FRONT, GL_AMBIENT_AND_DIFFUSE, Diffuse(0)
glVertex3f v3(0), v3(1), v3(2)
glEnd

End Sub

Public Sub normalize(out() As GLfloat)
Dim d As GLfloat
d = Sqr(out(0) * out(0) + out(1) * out(1) + out(2) * out(2))
If (d = 0) Then
Exit Sub
End If
out(0) = out(0) / d
out(1) = out(1) / d
out(2) = out(2) / d

End Sub

Public Sub normcrossprod(v() As GLfloat, w() As GLfloat, out() As GLfloat)
'[Vx Vy Vz] X [Wx Wy Wz] =[(Vy*Wz-Wy*Vz),(Wx*Vz-Vx*Wz),(Vx*Wy-Wx*Vy)]
out(0) = v(1) * w(2) - w(1) * v(2)
out(1) = w(0) * v(2) - v(0) * w(2)
out(2) = v(0) * w(1) - w(0) * v(1)
Call normalize(out)

End Sub

---------------------------------------------------------------------------------------------------------------------------


'Module ve Programa yazilacaklar bu kadar umarim isinize yarar

OpenGl Destekleyen Ekran Kartların da Bu Code Ekran Kartının Verimliliği Artırmaktadır.
İsteyen Olursa OpenGl Ekran Kartlarının Driver Code’lerini Paylaşabilirim.
Code’ler Basit Görünebilir, Ancak Ekran Pixsel Resulation Hesaplandığı İçin Aslında Baya Nitelikli.Kolay gelsin
Cevapla
#2
teşekkürler sn. sesman
Cevapla
#3
Teşekkürler..
Cevapla
#4
Paylaşımlarınız için teşekkürler.
Kaplumbağa ya dikkat et...
Sadece  başını çıkartıp risk aldığında ilerleyebiliyor...
Cevapla
#5
Sn sesman paylaşım için teşekkürler. Saygılar...
Bilgi paylaşıldıkça çoğalır....
Her engel, yaşam koşullarınızı daha iyileştirecek bir fırsattır.


Access için her zaman lazım olacak konu başlıkları listesi


Cevapla
#6
deneyen oldumu acaba
@benbendedeilem
Cevapla

Bir hesap oluşturun veya yorum yapmak için giriş yapın

Yorum yapmak için üye olmanız gerekiyor

ya da
Task