Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Python OpenGL

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to configure Python OpenGL". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to configure Python OpenGL" can help you solve the problem.

OpenGL basic configuration pip installs PyOpenGLpip install PyOpenGL PyOpenGL_accelerate test code from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * def init (): glClearColor (1) gluOrtho2D (- 1) gluOrtho2D (- 1) def triangle (): glClear (GL_COLOR_BUFFER_BIT) glColor3f (1) glBegin (GL_TRIANGLES) glColor3f (1) glVertex2f (- 1) glColor3f (0) glVertex2f (1) -1) glColor3f (0penol 1) glVertex2f (0jue 1) glEnd () glFlush () def main (): glutInit (sys.argv) glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB) glutInitWindowSize (800600) glutInitWindowPosition (50) glutCreateWindow ("Triangle") glutDisplayFunc (triangle) init () glutMainLoop () if _ name__ = ='_ main__': main ()

As a result, a color triangle is drawn:

Python+OpenGL library understanding and code application 1. Read off file

It takes only four steps to create an OpenGL application using the tool library (GLUT):

(1) initialize the glut library: glutInit ()

(2) create glut window: glutCreateWindow ('Quidam Of OpenGL')

(3) callback function for registering drawings: glutDisplayFunc (draw)

(4) enter the main cycle of glut: glutMainLoop ()

In addition to the basic composition, you can also:

(5) set the initial display mode of the window: when initializing the glut library, you usually use glutInitDisplayMode () to set the initial display mode. For example:

GlutInitDisplayMode (GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)

(6) initialize the canvas

GlClearColor (0.0,0.0,0.0,1.0) # sets the canvas background color. Note: here must be 4 parameters glEnable (GL_DEPTH_TEST) # to enable the depth test to implement the occlusion relation glDepthFunc (GL_LEQUAL) # set the depth test function

About the basic composition of the draw () function:

(1) clear the screen and depth cache

GlClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

(2) set projection

The projection setting is also one of the steps required for each redraw. GlOrtho () is used to set parallel projection, and glFrustum () is used to set perspective projection. The parameters of these two functions are the same, which are the six faces of left / right / bottom / top / near / far of the scene body. The rectangle surrounded by the four faces of the left / right / bottom / top of the scene object is the viewport. Near is the projection plane, and its value is the distance between the projection plane and the viewpoint. Far is the rear section of the scene body, and its value is the distance between the rear section and the viewpoint. The difference between far and near is the depth of the scene. The relative position relationship between the viewpoint and the scene is fixed, and when the viewpoint moves, the scene moves with it. Assuming that view is the scene, and width and height are the width and height of the window, before the projection transformation, you need to declare that it is an operation on the projection matrix, and unify the projection matrix:

GlMatrixMode (GL_PROJECTION), which means that the selected matrix is the model-observation transformation matrix

All the transformation commands in OpenGL operate on the current matrix (the current matrix is the matrix to be used in the future graphic transformation), so after selecting the modifiable # transformation matrix, we should first use the glLoadIdentity () command to set the current operation matrix as the unit matrix.

GlMatrixMode (GL_PROJECTION) glLoadIdentity () if width > height: K = width / height glFrustum (view [0] * k, view [1] * k, view [2], view [3], view [4], view [5]) else: K = height / width glFrustum (view [0], view [1], view [2] * k, view [3] * k, view [4], view [5])

(3) set the viewpoint

A viewpoint is a concept associated with a visual object. To set the viewpoint, you need to consider where the eyes are, where to look, and where the head is facing, corresponding to eye, lookat and eye_up, respectively.

