Opengl全屏分辨率错误

Opengl全屏分辨率错误

本文介绍了Opengl全屏分辨率错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了全屏问题,纹理中的像素一起模糊了.问题不在于绘制图像,而是纹理本身的缩放比例,感觉到我可以看到我的纹理表中的相邻纹理彼此模糊,从而导致其他纹理中的部分纹理和各处的假象.从我的代码来看,我不知道为什么会发生这种情况.

I have this fullscreen problem where the pixels in the textures blur together. The issue is NOT with drawing the images, but rather the scaling of the textures themselves, sense I can see adjacent textures in my texture sheets blur into eachother resulting in parts of textures in other textures and artifacts everywhere. I have no idea why this is happening, sense, from looking at my code, there are no apparent resolution differences.

有人可以帮我吗?我已经在这个问题上停留了很长时间了.

Could someone please help me? I've been stuck on this problem for quite a while.

设置全屏显示的代码:

public static final int WIDTH=256, HEIGHT=240;
public static int viewWidth, viewHeight, viewX, viewY;

public static void BeginSession(){

    Display.setTitle("Mega Man");

    try{
        Display.setDisplayMode(Display.getDisplayMode());
        Display.create();
    }catch(LWJGLException e){
        e.printStackTrace();
    }

    viewHeight=Display.getHeight();
    viewWidth=WIDTH*viewHeight/HEIGHT;

    Display.setVSyncEnabled(true);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,WIDTH,HEIGHT,0,1,-1);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);


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

    glEnable(GL11.GL_LINE_SMOOTH);
    glHint(GL11.GL_LINE_SMOOTH, GL_NICEST);

    glEnable(GL11.GL_POINT_SMOOTH);
    glHint(GL11.GL_POINT_SMOOTH, GL_NICEST);


    try{Display.setFullscreen(true);}catch(Exception e){}

    viewX=Display.getWidth()/2-viewWidth/2;
    viewY=Display.getHeight()/2-viewHeight/2;

    glViewport(viewX, viewY, viewWidth, viewHeight);
    //glViewport(viewX,viewY,WIDTH,HEIGHT);

    System.out.println("View Width: "+viewWidth+"\nView Height: "+viewHeight);

    textSpriteSheet = QuickLoad("Mega_Man_Font_8x8");
    highlightedTextSpriteSheet = QuickLoad("Mega_Man_Font_8x8_Yellow");
}

这是将图像渲染到屏幕上的代码.

Here's the code that renders images to the screen.

public static void DrawQuadTex(Texture tex, int x, int y, float width, float height, float texWidth, float texHeight, float subx, float suby, float subd, String mirror){

    if (tex==null){return;}
    if (mirror==null){mirror = "";}

    //subx, suby, and subd are to grab sprites from a sprite sheet. subd is the measure of both the width and length of the sprite, as only images with dimensions that are the same and are powers of 2 are properly displayed.

    int xinner = 0;
    int xouter = (int) width;
    int yinner = 0;
    int youter = (int) height;

    if (mirror.indexOf("h")>-1){
        xinner = xouter;
        xouter = 0;
    }

    if (mirror.indexOf("v")>-1){
        yinner = youter;
        youter = 0;
    }

    tex.bind();
    glTranslatef(x,y,0);

    glBegin(GL_QUADS);

    //top left
    glTexCoord2f((subx)/texWidth,(suby)/texHeight);
    glVertex2f(xinner,yinner);
    //top right
    glTexCoord2f((subx+subd)/texWidth,(suby)/texHeight);
    glVertex2f(xouter,yinner);
    //bottom right
    glTexCoord2f((subx+subd)/texWidth,(suby+subd)/texHeight);
    glVertex2f(xouter,youter);
    //bottom left
    glTexCoord2f((subx)/texWidth,(suby+subd)/texHeight);
    glVertex2f(xinner,youter);

    glEnd();

    glLoadIdentity();
}

推荐答案

您的问题与在错误位置采样纹理像素有关.

您希望在每个纹理像素的中心进行采样以避免插值,但是如果您天真地使用[ 0.0 1.0 ]范围,则​​不会发生这种情况.这是因为 0.0 位于图像的边缘,它同样靠近第一个纹理像素的中心,因为它是纹理另一侧的最后一个纹理像素的中心(当使用默认为GL_REPEAT环绕模式).

Your problem has to do with sampling texels at the wrong position.

You want to sample at the center of each texel to avoid interpolation, but that does not happen if you naively use the range [0.0, 1.0]. This is because 0.0 is on the edge of your image, it is equally close to the center of the first texel as it is the center of the last texel on the other side of the texture (when using the default GL_REPEAT wrap mode).

   http://i.msdn.microsoft.com/dynimg /IC83860.gif

  http://i.msdn.microsoft.com/dynimg/IC83860.gif

您实际上需要将坐标划分为以下范围:[ 1.0/(2.0 * N) 1.0-1.0/(2.0 * N)]

You actually need to divide your coordinates into the range: [1.0 / (2.0 * N), 1.0 - 1.0 / (2.0 * N)]

(subx + 0.5)/texWidth, (suby + 0.5)/texHeight

一般来说,您只需要移位半个像素,那么您的问题就会消失.

Baiscally you just need a half-texel shift, and then your problem will go away.

这篇关于Opengl全屏分辨率错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:29