2008/04/28

Translate

モデル・ビュー変換というものを使って三角形を回転させてみましょう。今回も、
http://wisdom.sakura.ne.jp/system/opengl/gl11.html
を参考にさせて頂きました。Pythonに変換した時にわかったことは、C言語の配列
GLfloat translate[] = {
1 , 0 , 0 , 0 ,
0 , 1 , 0 , 0 ,
0 , 0 , 1 , 0 ,
0 , 0 , 0 , 1
};

はPythonでは単に
translate = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
と書けば良いようです。

By using Model-View translation, a triangle is rotated around Y axis.
During making Python sample from original C sample, I found that array in C,

GLfloat translate[] = {
1 , 0 , 0 , 0 ,
0 , 1 , 0 , 0 ,
0 , 0 , 1 , 0 ,
0 , 0 , 0 , 1
};
can be implemented in Python as follows.
translate = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]


import sys
from math import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

ANGLE = 0.2
translate = [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]

def disp():
glClear(GL_COLOR_BUFFER_BIT)
glBegin(GL_POLYGON)
glColor3f(1 , 0 , 0)
glVertex2f(-0.9 , -0.9)
glColor3f(0 , 1 , 0)
glVertex2f(0 , 0.9)
glColor3f(0 , 0 , 1)
glVertex2f(0.9 , -0.9)
glEnd()
glFlush()

def timer(value):
glMultMatrixf(translate)
glutPostRedisplay()
glutTimerFunc(50 , timer , 0)

glutInit(sys.argv)
glutInitWindowPosition(100 , 50)
glutInitWindowSize(400 , 300)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutCreateWindow("rotate3.py")
glutDisplayFunc(disp)
glutTimerFunc(100 , timer , 0)
translate[0] = cos(ANGLE)
translate[2] = -sin(ANGLE)
translate[8] = sin(ANGLE)
translate[10] = cos(ANGLE)
glutMainLoop()

0 件のコメント: