繁体   English   中英

图块集的 Jogl uv 坐标

[英]Jogl uv coordinates for tilesets

我目前正在学习 jogl,所以我可以制作自己的 2d 游戏。 瓦片的所有纹理都存储在一个大瓦片集中。 我尝试使用Texture.getSubImageTexCoords()绘制单个图块,但结果很奇怪: 奇怪的结果

我不确定如何准确地描述它,但构成四边形的左上三角形正在以一种非常奇怪的方式拉伸。 我只想显示图块集的一部分。 我尝试在网上寻找一些东西,但找不到任何东西。

这是我的代码:

public class TestSubImage {
    private GLU glu = new GLU();
    private GLCanvas mainCanvas;
    private Texture texture;
    private TextureCoords tc;

    class Listener implements GLEventListener {
        public void init(GLAutoDrawable drawable) {
            // Load texture
            try {
                texture = TextureIO.newTexture(TestSubImage.class.getResource("/images/sheet.png"), false, null);
                texture.setTexParameteri(drawable.getGL(), drawable.getGL().GL_TEXTURE_MAG_FILTER, drawable.getGL().GL_NEAREST);
                tc = texture.getSubImageTexCoords(0, 16, 16, 32); // sub texture coords
            } catch (IOException e) {
                e.printStackTrace();
            }

            // Set up
            drawable.getGL().glClearColor(0.3f, 0.3f, 0.3f, 0);
            glu = new GLU();
            glu.gluOrtho2D(0, 512, 512, 0);
        }

        public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

        }

        public void dispose(GLAutoDrawable drawable) {
        }

        public void display(GLAutoDrawable drawable) {
            GL2 gl = drawable.getGL().getGL2();
            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

            // where to draw the image
            int x = 100;
            int y = 100;
            int width = 200;
            int height = 200;

            gl.glColor3f(1, 1, 1);

            // Draw the image
            texture.enable(gl);
            texture.bind(gl);

            gl.glBegin(GL2.GL_QUADS);
            gl.glVertex2f(x, y);
            gl.glTexCoord2f(tc.left(), tc.bottom());
            gl.glVertex2f(x, y + height);
            gl.glTexCoord2f(tc.right(), tc.bottom());
            gl.glVertex2f(x + width, y + height);
            gl.glTexCoord2f(tc.right(), tc.top());
            gl.glVertex2f(x + width, y);
            gl.glTexCoord2f(tc.left(), tc.top());
            gl.glEnd();
            texture.disable(gl);
        }
    }

    private void run() {

        // Create window
        JFrame frame = new JFrame("Texture Coords Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create profile and capabilities
        final GLProfile profile = GLProfile.get(GLProfile.GL2);
        GLCapabilities caps = new GLCapabilities(profile);

        // Now set up the main GLCanvas
        mainCanvas = new GLCanvas(caps);
        mainCanvas.addGLEventListener(new Listener());

        frame.getContentPane().add(mainCanvas);
        frame.setSize(512, 512);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new TestSubImage().run();
    }
}

瓷砖集在这里

我不使用 JOGL,但它看起来像你的

            gl.glVertex2f(x, y);
            gl.glTexCoord2f(tc.left(), tc.bottom());
            gl.glVertex2f(x, y + height);
            gl.glTexCoord2f(tc.right(), tc.bottom());
            gl.glVertex2f(x + width, y + height);
            gl.glTexCoord2f(tc.right(), tc.top());
            gl.glVertex2f(x + width, y);
            gl.glTexCoord2f(tc.left(), tc.top());

是不正确的。 如果我们将每个glVertex2f与它下面的行配对,我想它应该看起来像

            gl.glVertex2f(x, y);
            gl.glTexCoord2f(tc.left(), tc.bottom());

            gl.glVertex2f(x, y + height);
            gl.glTexCoord2f(tc.left(), tc.top());

            gl.glVertex2f(x + width, y + height);
            gl.glTexCoord2f(tc.right(), tc.top());

            gl.glVertex2f(x + width, y);
            gl.glTexCoord2f(tc.right, tc.bottom());

glTexCoord2f设置下一个顶点的纹理坐标。 因此,您使用默认 texcoord(无论是什么)渲染一个顶点,然后您使用前一个顶点的 texcoord 渲染每个顶点。 最后一个 texcoord 调用什么也不做。 (好吧,它可能作为当前的 texcoord 挂起并用于下一帧的第一个顶点)

您需要在 glVertex2f之前调用 glTexCoord2f。

PS 对于 glColor3f/4f、glNormal3f 等,它的工作原理相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM