1 from __future__ import absolute_import
2 from __future__ import division
5 from ctypes import c_void_p
8 OpenGL.ERROR_CHECKING = False
9 from OpenGL.GLU import *
10 from OpenGL.GL import *
12 from Cura.util import profile
13 from Cura.util import meshLoader
14 from Cura.gui.util import opengl
15 from Cura.gui.util import openglGui
17 class SceneView(openglGui.glGuiPanel):
18 def __init__(self, parent):
19 super(SceneView, self).__init__(parent)
25 self._objectShader = None
26 self._objColors = [None,None,None,None]
27 self._tmpVertex = None
28 self.updateProfileToControls()
30 def loadScene(self, fileList):
31 for filename in fileList:
32 for obj in meshLoader.loadMeshes(filename):
33 self._objectList.append(obj)
35 def updateProfileToControls(self):
36 self._objColors[0] = profile.getPreferenceColour('model_colour')
37 self._objColors[1] = profile.getPreferenceColour('model_colour2')
38 self._objColors[2] = profile.getPreferenceColour('model_colour3')
39 self._objColors[3] = profile.getPreferenceColour('model_colour4')
41 def OnMouseMotion(self,e):
42 if e.Dragging() and e.LeftIsDown():
43 self._yaw += e.GetX() - self.oldX
44 self._pitch -= e.GetY() - self.oldY
49 if e.Dragging() and e.RightIsDown():
50 self._zoom += e.GetY() - self.oldY
58 def _init3DView(self):
59 # set viewing projection
61 glViewport(0, 0, size.GetWidth(), size.GetHeight())
63 glLightfv(GL_LIGHT0, GL_POSITION, [0.2, 0.2, 1.0, 0.0])
65 glDisable(GL_RESCALE_NORMAL)
66 glDisable(GL_LIGHTING)
68 glEnable(GL_DEPTH_TEST)
69 glDisable(GL_CULL_FACE)
72 glClearColor(0.8, 0.8, 0.8, 1.0)
76 glMatrixMode(GL_PROJECTION)
78 aspect = float(size.GetWidth()) / float(size.GetHeight())
79 gluPerspective(45.0, aspect, 1.0, 1000.0)
81 glMatrixMode(GL_MODELVIEW)
83 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
86 if self._objectShader is None:
87 self._objectShader = opengl.GLShader("""
88 uniform float cameraDistance;
89 varying float light_amount;
93 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
94 gl_FrontColor = gl_Color;
96 light_amount = abs(dot(normalize(gl_NormalMatrix * gl_Normal), normalize(gl_LightSource[0].position.xyz)));
97 light_amount *= 1 - (length(gl_Position.xyz - vec3(0,0,cameraDistance)) / 1.5 / cameraDistance);
101 uniform float cameraDistance;
102 varying float light_amount;
106 gl_FragColor = gl_Color * light_amount;
110 glTranslate(0,0,-self._zoom)
111 glRotate(-self._pitch, 1,0,0)
112 glRotate(self._yaw, 0,0,1)
114 glColor3f(self._objColors[0][0], self._objColors[0][1], self._objColors[0][2])
116 self._objectShader.bind()
117 self._objectShader.setUniform('cameraDistance', self._zoom)
118 if self._tmpVertex is None:
119 for obj in self._objectList:
120 for m in obj._meshList:
121 self._tmpVertex = opengl.GLVBO(m.vertexes, m.normal)
123 self._tmpVertex.render()
124 self._objectShader.unbind()