diff --git a/src/test001.cpp b/src/cpp/test001.cpp similarity index 100% rename from src/test001.cpp rename to src/cpp/test001.cpp diff --git a/src/test002.cpp b/src/cpp/test002.cpp old mode 100755 new mode 100644 similarity index 100% rename from src/test002.cpp rename to src/cpp/test002.cpp diff --git a/src/test003.cpp b/src/cpp/test003.cpp old mode 100755 new mode 100644 similarity index 100% rename from src/test003.cpp rename to src/cpp/test003.cpp diff --git a/src/pyqt/test001.py b/src/pyqt/test001.py new file mode 100755 index 0000000..66b7848 --- /dev/null +++ b/src/pyqt/test001.py @@ -0,0 +1,176 @@ +import array + +from PyQt5.QtCore import QEvent +from PyQt5.QtGui import (QGuiApplication, QMatrix4x4, QOpenGLContext, + QOpenGLShader, QOpenGLShaderProgram, QSurfaceFormat, QWindow) + + +class OpenGLWindow(QWindow): + def __init__(self, parent=None): + super(OpenGLWindow, self).__init__(parent) + + self.m_update_pending = False + self.m_animating = False + self.m_context = None + self.m_gl = None + + self.setSurfaceType(QWindow.OpenGLSurface) + + def initialize(self): + pass + + def setAnimating(self, animating): + self.m_animating = animating + + if animating: + self.renderLater() + + def renderLater(self): + if not self.m_update_pending: + self.m_update_pending = True + QGuiApplication.postEvent(self, QEvent(QEvent.UpdateRequest)) + + def renderNow(self): + if not self.isExposed(): + return + + self.m_update_pending = False + + needsInitialize = False + + if self.m_context is None: + self.m_context = QOpenGLContext(self) + self.m_context.setFormat(self.requestedFormat()) + self.m_context.create() + + needsInitialize = True + + self.m_context.makeCurrent(self) + + if needsInitialize: + self.m_gl = self.m_context.versionFunctions() + self.m_gl.initializeOpenGLFunctions() + + self.initialize() + + self.render(self.m_gl) + + self.m_context.swapBuffers(self) + + if self.m_animating: + self.renderLater() + + def event(self, event): + if event.type() == QEvent.UpdateRequest: + self.renderNow() + return True + + return super(OpenGLWindow, self).event(event) + + def exposeEvent(self, event): + self.renderNow() + + def resizeEvent(self, event): + self.renderNow() + + +class TriangleWindow(OpenGLWindow): + vertexShaderSource = ''' +attribute highp vec4 posAttr; +attribute lowp vec4 colAttr; +varying lowp vec4 col; +uniform highp mat4 matrix; +void main() { + col = colAttr; + gl_Position = matrix * posAttr; +} +''' + + fragmentShaderSource = ''' +varying lowp vec4 col; +void main() { + gl_FragColor = col; +} +''' + + def __init__(self): + super(TriangleWindow, self).__init__() + + self.m_program = 0 + self.m_frame = 0 + + self.m_posAttr = 0 + self.m_colAttr = 0 + self.m_matrixUniform = 0 + + def initialize(self): + self.m_program = QOpenGLShaderProgram(self) + + self.m_program.addShaderFromSourceCode(QOpenGLShader.Vertex, + self.vertexShaderSource) + self.m_program.addShaderFromSourceCode(QOpenGLShader.Fragment, + self.fragmentShaderSource) + + self.m_program.link() + + self.m_posAttr = self.m_program.attributeLocation('posAttr') + self.m_colAttr = self.m_program.attributeLocation('colAttr') + self.m_matrixUniform = self.m_program.uniformLocation('matrix') + + def render(self, gl): + gl.glViewport(0, 0, self.width(), self.height()) + + gl.glClear(gl.GL_COLOR_BUFFER_BIT) + + self.m_program.bind() + + matrix = QMatrix4x4() + matrix.perspective(60, 4.0/3.0, 0.1, 100.0) + matrix.translate(0, 0, -2) + matrix.rotate(100.0 * self.m_frame / self.screen().refreshRate(), + 0, 1, 0) + + self.m_program.setUniformValue(self.m_matrixUniform, matrix) + + vertices = array.array('f', [ + 0.0, 0.707, + -0.5, -0.5, + 0.5, -0.5]) + + gl.glVertexAttribPointer(self.m_posAttr, 2, gl.GL_FLOAT, False, 0, + vertices) + gl.glEnableVertexAttribArray(self.m_posAttr) + + colors = array.array('f', [ + 1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0]) + + gl.glVertexAttribPointer(self.m_colAttr, 3, gl.GL_FLOAT, False, 0, + colors) + gl.glEnableVertexAttribArray(self.m_colAttr) + + gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3) + + self.m_program.release() + + self.m_frame += 1 + + +if __name__ == '__main__': + + import sys + + app = QGuiApplication(sys.argv) + + format = QSurfaceFormat() + format.setSamples(4) + + window = TriangleWindow() + window.setFormat(format) + window.resize(640, 480) + window.show() + + window.setAnimating(True) + +sys.exit(app.exec_()) diff --git a/src/pyqt/test002.py b/src/pyqt/test002.py new file mode 100755 index 0000000..e3c6218 --- /dev/null +++ b/src/pyqt/test002.py @@ -0,0 +1,85 @@ +import numpy as np +from OpenGL import GL, GLU +from PyQt5.QtWidgets import QOpenGLWidget, QApplication + +global eyeX, eyeY, eyeZ + + +# gluLookAt 'eye' coordinates +eyeX = 0.0 +eyeY = 0.0 +eyeZ = 10.0 + +class OpenGLWidget(QOpenGLWidget): + + def initializeGL(self): + + GL.glClearColor(0,0,0,1); +# GL.glEnable(GL.GL_DEPTH_TEST); +# GL.glEnable(GL.GL_LIGHT0); +# GL.glEnable(GL.GL_LIGHTING); +# GL.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE); +# GL.glEnable(GL.GL_COLOR_MATERIAL); + + GL.glMatrixMode(GL.GL_PROJECTION); + GL.glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0) + GL.glClear(GL.GL_COLOR_BUFFER_BIT) + GL.glMatrixMode(GL.GL_MODELVIEW); + GL.glLoadIdentity(); + + #GL.glClearColor(1.0, 0.0, 1.0, 0.0); +# vertices = np.array([0.0, 1.0, -1.0, -1.0, 1.0, -1.0], dtype=np.float32) +# +# bufferId = GL.glGenBuffers(1) +# GL.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferId) +# GL.glBufferData(GL.GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL.GL_STATIC_DRAW) +# +# GL.glEnableVertexAttribArray(0) +# GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, 0, None) + + def paintGL(self): +# GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3) + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + + GLU.gluLookAt(eyeX, eyeY, eyeZ, + 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0); +# GL.glBegin(GL.GL_TRIANGLES); +# GL.glColor3f(1.0, 0.0, 0.0); +# GL.glVertex3f(-0.5, -0.5, 0); +# GL.glColor3f(0.0, 1.0, 0.0); +# GL.glVertex3f( 0.5, -0.5, 0); +# GL.glColor3f(0.0, 0.0, 1.0); +# GL.glVertex3f( 0.0, 0.5, 0); +# GL.glEnd(); + + from math import sin, cos + radius = 0.5 + halfLength = 3.0 + slices = 10 + + for i in range(slices): + theta = float(i) * 2.0 * 3.14 / float(slices) + nextTheta = float(i + 1) * 2.0 * 3.14 / float(slices) + + GL.glColor3f(1.0, 0.0, 0.0) + GL.glBegin(GL.GL_TRIANGLE_STRIP) + + GL.glVertex3f(0.0, halfLength, 0.0) + GL.glVertex3f(radius*cos(theta), halfLength, radius*sin(theta)) + GL.glVertex3f (radius*cos(nextTheta), halfLength, radius*sin(nextTheta)) + + GL.glVertex3f (radius*cos(nextTheta), -halfLength, radius*sin(nextTheta)) + GL.glVertex3f(radius*cos(theta), -halfLength, radius*sin(theta)) + GL.glVertex3f(0.0, -halfLength, 0.0) + + GL.glEnd() + + +app = QApplication([]) +widget = OpenGLWidget() +widget.show() + +app.exec_() + + diff --git a/src/pyqt/test003.py b/src/pyqt/test003.py new file mode 100755 index 0000000..1593f6d --- /dev/null +++ b/src/pyqt/test003.py @@ -0,0 +1,124 @@ +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * +import numpy as np +import sys + +global lp, lq, rp, rq, z, eyeX, eyeY, eyeZ +lp = 0.0 +lq = 2.5 +rp = 0.0 +rq = 2.5 +z = 0.0 + +# gluLookAt 'eye' coordinates +eyeX = 0.0 +eyeY = 0.0 +eyeZ = 10.0 + +def init(): + global eyeX, eyeY, eyeZ + glClearColor(0.0, 0.0, 0.0, 1.0) + glMatrixMode(GL_PROJECTION) + glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0) + glClear(GL_COLOR_BUFFER_BIT) + glMatrixMode(GL_MODELVIEW) + glLoadIdentity() + gluLookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) + +def plotdial(): + global lp, lq, rp, rq, eyeX, eyeY, eyeZ + #lOrigin = np.array((0.0, 0.0, 0.0)) + rOrigin = np.array((5.0, 0.0, 0.0)) + radius = 2.5 + + glClear(GL_COLOR_BUFFER_BIT) + glMatrixMode(GL_MODELVIEW) + glLoadIdentity() + gluLookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) + + # Left arm unit vector calculation + lVec = np.array((lp, lq, z)) + lMag = np.linalg.norm(lVec) + lVec = (lVec/lMag)*radius + glColor3f(1.0, 0.0, 0.0) + glLineWidth(3.0) + glBegin(GL_LINES) + glVertex3f(0.0, 0.0, z) + glVertex3f(lVec[0], lVec[1], z) + glEnd() + glLineWidth(1.0) + glColor3f(0.0, 0.0, 1.0) + glutWireSphere(2.5, 25, 25) + + # Right + glPushMatrix() + glTranslatef(rOrigin[0], rOrigin[1], z) + glColor3f(0.0, 1.0, 0.0) + glLineWidth(3.0) + glBegin(GL_LINES) + glVertex3f(0.0, 0.0, z) + rVec = np.array((rp, rq, z)) + rMag = np.linalg.norm(rVec) + rVec = (rVec/rMag)*radius + glVertex3f(rVec[0], rVec[1], z) + glEnd() + glLineWidth(1.0) + glColor3f(0.0, 0.0, 1.0) + glutWireSphere(2.5, 25, 25) + glPopMatrix() + + glFlush() + +def keyboard(key, x, y): + global glutshape, solid, eyeX, eyeY, eyeZ + step = 0.01 + if key == chr(27) or key == "q": + sys.exit() + if key == "x": + eyeX -= step + if key == "X": + eyeX += step + if key == "y": + eyeY -= step + if key == "Y": + eyeY += step + if key == "z": + eyeZ -= step + if key == "Z": + eyeZ -= step + glutPostRedisplay() + +def mouse(button, state, x, y): + global lp, lq, rp, rq + o_lp = lp + o_lq = lq + o_rp = rp + o_rq = rq + if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN: + lp = -10.0 + x/500.0 * 20 + lq = 10 - y/500.0 * 20 + if lq < 0.0: + lp = o_lp + lq = o_lq + if button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN: + rp = -10.0 + x/500.0 * 20 - 5 # -5 is 2*radius + rq = 10 - y/500.0 * 20 + if rq < 0.0: + rp = o_rp + rq = o_rq + glutPostRedisplay() + +def main(): + glutInit(sys.argv) + glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB) + glutInitWindowSize(500,500) + glutInitWindowPosition(50,50) + glutCreateWindow('Dial Trial') + glutDisplayFunc(plotdial) + glutKeyboardFunc(keyboard) + glutMouseFunc(mouse) + init() + glutMainLoop() + +main() diff --git a/src/pyqt/test004.py b/src/pyqt/test004.py new file mode 100755 index 0000000..62b1b64 --- /dev/null +++ b/src/pyqt/test004.py @@ -0,0 +1,237 @@ +import sys +import math + +from OpenGL import GL, GLU + +from PyQt5.QtCore import pyqtSignal, QPoint, QSize, Qt +from PyQt5.QtGui import QColor +from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QOpenGLWidget, QSlider, + QWidget) + + +class Window(QWidget): + def __init__(self): + super(Window, self).__init__() + + self.glWidget = GLWidget() + + self.xSlider = self.createSlider() + self.ySlider = self.createSlider() + self.zSlider = self.createSlider() + + self.xSlider.valueChanged.connect(self.glWidget.setXRotation) + self.glWidget.xRotationChanged.connect(self.xSlider.setValue) + self.ySlider.valueChanged.connect(self.glWidget.setYRotation) + self.glWidget.yRotationChanged.connect(self.ySlider.setValue) + self.zSlider.valueChanged.connect(self.glWidget.setZRotation) + self.glWidget.zRotationChanged.connect(self.zSlider.setValue) + + mainLayout = QHBoxLayout() + mainLayout.addWidget(self.glWidget) + mainLayout.addWidget(self.xSlider) + mainLayout.addWidget(self.ySlider) + mainLayout.addWidget(self.zSlider) + self.setLayout(mainLayout) + + self.xSlider.setValue(15 * 16) + self.ySlider.setValue(345 * 16) + self.zSlider.setValue(0 * 16) + + self.setWindowTitle("Hello GL") + + def createSlider(self): + slider = QSlider(Qt.Vertical) + + slider.setRange(0, 360 * 16) + slider.setSingleStep(16) + slider.setPageStep(15 * 16) + slider.setTickInterval(15 * 16) + slider.setTickPosition(QSlider.TicksRight) + + return slider + + +class GLWidget(QOpenGLWidget): + xRotationChanged = pyqtSignal(int) + yRotationChanged = pyqtSignal(int) + zRotationChanged = pyqtSignal(int) + + def __init__(self, parent=None): + super(GLWidget, self).__init__(parent) + + self.object = 0 + self.xRot = 0 + self.yRot = 0 + self.zRot = 0 + + self.lastPos = QPoint() + + self.trolltechGreen = QColor.fromCmykF(0.40, 0.0, 1.0, 0.0) + self.trolltechPurple = QColor.fromCmykF(0.39, 0.39, 0.0, 0.0) + + def minimumSizeHint(self): + return QSize(50, 50) + + def sizeHint(self): + return QSize(400, 400) + + def setXRotation(self, angle): + angle = self.normalizeAngle(angle) + if angle != self.xRot: + self.xRot = angle + self.xRotationChanged.emit(angle) + self.update() + + def setYRotation(self, angle): + angle = self.normalizeAngle(angle) + if angle != self.yRot: + self.yRot = angle + self.yRotationChanged.emit(angle) + self.update() + + def setZRotation(self, angle): + angle = self.normalizeAngle(angle) + if angle != self.zRot: + self.zRot = angle + self.zRotationChanged.emit(angle) + self.update() + + def initializeGL(self): + + self.setClearColor(self.trolltechPurple.darker()) + self.object = self.makeObject() + GL.glShadeModel(GL.GL_FLAT) + GL.glEnable(GL.GL_DEPTH_TEST) + GL.glEnable(GL.GL_CULL_FACE) + + def paintGL(self): + GL.glClear( + GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) + GL.glLoadIdentity() + GL.glTranslated(0.0, 0.0, -10.0) + GL.glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0) + GL.glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0) + GL.glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0) + GL.glCallList(self.object) + + def resizeGL(self, width, height): + side = min(width, height) + if side < 0: + return + + GL.glViewport((width - side) // 2, (height - side) // 2, side, + side) + + GL.glMatrixMode(GL.GL_PROJECTION) + GL.glLoadIdentity() + GL.glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0) + GL.glMatrixMode(GL.GL_MODELVIEW) + + def mousePressEvent(self, event): + self.lastPos = event.pos() + + def mouseMoveEvent(self, event): + dx = event.x() - self.lastPos.x() + dy = event.y() - self.lastPos.y() + + if event.buttons() & Qt.LeftButton: + self.setXRotation(self.xRot + 8 * dy) + self.setYRotation(self.yRot + 8 * dx) + elif event.buttons() & Qt.RightButton: + self.setXRotation(self.xRot + 8 * dy) + self.setZRotation(self.zRot + 8 * dx) + + self.lastPos = event.pos() + + def makeObject(self): + genList = GL.glGenLists(1) + GL.glNewList(genList, GL.GL_COMPILE) + + GL.glBegin(GL.GL_QUADS) + + x1 = +0.06 + y1 = -0.14 + x2 = +0.14 + y2 = -0.06 + x3 = +0.08 + y3 = +0.00 + x4 = +0.30 + y4 = +0.22 + + self.quad(x1, y1, x2, y2, y2, x2, y1, x1) + self.quad(x3, y3, x4, y4, y4, x4, y3, x3) + + self.extrude(x1, y1, x2, y2) + self.extrude(x2, y2, y2, x2) + self.extrude(y2, x2, y1, x1) + self.extrude(y1, x1, x1, y1) + self.extrude(x3, y3, x4, y4) + self.extrude(x4, y4, y4, x4) + self.extrude(y4, x4, y3, x3) + + NumSectors = 200 + + for i in range(NumSectors): + angle1 = (i * 2 * math.pi) / NumSectors + x5 = 0.30 * math.sin(angle1) + y5 = 0.30 * math.cos(angle1) + x6 = 0.20 * math.sin(angle1) + y6 = 0.20 * math.cos(angle1) + + angle2 = ((i + 1) * 2 * math.pi) / NumSectors + x7 = 0.20 * math.sin(angle2) + y7 = 0.20 * math.cos(angle2) + x8 = 0.30 * math.sin(angle2) + y8 = 0.30 * math.cos(angle2) + + self.quad(x5, y5, x6, y6, x7, y7, x8, y8) + + self.extrude(x6, y6, x7, y7) + self.extrude(x8, y8, x5, y5) + + GL.glEnd() + GL.glEndList() + + return genList + + def quad(self, x1, y1, x2, y2, x3, y3, x4, y4): + self.setColor(self.trolltechGreen) + + GL.glVertex3d(x1, y1, -0.05) + GL.glVertex3d(x2, y2, -0.05) + GL.glVertex3d(x3, y3, -0.05) + GL.glVertex3d(x4, y4, -0.05) + + GL.glVertex3d(x4, y4, +0.05) + GL.glVertex3d(x3, y3, +0.05) + GL.glVertex3d(x2, y2, +0.05) + GL.glVertex3d(x1, y1, +0.05) + + def extrude(self, x1, y1, x2, y2): + self.setColor(self.trolltechGreen.darker(250 + int(100 * x1))) + + GL.glVertex3d(x1, y1, +0.05) + GL.glVertex3d(x2, y2, +0.05) + GL.glVertex3d(x2, y2, -0.05) + GL.glVertex3d(x1, y1, -0.05) + + def normalizeAngle(self, angle): + while angle < 0: + angle += 360 * 16 + while angle > 360 * 16: + angle -= 360 * 16 + return angle + + def setClearColor(self, c): + GL.glClearColor(c.redF(), c.greenF(), c.blueF(), c.alphaF()) + + def setColor(self, c): + GL.glColor4f(c.redF(), c.greenF(), c.blueF(), c.alphaF()) + + +if __name__ == '__main__': + + app = QApplication(sys.argv) + window = Window() + window.show() +sys.exit(app.exec_())