pyopengl之texture

 

# coding: utf-8
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
from ctypes import sizeof, c_float, c_void_p, c_uint, string_at

from shader import *


def loadImage(imageName = "beau.jpg" ):
    """Load an image from a file using PIL.
    This is closer to what you really want to do than the
    original port's crammed-together stuff that set global
    state in the loading method.  Note the process of binding
    the texture to an ID then loading the texture into memory.
    This didn't seem clear to me somehow in the tutorial.
    """
    try:
        from PIL.Image import open
    except ImportError:
        from Image import open
    im = open(imageName)
    try:
        ix, iy, image = im.size[0], im.size[1], im.tobytes("raw", "RGBA", 0, -1)
    except SystemError:
        ix, iy, image = im.size[0], im.size[1], im.tobytes("raw", "RGBX", 0, -1)
    # generate a texture ID
    ID=glGenTextures(1)
    # make it current
    glBindTexture(GL_TEXTURE_2D, ID)
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    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_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    # copy the texture into the current texture ID
    glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
    print(ix,iy)
    #glGenerateMipmap(GL_TEXTURE_2D)
    # return the ID for use
    return ID
    
myvertices = [
     0.5,  0.5, 0.0,0.0,1.0,  # top right
     0.5, -0.5, 0.0,1.0,1.0,  # bottom right
    -0.5,  0.5, 0.0, 1.0,0.0, # top left 
    
     0.5, -0.5, 0.0,0.0,1.0, # bottom right
    -0.5, -0.5, 0.0,0.0,0.0,  # bottom left
    -0.5,  0.5, 0.0,1.0,0.0   # top left
    ]
def mydraw():

    # clear
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # view
    #glMatrixMode(GL_MODELVIEW)
    #glLoadIdentity()
    
    buffers = glGenBuffers(1)
    float_size = sizeof(c_float)
    vertex_offset    = c_void_p(0 * float_size)
    text_offset=c_void_p(4 * float_size)
    glBindBuffer(GL_ARRAY_BUFFER, buffers)
    glBufferData(GL_ARRAY_BUFFER, 
            len(myvertices)*4,  # byte size
            (ctypes.c_float*len(myvertices))(*myvertices), 
            GL_STATIC_DRAW)
    
    
    
    
    glVertexAttribPointer(0,3,GL_FLOAT, False, 5 * float_size, vertex_offset)
    glEnableVertexAttribArray(0) 
    
    glVertexAttribPointer(1, 2, GL_FLOAT, False, 5 * float_size, text_offset);
    glEnableVertexAttribArray(1);  
    
    global shader
    if shader==None:
            shader=Shader()
            shader.initShader('''
   
    layout (location = 0) in vec3 aPos;
    layout (location = 1) in vec2 aTexCoord;
    out vec2 TexCoord;
    void main()
    {
        gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
        TexCoord = aTexCoord;
       
    }
            ''',
            '''
    out vec4 FragColor;
    in vec2 TexCoord;
    uniform sampler2D ourTexture;
    void main()
    {
        FragColor =texture(ourTexture, TexCoord); 
    }
            ''')
 
    shader.begin()
    glDrawArrays(GL_TRIANGLES, 0, 6)
   
    shader.end()
    glDisableVertexAttribArray(0)
    glDisableVertexAttribArray(1)
def disp_func():
   
    mydraw()
    glutSwapBuffers()
    
if __name__=="__main__":
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
    glutInitWindowSize(960, 960)
    glutCreateWindow(b"vbo")
    glEnable(GL_DEPTH_TEST)
    loadImage("122.jpg")
    glutDisplayFunc(disp_func)
    glutIdleFunc(disp_func)
    #glutReshapeFunc(reshape_func)
    
    

    glutMainLoop()

 

以上为122.jpg原图,texture后的效果图

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PyOpenGL 是 Python 的 OpenGL 编程接口,可以让 Python 开发者使用 OpenGL 进行图形渲染。贴图是 OpenGL 中一种常用的技术,可以将图片或者纹理映射到 3D 模型上,增加模型的真实感和细节。 PyOpenGL 中使用贴图需要以下几个步骤: 1. 加载贴图图片 使用 Pillow 库中的 Image 模块加载图片,并将其转换为 OpenGL 中的纹理对象。代码如下: ``` from PIL import Image from OpenGL.GL import * from OpenGL.GLU import * # 加载图片 image = Image.open("texture.jpg") # 转换为 OpenGL 纹理对象 texture_data = image.tobytes("raw", "RGBX", 0, -1) texture_id = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture_id) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_data) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ``` 2. 绑定贴图 在绘制模型之前,需要将贴图绑定到对应的纹理单元上。代码如下: ``` glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, texture_id) ``` 3. 设置纹理坐标 在绘制模型的每个顶点上,需要设置对应的纹理坐标。纹理坐标的范围是 [0, 1],表示图片上的像素位置。代码如下: ``` glTexCoord2f(0.0, 0.0) glVertex3f(-1.0, -1.0, 0.0) glTexCoord2f(1.0, 0.0) glVertex3f(1.0, -1.0, 0.0) glTexCoord2f(1.0, 1.0) glVertex3f(1.0, 1.0, 0.0) glTexCoord2f(0.0, 1.0) glVertex3f(-1.0, 1.0, 0.0) ``` 4. 关闭贴图 在绘制完成后,需要关闭贴图。代码如下: ``` glDisable(GL_TEXTURE_2D) ``` 完整的 PyOpenGL 贴图代码示例: ``` from PIL import Image from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * def init(): glClearColor(1.0, 1.0, 1.0, 1.0) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_TEXTURE_2D) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(-2.0, 2.0, -2.0, 2.0) def display(): glClear(GL_COLOR_BUFFER_BIT) glColor3f(1.0, 1.0, 1.0) glBegin(GL_QUADS) glTexCoord2f(0.0, 0.0) glVertex3f(-1.0, -1.0, 0.0) glTexCoord2f(1.0, 0.0) glVertex3f(1.0, -1.0, 0.0) glTexCoord2f(1.0, 1.0) glVertex3f(1.0, 1.0, 0.0) glTexCoord2f(0.0, 1.0) glVertex3f(-1.0, 1.0, 0.0) glEnd() glFlush() def main(): glutInit() glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) glutInitWindowSize(400, 400) glutCreateWindow(b"PyOpenGL Texture") glutDisplayFunc(display) init() # 加载贴图 image = Image.open("texture.jpg") texture_data = image.tobytes("raw", "RGBX", 0, -1) texture_id = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture_id) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_data) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # 绑定贴图 glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, texture_id) glutMainLoop() # 关闭贴图 glDisable(GL_TEXTURE_2D) if __name__ == "__main__": main() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

proware

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值