纹理周围的蓝色/黑色边框:OpenGL?

时间:2015-08-24 20:27:19

标签: java opengl

Tiles are pulled from tilesheet

我已经有这个错误了一段时间了。我似乎无法摆脱它。我想我可能只需要将我的四边形画成普通的矩形;当它们不是(使用纹理)时,创造它们被“旋转”的幻觉。

无论如何,正如您从图像中看到的那样,我的所有瓷砖周围都有蓝色边框。我似乎无法让它们消失。我尝试过其他瓷砖板并出现同样的问题 - 除非有时它是黑色边框。

我会发布一些不错的代码 - 这样你就拥有了解我正在做的事情所需的所有资源。

我的OpenGL设置代码:

//******** SET UP OPENGL *******
public void setUpOpenGL() {
    GL11.glEnable(GL11.GL_TEXTURE_2D);               
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);      
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
    // enable alpha blending
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 800, 0, 600, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

}

绘图代码:

@Override
public void drawTile(int tileX,  int tileY, Texture atlas) {

    int tileXcoord, tileYcoord;

    tileXcoord = (tileX - tileY) * (TILE_XHALF); // TILE_WIDTH = 128
    tileYcoord = (tileX + tileY) * (TILE_YHALF); // TILE_HEIGHT = 64

    atlas.bind();       

    GL11.glColor3f(255, 255, 255);  

    GL11.glBegin(GL11.GL_QUADS);
    {
        setTextureCoordinates(1);
        GL11.glVertex2f(tileXcoord, tileYcoord);
        setTextureCoordinates(2);
        GL11.glVertex2f(tileXcoord + TILE_XHALF, tileYcoord + TILE_YHALF);
        setTextureCoordinates(3);
        GL11.glVertex2f(tileXcoord + (TILE_XHALF * 2), tileYcoord);
        setTextureCoordinates(4);
        GL11.glVertex2f(tileXcoord + TILE_XHALF, tileYcoord - TILE_YHALF);
    }
    GL11.glEnd();

}

//***** SET TEXTURE COORDINATES *****
public void setTextureCoordinates(int pos) {
        Rectangle rect = textureAtlas.getTexture(ID);

        float x = rect.getX(), y = rect.getY();
        float height = rect.getHeight(), width = rect.getWidth();

        switch (pos) {
        case 1 :
            GL11.glTexCoord2f(x, y + (height / 2)); 
            //GL11.glTexCoord2f(0.0f, 0.03125f);
            //GL11.glTexCoord2f(0.0f, 0.5f);
            break;

        case 2:
            GL11.glTexCoord2f(x + (width / 2), y + height);
            //GL11.glTexCoord2f(0.03125f, 0.0625f);
            //GL11.glTexCoord2f(0.5f, 1f);
            break;

        case 3:
            GL11.glTexCoord2f(x + width, y + (height / 2));
            //GL11.glTexCoord2f(0.0625f, 0.03125f);
            //GL11.glTexCoord2f(1f, 0.5f);
            break;

        case 4:
            GL11.glTexCoord2f(x + (width / 2), y);
            //GL11.glTexCoord2f(0.03125f, 0.0f);
            //GL11.glTexCoord2f(0.5f, 0.0f);
            break;
        }

}

绘图代码的坐标来自(gridW = 9,gridH = 9,tilePixelWidth = 128,tilePixelHeight = 64,tilePercentOfWidth / Height也是0.0625;总图像分辨率= 2048 x 1024):

//**** GET ID SPECIFIC TEXTURE CELL ******
public Rectangle getTexture(int id) {

    int columnNumber = (int)(id % gridW); // Column of texture (using MOD so    I can get textures beyond next column [next row])
    int rowNumber = (int)(id / gridH); // Row number for texture            
    // TODO: Get texture selection working!
    // Create bounding box
    float tilePercentOfWidth = this.tilePixelWidth / this.tPWidth;
    float tilePercentOfHeight = this.tilePixelHeight / this.tPHeight;

    float boxX = (columnNumber * (tilePixelWidth / tPWidth));
    float boxY = (rowNumber * (tilePixelHeight / tPHeight));
    float boxWidth = tilePercentOfWidth;
    float boxHeight = tilePercentOfHeight;      
    // End bounding box creation

    Rectangle textureBounds = new Rectangle (boxX, boxY, boxWidth,     boxHeight);

    return textureBounds;
} 

0 个答案:

没有答案