2014-07-26 65 views
-1

我試圖將紋理映射到已編程爲淡入淡出的四邊形。代碼運行時沒有錯誤,但紋理不會加載。我得到的是一個白色的四邊形。我是Java和OpenGL的新手,不太明白爲什麼它不起作用。我試圖用很多方式改變它,但仍然無法弄清楚什麼是錯誤的。請幫幫忙,來源是:如何更改綁定到四邊形的OpenGL中的紋理的不透明度?

package smooth_transitions; 

import static org.lwjgl.opengl.GL11.*; 

import org.lwjgl.opengl.*; 
import org.lwjgl.*; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

import org.lwjgl.LWJGLException; 
import org.lwjgl.input.Keyboard; 
import org.lwjgl.input.Mouse; 
import org.lwjgl.opengl.Display; 
import org.lwjgl.opengl.DisplayMode; 
import org.newdawn.slick.opengl.Texture; 
import org.newdawn.slick.opengl.TextureLoader; 

public class SmoothTransitions { 

    private static enum State{ 
     STUDIO, INTRO, FADING, MAIN; 
    } 

    private State state = State.INTRO; 

    private Texture background; 
    private Texture menu; 
    private Texture intro; 

    public static final int WIDTH = 640; 
    public static final int HEIGHT = 480; 

    public SmoothTransitions(){ 
     try{ 
      Display.setDisplayMode(new DisplayMode(640,480)); 
      Display.setTitle("Hello, LWJGL!"); 
      Display.setVSyncEnabled(true); 
      Display.create(); 
     } catch (LWJGLException e){ 
      System.err.println("Creation of the display failed."); 
      e.printStackTrace(); 
      System.exit(1); 
     } 

     try { 
      Texture texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/football.png"))); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     //Initialisation code OpenGL 

     glMatrixMode(GL_PROJECTION); //Just do it lol 
     glLoadIdentity(); 
     glOrtho(0, 640, 480, 0, 1, -1); //This time it sets the origin to the upper-right whilst -1,-1 is bottom left 
     glMatrixMode(GL_MODELVIEW); 
     glEnable(GL_BLEND); //Enables trancluency 
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

     float fade = 0f; //Amount of fade. 

     while (!Display.isCloseRequested()){ //While close is NOT requested. 
      //Render 

      glClear(GL_COLOR_BUFFER_BIT); //Just buffers it I guess? Makes it perform better maybe. 

      switch (state){ 
       case FADING: 
        background = loadTexture("football"); 
        if (fade < 90){ // If opacity is under 100%, increment by 1.5 
         fade += 2.5f; 
        } else { 
         fade = 0; 
         background.bind(); 
         glColor4f(1.0f, 1.0f, 1.0f, 1.0f); 
         glBegin(GL_QUADS); 
          glTexCoord2f(0, 0); 
          glVertex2i(WIDTH/2 - 150, 0); 
          glTexCoord2f(1, 0); 
          glVertex2i(WIDTH/2 + 150, 0); 
          glTexCoord2f(1, 1); 
          glVertex2i(WIDTH/2 + 150, HEIGHT); 
          glTexCoord2f(0, 1); 
          glVertex2i(WIDTH/2 - 150, HEIGHT); 
         glEnd(); 
          state = State.MAIN; 
          System.out.println("State changed: " + state); 
          break; 
        } 

        background = loadTexture("football"); 
        background.bind(); 
        glColor4f(1.0f, 1.0f, 1.0f, (float) Math.sin(Math.toRadians(fade))); 
        glBegin(GL_QUADS); 
         glTexCoord2f(0, 0); 
         glVertex2i(WIDTH/2 - 150, 0); 
         glTexCoord2f(1, 0); 
         glVertex2i(WIDTH/2 + 150, 0); 
         glTexCoord2f(1, 1); 
         glVertex2i(WIDTH/2 + 150, HEIGHT); 
         glTexCoord2f(0, 1); 
         glVertex2i(WIDTH/2 - 150, HEIGHT); 
        glEnd(); 
        break; 
       case INTRO: 

        break; 
       case MAIN: 
        background.bind(); 
        glBegin(GL_QUADS); 
         glTexCoord2f(0, 0); 
         glVertex2i(WIDTH/2 - 150, 0); 
         glTexCoord2f(1, 0); 
         glVertex2i(WIDTH/2 + 150, 0); 
         glTexCoord2f(1, 1); 
         glVertex2i(WIDTH/2 + 150, HEIGHT); 
         glTexCoord2f(0, 1); 
         glVertex2i(WIDTH/2 - 150, HEIGHT); 
        glEnd(); 
        break; 
      } 

      while (Keyboard.next()){ 
       if (Keyboard.isKeyDown(Keyboard.KEY_RETURN)){ 
        switch (state){ 
         case FADING: 
          fade = 0; 
          state = State.MAIN; 
          System.out.println("State changed: " + state); 
          break; 
         case INTRO: 
          state = State.FADING; 
          System.out.println("State changed: " + state); 
          break; 
         case MAIN: 
          state = State.INTRO; 
          System.out.println("State changed: " + state); 
          break; 
        } 
       } 
      } 


      Display.update(); //This updates the screen. 
      Display.sync(60); //This sets the framerate. 
     } 

     Display.destroy(); //This closes the window. 
    } 

    private Texture loadTexture(String key){ 
     try { 
      return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png"))); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 


    public static void main(String[] args) { 
     new SmoothTransitions(); 
    } 

} 
+0

別擔心,固定它:)但是,如果有人讀它,他們會介意告訴我是什麼矩陣和什麼所有這一切,或至少直接我一個初學者將能夠了解他們的地方?謝謝。 glMatrixMode(GL_PROJECTION); //只要做到這一點大聲笑 glLoadIdentity(); gl0rtho(0,640,480,0,1,-1); //這一次,它將原點設置在右上方,而-1,-1是左下角 glMatrixMode(GL_MODELVIEW); glEnable(GL_BLEND); //啓用交易 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); –

+2

1)不要在代碼中粘貼代碼,它永遠不會工作。 2)如果你解決它,回答你自己的問題,以便其他人可以參考它。 –

回答

0

我將首先從評論發表您自己的解決問題的辦法:

glMatrixMode(GL_PROJECTION); //Just do it lol 
glLoadIdentity(); 
glOrtho(0, 640, 480, 0, 1, -1); //This time it sets the origin to the upper-right whilst -1,-1 is bottom left 
glMatrixMode(GL_MODELVIEW); 
glEnable(GL_BLEND); //Enables trancluency 
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

至於在評論你的問題:
矩陣告訴OpenGl如何操縱一個頂點來顯示它。 ModelView矩陣操縱3D世界中的一個頂點。它可以用於縮放/旋轉/平移頂點。例如,這可以用於相對於彼此移動對象。
投影矩陣告訴OGL如何將3D世界映射到2D屏幕。 還有一個GL_TEXTURE和GL_COLOR矩陣。由於我沒有與他們合作,所以我不能告訴你比名字更可疑。

這方面的一個概述,可以發現here

+0

感謝您的回覆。我想我更瞭解xD –