OpenGL:如何将纹理应用于此立方体?

您所在的位置:网站首页 opengl在哪 OpenGL:如何将纹理应用于此立方体?

OpenGL:如何将纹理应用于此立方体?

2023-03-03 08:43| 来源: 网络整理| 查看: 265

我正在尝试学习OpenGL,并且我已经经历了许多关于加载纹理的教程,但是每一个教程似乎都错过了最重要的一步:我如何实际在某些东西上放置纹理?

我正在使用Python,这是我加载纹理的函数:

def loadTexture(): textureSurface = pygame.image.load('test_image.png') textureData = pygame.image.tostring(textureSurface,"RGBA",1) width = textureSurface.get_width() height = textureSurface.get_height() glEnable(GL_TEXTURE_2D) texid = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texid) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) return texid

这是加载我的多维数据集的函数:

vertices = ( # x y z ( 1,-1,-1), ( 1, 1,-1), (-1, 1,-1), (-1,-1,-1), ( 1,-1, 1), ( 1, 1, 1), (-1,-1, 1), (-1, 1, 1) ) edges = ( (0,1), (0,3), (0,4), (2,1), (2,3), (2,7), (6,3), (6,4), (6,7), (5,1), (5,4), (5,7) ) def Cube(): glBegin(GL_LINES) for edge in edges: glColor3fv((1,1,1)) for vertex in edge: glVertex3fv(vertices[vertex]) glEnd()

这是主循环:

pygame.init() display = (800,600) screen = pygame.display.set_mode(display, DOUBLEBUF | OPENGL | OPENGLBLIT) gluPerspective(45, display[0]/display[1],0.1,50.0) glTranslatef(0.0,0.0,-5) while True: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) Cube() pygame.display.flip() pygame.time.wait(10)

但是立方体没有纹理.我不知道如何在立方体上实际使用加载的纹理,我找到的每个纹理教程都会将我带到loadTexture函数,而不会实际告诉我如何使用它.我在哪里打电话给它?我怎么处理texid?

1> BPL..:

您的实际代码几乎没有问题:

你没有调用加载纹理的方法.

您只绘制立方体的线条,您需要用实际纹理填充多边形,这意味着使用具有纹理坐标的三角形或四边形.

你没有处理pygame事件

以下是对代码的一些修改:

import pygame import sys from OpenGL.GL import * from OpenGL.GLU import * vertices = ( # x y z (1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, -1), (1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1) ) edges = ( (0, 1), (0, 3), (0, 4), (2, 1), (2, 3), (2, 7), (6, 3), (6, 4), (6, 7), (5, 1), (5, 4), (5, 7) ) def loadTexture(): textureSurface = pygame.image.load('test_image.png') textureData = pygame.image.tostring(textureSurface, "RGBA", 1) width = textureSurface.get_width() height = textureSurface.get_height() glEnable(GL_TEXTURE_2D) texid = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texid) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) return texid def draw_cube(lines=False): if lines: glBegin(GL_LINES) for edge in edges: glColor3fv((1, 1, 1)) for vertex in edge: glVertex3fv(vertices[vertex]) glEnd() else: glBegin(GL_QUADS) glTexCoord2f(0.0, 0.0) glVertex3f(-1.0, -1.0, 1.0) glTexCoord2f(1.0, 0.0) glVertex3f(1.0, -1.0, 1.0) glTexCoord2f(1.0, 1.0) glVertex3f(1.0, 1.0, 1.0) glTexCoord2f(0.0, 1.0) glVertex3f(-1.0, 1.0, 1.0) glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0, -1.0) glTexCoord2f(1.0, 1.0) glVertex3f(-1.0, 1.0, -1.0) glTexCoord2f(0.0, 1.0) glVertex3f(1.0, 1.0, -1.0) glTexCoord2f(0.0, 0.0) glVertex3f(1.0, -1.0, -1.0) glTexCoord2f(0.0, 1.0) glVertex3f(-1.0, 1.0, -1.0) glTexCoord2f(0.0, 0.0) glVertex3f(-1.0, 1.0, 1.0) glTexCoord2f(1.0, 0.0) glVertex3f(1.0, 1.0, 1.0) glTexCoord2f(1.0, 1.0) glVertex3f(1.0, 1.0, -1.0) glTexCoord2f(1.0, 1.0) glVertex3f(-1.0, -1.0, -1.0) glTexCoord2f(0.0, 1.0) glVertex3f(1.0, -1.0, -1.0) glTexCoord2f(0.0, 0.0) glVertex3f(1.0, -1.0, 1.0) glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0, 1.0) glTexCoord2f(1.0, 0.0) glVertex3f(1.0, -1.0, -1.0) glTexCoord2f(1.0, 1.0) glVertex3f(1.0, 1.0, -1.0) glTexCoord2f(0.0, 1.0) glVertex3f(1.0, 1.0, 1.0) glTexCoord2f(0.0, 0.0) glVertex3f(1.0, -1.0, 1.0) glTexCoord2f(0.0, 0.0) glVertex3f(-1.0, -1.0, -1.0) glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0, 1.0) glTexCoord2f(1.0, 1.0) glVertex3f(-1.0, 1.0, 1.0) glTexCoord2f(0.0, 1.0) glVertex3f(-1.0, 1.0, -1.0) glEnd() pygame.init() display = (800, 600) screen = pygame.display.set_mode( display, pygame.DOUBLEBUF | pygame.OPENGL | pygame.OPENGLBLIT) loadTexture() gluPerspective(45, display[0] / display[1], 0.1, 50.0) glTranslatef(0.0, 0.0, -5) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) draw_cube(lines=False) pygame.display.flip()

上面的代码没有使用最佳做法,但它会帮助您解决您的评论问题.

但有一条建议,我建议你在从互联网上随机转储一些代码之前先阅读一些opengl教程,尝试理解图形管道是如何工作的,然后一切都会开始变得有意义.另外,我建议你使用旧的opengl固定管道学习现代的Opengl



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3