Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

openGLDemo.py 9.6KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. # Ported from the win32 and MFC OpenGL Samples.
  2. import sys
  3. from pywin.mfc import docview
  4. try:
  5. from OpenGL.GL import * # nopycln: import
  6. from OpenGL.GLU import * # nopycln: import
  7. except ImportError:
  8. print("The OpenGL extensions do not appear to be installed.")
  9. print("This Pythonwin demo can not run")
  10. sys.exit(1)
  11. import timer
  12. import win32api
  13. import win32con
  14. import win32ui
  15. PFD_TYPE_RGBA = 0
  16. PFD_TYPE_COLORINDEX = 1
  17. PFD_MAIN_PLANE = 0
  18. PFD_OVERLAY_PLANE = 1
  19. PFD_UNDERLAY_PLANE = -1
  20. PFD_DOUBLEBUFFER = 0x00000001
  21. PFD_STEREO = 0x00000002
  22. PFD_DRAW_TO_WINDOW = 0x00000004
  23. PFD_DRAW_TO_BITMAP = 0x00000008
  24. PFD_SUPPORT_GDI = 0x00000010
  25. PFD_SUPPORT_OPENGL = 0x00000020
  26. PFD_GENERIC_FORMAT = 0x00000040
  27. PFD_NEED_PALETTE = 0x00000080
  28. PFD_NEED_SYSTEM_PALETTE = 0x00000100
  29. PFD_SWAP_EXCHANGE = 0x00000200
  30. PFD_SWAP_COPY = 0x00000400
  31. PFD_SWAP_LAYER_BUFFERS = 0x00000800
  32. PFD_GENERIC_ACCELERATED = 0x00001000
  33. PFD_DEPTH_DONTCARE = 0x20000000
  34. PFD_DOUBLEBUFFER_DONTCARE = 0x40000000
  35. PFD_STEREO_DONTCARE = 0x80000000
  36. # threeto8 = [0, 0o111>>1, 0o222>>1, 0o333>>1, 0o444>>1, 0o555>>1, 0o666>>1, 0o377]
  37. threeto8 = [0, 73 >> 1, 146 >> 1, 219 >> 1, 292 >> 1, 365 >> 1, 438 >> 1, 255]
  38. twoto8 = [0, 0x55, 0xAA, 0xFF]
  39. oneto8 = [0, 255]
  40. def ComponentFromIndex(i, nbits, shift):
  41. # val = (unsigned char) (i >> shift);
  42. val = (i >> shift) & 0xF
  43. if nbits == 1:
  44. val = val & 0x1
  45. return oneto8[val]
  46. elif nbits == 2:
  47. val = val & 0x3
  48. return twoto8[val]
  49. elif nbits == 3:
  50. val = val & 0x7
  51. return threeto8[val]
  52. else:
  53. return 0
  54. OpenGLViewParent = docview.ScrollView
  55. class OpenGLView(OpenGLViewParent):
  56. def PreCreateWindow(self, cc):
  57. self.HookMessage(self.OnSize, win32con.WM_SIZE)
  58. # An OpenGL window must be created with the following flags and must not
  59. # include CS_PARENTDC for the class style. Refer to SetPixelFormat
  60. # documentation in the "Comments" section for further information.
  61. style = cc[5]
  62. style = style | win32con.WS_CLIPSIBLINGS | win32con.WS_CLIPCHILDREN
  63. cc = cc[0], cc[1], cc[2], cc[3], cc[4], style, cc[6], cc[7], cc[8]
  64. cc = self._obj_.PreCreateWindow(cc)
  65. return cc
  66. def OnSize(self, params):
  67. lParam = params[3]
  68. cx = win32api.LOWORD(lParam)
  69. cy = win32api.HIWORD(lParam)
  70. glViewport(0, 0, cx, cy)
  71. if self.oldrect[2] > cx or self.oldrect[3] > cy:
  72. self.RedrawWindow()
  73. self.OnSizeChange(cx, cy)
  74. self.oldrect = self.oldrect[0], self.oldrect[1], cx, cy
  75. def OnInitialUpdate(self):
  76. self.SetScaleToFitSize(
  77. (100, 100)
  78. ) # or SetScrollSizes() - A Pythonwin requirement
  79. return self._obj_.OnInitialUpdate()
  80. # return rc
  81. def OnCreate(self, cs):
  82. self.oldrect = self.GetClientRect()
  83. self._InitContexts()
  84. self.Init()
  85. def OnDestroy(self, msg):
  86. self.Term()
  87. self._DestroyContexts()
  88. return OpenGLViewParent.OnDestroy(self, msg)
  89. def OnDraw(self, dc):
  90. self.DrawScene()
  91. def OnEraseBkgnd(self, dc):
  92. return 1
  93. # The OpenGL helpers
  94. def _SetupPixelFormat(self):
  95. dc = self.dc.GetSafeHdc()
  96. pfd = CreatePIXELFORMATDESCRIPTOR()
  97. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER
  98. pfd.iPixelType = PFD_TYPE_RGBA
  99. pfd.cColorBits = 24
  100. pfd.cDepthBits = 32
  101. pfd.iLayerType = PFD_MAIN_PLANE
  102. pixelformat = ChoosePixelFormat(dc, pfd)
  103. SetPixelFormat(dc, pixelformat, pfd)
  104. self._CreateRGBPalette()
  105. def _CreateRGBPalette(self):
  106. dc = self.dc.GetSafeHdc()
  107. n = GetPixelFormat(dc)
  108. pfd = DescribePixelFormat(dc, n)
  109. if pfd.dwFlags & PFD_NEED_PALETTE:
  110. n = 1 << pfd.cColorBits
  111. pal = []
  112. for i in range(n):
  113. this = (
  114. ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift),
  115. ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift),
  116. ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift),
  117. 0,
  118. )
  119. pal.append(this)
  120. hpal = win32ui.CreatePalette(pal)
  121. self.dc.SelectPalette(hpal, 0)
  122. self.dc.RealizePalette()
  123. def _InitContexts(self):
  124. self.dc = self.GetDC()
  125. self._SetupPixelFormat()
  126. hrc = wglCreateContext(self.dc.GetSafeHdc())
  127. wglMakeCurrent(self.dc.GetSafeHdc(), hrc)
  128. def _DestroyContexts(self):
  129. hrc = wglGetCurrentContext()
  130. wglMakeCurrent(0, 0)
  131. if hrc:
  132. wglDeleteContext(hrc)
  133. # The methods to support OpenGL
  134. def DrawScene(self):
  135. assert 0, "You must override this method"
  136. def Init(self):
  137. assert 0, "You must override this method"
  138. def OnSizeChange(self, cx, cy):
  139. pass
  140. def Term(self):
  141. pass
  142. class TestView(OpenGLView):
  143. def OnSizeChange(self, right, bottom):
  144. glClearColor(0.0, 0.0, 0.0, 1.0)
  145. glClearDepth(1.0)
  146. glEnable(GL_DEPTH_TEST)
  147. glMatrixMode(GL_PROJECTION)
  148. if bottom:
  149. aspect = right / bottom
  150. else:
  151. aspect = 0 # When window created!
  152. glLoadIdentity()
  153. gluPerspective(45.0, aspect, 3.0, 7.0)
  154. glMatrixMode(GL_MODELVIEW)
  155. near_plane = 3.0
  156. far_plane = 7.0
  157. maxObjectSize = 3.0
  158. self.radius = near_plane + maxObjectSize / 2.0
  159. def Init(self):
  160. pass
  161. def DrawScene(self):
  162. glClearColor(0.0, 0.0, 0.0, 1.0)
  163. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  164. glPushMatrix()
  165. glTranslatef(0.0, 0.0, -self.radius)
  166. self._DrawCone()
  167. self._DrawPyramid()
  168. glPopMatrix()
  169. glFinish()
  170. SwapBuffers(wglGetCurrentDC())
  171. def _DrawCone(self):
  172. glColor3f(0.0, 1.0, 0.0)
  173. glPushMatrix()
  174. glTranslatef(-1.0, 0.0, 0.0)
  175. quadObj = gluNewQuadric()
  176. gluQuadricDrawStyle(quadObj, GLU_FILL)
  177. gluQuadricNormals(quadObj, GLU_SMOOTH)
  178. gluCylinder(quadObj, 1.0, 0.0, 1.0, 20, 10)
  179. # gluDeleteQuadric(quadObj);
  180. glPopMatrix()
  181. def _DrawPyramid(self):
  182. glPushMatrix()
  183. glTranslatef(1.0, 0.0, 0.0)
  184. glBegin(GL_TRIANGLE_FAN)
  185. glColor3f(1.0, 0.0, 0.0)
  186. glVertex3f(0.0, 1.0, 0.0)
  187. glColor3f(0.0, 1.0, 0.0)
  188. glVertex3f(-1.0, 0.0, 0.0)
  189. glColor3f(0.0, 0.0, 1.0)
  190. glVertex3f(0.0, 0.0, 1.0)
  191. glColor3f(0.0, 1.0, 0.0)
  192. glVertex3f(1.0, 0.0, 0.0)
  193. glEnd()
  194. glPopMatrix()
  195. class CubeView(OpenGLView):
  196. def OnSizeChange(self, right, bottom):
  197. glClearColor(0.0, 0.0, 0.0, 1.0)
  198. glClearDepth(1.0)
  199. glEnable(GL_DEPTH_TEST)
  200. glMatrixMode(GL_PROJECTION)
  201. if bottom:
  202. aspect = right / bottom
  203. else:
  204. aspect = 0 # When window created!
  205. glLoadIdentity()
  206. gluPerspective(45.0, aspect, 3.0, 7.0)
  207. glMatrixMode(GL_MODELVIEW)
  208. near_plane = 3.0
  209. far_plane = 7.0
  210. maxObjectSize = 3.0
  211. self.radius = near_plane + maxObjectSize / 2.0
  212. def Init(self):
  213. self.busy = 0
  214. self.wAngleY = 10.0
  215. self.wAngleX = 1.0
  216. self.wAngleZ = 5.0
  217. self.timerid = timer.set_timer(150, self.OnTimer)
  218. def OnTimer(self, id, timeVal):
  219. self.DrawScene()
  220. def Term(self):
  221. timer.kill_timer(self.timerid)
  222. def DrawScene(self):
  223. if self.busy:
  224. return
  225. self.busy = 1
  226. glClearColor(0.0, 0.0, 0.0, 1.0)
  227. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  228. glPushMatrix()
  229. glTranslatef(0.0, 0.0, -self.radius)
  230. glRotatef(self.wAngleX, 1.0, 0.0, 0.0)
  231. glRotatef(self.wAngleY, 0.0, 1.0, 0.0)
  232. glRotatef(self.wAngleZ, 0.0, 0.0, 1.0)
  233. self.wAngleX = self.wAngleX + 1.0
  234. self.wAngleY = self.wAngleY + 10.0
  235. self.wAngleZ = self.wAngleZ + 5.0
  236. glBegin(GL_QUAD_STRIP)
  237. glColor3f(1.0, 0.0, 1.0)
  238. glVertex3f(-0.5, 0.5, 0.5)
  239. glColor3f(1.0, 0.0, 0.0)
  240. glVertex3f(-0.5, -0.5, 0.5)
  241. glColor3f(1.0, 1.0, 1.0)
  242. glVertex3f(0.5, 0.5, 0.5)
  243. glColor3f(1.0, 1.0, 0.0)
  244. glVertex3f(0.5, -0.5, 0.5)
  245. glColor3f(0.0, 1.0, 1.0)
  246. glVertex3f(0.5, 0.5, -0.5)
  247. glColor3f(0.0, 1.0, 0.0)
  248. glVertex3f(0.5, -0.5, -0.5)
  249. glColor3f(0.0, 0.0, 1.0)
  250. glVertex3f(-0.5, 0.5, -0.5)
  251. glColor3f(0.0, 0.0, 0.0)
  252. glVertex3f(-0.5, -0.5, -0.5)
  253. glColor3f(1.0, 0.0, 1.0)
  254. glVertex3f(-0.5, 0.5, 0.5)
  255. glColor3f(1.0, 0.0, 0.0)
  256. glVertex3f(-0.5, -0.5, 0.5)
  257. glEnd()
  258. glBegin(GL_QUADS)
  259. glColor3f(1.0, 0.0, 1.0)
  260. glVertex3f(-0.5, 0.5, 0.5)
  261. glColor3f(1.0, 1.0, 1.0)
  262. glVertex3f(0.5, 0.5, 0.5)
  263. glColor3f(0.0, 1.0, 1.0)
  264. glVertex3f(0.5, 0.5, -0.5)
  265. glColor3f(0.0, 0.0, 1.0)
  266. glVertex3f(-0.5, 0.5, -0.5)
  267. glEnd()
  268. glBegin(GL_QUADS)
  269. glColor3f(1.0, 0.0, 0.0)
  270. glVertex3f(-0.5, -0.5, 0.5)
  271. glColor3f(1.0, 1.0, 0.0)
  272. glVertex3f(0.5, -0.5, 0.5)
  273. glColor3f(0.0, 1.0, 0.0)
  274. glVertex3f(0.5, -0.5, -0.5)
  275. glColor3f(0.0, 0.0, 0.0)
  276. glVertex3f(-0.5, -0.5, -0.5)
  277. glEnd()
  278. glPopMatrix()
  279. glFinish()
  280. SwapBuffers(wglGetCurrentDC())
  281. self.busy = 0
  282. def test():
  283. template = docview.DocTemplate(None, None, None, CubeView)
  284. # template = docview.DocTemplate(None, None, None, TestView )
  285. template.OpenDocumentFile(None)
  286. if __name__ == "__main__":
  287. test()