SDL2 で OpenGL - 三角形の出力
はじめに
pysdl2
でOpenGLを使ってみたメモです。
環境
- Ubuntu14.04
- Python3.4.3
PySDL2のインストール
http://pysdl2.readthedocs.io/en/rel_0_9_4/install.html#getting-the-sources
に従いインストール。
回るトライアングル
import sdl2
from OpenGL import GL, GLU
import ctypes
def main():
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 300
if sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) != 0:
print(sdl2.SDL_GetError())
return -1
window = sdl2.SDL_CreateWindow(
b"Triangle",
sdl2.SDL_WINDOWPOS_UNDEFINED,
sdl2.SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT,
sdl2.SDL_WINDOW_OPENGL
)
if not window:
print(sdl2.SDL_GetError())
return -1
ctx = sdl2.SDL_GL_CreateContext(window)
GL.glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
GL.glMatrixMode(GL.GL_PROJECTION | GL.GL_MODELVIEW)
GL.glLoadIdentity()
GLU.gluPerspective(30.0, WINDOW_WIDTH/WINDOW_HEIGHT, 1.0, 100.0)
GLU.gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
ev = sdl2.SDL_Event()
isrunning = True
while isrunning:
while sdl2.SDL_PollEvent(ctypes.byref(ev)) != 0:
if ev.type == sdl2.SDL_QUIT:
isrunning = not isrunning
GL.glClearColor(0, 0, 0, 1)
GL.glClear(GL.GL_COLOR_BUFFER_BIT)
GL.glRotatef(1.0, 0.0, 1.0, 0.0)
GL.glBegin(GL.GL_TRIANGLES)
GL.glColor3f(1.0, 0.0, 0.0)
GL.glVertex2f(0.0, 1.0)
GL.glColor3f(0.0, 1.0, 0.0)
GL.glVertex2f(1.0, -1.0)
GL.glColor3f(0.0, 0.0, 1.0)
GL.glVertex2f(-1.0, -1.0)
GL.glEnd()
sdl2.SDL_GL_SwapWindow(window)
sdl2.SDL_Delay(10)
sdl2.SDL_GL_DeleteContext(ctx)
sdl2.SDL_DestroyWindow(window)
sdl2.SDL_Quit()
return 0
if __name__ == '__main__':
import sys
sys.exit(main())
Author And Source
この問題について(SDL2 で OpenGL - 三角形の出力), 我々は、より多くの情報をここで見つけました https://qiita.com/narupo/items/6ccc36f4fe9a3af28977著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .