opengl 缩放 预览摄像头

目录

opengl安装:

有的报错:

opengl pyqt渲染缩放

opengl 和控件分屏

opengl旋转

android预览摄像头:


opengl安装:

pip install -U pyopengl

有的报错:

OpenGL.error.NullFunctionError: Attempt to call an undefined function glutIn

问题:使用 pip 安装了 PyOpenGL 包,然后运行程序,结果提示:OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit

原因:使用 pip 安装的 OpenGL 包是 32 位,与 64 位电脑不匹配,故出现此错误。

解决办法:pip 不能在线安装 64 位的 OpenGL,只能手动下载后安装。
下载地址:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl

opengl pyqt渲染缩放

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GLUT import *

class OpenGLWidget(QGLWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMinimumSize(200, 200)
        self.scale_factor = 1.0

    def initializeGL(self):
        glClearColor(0.0, 0.0, 0.0, 1.0)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()
        glScalef(self.scale_factor, self.scale_factor, 1.0)
        # 绘制一个简单的三角形
        glBegin(GL_TRIANGLES)
        glColor3f(1.0, 0.0, 0.0)
        glVertex2f(0.0, 1.0)
        glColor3f(0.0, 1.0, 0.0)
        glVertex2f(-1.0, -1.0)
        glColor3f(0.0, 0.0, 1.0)
        glVertex2f(1.0, -1.0)
        glEnd()

    def resizeGL(self, w, h):
        glViewport(0, 0, w, h)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0)
        glMatrixMode(GL_MODELVIEW)

    def wheelEvent(self, event):
        # 获取滚轮滚动的距离
        num_degrees = event.angleDelta().y() / 8
        num_steps = num_degrees / 15.0  # 每次缩放15度
        self.scale_factor *= pow(1.1, num_steps)  # 乘以一个缩放因子
        self.update()

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("OpenGL Widget Example")
        self.setGeometry(100, 100, 400, 400)
        self.gl_widget = OpenGLWidget(self)
        self.setCentralWidget(self.gl_widget)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

opengl 和控件分屏

import sys

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QSplitter, QLabel
from PyQt5.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GLUT import *


class OpenGLWidget(QGLWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMinimumSize(200, 200)
        self.scale_factor = 1.0

    def initializeGL(self):
        glClearColor(0.0, 0.0, 0.0, 1.0)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()
        glScalef(self.scale_factor, self.scale_factor, 1.0)
        # 绘制一个简单的三角形
        glBegin(GL_TRIANGLES)
        glColor3f(1.0, 0.0, 0.0)
        glVertex2f(0.0, 1.0)
        glColor3f(0.0, 1.0, 0.0)
        glVertex2f(-1.0, -1.0)
        glColor3f(0.0, 0.0, 1.0)
        glVertex2f(1.0, -1.0)
        glEnd()

    def resizeGL(self, w, h):
        glViewport(0, 0, w, h)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0)
        glMatrixMode(GL_MODELVIEW)

    def wheelEvent(self, event):
        # 获取滚轮滚动的距离
        num_degrees = event.angleDelta().y() / 8
        num_steps = num_degrees / 15.0  # 每次缩放15度
        self.scale_factor *= pow(1.1, num_steps)  # 乘以一个缩放因子
        self.update()


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("OpenGL Widget Example")
        self.setGeometry(100, 100, 400, 400)
        self.gl_widget = OpenGLWidget(self)

        splitter = QSplitter()
        self.setCentralWidget(splitter)

        splitter.addWidget(self.gl_widget)
        self.image_label = QLabel()
        pixmap = QPixmap(r"C:\Users\Administrator\Pictures\mm\pics\005953_4.jpg")  # 替换为你的图片路径
        self.image_label.setPixmap(pixmap)
        splitter.addWidget(self.image_label)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

opengl旋转

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QMouseEvent
from PyQt5.QtWidgets import QApplication, QMainWindow, QSplitter, QLabel
from PyQt5.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GLUT import *


class OpenGLWidget(QGLWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMinimumSize(200, 200)
        self.scale_factor = 1.0

        self.x_rotation = 0.0
        self.y_rotation = 0.0
        self.z_rotation = 0.0
        self.last_pos = None

    def initializeGL(self):
        glClearColor(0.0, 0.0, 0.0, 1.0)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()
        glScalef(self.scale_factor, self.scale_factor, 1.0)
        # glTranslatef(0.0, 0.0, -5.0)
        glRotatef(self.x_rotation, 1.0, 0.0, 0.0)
        glRotatef(self.y_rotation, 0.0, 1.0, 0.0)
        glRotatef(self.z_rotation, 0.0, 0.0, 1.0)

        glBegin(GL_TRIANGLES)
        glColor3f(1.0, 0.0, 0.0)
        glVertex2f(0.0, 1.0)
        glColor3f(0.0, 1.0, 0.0)
        glVertex2f(-1.0, -1.0)
        glColor3f(0.0, 0.0, 1.0)
        glVertex2f(1.0, -1.0)
        glEnd()

    def resizeGL(self, w, h):
        glViewport(0, 0, w, h)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0)
        glMatrixMode(GL_MODELVIEW)

    def wheelEvent(self, event):
        num_degrees = event.angleDelta().y() / 8
        num_steps = num_degrees / 15.0  # 每次缩放15度
        self.scale_factor *= pow(1.1, num_steps)  # 乘以一个缩放因子
        self.update()
    def mousePressEvent(self, event: QMouseEvent):
        self.last_pos = event.pos()

    def mouseMoveEvent(self, event: QMouseEvent):
        if self.last_pos is None:
            return
        dx = event.x() - self.last_pos.x()
        dy = event.y() - self.last_pos.y()
        if event.buttons() & Qt.LeftButton:
            self.x_rotation += dy * 0.5
            self.y_rotation += dx * 0.5
        elif event.buttons() & Qt.RightButton:
            self.x_rotation += dy * 0.5
            self.z_rotation += dx * 0.5
        self.last_pos = event.pos()
        self.update()

    def mouseReleaseEvent(self, event: QMouseEvent):
        self.last_pos = None

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("OpenGL Widget Example")
        self.setGeometry(100, 100, 800, 400)
        self.gl_widget = OpenGLWidget(self)

        splitter = QSplitter()
        self.setCentralWidget(splitter)

        splitter.addWidget(self.gl_widget)
        self.image_label = QLabel()
        self.image_label.setFixedSize(400, 400)
        pixmap = QPixmap(r"C:\Users\Administrator\Pictures\mm\pics\005953_4.jpg")  # 替换为你的图片路径
        self.image_label.setPixmap(pixmap)
        splitter.addWidget(self.image_label)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

android预览摄像头:

GitHub - ChinaZeng/OpenGLESCameraDemo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值