codekingpro/portable-devtools
114k
1# Ported from the win32 and MFC OpenGL Samples.2 3import sys4 5from pywin.mfc import docview6 7try:8 from OpenGL.GL import * # nopycln: import9 from OpenGL.GLU import * # nopycln: import10except ImportError:11 print("The OpenGL extensions do not appear to be installed.")12 print("This Pythonwin demo can not run")13 sys.exit(1)14 15import timer16import win32api17import win32con18import win32ui19 20PFD_TYPE_RGBA = 021PFD_TYPE_COLORINDEX = 122PFD_MAIN_PLANE = 023PFD_OVERLAY_PLANE = 124PFD_UNDERLAY_PLANE = -125PFD_DOUBLEBUFFER = 0x0000000126PFD_STEREO = 0x0000000227PFD_DRAW_TO_WINDOW = 0x0000000428PFD_DRAW_TO_BITMAP = 0x0000000829PFD_SUPPORT_GDI = 0x0000001030PFD_SUPPORT_OPENGL = 0x0000002031PFD_GENERIC_FORMAT = 0x0000004032PFD_NEED_PALETTE = 0x0000008033PFD_NEED_SYSTEM_PALETTE = 0x0000010034PFD_SWAP_EXCHANGE = 0x0000020035PFD_SWAP_COPY = 0x0000040036PFD_SWAP_LAYER_BUFFERS = 0x0000080037PFD_GENERIC_ACCELERATED = 0x0000100038PFD_DEPTH_DONTCARE = 0x2000000039PFD_DOUBLEBUFFER_DONTCARE = 0x4000000040PFD_STEREO_DONTCARE = 0x8000000041 42 43# threeto8 = [0, 0o111>>1, 0o222>>1, 0o333>>1, 0o444>>1, 0o555>>1, 0o666>>1, 0o377]44threeto8 = [0, 73 >> 1, 146 >> 1, 219 >> 1, 292 >> 1, 365 >> 1, 438 >> 1, 255]45twoto8 = [0, 0x55, 0xAA, 0xFF]46oneto8 = [0, 255]47 48 49def ComponentFromIndex(i, nbits, shift):50 # val = (unsigned char) (i >> shift);51 val = (i >> shift) & 0xF52 if nbits == 1:53 val = val & 0x154 return oneto8[val]55 elif nbits == 2:56 val = val & 0x357 return twoto8[val]58 elif nbits == 3:59 val = val & 0x760 return threeto8[val]61 else:62 return 063 64 65OpenGLViewParent = docview.ScrollView66 67 68class OpenGLView(OpenGLViewParent):69 def PreCreateWindow(self, cc):70 self.HookMessage(self.OnSize, win32con.WM_SIZE)71 # An OpenGL window must be created with the following flags and must not72 # include CS_PARENTDC for the class style. Refer to SetPixelFormat73 # documentation in the "Comments" section for further information.74 style = cc[5]75 style = style | win32con.WS_CLIPSIBLINGS | win32con.WS_CLIPCHILDREN76 cc = cc[0], cc[1], cc[2], cc[3], cc[4], style, cc[6], cc[7], cc[8]77 cc = self._obj_.PreCreateWindow(cc)78 return cc79 80 def OnSize(self, params):81 lParam = params[3]82 cx = win32api.LOWORD(lParam)83 cy = win32api.HIWORD(lParam)84 glViewport(0, 0, cx, cy)85 86 if self.oldrect[2] > cx or self.oldrect[3] > cy:87 self.RedrawWindow()88 89 self.OnSizeChange(cx, cy)90 91 self.oldrect = self.oldrect[0], self.oldrect[1], cx, cy92 93 def OnInitialUpdate(self):94 self.SetScaleToFitSize(95 (100, 100)96 ) # or SetScrollSizes() - A Pythonwin requirement97 return self._obj_.OnInitialUpdate()98 99 # return rc100 101 def OnCreate(self, cs):102 self.oldrect = self.GetClientRect()103 self._InitContexts()104 self.Init()105 106 def OnDestroy(self, msg):107 self.Term()108 self._DestroyContexts()109 return OpenGLViewParent.OnDestroy(self, msg)110 111 def OnDraw(self, dc):112 self.DrawScene()113 114 def OnEraseBkgnd(self, dc):115 return 1116 117 # The OpenGL helpers118 def _SetupPixelFormat(self):119 dc = self.dc.GetSafeHdc()120 pfd = CreatePIXELFORMATDESCRIPTOR()121 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER122 pfd.iPixelType = PFD_TYPE_RGBA123 pfd.cColorBits = 24124 pfd.cDepthBits = 32125 pfd.iLayerType = PFD_MAIN_PLANE126 pixelformat = ChoosePixelFormat(dc, pfd)127 SetPixelFormat(dc, pixelformat, pfd)128 self._CreateRGBPalette()129 130 def _CreateRGBPalette(self):131 dc = self.dc.GetSafeHdc()132 n = GetPixelFormat(dc)133 pfd = DescribePixelFormat(dc, n)134 if pfd.dwFlags & PFD_NEED_PALETTE:135 n = 1 << pfd.cColorBits136 pal = []137 for i in range(n):138 this = (139 ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift),140 ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift),141 ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift),142 0,143 )144 pal.append(this)145 hpal = win32ui.CreatePalette(pal)146 self.dc.SelectPalette(hpal, 0)147 self.dc.RealizePalette()148 149 def _InitContexts(self):150 self.dc = self.GetDC()151 self._SetupPixelFormat()152 hrc = wglCreateContext(self.dc.GetSafeHdc())153 wglMakeCurrent(self.dc.GetSafeHdc(), hrc)154 155 def _DestroyContexts(self):156 hrc = wglGetCurrentContext()157 wglMakeCurrent(0, 0)158 if hrc:159 wglDeleteContext(hrc)160 161 # The methods to support OpenGL162 def DrawScene(self):163 assert 0, "You must override this method"164 165 def Init(self):166 assert 0, "You must override this method"167 168 def OnSizeChange(self, cx, cy):169 pass170 171 def Term(self):172 pass173 174 175class TestView(OpenGLView):176 def OnSizeChange(self, right, bottom):177 glClearColor(0.0, 0.0, 0.0, 1.0)178 glClearDepth(1.0)179 glEnable(GL_DEPTH_TEST)180 181 glMatrixMode(GL_PROJECTION)182 if bottom:183 aspect = right / bottom184 else:185 aspect = 0 # When window created!186 glLoadIdentity()187 gluPerspective(45.0, aspect, 3.0, 7.0)188 glMatrixMode(GL_MODELVIEW)189 190 near_plane = 3.0191 far_plane = 7.0192 maxObjectSize = 3.0193 self.radius = near_plane + maxObjectSize / 2.0194 195 def Init(self):196 pass197 198 def DrawScene(self):199 glClearColor(0.0, 0.0, 0.0, 1.0)200 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)201 202 glPushMatrix()203 glTranslatef(0.0, 0.0, -self.radius)204 205 self._DrawCone()206 207 self._DrawPyramid()208 209 glPopMatrix()210 glFinish()211 212 SwapBuffers(wglGetCurrentDC())213 214 def _DrawCone(self):215 glColor3f(0.0, 1.0, 0.0)216 217 glPushMatrix()218 glTranslatef(-1.0, 0.0, 0.0)219 quadObj = gluNewQuadric()220 gluQuadricDrawStyle(quadObj, GLU_FILL)221 gluQuadricNormals(quadObj, GLU_SMOOTH)222 gluCylinder(quadObj, 1.0, 0.0, 1.0, 20, 10)223 # gluDeleteQuadric(quadObj);224 glPopMatrix()225 226 def _DrawPyramid(self):227 glPushMatrix()228 glTranslatef(1.0, 0.0, 0.0)229 glBegin(GL_TRIANGLE_FAN)230 glColor3f(1.0, 0.0, 0.0)231 glVertex3f(0.0, 1.0, 0.0)232 glColor3f(0.0, 1.0, 0.0)233 glVertex3f(-1.0, 0.0, 0.0)234 glColor3f(0.0, 0.0, 1.0)235 glVertex3f(0.0, 0.0, 1.0)236 glColor3f(0.0, 1.0, 0.0)237 glVertex3f(1.0, 0.0, 0.0)238 glEnd()239 glPopMatrix()240 241 242class CubeView(OpenGLView):243 def OnSizeChange(self, right, bottom):244 glClearColor(0.0, 0.0, 0.0, 1.0)245 glClearDepth(1.0)246 glEnable(GL_DEPTH_TEST)247 248 glMatrixMode(GL_PROJECTION)249 if bottom:250 aspect = right / bottom251 else:252 aspect = 0 # When window created!253 glLoadIdentity()254 gluPerspective(45.0, aspect, 3.0, 7.0)255 glMatrixMode(GL_MODELVIEW)256 257 near_plane = 3.0258 far_plane = 7.0259 maxObjectSize = 3.0260 self.radius = near_plane + maxObjectSize / 2.0261 262 def Init(self):263 self.busy = 0264 self.wAngleY = 10.0265 self.wAngleX = 1.0266 self.wAngleZ = 5.0267 self.timerid = timer.set_timer(150, self.OnTimer)268 269 def OnTimer(self, id, timeVal):270 self.DrawScene()271 272 def Term(self):273 timer.kill_timer(self.timerid)274 275 def DrawScene(self):276 if self.busy:277 return278 self.busy = 1279 280 glClearColor(0.0, 0.0, 0.0, 1.0)281 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)282 283 glPushMatrix()284 285 glTranslatef(0.0, 0.0, -self.radius)286 glRotatef(self.wAngleX, 1.0, 0.0, 0.0)287 glRotatef(self.wAngleY, 0.0, 1.0, 0.0)288 glRotatef(self.wAngleZ, 0.0, 0.0, 1.0)289 290 self.wAngleX = self.wAngleX + 1.0291 self.wAngleY = self.wAngleY + 10.0292 self.wAngleZ = self.wAngleZ + 5.0293 294 glBegin(GL_QUAD_STRIP)295 glColor3f(1.0, 0.0, 1.0)296 glVertex3f(-0.5, 0.5, 0.5)297 298 glColor3f(1.0, 0.0, 0.0)299 glVertex3f(-0.5, -0.5, 0.5)300 301 glColor3f(1.0, 1.0, 1.0)302 glVertex3f(0.5, 0.5, 0.5)303 304 glColor3f(1.0, 1.0, 0.0)305 glVertex3f(0.5, -0.5, 0.5)306 307 glColor3f(0.0, 1.0, 1.0)308 glVertex3f(0.5, 0.5, -0.5)309 310 glColor3f(0.0, 1.0, 0.0)311 glVertex3f(0.5, -0.5, -0.5)312 313 glColor3f(0.0, 0.0, 1.0)314 glVertex3f(-0.5, 0.5, -0.5)315 316 glColor3f(0.0, 0.0, 0.0)317 glVertex3f(-0.5, -0.5, -0.5)318 319 glColor3f(1.0, 0.0, 1.0)320 glVertex3f(-0.5, 0.5, 0.5)321 322 glColor3f(1.0, 0.0, 0.0)323 glVertex3f(-0.5, -0.5, 0.5)324 325 glEnd()326 327 glBegin(GL_QUADS)328 glColor3f(1.0, 0.0, 1.0)329 glVertex3f(-0.5, 0.5, 0.5)330 331 glColor3f(1.0, 1.0, 1.0)332 glVertex3f(0.5, 0.5, 0.5)333 334 glColor3f(0.0, 1.0, 1.0)335 glVertex3f(0.5, 0.5, -0.5)336 337 glColor3f(0.0, 0.0, 1.0)338 glVertex3f(-0.5, 0.5, -0.5)339 glEnd()340 341 glBegin(GL_QUADS)342 glColor3f(1.0, 0.0, 0.0)343 glVertex3f(-0.5, -0.5, 0.5)344 345 glColor3f(1.0, 1.0, 0.0)346 glVertex3f(0.5, -0.5, 0.5)347 348 glColor3f(0.0, 1.0, 0.0)349 glVertex3f(0.5, -0.5, -0.5)350 351 glColor3f(0.0, 0.0, 0.0)352 glVertex3f(-0.5, -0.5, -0.5)353 glEnd()354 355 glPopMatrix()356 357 glFinish()358 SwapBuffers(wglGetCurrentDC())359 360 self.busy = 0361 362 363def test():364 template = docview.DocTemplate(None, None, None, CubeView)365 # template = docview.DocTemplate(None, None, None, TestView )366 template.OpenDocumentFile(None)367 368 369if __name__ == "__main__":370 test()371 