pyglet and opengl -- 纹理映射以及动画

目录

可以加载3d obj,不能显示背景图片,obj渲染的结果不对。 

渲染图片:


可以加载3d obj,不能显示背景图片,obj渲染的结果不对。 

import os

import pyglet

from pyglet.gl import glEnable, GL_DEPTH_TEST, GL_CULL_FACE
from pyglet.math import Mat4, Vec3
from pyglet.sprite import Sprite


time = 0
window = pyglet.window.Window(width=1280, height=720, resizable=False)
batch = pyglet.graphics.Batch()

@window.event
def on_resize(width, height):
    window.viewport = (0, 0, *window.get_framebuffer_size())
    window.projection = Mat4.perspective_projection(window.aspect_ratio, z_near=0.1, z_far=255)
    # window.projection = Mat4.perspective_projection(window.aspect_ratio, z_near=0.8, z_far=2)
    return pyglet.event.EVENT_HANDLED

index=0

@window.event
def on_draw():
    global index
    index+=1
    window.clear()
    print("on_draw",index)

    batch.draw()
    object_sprite.draw()
    # image.blit(-4, -4)  # 重绘窗口,从左下角的(0,0)处开始绘制

    # filename = f"imgs/frame-sec_{index}.png"
    # pyglet.image.get_buffer_manager().get_color_buffer().save(filename)


def animate(dt):
    global time
    time += dt
    print("animate",time)
    rot_x = Mat4.from_rotation(time, Vec3(1, 0, 0))
    rot_y = Mat4.from_rotation(time/2, Vec3(0, 1, 0))
    rot_z = Mat4.from_rotation(time/3, Vec3(0, 0, 1))
    trans = Mat4.from_translation(Vec3(1.25, 0, 2))
    model_logo.matrix = trans @ rot_x @ rot_y @ rot_z

    rot_x = Mat4.from_rotation(time, Vec3(1, 0, 0))
    rot_y = Mat4.from_rotation(time/3, Vec3(0, 1, 0))
    rot_z = Mat4.from_rotation(time/2, Vec3(0, 0, 1))
    trans = Mat4.from_translation(Vec3(-1.75+2, 0, 0))
    model_box.matrix = trans @ rot_x @ rot_y @ rot_z


if __name__ == "__main__":


    glEnable(GL_DEPTH_TEST)
    glEnable(GL_CULL_FACE)

    os.makedirs('imgs',exist_ok=True)

    model_logo = pyglet.resource.model("mesh.obj", batch=batch)
    model_box = pyglet.resource.model("box.obj", batch=batch)

    # Set the application wide view matrix (camera):
    window.view = Mat4.look_at(position=Vec3(0, 0, 5), target=Vec3(0, 0, 0), up=Vec3(0, 1, 0))

    image = pyglet.image.load(r'D:\project\detect\jupter_demo\wangxin.jpg')  # 读取图片

    object_sprite = Sprite(image)
    pyglet.clock.schedule_interval(animate, 1 / 60)
    pyglet.app.run()

渲染图片:

#-*- coding:gbk -*-
#from pyglet.gl import *
from OpenGL.GL import *
import pyglet
from pyglet import clock
from PIL import Image
from ctypes import *

def draw_rect(x, y,z,width, height,ang):
    """
    实际矩形代码
    """
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glPushMatrix()
    glLoadIdentity()
    glTranslatef(x+width/2,y+height/2,0.0)
    glRotatef(ang,0.0,1.0,0.0)
    glBegin(GL_QUADS)
    glTexCoord2f(0.0,0.0)  #纹理坐标
    glVertex3f(x-width/2, y-height/2,z)
    glTexCoord2f(1.0,0.0)
    glVertex3f(x + width/2, y-height/2,z)
    glTexCoord2f(1.0,1.0)
    glVertex3f(x + width/2, y + height/2,z)
    glTexCoord2f(0.0,1.0)
    glVertex3f(x-width/2, y + height/2,z)
    glEnd()
    glPopMatrix()


class Button():
    x=y=z=0
    width=height=10.0
    ang=0
    TEXTUREWIDTH=100
    TEXTUREHEIGHT=100
    image_data=[] #保存纹理数据
    def draw(self):
        """
        画矩形
        """
        draw_rect(self.x,self.y,self.z,self.width,self.height,self.ang)

    def loadTexture(self):
        """
        载入纹理
        """
        #创建纹理对象
        texid=glGenTextures(1)        #绑定纹理
        glBindTexture(GL_TEXTURE_2D,texid)
        glPixelStorei(GL_UNPACK_ALIGNMENT,1)
        #开启纹理功能
        glEnable(GL_TEXTURE_2D)
        #说明映射方式
        glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL)
        #设置滤波方式
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
        #纹理图
        glTexImage2D(GL_TEXTURE_2D,0,3,self.TEXTUREWIDTH,self.TEXTUREHEIGHT,0,GL_RGBA,GL_UNSIGNED_BYTE,self.image_data)


    def load_image(self,imagePath):
        """
        读取图像的数据
        """
        imageData=Image.open(imagePath)
        try:
            imageData=imageData.convert('RGB')
            self.TEXTUREWIDTH,self.TEXTUREHEIGHT,self.image_data=imageData.size[0],imageData.size[1],imageData.tostring('raw','RGBA',0,-1)
        except SystemError:
            self.TEXTUREWIDTH,self.TEXTUREHEIGHT,self.image_data=imageData.size[0],imageData.size[1],imageData.tostring('raw','RGBX',0,-1)
        assert self.TEXTUREWIDTH*self.TEXTUREHEIGHT*4 == len(self.image_data)




class MyWindow(pyglet.window.Window):
    def __init__(self):
        super(MyWindow,self).__init__()
        #按钮
        self.button=Button()
        self.button.x=10
        self.button.y=10
        self.button.z=0
        self.button.width=self.width/2
        self.button.height=self.height/2
        self.button.ang=0

        #所有要画的图元
        self.need_draw=[
                self.button,
                            ]
    def on_draw(self):
        """
        画图,刷新
        """
        self.clear()
        for draw_object in self.need_draw:
            draw_object.draw()
    def value_change(self,dt):
        self.button.x+=10.0





if __name__ == "__main__":
    wn=MyWindow()
    wn.button.load_image('tex.jpg')
    wn.button.loadTexture()
    clock.schedule_interval(wn.value_change,0.5)
pyglet.app.run()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI算法网奇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值