我試圖將紋理映射到已編程爲淡入淡出的四邊形。代碼運行時沒有錯誤,但紋理不會加載。我得到的是一個白色的四邊形。我是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();
}
}
別擔心,固定它:)但是,如果有人讀它,他們會介意告訴我是什麼矩陣和什麼所有這一切,或至少直接我一個初學者將能夠了解他們的地方?謝謝。 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); –
1)不要在代碼中粘貼代碼,它永遠不會工作。 2)如果你解決它,回答你自己的問題,以便其他人可以參考它。 –