光滑的纹理有线条,不适合

时间:2014-04-25 17:19:17

标签: java opengl lwjgl

今天我在使用LWJGL和Slick-Util

制作游戏时遇到了问题

我的纹理是1280x720,但当我绑定它时,它看起来像这样,即使我的窗口是1280x720:

Screenshot http://puu.sh/8n2fN.png

Cube Renderer:

package org.cel.api;

import org.lwjgl.opengl.GL11;

public class CubeRenderer
{
    public static void renderCube(float x, float y, float width, float height)
    {
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(x,y);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex2f(x+width,y);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex2f(x+width,y+height);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex2f(x,y+height);
        GL11.glEnd();
        GL11.glDisable(GL11.GL_TEXTURE_2D);
    }
}

GuiMainMenu

package org.cel.client.gui;

import org.cel.api.CubeRenderer;
import org.cel.api.IGui;
import org.cel.api.Resources;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.TextureImpl;

public class GuiMainMenu implements IGui
{

    @Override
    public void update()
    {       
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);  

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, Resources.getTexture("mainMenu").getTextureID());
        GL11.glColor3f(1f, 1f, 1f);
        CubeRenderer.renderCube(0, 0, 1270, 710);
        TextureImpl.bindNone();
    }

    @Override
    public void init()
    {

    }

    @Override
    public void keyboardEvent(int key, boolean state)
    {

    }

    @Override
    public void mouseEvent(int button, boolean state, int x, int y)
    {
        System.out.println(x + " : " + y);
    }

}

主类

package org.cel;

import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_SMOOTH;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glClearDepth;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glShadeModel;
import static org.lwjgl.opengl.GL11.glViewport;

import org.cel.api.FontRenderer;
import org.cel.api.IGui;
import org.cel.api.Resources;
import org.cel.client.gui.GuiMainMenu;
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.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Cel
{
    public static Cel instance;

    public IGui currentGui;

    public FontRenderer fontRenderer;

    public static void main(String[] args)
    {
        instance = new Cel();
        instance.main(args, true);
    }

    public void main(String args[], boolean isInstance)
    {
        try
        {
            Display.setDisplayMode(new DisplayMode(1280, 720));
            Display.setTitle("Cel");
            Display.create();
            initGL();
            Resources.registerTexture("mainMenu", "gui/MainMenu.png");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            System.exit(-1);
        }
        gameLoop(args);
    }

    public void gameLoop(String args[])
    {
        currentGui = new GuiMainMenu();

        while(!Display.isCloseRequested())
        {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

            if(!currentGui.isInitialized)
            {
                currentGui.init();
            }

            currentGui.update();

            input();

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

    private void initGL() throws LWJGLException
    {
        glEnable(GL11.GL_TEXTURE_2D);
        glShadeModel(GL_SMOOTH);

        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glDisable(GL11.GL_LIGHTING);

        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClearDepth(1);

        glViewport(0, 0, 1280, 720);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        GL11.glOrtho(0, 1280, 720, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        fontRenderer = new FontRenderer();
    }

    public void input()
    {       
        while(Keyboard.next())
        {
            currentGui.keyboardEvent(Keyboard.getEventKey(), Keyboard.getEventKeyState());
        }

        while(Mouse.next())
        {
            currentGui.mouseEvent(Mouse.getEventButton(), Mouse.getEventButtonState(), Mouse.getX(), Mouse.getY());
        }
    }
}

2 个答案:

答案 0 :(得分:0)

可能是您一直关闭GL_TEXTURE_2D,但尝试在纹理关闭时绑定它。尝试删除CubeRenderer中的glEnable + glDisable

答案 1 :(得分:0)

可见图像是793x500,而它应该是1270x710,它与1280/2048和720/1024的比例完全相同(很好,舍入到整个像素)。对我来说,这看起来像你的纹理加载代码正在尝试智能化 - 对于一个90年代的场景:它创建下一个更大的二次幂值的纹理(早期的GPU不支持非二次幂纹理),并且只使用子图像。所以你可以改变texcoords(尝试1280.f / 2048.f而不是1.0的宽度,720.f / 1024 / f而不是1.0的高度),或者你的一些纹理加载代码,从目标GPU目前的千年。