Forum Programmation.python Rotation cube en WXGlcanvas

Posté par  (site web personnel) .
Étiquettes : aucune
0
15
juin
2005
Bonjour :)

J'ai un petit soucis je n'arrive pas a avior de maniere fluide la rotation d'un cube sur un wxglcanvas, ca rame pas mais des fois ca accroche -_-, alors que c'est un simple cube avec un boucle sur la frame avec une incrementation de l'angle sur 1 axe .

Voici le code :


#!/usr/bin/env python
# -*- coding: ISO-8859-15 -*-
# generated by wxGlade 0.4cvs on Wed Jun 15 16:02:52 2005

import wx

from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.GL import *
from wx import glcanvas
import wx.lib.newevent
(UpdatePanel, EVT_OGL_PANEL) = wx.lib.newevent.NewEvent()
class MyCanvasBase(glcanvas.GLCanvas):
def __init__(self, parent,parents):
glcanvas.GLCanvas.__init__(self, parent, -1)
self.init = False
self.lastx = self.x = 30
self.lasty = self.y = 30
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(EVT_OGL_PANEL, self.Epanel)

def OnEraseBackground(self, event):
pass # Do nothing, to avoid flashing on MSW.

def OnSize(self, event):
size = self.GetClientSize()
if self.GetContext():
self.SetCurrent()
glViewport(0, 0, size.width, size.height)
event.Skip()

def Epanel(self,evt):
self.Refresh(True)
return

def OnPaint(self, event):
dc = wx.PaintDC(self)
self.SetCurrent()
if not self.init:
self.InitGL()
self.init = True
self.OnDraw()
return

class OGLPanel(MyCanvasBase):
def InitGL( self ):
# set viewing projection
glMatrixMode(GL_PROJECTION);
glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);

# position viewer
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, -2.0);

# position object
glRotatef(self.y, 1.0, 0.0, 0.0);
glRotatef(self.x, 0.0, 1.0, 0.0);

glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
return

def OnDraw(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS)
glNormal3f( 0.0, 0.0, 1.0)
glVertex3f( 0.5, 0.5, 0.5)
glVertex3f(-0.5, 0.5, 0.5)
glVertex3f(-0.5,-0.5, 0.5)
glVertex3f( 0.5,-0.5, 0.5)

glNormal3f( 0.0, 0.0,-1.0)
glVertex3f(-0.5,-0.5,-0.5)
glVertex3f(-0.5, 0.5,-0.5)
glVertex3f( 0.5, 0.5,-0.5)
glVertex3f( 0.5,-0.5,-0.5)

glNormal3f( 0.0, 1.0, 0.0)
glVertex3f( 0.5, 0.5, 0.5)
glVertex3f( 0.5, 0.5,-0.5)
glVertex3f(-0.5, 0.5,-0.5)
glVertex3f(-0.5, 0.5, 0.5)

glNormal3f( 0.0,-1.0, 0.0)
glVertex3f(-0.5,-0.5,-0.5)
glVertex3f( 0.5,-0.5,-0.5)
glVertex3f( 0.5,-0.5, 0.5)
glVertex3f(-0.5,-0.5, 0.5)

glNormal3f( 1.0, 0.0, 0.0)
glVertex3f( 0.5, 0.5, 0.5)
glVertex3f( 0.5,-0.5, 0.5)
glVertex3f( 0.5,-0.5,-0.5)
glVertex3f( 0.5, 0.5,-0.5)

glNormal3f(-1.0, 0.0, 0.0)
glVertex3f(-0.5,-0.5,-0.5)
glVertex3f(-0.5,-0.5, 0.5)
glVertex3f(-0.5, 0.5, 0.5)
glVertex3f(-0.5, 0.5,-0.5)
glEnd()

glRotatef((50)/100., 1.0, 0.0, 0.0);
glRotatef((self.lastx - self.x)/100., 0.0, 1.0, 0.0);
self.x=self.x+5
if (self.x> 360):
self.x=0;
#glutPostRedisplay();
self.SwapBuffers()
self.lastx = self.x
#self.lasty = self.y
self.x=self.x+1
#self.y=self.y+1
evt = UpdatePanel()
wx.PostEvent(self, evt)
return

class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.button_1 = wx.Button(self, -1, "button_1")
self.Bind(wx.EVT_BUTTON, self.StartOGL, self.button_1)
self.__set_properties()
self.__do_layout()
# end wxGlade

def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("frame_1")
# end wxGlade

def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.button_1, 0, wx.ADJUST_MINSIZE, 0)
self.SetAutoLayout(True)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
sizer_1.SetSizeHints(self)
self.Layout()

def StartOGL(self,event):
self.frame = wx.Frame(None, -1, "test", size=(640,480))
canvas = OGLPanel(self.frame,self)
self.frame.Show(True)

class MyApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
self.SetTopWindow(frame_1)
frame_1.Show()
return 1

# end of class MyApp

if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()


Merci de votre aide :)

Suivre le flux des commentaires

Note : les commentaires appartiennent à celles et ceux qui les ont postés. Nous n’en sommes pas responsables.