1 from __future__ import absolute_import
2 from __future__ import division
7 OpenGL.ERROR_CHECKING = False
8 from OpenGL.GLU import *
9 from OpenGL.GL import *
11 from Cura.util import profile
12 from Cura.util import meshLoader
13 from Cura.gui.util import opengl
14 from Cura.gui.util import openglGui
16 class SceneView(openglGui.glGuiPanel):
17 def __init__(self, parent):
18 super(SceneView, self).__init__(parent)
24 self._objectShader = None
26 self._selectedObj = None
27 self._objColors = [None,None,None,None]
30 self.updateProfileToControls()
32 def loadScene(self, fileList):
33 for filename in fileList:
34 for obj in meshLoader.loadMeshes(filename):
35 self._objectList.append(obj)
37 def _deleteObject(self, obj):
38 if obj == self._selectedObj:
39 self._selectedObj = None
40 if obj == self._focusObj:
42 self._objectList.remove(obj)
43 for m in obj._meshList:
45 self.glReleaseList.append(m.vbo)
47 def updateProfileToControls(self):
48 self._objColors[0] = profile.getPreferenceColour('model_colour')
49 self._objColors[1] = profile.getPreferenceColour('model_colour2')
50 self._objColors[2] = profile.getPreferenceColour('model_colour3')
51 self._objColors[3] = profile.getPreferenceColour('model_colour4')
53 def OnKeyChar(self, keyCode):
54 if keyCode == wx.WXK_DELETE or keyCode == wx.WXK_NUMPAD_DELETE:
55 if self._selectedObj is not None:
56 self._deleteObject(self._selectedObj)
59 def OnMouseDown(self,e):
60 if self._focusObj is not None:
61 self._selectedObj = self._focusObj
63 def OnMouseMotion(self,e):
64 if e.Dragging() and e.LeftIsDown():
65 self._yaw += e.GetX() - self._mouseX
66 self._pitch -= e.GetY() - self._mouseY
71 if e.Dragging() and e.RightIsDown():
72 self._zoom += e.GetY() - self._mouseY
77 self._mouseX = e.GetX()
78 self._mouseY = e.GetY()
80 def _init3DView(self):
81 # set viewing projection
83 glViewport(0, 0, size.GetWidth(), size.GetHeight())
86 glLightfv(GL_LIGHT0, GL_POSITION, [0.2, 0.2, 1.0, 0.0])
88 glDisable(GL_RESCALE_NORMAL)
89 glDisable(GL_LIGHTING)
91 glEnable(GL_DEPTH_TEST)
92 glDisable(GL_CULL_FACE)
95 glClearColor(0.8, 0.8, 0.8, 1.0)
99 glMatrixMode(GL_PROJECTION)
101 aspect = float(size.GetWidth()) / float(size.GetHeight())
102 gluPerspective(45.0, aspect, 1.0, 1000.0)
104 glMatrixMode(GL_MODELVIEW)
106 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
109 if self._objectShader is None:
110 self._objectShader = opengl.GLShader(self, """
111 uniform float cameraDistance;
112 varying float light_amount;
116 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
117 gl_FrontColor = gl_Color;
119 light_amount = abs(dot(normalize(gl_NormalMatrix * gl_Normal), normalize(gl_LightSource[0].position.xyz)));
120 light_amount *= 1 - (length(gl_Position.xyz - vec3(0,0,cameraDistance)) / 1.5 / cameraDistance);
124 uniform float cameraDistance;
125 varying float light_amount;
129 gl_FragColor = vec4(gl_Color.xyz * light_amount, gl_Color[3]);
133 glTranslate(0,0,-self._zoom)
134 glRotate(-self._pitch, 1,0,0)
135 glRotate(self._yaw, 0,0,1)
137 glClearColor(1,1,1,1)
138 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
140 for n in xrange(0, len(self._objectList)):
141 obj = self._objectList[n]
142 glColor4ub((n >> 24) & 0xFF, (n >> 16) & 0xFF, (n >> 8) & 0xFF, n & 0xFF)
143 self._renderObject(obj)
145 if self._mouseX > -1:
146 n = glReadPixels(self._mouseX, self.GetSize().GetHeight() - 1 - self._mouseY, 1, 1, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8)[0][0]
147 if n < len(self._objectList):
148 self._focusObj = self._objectList[n]
150 self._focusObj = None
153 glTranslate(0,0,-self._zoom)
154 glRotate(-self._pitch, 1,0,0)
155 glRotate(self._yaw, 0,0,1)
158 self._objectShader.bind()
159 self._objectShader.setUniform('cameraDistance', self._zoom)
160 for obj in self._objectList:
161 col = self._objColors[0]
162 if self._selectedObj == obj:
163 col = map(lambda n: n * 1.3, col)
164 elif self._focusObj == obj:
165 col = map(lambda n: n * 1.2, col)
166 elif self._focusObj is not None or self._selectedObj is not None:
167 col = map(lambda n: n * 0.8, col)
168 glColor4f(col[0], col[1], col[2], col[3])
169 self._renderObject(obj)
170 self._objectShader.unbind()
172 def _renderObject(self, obj):
174 offset = (obj.getMinimum() + obj.getMaximum()) / 2
175 glTranslate(-offset[0], -offset[1], -obj.getMinimum()[2])
176 for m in obj._meshList:
178 m.vbo = opengl.GLVBO(self, m.vertexes, m.normal)