如何更改绑定到四边形的OpenGL中纹理的不透明度?

时间:2014-07-26 16:07:07

标签: java opengl textures lwjgl opacity

我正在尝试将纹理映射到四边形,该四边形被编程为淡入和淡出。代码运行没有错误但纹理不会加载。我得到的只是一个白色的四边形。我是Java和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();
    }

}

1 个答案:

答案 0 :(得分: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世界中的顶点。它可以用于缩放/旋转/平移顶点。例如,它可以用于相对于彼此移动对象。
Projection矩阵告诉OGL如何将3D世界映射到2D屏幕。 还有GL_TEXTURE和GL_COLOR矩阵。由于我没有和他们合作过,所以我不能告诉你更多的信息。

可以找到here

的概述