# -*- coding:sjis -*-
from xml.dom.minidom import parse
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import unicodedata
import sys
import re
def init():
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
def display():
global g_rot_x, g_rot_y, mesh
glClearColor(0.0, 0.0, 0.5, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()
gluLookAt(-5, 3.6, 7, 0, 0.5, 0, 0, 1, 0)
glRotatef(g_rot_y, 1, 0, 0)
glRotatef(g_rot_x, 0, 1, 0)
glBegin(GL_QUADS)
for m in mesh:
for p in m["vindex"]:
for i in p:
v = m["vertex"]
glVertex3f(v[i][0], v[i][1], v[i][2])
glEnd()
glPopMatrix()
glutSwapBuffers()
def reshape(w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, (0.0 + w) / h, 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def keyboard(key, x, y):
if key == chr(27):
sys.exit(0)
def motion(x, y):
global g_rot_x, g_rot_y, g_pre_x, g_pre_y
g_rot_x += (float)(x - g_pre_x)
g_rot_y += (float)(y - g_pre_y)
g_pre_x = x
g_pre_y = y
glutPostRedisplay()
def mouse(button, state, x, y):
global g_pre_x, g_pre_y
if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
g_pre_x = x
g_pre_y = y
def parseNode(node):
global mesh
c = node.childNodes
for i in range(len(c)):
if c[i].nodeName == "mesh":
mesh.append({})
mesh[len(mesh)-1]["vindex"] = []
if c[i].nodeName == "array" and re.search("Pos-array", c[i].getAttribute("id").encode("ASCII")):
c2 = []
c3 = c[i].firstChild.data.encode("ASCII").split()
for j in range(len(c3)/3): c2.append([float(c3[j*3]), float(c3[j*3+1]), float(c3[j*3+2])])
mesh[len(mesh)-1]["vertex"] = c2
elif c[i].nodeName == "polygons":
ic = 0 # データ項目の種類の数
for c2 in c[i].childNodes:
if c2.nodeName == "input":
ic = ic + 1
elif c2.nodeName == "p":
d = c2.firstChild.data.encode("ASCII").split()
d2 = []
for d3 in d: d2.append(int(d3))
mesh[len(mesh)-1]["vindex"].append([d2[0], d2[ic], d2[ic*2], d2[ic*3]])
parseNode(c[i])
mesh = []
dom = parse('multimtl.xml')
parseNode(dom)
g_rot_x = g_rot_y = g_pre_x = g_pre_y = 0
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(320, 240)
glutCreateWindow('COLLADA')
glutReshapeFunc(reshape)
glutKeyboardFunc(keyboard)
glutDisplayFunc(display)
glutMotionFunc(motion)
glutMouseFunc(mouse)
init()
glutMainLoop()