opengl based render!

This commit is contained in:
shenjack 2023-08-12 23:32:43 +08:00
parent e888ddfdad
commit d398ed1af9
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 11 additions and 13 deletions

View File

@ -11,6 +11,8 @@
- 将 `sr1_ship` 渲染器使用的 Camera 改成 `CenterGroupCamera` - 将 `sr1_ship` 渲染器使用的 Camera 改成 `CenterGroupCamera`
- 删除了之前的 Camera 相关代码 - 删除了之前的 Camera 相关代码
- 将用于渲染到材质的代码部分改为使用 `glScissor``glViewport`
- 优化了一点性能 ( 毕竟是OpenGL )
## 20230809 DR game 0.3.2.1 ## 20230809 DR game 0.3.2.1

View File

@ -12,12 +12,11 @@ import traceback
from pathlib import Path from pathlib import Path
from typing import List, TYPE_CHECKING, Dict, Optional, Generator, Tuple from typing import List, TYPE_CHECKING, Dict, Optional, Generator, Tuple
from pyglet.math import Vec4, Mat4 from pyglet.gl import gl
from pyglet.text import Label from pyglet.text import Label
from pyglet.sprite import Sprite from pyglet.sprite import Sprite
from pyglet.graphics import Batch, Group from pyglet.graphics import Batch, Group
from pyglet.shapes import Line, Rectangle from pyglet.shapes import Line, Rectangle
from pyglet.image import Framebuffer, Texture
from . import DR_mod_runtime from . import DR_mod_runtime
from .types import SR1Textures, SR1Rotation from .types import SR1Textures, SR1Rotation
@ -75,12 +74,9 @@ class SR1ShipRender(BaseScreen):
self.dy = 0 self.dy = 0
self.width = main_window.width self.width = main_window.width
self.height = main_window.height self.height = main_window.height
self.buffer = Framebuffer()
self.render_texture = Texture.create(self.width, self.height)
self.buffer.attach_texture(self.render_texture)
self.main_batch = Batch() self.main_batch = Batch()
self.group_camera = CenterGroupCamera(window=self, self.group_camera = CenterGroupCamera(window=main_window,
order=10, order=10,
parent=main_window.main_group, parent=main_window.main_group,
min_zoom=(1 / 2) ** 10, min_zoom=(1 / 2) ** 10,
@ -129,8 +125,6 @@ class SR1ShipRender(BaseScreen):
def size(self, value: Tuple[int, int]): def size(self, value: Tuple[int, int]):
if not self.width == value[0] or not self.height == value[1]: if not self.width == value[0] or not self.height == value[1]:
self.width, self.height = value self.width, self.height = value
self.render_texture = Texture.create(self.width, self.height)
self.buffer.attach_texture(self.render_texture)
def load_xml(self, file_path: str) -> bool: def load_xml(self, file_path: str) -> bool:
""" """
@ -281,11 +275,14 @@ class SR1ShipRender(BaseScreen):
self.window_pointer.height / 2) + 10, 0 # 0 for z self.window_pointer.height / 2) + 10, 0 # 0 for z
self.render_d_line.x2 = self.group_camera.view_x self.render_d_line.x2 = self.group_camera.view_x
self.render_d_line.y2 = self.group_camera.view_y self.render_d_line.y2 = self.group_camera.view_y
self.buffer.bind()
window.clear() gl.glEnable(gl.GL_SCISSOR_TEST)
gl.glScissor(int(self.dx), int(self.dy), int(self.width), int(self.height))
gl.glViewport(int(self.dx), int(self.dy), self.window_pointer.width, self.window_pointer.height)
self.main_batch.draw() # use group camera, no need to with self.main_batch.draw() # use group camera, no need to with
self.buffer.unbind() gl.glViewport(0, 0, self.window_pointer.width, self.window_pointer.height)
self.render_texture.blit(x=self.dx, y=self.dy, z=0, width=self.width, height=self.height) gl.glScissor(0, 0, self.window_pointer.width, self.window_pointer.height)
gl.glDisable(gl.GL_SCISSOR_TEST)
# def on_draw(self, dt: float, window): # TODO: wait for pyglet 2.1 # def on_draw(self, dt: float, window): # TODO: wait for pyglet 2.1
def on_draw(self, window: "ClientWindow"): def on_draw(self, window: "ClientWindow"):
@ -352,7 +349,6 @@ class SR1ShipRender(BaseScreen):
if command.find('render'): if command.find('render'):
if command.find('reset'): if command.find('reset'):
self.group_camera.reset() self.group_camera.reset()
self.window_pointer.view = Vec4()
else: else:
self.status.draw_call = True self.status.draw_call = True
print('应该渲染飞船的') print('应该渲染飞船的')