About to draw chunk
This commit is contained in:
28
tools/editortool/map/camera.py
Normal file
28
tools/editortool/map/camera.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import math
|
||||
from OpenGL.GL import *
|
||||
from OpenGL.GLU import *
|
||||
from dusk.defs import defs
|
||||
|
||||
pixelsPerUnit = float(defs.get('RPG_CAMERA_PIXELS_PER_UNIT'))
|
||||
yOffset = float(defs.get('RPG_CAMERA_Z_OFFSET'))
|
||||
fov = float(defs.get('RPG_CAMERA_FOV'))
|
||||
scale = 8.0
|
||||
|
||||
def setupCamera(vw, vh):
|
||||
z = (vh / 2.0) / (
|
||||
(pixelsPerUnit * scale) * math.tan(math.radians(fov / 2.0))
|
||||
)
|
||||
lookAt = ( 0, 0, 0 )
|
||||
aspectRatio = vw / vh
|
||||
position = lookAt[0], lookAt[1] - yOffset, lookAt[2] + z
|
||||
|
||||
glMatrixMode(GL_PROJECTION)
|
||||
glLoadIdentity()
|
||||
gluPerspective(fov, aspectRatio, 0.1, 1000.0)
|
||||
glMatrixMode(GL_MODELVIEW)
|
||||
glLoadIdentity()
|
||||
gluLookAt(
|
||||
position[0], position[1], position[2],
|
||||
lookAt[0], lookAt[1], lookAt[2],
|
||||
0.0, 1.0, 0.0
|
||||
)
|
||||
0
tools/editortool/map/chunk.py
Normal file
0
tools/editortool/map/chunk.py
Normal file
@@ -2,104 +2,39 @@ from PyQt5.QtCore import QTimer
|
||||
from PyQt5.QtWidgets import QOpenGLWidget
|
||||
from OpenGL.GL import *
|
||||
from OpenGL.GLU import *
|
||||
from editortool.map.selectbox import drawSelectBox
|
||||
from editortool.map.tile import drawTile
|
||||
from editortool.map.camera import setupCamera
|
||||
|
||||
class GLWidget(QOpenGLWidget):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.xRot = 20.0
|
||||
self.yRot = 30.0
|
||||
self.zRot = 0.0
|
||||
self.rotation_speed = 2.0
|
||||
self.timer = QTimer(self)
|
||||
self.timer.timeout.connect(self.updateRotation)
|
||||
self.timer.timeout.connect(self.update)
|
||||
self.timer.start(16) # ~60 FPS
|
||||
|
||||
def initializeGL(self):
|
||||
version = glGetString(GL_VERSION)
|
||||
print("GL version:", version)
|
||||
glClearColor(0.1, 0.1, 0.1, 1.0)
|
||||
glClearColor(0.392, 0.584, 0.929, 1.0)
|
||||
glEnable(GL_DEPTH_TEST)
|
||||
glShadeModel(GL_SMOOTH)
|
||||
glEnable(GL_COLOR_MATERIAL)
|
||||
glEnable(GL_LIGHTING)
|
||||
glEnable(GL_LIGHT0)
|
||||
light_position = [4.0, 4.0, 8.0, 1.0]
|
||||
glLightfv(GL_LIGHT0, GL_POSITION, light_position)
|
||||
|
||||
def resizeGL(self, w, h):
|
||||
if h == 0:
|
||||
h = 1
|
||||
glViewport(0, 0, w, h)
|
||||
glMatrixMode(GL_PROJECTION)
|
||||
glLoadIdentity()
|
||||
glLoadIdentity()
|
||||
gluPerspective(45.0, float(w) / float(h), 0.1, 100.0)
|
||||
glMatrixMode(GL_MODELVIEW)
|
||||
pass
|
||||
|
||||
|
||||
def paintGL(self):
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
||||
glLoadIdentity()
|
||||
glTranslatef(0.0, 0.0, -7.0)
|
||||
glRotatef(self.xRot, 1.0, 0.0, 0.0)
|
||||
glRotatef(self.yRot, 0.0, 1.0, 0.0)
|
||||
glRotatef(self.zRot, 0.0, 0.0, 1.0)
|
||||
self.drawCube()
|
||||
|
||||
def drawCube(self):
|
||||
glBegin(GL_QUADS)
|
||||
# Front (red)
|
||||
glColor3f(1.0, 0.0, 0.0)
|
||||
glVertex3f(-1.0, -1.0, 1.0)
|
||||
glVertex3f( 1.0, -1.0, 1.0)
|
||||
glVertex3f( 1.0, 1.0, 1.0)
|
||||
glVertex3f(-1.0, 1.0, 1.0)
|
||||
# Back (green)
|
||||
glColor3f(0.0, 1.0, 0.0)
|
||||
glVertex3f(-1.0, -1.0, -1.0)
|
||||
glVertex3f(-1.0, 1.0, -1.0)
|
||||
glVertex3f( 1.0, 1.0, -1.0)
|
||||
glVertex3f( 1.0, -1.0, -1.0)
|
||||
# Top (blue)
|
||||
glColor3f(0.0, 0.0, 1.0)
|
||||
glVertex3f(-1.0, 1.0, -1.0)
|
||||
glVertex3f(-1.0, 1.0, 1.0)
|
||||
glVertex3f( 1.0, 1.0, 1.0)
|
||||
glVertex3f( 1.0, 1.0, -1.0)
|
||||
# Bottom (yellow)
|
||||
glColor3f(1.0, 1.0, 0.0)
|
||||
glVertex3f(-1.0, -1.0, -1.0)
|
||||
glVertex3f( 1.0, -1.0, -1.0)
|
||||
glVertex3f( 1.0, -1.0, 1.0)
|
||||
glVertex3f(-1.0, -1.0, 1.0)
|
||||
# Right (magenta)
|
||||
glColor3f(1.0, 0.0, 1.0)
|
||||
glVertex3f( 1.0, -1.0, -1.0)
|
||||
glVertex3f( 1.0, 1.0, -1.0)
|
||||
glVertex3f( 1.0, 1.0, 1.0)
|
||||
glVertex3f( 1.0, -1.0, 1.0)
|
||||
# Left (cyan)
|
||||
glColor3f(0.0, 1.0, 1.0)
|
||||
glVertex3f(-1.0, -1.0, -1.0)
|
||||
glVertex3f(-1.0, -1.0, 1.0)
|
||||
glVertex3f(-1.0, 1.0, 1.0)
|
||||
glVertex3f(-1.0, 1.0, -1.0)
|
||||
glEnd()
|
||||
w = self.width()
|
||||
h = self.height()
|
||||
if h <= 0:
|
||||
h = 1
|
||||
if w <= 0:
|
||||
w = 1
|
||||
|
||||
def startRotation(self):
|
||||
if not self.timer.isActive():
|
||||
self.timer.start(30)
|
||||
glViewport(0, 0, w, h)
|
||||
setupCamera(self.width(), self.height()) # <-- Add this here
|
||||
|
||||
def stopRotation(self):
|
||||
if self.timer.isActive():
|
||||
self.timer.stop()
|
||||
|
||||
def resetView(self):
|
||||
self.xRot = 20.0
|
||||
self.yRot = 30.0
|
||||
self.zRot = 0.0
|
||||
self.update()
|
||||
|
||||
def updateRotation(self):
|
||||
self.xRot += self.rotation_speed
|
||||
self.yRot += self.rotation_speed * 0.8
|
||||
self.zRot += self.rotation_speed * 0.5
|
||||
self.update()
|
||||
# Draw test quad at 0,0,0 to 1,1,0
|
||||
drawTile(0, 0, 0)
|
||||
drawSelectBox(0, 0, 0)
|
||||
48
tools/editortool/map/selectbox.py
Normal file
48
tools/editortool/map/selectbox.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import OpenGL.GL as gl
|
||||
from dusk.defs import defs
|
||||
import colorsys
|
||||
|
||||
hue = [0.0] # Mutable container for static hue
|
||||
|
||||
def drawSelectBox(x, y, z):
|
||||
w = float(defs.get('TILE_WIDTH'))
|
||||
h = float(defs.get('TILE_HEIGHT'))
|
||||
d = float(defs.get('TILE_DEPTH'))
|
||||
|
||||
x = x * w
|
||||
y = y * h
|
||||
z = z * d
|
||||
|
||||
# Center box.
|
||||
x -= w / 2.0
|
||||
y -= h / 2.0
|
||||
z -= d / 2.0
|
||||
|
||||
# Define the 8 vertices of the cube with w=h=d=1
|
||||
vertices = [
|
||||
(x, y, z), # 0: min corner
|
||||
(x+w, y, z), # 1
|
||||
(x+w, y+h, z), # 2
|
||||
(x, y+h, z), # 3
|
||||
(x, y, z+d), # 4
|
||||
(x+w, y, z+d), # 5
|
||||
(x+w, y+h, z+d), # 6
|
||||
(x, y+h, z+d) # 7
|
||||
]
|
||||
# List of edges as pairs of vertex indices
|
||||
edges = [
|
||||
(0, 1), (1, 2), (2, 3), (3, 0), # bottom face
|
||||
(4, 5), (5, 6), (6, 7), (7, 4), # top face
|
||||
(0, 4), (1, 5), (2, 6), (3, 7) # vertical edges
|
||||
]
|
||||
|
||||
# Cycle hue
|
||||
hue[0] = (hue[0] + 0.01) % 1.0
|
||||
r, g, b = colorsys.hsv_to_rgb(hue[0], 1.0, 1.0)
|
||||
gl.glColor3f(r, g, b)
|
||||
gl.glLineWidth(2.0)
|
||||
gl.glBegin(gl.GL_LINES)
|
||||
for edge in edges:
|
||||
for vertex in edge:
|
||||
gl.glVertex3f(*vertices[vertex])
|
||||
gl.glEnd()
|
||||
25
tools/editortool/map/tile.py
Normal file
25
tools/editortool/map/tile.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from OpenGL.GL import *
|
||||
from dusk.defs import defs
|
||||
|
||||
def drawTile(x, y, z):
|
||||
w = float(defs.get('TILE_WIDTH'))
|
||||
h = float(defs.get('TILE_HEIGHT'))
|
||||
d = float(defs.get('TILE_DEPTH'))
|
||||
|
||||
x = x * w
|
||||
y = y * h
|
||||
z = z * d
|
||||
|
||||
# Center tile.
|
||||
x -= w / 2.0
|
||||
y -= h / 2.0
|
||||
z -= d / 2.0
|
||||
|
||||
# Draw the tile as a flat square on the X-Y plane at depth z.
|
||||
glColor3f(1.0, 0.0, 0.0) # Red color
|
||||
glBegin(GL_QUADS)
|
||||
glVertex3f(x, y, z) # Bottom-left
|
||||
glVertex3f(x + w, y, z) # Bottom-right
|
||||
glVertex3f(x + w, y + h, z) # Top-right
|
||||
glVertex3f(x, y + h, z) # Top-left
|
||||
glEnd()
|
||||
Reference in New Issue
Block a user