2016-08-28 58 views
0

我努力學習OpenGL的,我已經經歷了很多裝載紋理的教程,但每一個似乎錯過了最重要的一步:我怎麼居然把紋理上的東西嗎?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() 

而這裏的主loop:

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?

回答

4

沒有與您的實際代碼幾個問題:

  • 你不打電話來加載紋理的方法。
  • 你只畫立方體的線,你需要多邊形與實際的紋理,這意味着無論是使用三角形或四邊形與紋理座標被填滿。
  • 你不處理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的教程從互聯網上隨意傾倒一些代碼之前,試着去了解如何在圖形管線工程,那麼一切都將開始製作感。另外,我建議你瞭解modern Opengl而不是使用舊opengl fixed pipeline

+0

好了,所以我錯過了glTexCoord然後。謝謝。我正在通過一些教程工作,並且我掛上了這個東西。我開始更多地理解它。我會先嚐試一些更一般的教程。 –

相關問題