Java OpenGL中的纹理缩放

Java OpenGL中的纹理缩放,java,opengl,Java,Opengl,我写了一个方法,当你设置正常图片的大小时,可以很容易地绘制图像,这没有问题,但是当你尝试绘制一个alpha通道的图像时,图像比它应该的小 为了让您更清晰地看到两张图片: 从理论上讲,骑士左与绿色广场的大小相同 在第二张图片中,您可以清楚地看到它。我不知道问题在哪里,通常的图像可以设置问题的大小,但不包括alpha通道 @SuppressWarnings("unused") public class Start { float x = 400, y = 300;

我写了一个方法,当你设置正常图片的大小时,可以很容易地绘制图像,这没有问题,但是当你尝试绘制一个alpha通道的图像时,图像比它应该的小

为了让您更清晰地看到两张图片:


从理论上讲,骑士左与绿色广场的大小相同


在第二张图片中,您可以清楚地看到它。我不知道问题在哪里,通常的图像可以设置问题的大小,但不包括alpha通道

    @SuppressWarnings("unused")
public class Start {

      float x = 400, y = 300;
    float rotation = 0;

    /** time at last frame */
    long lastFrame;

    /** frames per second */
    int fps;
    /** last fps time */
    long lastFPS;

    /** is VSync Enabled */
    boolean vsync;

    public void start() {
        try {
            Display.setDisplayMode(new DisplayMode(1280, 720));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 1280, 720, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);
        glEnable(GL_TEXTURE_2D);

        getDelta(); // call once before loop to initialise lastFrame
        lastFPS = getTime(); // call before loop to initialise fps timer
        Texture tex = LoadTexture("res/1.png", "PNG");
        Texture t2 = LoadTexture("res/image.png", "PNG");
        Texture t3 = LoadTexture("res/atack1/1.png", "PNG");

        while (!Display.isCloseRequested()) {
            int delta = getDelta();
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            update(delta);
            DrawImage(t3, 0, 0, 100, 120);  

                glEnd();

             Display.update();
            Display.sync(60); // cap fps to 60fps
        }

        Display.destroy();
        System.exit(0);
    }

    public void update(int delta) {
        // rotate quad
        rotation += 0.15f * delta;

        if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) x -= 0.35f * delta;
        if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) x += 0.35f * delta;

        if (Keyboard.isKeyDown(Keyboard.KEY_UP)) y -= 0.35f * delta;
        if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) y += 0.35f * delta;

        while (Keyboard.next()) {
            if (Keyboard.getEventKeyState()) {
                if (Keyboard.getEventKey() == Keyboard.KEY_F) {
                    setDisplayMode(1280, 720, !Display.isFullscreen());
                }
                else if (Keyboard.getEventKey() == Keyboard.KEY_V) {
                    vsync = !vsync;
                    Display.setVSyncEnabled(vsync);
                }
            }
        }

        // keep quad on the screen
        if (x < 0) x = 0;
        if (x > 800) x = 800;
        if (y < 0) y = 0;
        if (y > 600) y = 600;

        updateFPS(); // update FPS Counter
    }

    /**
     * Set the display mode to be used 
     * 
     * @param width The width of the display required
     * @param height The height of the display required
     * @param fullscreen True if we want fullscreen mode
     */
    public void setDisplayMode(int width, int height, boolean fullscreen) {

        // return if requested DisplayMode is already set
                if ((Display.getDisplayMode().getWidth() == width) && 
            (Display.getDisplayMode().getHeight() == height) && 
            (Display.isFullscreen() == fullscreen)) {
            return;
        }

        try {
            DisplayMode targetDisplayMode = null;

            if (fullscreen) {
                DisplayMode[] modes = Display.getAvailableDisplayModes();
                int freq = 0;

                for (int i=0;i<modes.length;i++) {
                    DisplayMode current = modes[i];

                    if ((current.getWidth() == width) && (current.getHeight() == height)) {
                        if ((targetDisplayMode == null) || (current.getFrequency() >= freq)) {
                            if ((targetDisplayMode == null) || (current.getBitsPerPixel() > targetDisplayMode.getBitsPerPixel())) {
                                targetDisplayMode = current;
                                freq = targetDisplayMode.getFrequency();
                            }
                        }

                        // if we've found a match for bpp and frequence against the 
                        // original display mode then it's probably best to go for this one
                        // since it's most likely compatible with the monitor
                        if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel()) &&
                            (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())) {
                            targetDisplayMode = current;
                            break;
                        }
                    }
                }
            } else {
                targetDisplayMode = new DisplayMode(width,height);
            }

            if (targetDisplayMode == null) {
                System.out.println("Failed to find value mode: "+width+"x"+height+" fs="+fullscreen);
                return;
            }

            Display.setDisplayMode(targetDisplayMode);
            Display.setFullscreen(fullscreen);

        } catch (LWJGLException e) {
            System.out.println("Unable to setup mode "+width+"x"+height+" fullscreen="+fullscreen + e);
        }
    }

    /** 
     * Calculate how many milliseconds have passed 
     * since last frame.
     * 
     * @return milliseconds passed since last frame 
     */
    public int getDelta() {
        long time = getTime();
        int delta = (int) (time - lastFrame);
        lastFrame = time;

        return delta;
    }

    /**
     * Get the accurate system time
     * 
     * @return The system time in milliseconds
     */
    public long getTime() {
        return (Sys.getTime() * 1000) / Sys.getTimerResolution();
    }

    /**
     * Calculate the FPS and set it in the title bar
     */
    public void updateFPS() {
        if (getTime() - lastFPS > 1000) {
            Display.setTitle("FPS: " + fps);
            fps = 0;
            lastFPS += 1000;
        }
        fps++;
    }

    public void initGL() {
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 800, 0, 600, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
    }


    public static void main(String[] argv) {
        Start fullscreenExample = new Start();
        fullscreenExample.start();

    }




     public static void DrawImage(Texture texture,float x,float y, float width, float height){
        if(texture != null){
            texture.bind();

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

                glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

                glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE  );
                glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE  );


            //set transparency
            glColor4f(1, 1, 1,1);
            //
            glTranslatef(x, y, 0);
            glBegin(GL_QUADS);
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);
            glTexCoord2f(1, 0);
            glVertex2f(width, 0);
            glTexCoord2f(1, 1);
            glVertex2f(width, height);
            glTexCoord2f(0, 1);
            glVertex2f(0, height);
            glEnd();
            glLoadIdentity(); 


        }
    }


     public static Texture LoadTexture(String path,String fileType){
        Texture tex = null;
        InputStream in = ResourceLoader.getResourceAsStream(path);
        try {
            tex = TextureLoader.getTexture(fileType, in);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return tex;

    }









}
@SuppressWarnings(“未使用”)
公开课开始{
浮动x=400,y=300;
浮动旋转=0;
/**最后一帧的时间*/
长框架;
/**每秒帧数*/
int fps;
/**最后一帧时间*/
长余辉;
/**是否启用了VSync*/
布尔同步;
公开作废开始(){
试一试{
Display.setDisplayMode(新显示模式(1280720));
Display.create();
}捕获(LWJGLEXE){
e、 printStackTrace();
系统出口(0);
}
glMatrixMode(GL_投影);
glLoadIdentity();
格洛托(0,1280,720,0,1,-1);
glMatrixMode(GLU模型视图);
glEnable(GL_纹理_2D);
getDelta();//在循环之前调用一次以初始化lastFrame
lastFPS=getTime();//在循环之前调用以初始化fps计时器
纹理tex=LoadTexture(“res/1.png”、“png”);
纹理t2=加载纹理(“res/image.png”、“png”);
纹理t3=加载纹理(“res/atack1/1.png”、“png”);
而(!Display.isCloseRequested()){
int delta=getDelta();
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
更新(增量);
DrawImage(t3、0、0、100、120);
格伦德();
Display.update();
Display.sync(60);//将fps上限设置为60fps
}
Display.destroy();
系统出口(0);
}
公共无效更新(整数增量){
//旋转四边形
旋转+=0.15f*delta;
如果(Keyboard.isKeyDown(Keyboard.KEY_LEFT))x-=0.35f*delta;
如果(Keyboard.isKeyDown(Keyboard.KEY_RIGHT))x+=0.35f*delta;
如果(Keyboard.isKeyDown(Keyboard.KEY_UP))y-=0.35f*delta;
如果(Keyboard.isKeyDown(Keyboard.keydown))y+=0.35f*delta;
while(Keyboard.next()){
if(Keyboard.getEventKeyState()){
if(Keyboard.getEventKey()==Keyboard.KEY\u F){
setDisplayMode(1280720,!Display.isFullscreen());
}
else if(Keyboard.getEventKey()==Keyboard.KEY\u V){
vsync=!vsync;
Display.setVSyncEnabled(vsync);
}
}
}
//在屏幕上显示quad
如果(x<0)x=0;
如果(x>800)x=800;
如果(y<0)y=0;
如果(y>600)y=600;
updateFPS();//更新FPS计数器
}
/**
*设置要使用的显示模式
* 
*@param width所需的显示宽度
*@param height所需的显示高度
*@param fullscreen True如果我们想要全屏模式
*/
public void setDisplayMode(整数宽度、整数高度、布尔全屏){
//如果已设置请求的显示模式,则返回
如果((Display.getDisplayMode().getWidth()==宽度)&&
(Display.getDisplayMode().getHeight()==高度)和
(Display.isFullscreen()=全屏){
返回;
}
试一试{
DisplayMode targetDisplayMode=null;
如果(全屏){
DisplayMode[]modes=Display.getAvailableDisplayModes();
intfreq=0;
对于(int i=0;i=freq)){
if((targetDisplayMode==null)| |(current.getBitsPerPixel()>targetDisplayMode.getBitsPerPixel()){
targetDisplayMode=当前;
freq=targetDisplayMode.getFrequency();
}
}
//如果我们发现bpp和频率与
//原来的显示模式,那么它可能是最好的选择这个
//因为它很可能与显示器兼容
如果((当前.GetBitSperpix()==Display.getDesktopDisplayMode().GetBitSperpix())&&
(current.getFrequency()==Display.getDesktopDisplayMode().getFrequency()){
targetDisplayMode=当前;
打破
}
}
}
}否则{
targetDisplayMode=新的显示模式(宽度、高度);
}
如果(targetDisplayMode==null){
System.out.println(“未能找到值模式:“+width+”x“+height+”fs=“+全屏”);
返回;
}
Display.setDisplayMode(targetDisplayMode);
显示。设置全屏(全屏);
}捕获(LWJGLEXE){
System.out.println(“无法设置模式”+宽度+“x”+高度+“全屏=”+全屏+e);
}
}
/** 
*计算经过了多少毫秒
*从上一帧开始。
* 
*@return自上一帧起已过毫秒
*/
公共int getDelta(){
长时间=getTime();
int delta=(int)(时间-最后一帧);
lastFrame=时间;
返回三角洲;
}
/**
*获得准确的系统时间
* 
*@以毫秒为单位返回系统时间
*/
公共长getTime(){
返回(Sys.getTime()*1000)/Sys.getTimerResolution();
}
/**
*计算FPS并在标题栏中进行设置
*/
公共void updateFPS(){
如果(getTime()-lastFPS>1000){
迪普拉