Draw a line segment with PyOpenGL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Draw a line segment with PyOpenGL相关的知识,希望对你有一定的参考价值。

Hardware: 

  Memory: 11.7 GiB

  Processor: Intel? Core? i5-3570 CPU @ 3.40GHz × 4 

  Graphics: Gallium 0.4 on AMD CAICOS (DRM 2.43.0, LLVM 3.8.0)

  OS type: 64-bit

Dependencies:

  python3.5

  pygame

  PyOpenGL

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
def init():
    glClearColor(1,1,1,0)
    glMatrixMode(GL_PROJECTION)
    gluOrtho2D(0,200,0,150)
def lineSegment():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)#create display window
    glColor3f(0,0.4,0.2)
    glBegin(GL_LINES)
    glVertex3f(180,15,0)
    glVertex3f(10,145,0)
    glEnd()
    glFlush()
def main():
    pygame.init()
    display = (400, 300)
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
    init()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    glTranslatef(-0.5,0,0)
                if event.key == pygame.K_RIGHT:
                    glTranslatef(0.5,0,0)

                if event.key == pygame.K_UP:
                    glTranslatef(0,1,0)
                if event.key == pygame.K_DOWN:
                    glTranslatef(0,-1,0)

            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 4:
                    glTranslatef(0,0,1.0)

                if event.button == 5:
                    glTranslatef(0,0,-1.0)

        lineSegment()
        code=glGetError()
        if code!=GL_NO_ERROR:
            string=gluErrorString(code)
            print(string)
        pygame.display.flip()
        pygame.time.wait(10)
if __name__ == ‘__main__‘:
    main()

The result is as below:

 

技术分享图片

 

以上是关于Draw a line segment with PyOpenGL的主要内容,如果未能解决你的问题,请参考以下文章

How to determine which grid cells a line segment passes through?

xml 画一条线。字体:https://stackoverflow.com/questions/3616676/how-to-draw-a-line-in-android

A Monte Carlo Simulation to Draw 3 same Color Balls Without Replaced From A Bucket With 3 Red Balls

UML state diagrams with draw.io

iOS App Mapview Line Draw

semantic segmentation with deeplearning