尝试在LWJGL中将纹理绑定到四边形

3

我在尝试使用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;
        }

    }
1个回答

2

你需要启用纹理

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的专家,但我总是按照上面代码中所示的方式绑定纹理。
如果您像这样导入OpenGL,您也可以避免为每个调用使用GL11。
import static org.lwjgl.opengl.GL11.*;

祝你好运


非常感谢您的回答。纹理现在可以正常工作了。您帮了我很大的忙,也可能帮助了许多其他未来的LWJGL初学者。(我遵循了LWJGL官方网站上的教程,但这个问题在任何地方都没有被提到) - Tomato
没有问题。在java中使用lwjgl学习opengl很难找到好的教程。在youtube上有一个关于lwjgl的好系列,但关于opengl如何工作的内容并不多。http://www.youtube.com/watch?v=0v56I5UWrYY - richi lonsdale

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接