【问题标题】:Trying to bind a texture to a quad in LWJGL尝试将纹理绑定到 LWJGL 中的四边形
【发布时间】:2013-07-02 11:25:07
【问题描述】:

我正在尝试使用 LWJGL 将简单纹理绑定到四边形。

到目前为止,我已经制作了一个四边形,我可以用箭头键四处移动, 但是当我尝试使用 .bind() 方法为其添加纹理时, 什么都没有发生。

这是我的代码:

    import java.io.*;
    import org.lwjgl.LWJGLException;
    import org.lwjgl.input.Keyboard;
    import org.lwjgl.opengl.Display;
    import org.lwjgl.opengl.DisplayMode;
    import org.lwjgl.opengl.GL11;
    import org.newdawn.slick.Color;
    import org.newdawn.slick.opengl.Texture;
    import org.newdawn.slick.opengl.TextureLoader;
    import org.newdawn.slick.util.ResourceLoader;


    public class Test {

        Texture texture;

        boolean stop;

        float x = 400;
        float y = 300;

        public Test(){
            stop = false;
        }

        public static void main(String [ ] args){
            Test game = new Test();
            game.start();
        }

        public void start(){
            try{
                Display.setDisplayMode(new DisplayMode(800, 600));
                Display.setTitle("Jepla");
                Display.create();
                Display.setVSyncEnabled(true);
            }
            catch(LWJGLException e){
                System.out.println(e);
            }

            initGL();

            while(!Display.isCloseRequested()){
                updateGL();
                renderGL();

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

            Display.destroy();
        }

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

            try{
                texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("men.png"));

                System.out.println("Texture loaded: "+texture);
                System.out.println(">> Image width: "+texture.getImageWidth());
                System.out.println(">> Image height: "+texture.getImageHeight());
                System.out.println(">> Texture width: "+texture.getTextureWidth());
                System.out.println(">> Texture height: "+texture.getTextureHeight());
                System.out.println(">> Texture ID: "+texture.getTextureID());

            }
            catch(IOException ioe){
                System.out.println(ioe);
            }
        }

        public void renderGL(){

            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

            GL11.glColor3f(0.5f,0.5f,0.2f);

            GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());

            GL11.glPushMatrix();

                GL11.glBegin(GL11.GL_QUADS);
                    GL11.glVertex2f(x,y);
                    GL11.glVertex2f(x+texture.getTextureWidth(),y);
                    GL11.glVertex2f(x+texture.getTextureWidth(),y+texture.getTextureHeight());
                    GL11.glVertex2f(x,y+texture.getTextureHeight());
                GL11.glEnd();
            GL11.glPopMatrix();



        }

        public void updateGL(){

            if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) x = x-1;
            if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) x = x+1;

            if (Keyboard.isKeyDown(Keyboard.KEY_UP)) y = y+1;
            if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) y = y-1;

            if (x < 0) x = 0;
            if (x > 800) x = 800;
            if (y < 0) y = 0;
            if (y > 600) y = 600;
        }

    }

【问题讨论】:

    标签: textures bind lwjgl


    【解决方案1】:

    您需要启用纹理

    glEnable(GL_TEXTURE_2D);
    

    您还需要告诉 opengl 在哪里将纹理固定在您正在绘制的任何东西上

    texture.bind();
    glBegin(GL11.GL_QUADS);
                    glTexCoord2f(0, 0); // top left
                    glVertex2f(x,y);
    
                    glTexCoord2f(0, 1); // bottom left 
                    glVertex2f(x, y + objects_Y_size);
    
                    glTexCoord2f(1, 1); // bottom right
                    glVertex2f(x + objects_X_size,y + objects_Y_size);
    
                    glTexCoord2f(1, 0); // top right
                    glVertex2f(x + objects_X_size, y);
                glEnd();
    

    我不是 opengl 或 LWJGL 的专业人士,但我总是按照您在上面代码中看到的方式绑定纹理

    您也可以避免使用 GL11。如果您像这样导入opengl,则每次通话

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

    朋友们

    【讨论】:

    • 非常感谢您的回答。纹理现在正在工作。你帮助了我很多,可能还有很多其他未来的 LWJGL 初学者。 (我按照 LWJGL 官方网站上的教程进行操作,但没有在任何地方提及)
    • 没问题,用lwjgl很难在java中的opengl上找到好的tut。 youtube 上有一个关于 lwjgl 的好系列,但关于 opengl 是如何工作的却不多youtube.com/watch?v=0v56I5UWrYY
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多