PyOpenGL はじめました:glTexImage2D

PyOpenGL はじめました記事一覧
glTexImage2D

《著》小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部
第1版♪2006/09/21 ● 第2版♪2009/04/07

■ 概要

glTexImage2D:2次元のテクスチャー画像を設定します。

  • glTexImage2D, glTexImage2Db, glTexImage2Df, glTexImage2Di, glTexImage2Ds, glTexImage2Dub, glTexImage2Dui, glTexImage2Dus
■ 関連記事
GL redbook bluebook Description
glTexImage2D Chapter 9 - OpenGL Programming Guide
・Example 9-1 : Texture-Mapped Checkerboard: checker.c
Chapter 5. OpenGL Reference Pages
glTexImage2D
specify a two-dimensional texture image
□□□□□□□□

概要:glTexImage2D

$ cd ../redbook/Chapter9\,Texture\ Mapping/glTexImage2D/
$ python2.5 -i glTexImage2D.py -ss
['glTexImage2D.py', '-ss']
>>> print glTexImage2D.__doc__
glTexImage2D(
    GLenum(target), GLint(level), GLint(internalformat),
    GLsizei(width), GLsizei(height), GLint(border), GLenum(format),
    GLenum(type), POINTER(GLvoid)(pixels)
    ) -> None

事例

$ python2.5 glTexImage2D.py
image/SmalltalkBalloon.gif
省略時に指定した画像ファイルを表示します。
$ python2.5 glTexImage2D.py image/Pig.gif
image/Pig.gif
コマンドラインで指定した画像ファイルを表示します。
□□□□□□□□□□□□□□□□□□□□□□□□ □□□□□□□□□□□□□□□□□□□□□□□□
#! /usr/bin/python2.5
"""
OpenGL Programming Guide
Chapter 9 : OpenGL Programming Guide
Draw the Scene, Supplying Both Texture and Geometric Coordinates
Example 9-1 : Texture-Mapped Checkerboard: checker.c
"""
from OpenGL.GL   import *
from OpenGL.GLU  import *
from OpenGL.GLUT import *
from sys import argv

def init(width, height):
    glClearColor(0, 0, 0, 0)
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    texName = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, texName)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    makeTextureImage()
    gluPerspective(30.0, float(width)/float(height), 0.1, 100.0)

def makeTextureImage():
    width, height, pixels = pixImage()
    glTexImage2D(
        GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB,
        GL_UNSIGNED_BYTE, pixels)

def pixImage():
    image = photoImage()
    width  = image.width()
    height = image.height()
    pixels = []
    for y in range(height):
        for x in range(width):
            for e in image.get(x, y).split(" "):
                pixels.append(int(e))
    return width, height, pixels

from Tkinter import Tk, PhotoImage
def photoImage():
    filename = "image/SmalltalkBalloon.gif"
    if len(argv)>1: filename = argv[1]
    print filename

    spam = Tk()
    image = PhotoImage(file=filename)    
    del spam

    return image
	
def display():
    glClear(GL_COLOR_BUFFER_BIT)
    glEnable(GL_TEXTURE_2D)
    glTranslatef(0.0,0.0,-5.0)      # Move Into The Screen

    glBegin(GL_QUADS)
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,  1.0, 1.0)    # Top Left
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,  1.0, 1.0)    # Top Right
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, -1.0, 1.0)    # Bottom Right
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, -1.0, 1.0)    # Bottom Left
    glEnd()
    glFinish();

## ----------------------------------------
def ex_():
    glutInit(argv)
    glutInitDisplayMode(GLUT_RGBA)
    glutInitWindowSize(200, 150)
    glutInitWindowPosition(100, 100)
    glutCreateWindow(title())
    init(640, 480)
    glutDisplayFunc(display)
    glutKeyboardFunc(keyboard)
    glutMainLoop()
  • The checkerboard texture is generated in the routine makeCheckImage(), and all the texture-mapping initialization occurs in the routine init(). glGenTextures() and glBindTexture() name and create a texture object for a texture image. (See "Texture Objects.") The single, full-resolution texture map is specified by glTexImage2D(), whose parameters indicate the size of the image, type of the image, location of the image, and other properties of it. (See "Specifying the Texture" for more information about glTexImage2D().)

misc

def title():
    return argv[0][:-3]

def keyboard(*args):
    print args
    key, x, y = args
    if key == '\x1b':   # escape
        exit()

def ex_s(): print argv

EX = ""
if __name__ == '__main__':
    for e in argv:
        if e.startswith("-s"): EX = e[2:]
    eval("ex_%s()"%EX)

Tips

》作業中です《

Last updated♪09/04/22