GluLookAt (# sets the position of the camera in the world coordinate system eye [0], eye [1], eye [2], # the position of the object aligned by the camera lens in the world coordinate system look_at [0], look_at [1], look_at [2], # the direction of the camera up in the world coordinate system eye_up [0] Eye_up [1], eye_up [2])

(4) set up viewports

The size and size of the viewport is measured in the window coordinate system, and by default its coordinate origin is located in the lower-left corner of the window, and its size is the same as the size of the window.

# glViewport (GLint x, Glint y, Glsizei width, Glsizei height) glViewport (0,0, width, height)

(5) set model transformation

Geometric transformations such as model translation, rotation, scaling, etc., need to be switched to the model matrix:

The glMatrixMode (GL_MODELVIEW) glLoadIdentity () # translation operation function glTanslate (xpeniagenz) # translates, (xmemygrad1) multiplies N (4x4 matrix) to transform the matrix glMultMatrixf (N) # rotation function, and rotates the angle specified by angle counterclockwise around the vector v = (xQuery yPowerz) T. GlRotate (angle,x,y,z) # scaling function glScale (1.0,1.0,1.0)

At the same time, to use view transformation and model transformation, we need to call the view transformation function first, and then call the model transformation function to ensure that the model transformation works on the object first.

Many kinds of transformations combine the calling order. Pay attention to the order when writing functions, otherwise it may not work.

Specific code analysis

From OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * import numpy as np def readOFF (): file_path ='.. / off/m1.off' f = open (file_path, 'ringing, encoding='utf-8') lines = f.readlines () m = [] n = [] # read out the data in the off file by line and store it in the list. Because the number of edges is 0 in advance, it does not traverse the edges. If the edge is not 0, remember to traverse and add the drawing of the edge later: for line in lines: m.append (line.split ()) for i in range (len (m)): # skip the first line of OFF if m [I] [0] = 'OFF': continue # record the number of fixed points Number of patches and sides elif I = 1: v_cout = int (m [I] [0]) f_count = int (m [I] [1]) e_count = int (m [I] [2]) continue # convert character data into numeric data else: for j in range (len ( M [I]): M [I] [j] = float (m [I] [j]) n.append (m [I]) return v_cout F_count, e_count, n # paint model def draw (): global angle # reads vertices contained in the OFF file Facet Edge and off files store data information v_cout, f_count, e_count, n = readOFF () # set rendering background glClearColor (0.0,0.0,0.0,0.0) # clear cache glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # set projection (perspective projection) glMatrixMode (GL_PROJECTION) glLoadIdentity () # projection transform gluPerspective (100,1,0.5) Angle + = 0.05 while angle > 360: angle-= 360 glMatrixMode (GL_MODELVIEW) glLoadIdentity () # Viewport Transformation glViewport (100,100,500,500) # set View Transformation # set Viewpoint gluLookAt (- 5,5,0,0,0,0,0,0,1,0,) # set Model Transformation # Pan glTranslate (1,1) 0) # rotate glRotatef (angle,-5,5,0) # scale glScalef (5,5,5) for i in range (f_count): # get the number of vertices And vertex information cout, a, b, c = n [v _ cout + I] cout, a, b, c=int (cout), int (a), int (b), int (c) # obtain vertex positions A1, a2, A3 = n [a] b1, b2, b3 = n [b] C1, c2 C 3 = n [c] # drawing polyhedron glBegin (GL_POLYGON) glVertex3f (A1, a2, a3) glVertex3f (b1, b2, b3) glVertex3f (C1, c2, c3) glEnd () # refresh cache glFlush () # close window def close (key,x Y): if key==b'\ x1bounded: glutDestroyWindow (win_id) if _ _ name__ = "_ _ main__": angle = 0 # initialization glut window glutInit () # set window display mode: RGB quad channel | single cache | depth glutInitDisplayMode (GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH) # initialization window size glutInitWindowSize (1000) # create window win_id = glutCreateWindow ("CUBE") # set render function glutDisplayFunc (draw) # set window idle function glutIdleFunc (draw) # Open depth test glEnable (GL_DEPTH_TEST) # Open window main loop glutMainLoop () this is the end of the introduction to "how to configure Python OpenGL" Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report