Java纹理不会显示

时间:2014-12-29 06:30:52

标签: java opengl graphics textures

我是java编程的新手,我正在尝试绘制一个纹理方块,但它只显示为白色。 如果我添加glEnable(texture2d)它会扭曲颜色。当不是“状态格式”时,我可以得到纹理绑定,但就像我说的那样,glEnable(texture2d)会扭曲颜色。有什么东西我不见了?谢谢!

这是主要的课程

package main;
static org.lwjgl.opengl.GL11.*;

import org.newdawn.slick.openal.*;
import org.newdawn.slick.opengl.Texture;

import static helper.Artist.*;
import helper.Artist.*;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;

public class GameWindow {

public static enum states{

    MAIN, GAME; }

public states state = states.MAIN;




public void render(){
    switch(state){
    case MAIN:
        glColor3f(1.0f, 0, 0);
        glRectf(0, 0, 800, 600);
        break;
    case GAME:


        glColor3f(1,1,1);
        DrawQuad(grass,5,5,64,64);
    }


}

public void checkInput(){
    switch(state){
    case MAIN:
        if(Keyboard.isKeyDown(Keyboard.KEY_RETURN)){
            state = states.GAME;
        }

        break;
    case GAME:
        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
            state = states.MAIN;
        }
    }

}

public GameWindow(){


    try {
        Display.setDisplayMode(new DisplayMode(800, 600));
        Display.create();
    } catch (LWJGLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Display.setTitle("Hello");

    //initialization
    grass = LoadTexture("dirt64");
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 800, 600, 0, 1, -1 );
    glMatrixMode(GL_MODELVIEW);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    while(!Display.isCloseRequested()){
        //render code
        checkInput();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        render();

        Display.update();
        Display.sync(60);
    }

    Display.destroy();
}


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

}}

第二课

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

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

import org.newdawn.slick.openal.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
public class Artist {

public static Texture grass;
public void load(){

    try {
        grass = TextureLoader.getTexture("PNG", new FileInputStream("res/dirt64.png"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public static Texture LoadTexture(String key){
    try{
        return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));

    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}
public static void Draw(float x, float y, float width, float height){

    glBegin(GL_QUADS);
        glVertex2f(x,y);
        glVertex2f(x + width, y);
        glVertex2f(x + width, y + height);
        glVertex2f(x, y + height);
    glEnd();


}

public static void DrawQuad(Texture texture, float x, float y, float width, float height){


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    texture.bind();
    glBegin(GL_QUADS);
        glVertex2f(x, y);
        glVertex2f(x + width, y);
        glVertex2f(x + width, y + height);
        glVertex2f(x, y + height);
    glEnd();


}
}

0 个答案:

没有答案