python+opengl display three-dimensional model applet

1. Installation and preliminary use

1. Install PyOpenGL

Systems that already have python installed will automatically install pip, so you can install opengl with just one PIP command, as follows:

pip install PyOpenGL PyOpenGL_accelerate

2. Installation error

Find the appropriate version to download at this address: https://www.lfd.uci.edu/~gohlke/pythonlibs/

Installation reference: https://my.oschina.net/u/3018050/blog/1793640

----------------------

No errors were found and the following were not verified:

Then import related functions in python, errors will occur after running

OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling

Or:

SyntaxError: multiple statements found while compiling a single statement

The dll file may be missing and can be downloaded here at http://pan.baidu.com/s/1dFhC8G5

Copy to the directory of the project you created, which is the directory of the program you wrote.

-----------------------

3. A demo

Run the following program

# -*- coding:utf-8 -*-
# Author:WYC
from OpenGL.GL import *

from OpenGL.GLU import *
from OpenGL.GLUT import *
def drawFunc():
#Clear front picture
    glClear(GL_COLOR_BUFFER_BIT)
    glRotatef(0.1, 0,5,0)
#(angle, x,y,z)
    glutWireTeapot(0.5)
#Refresh Display
    glFlush()
#Initializing OpenGL with glut
glutInit()
#Display mode: GLUT_SINGLE direct display without buffer | GLUT_RGBA uses RGB(A is not alpha)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
#Window Position and Size-Generate
glutInitWindowPosition(0,0)
glutInitWindowSize(400,400)
glutCreateWindow(b"first")
#Call function to draw image
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
#Main cycle
glutMainLoop()


You should be able to display the teapot model

 

2. Simple examples of use

1. Point Parabola

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from numpy import *
import sys

def init():
    glClearColor(1.0,1.0,1.0,1.0)
    gluOrtho2D(-5.0,5.0,-5.0,5.0)

def plotfunc():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(1.0,0.2,0.6)
    glPointSize(3.0)

    glBegin(GL_POINTS)
    for x in arange(-5.0,5.0,0.1):#from -5.0 to 5.0 plus 0.1 every time
        y=x*x
        glVertex2f(x,y)
    glEnd()
    glFlush()

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
    glutInitWindowPosition(50,100)
    glutInitWindowSize(400,400)
    glutCreateWindow("Function Plotter")
    glutDisplayFunc(plotfunc)

    init()
    glutMainLoop()
main()

2. Turn the clock

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import math
import time

h = 0
m = 0
s = 0


def Draw():
    PI = 3.1415926
    R = 0.5
    TR = R - 0.05
    glClear(GL_COLOR_BUFFER_BIT)
    glLineWidth(5)
    glBegin(GL_LINE_LOOP)
    for i in range(100):
        glVertex2f(R * math.cos(2 * PI / 100 * i), R * math.sin(2 * PI / 100 * i))
    glEnd()
    glLineWidth(2)
    for i in range(100):
        glBegin(GL_LINES)
        glVertex2f(TR * math.sin(2 * PI / 12 * i), TR * math.cos(2 * PI / 12 * i))
        glVertex2f(R * math.sin(2 * PI / 12 * i), R * math.cos(2 * PI / 12 * i))
        glEnd()
    glLineWidth(1)

    h_Length = 0.2
    m_Length = 0.3
    s_Length = 0.4
    count = 60.0
    s_Angle = s / count
    count *= 60
    m_Angle = (m * 60 + s) / count
    count *= 12
    h_Angle = (h * 60 * 60 + m * 60 + s) / count
    glLineWidth(1)
    glBegin(GL_LINES)
    glVertex2f(0.0, 0.0)
    glVertex2f(s_Length * math.sin(2 * PI * s_Angle), s_Length * math.cos(2 * PI * s_Angle))
    glEnd()
    glLineWidth(5)
    glBegin(GL_LINES)
    glVertex2f(0.0, 0.0)
    glVertex2f(h_Length * math.sin(2 * PI * h_Angle), h_Length * math.cos(2 * PI * h_Angle))
    glEnd()
    glLineWidth(3)
    glBegin(GL_LINES)
    glVertex2f(0.0, 0.0)
    glVertex2f(m_Length * math.sin(2 * PI * m_Angle), m_Length * math.cos(2 * PI * m_Angle))
    glEnd()
    glLineWidth(1)
    glBegin(GL_POLYGON)
    for i in range(100):
        glVertex2f(0.03 * math.cos(2 * PI / 100 * i), 0.03 * math.sin(2 * PI / 100 * i));
    glEnd()
    glFlush()


def Update():
    global h, m, s
    t = time.localtime(time.time())
    h = int(time.strftime('%H', t))
    m = int(time.strftime('%M', t))
    s = int(time.strftime('%S', t))
    glutPostRedisplay()


glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow("My clock")
glutDisplayFunc(Draw)
glutIdleFunc(Update)
glutMainLoop()   

 

 

 

Reference documents:

1.python+opengl display 3D model applet: https://blog.csdn.net/renjiangui/article/details/76146160

2. Pilot PyOpenGL 1 (Python+OpenGL): https://www.cnblogs.com/zhouxin/p/3526402.html

3.Python's OpenGL program environment: https://blog.csdn.net/sssogs/article/details/8566169

Tags: Python pip

Posted on Mon, 16 Mar 2020 13:28:45 -0400 by Saeven