Last active
December 28, 2015 00:09
-
-
Save blink1073/7411284 to your computer and use it in GitHub Desktop.
Demonstrate an OpenGL example in enaml. This is a port of the PyQtGraph GLMesh Example to Enaml.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Mon Nov 11 04:16:39 2013 | |
@author: silvester | |
@license: MIT | |
""" | |
from enaml.qt import QtCore, QtGui | |
from enaml.widgets.api import * | |
from pyqtgraph import opengl as gl | |
import pyqtgraph as pg | |
class PyQtGraphWidget(RawWidget): | |
def create_widget(self, parent): | |
""" Create the toolkit widget for the control. | |
This method should create and initialize the widget. | |
Parameters | |
---------- | |
parent : toolkit widget or None | |
The parent toolkit widget for the control. | |
Returns | |
------- | |
result : toolkit widget | |
The toolkit specific widget for the control. | |
""" | |
#return scene.create_widget(parent) | |
w = gl.GLViewWidget(parent) | |
w.setCameraPosition(distance=40) | |
g = gl.GLGridItem() | |
g.scale(2,2,1) | |
w.addItem(g) | |
import numpy as np | |
## Example 1: | |
## Array of vertex positions and array of vertex indexes defining faces | |
## Colors are specified per-face | |
verts = np.array([ | |
[0, 0, 0], | |
[2, 0, 0], | |
[1, 2, 0], | |
[1, 1, 1], | |
]) | |
faces = np.array([ | |
[0, 1, 2], | |
[0, 1, 3], | |
[0, 2, 3], | |
[1, 2, 3] | |
]) | |
colors = np.array([ | |
[1, 0, 0, 0.3], | |
[0, 1, 0, 0.3], | |
[0, 0, 1, 0.3], | |
[1, 1, 0, 0.3] | |
]) | |
## Mesh item will automatically compute face normals. | |
m1 = gl.GLMeshItem(vertexes=verts, faces=faces, faceColors=colors, smooth=False) | |
m1.translate(5, 5, 0) | |
m1.setGLOptions('additive') | |
w.addItem(m1) | |
## Example 2: | |
## Array of vertex positions, three per face | |
## Colors are specified per-vertex | |
verts = verts[faces] ## Same mesh geometry as example 2, but now we are passing in 12 vertexes | |
colors = np.random.random(size=(verts.shape[0], 3, 4)) | |
#colors[...,3] = 1.0 | |
m2 = gl.GLMeshItem(vertexes=verts, vertexColors=colors, smooth=False, shader='balloon') | |
m2.translate(-5, 5, 0) | |
w.addItem(m2) | |
## Example 3: | |
## icosahedron | |
md = gl.MeshData.sphere(rows=10, cols=20) | |
#colors = np.random.random(size=(md.faceCount(), 4)) | |
#colors[:,3] = 0.3 | |
#colors[100:] = 0.0 | |
colors = np.ones((md.faceCount(), 4), dtype=float) | |
colors[::2,0] = 0 | |
colors[:,1] = np.linspace(0, 1, colors.shape[0]) | |
md.setFaceColors(colors) | |
m3 = gl.GLMeshItem(meshdata=md, smooth=False)#, shader='balloon') | |
#m3.translate(-5, -5, 0) | |
w.addItem(m3) | |
w.setMinimumSize(600, 600) | |
return w | |
enamldef Main(MainWindow): | |
attr model | |
Container: | |
PyQtGraphWidget: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment