1. 程式人生 > >Draw a line segment with PyOpenGL

Draw a line segment with PyOpenGL

cos glcolor3f window errors == 1.0 pri locals vertex

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