VBO纹理未渲染

时间:2019-01-21 20:09:43

标签: java render lwjgl quad

我开始使用LWJGL,并且在yoututbe上观看了som教程。我的四边形无法渲染有问题。每当我运行程序时,我都会得到黑屏。我搜索了很多答案,但找不到。我不知道我做错了什么。你能帮我吗?

这是我的代码:

主类:

package com.tiprojectes.JGames.renderEngine;

import static org.lwjgl.glfw.GLFW.*;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import static org.lwjgl.opengl.GL11.*;

public class Main 
{
    public static final int width = 600;
    public static final int height = 600;
    float x = 0;
    float y = 0;
    float speed = 0.002f;

    public Main()
    {   
        if (!glfwInit())
        {
            System.err.println("Could not initialize GLFW");
        }

        glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);

        long window = glfwCreateWindow(width, height, "No Name Game PRE_ALPHA_0.01", 0, 0);

        glfwMakeContextCurrent(window);

        GL.createCapabilities();

        glEnable(GL_TEXTURE_2D);

        float[] vertexes = new float[] 
        {   
            -0.5f, 0.5f, 0,
            0.5f, 0.5f, 0,
            0.5f, -0.5f, 0,
            -0.5f, -0.5f, 0
        };

        float[] textures = new float[]
        {
                0, 0,
                1, 0,
                1, 1,
                0, 1
        };

        int[] indices = new int[] 
        {
                0, 1, 2,
                2, 3, 0
        };

        Model model = new Model(vertexes, textures, indices);

        Textures texture = new Textures("./res/terrain.png");

        if (window == 0)
        {
            System.err.println("Could not create window");
        }

        GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());

        glfwSetWindowPos(window, (videoMode.width() - width) / 2, (videoMode.height() - height) / 2);

        glfwShowWindow(window);

        while (!glfwWindowShouldClose(window))
        {       
            glfwPollEvents();

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            model.render();

            texture.bind();

            glfwSwapBuffers(window);
        }

        glfwTerminate();
    }

    public static void main(String[] args)
    {
        new Main();
    }
}`

纹理类:

package com.tiprojectes.JGames.renderEngine;

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

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;

import org.lwjgl.BufferUtils;

public class Textures
{
    private int id;
    private int width;
    private int height;

    public Textures(String filename) 
    {
        BufferedImage bi;

        try
        {
            bi = ImageIO.read(new File(filename));
            width = bi.getWidth();
            height = bi.getHeight();

            int[] pixels_raw = new int[width * height];
            pixels_raw = bi.getRGB(0, 0, width, height, null, 0, width);

            ByteBuffer pixels = BufferUtils.createByteBuffer(width * height * 4);

            for(int i = 0; i < width; i++)
                for(int j = 0; j < height; j++)
                {
                    int pixel = pixels_raw[i * width + j];
                    pixels.put((byte) ((pixel >> 16) & 0xFF));  // RED
                    pixels.put((byte) ((pixel >> 8) & 0xFF));   // GREED
                    pixels.put((byte) ((pixel & 0xFF)));        // BLUE
                    pixels.put((byte) ((pixel >> 24) & 0xFF));  // ALPHA
                }

            pixels.flip();

            id = glGenTextures();

            glBindTexture(GL_TEXTURE_2D, id);

            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }

    public void bind()
    {
        glBindTexture(GL_TEXTURE_2D, id);
    }
}

模型类:

package com.tiprojectes.JGames.renderEngine;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;

public class Model {

    private int draw_count;
    private int v_id;
    private int t_id;
    private int i_id;

    public Model(float[] vertexes, float[] tex_coords, int[] indices)
    {
        draw_count = indices.length;

        v_id = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, v_id);
        glBufferData(GL_ARRAY_BUFFER, createBuffer(vertexes), GL_STATIC_DRAW);

        t_id = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, t_id);
        glBufferData(GL_ARRAY_BUFFER, createBuffer(tex_coords), GL_STATIC_DRAW);

        i_id = glGenBuffers();
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i_id);

        IntBuffer buffer = BufferUtils.createIntBuffer(indices.length);
        buffer.put(indices);
        buffer.flip();

        glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

    public void render()
    {
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glBindBuffer(GL_ARRAY_BUFFER, v_id);
        glVertexPointer(3, GL_FLOAT, 0, 0);

        glBindBuffer(GL_ARRAY_BUFFER, t_id);
        glTexCoordPointer(2, GL_FLOAT, 0, 0);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i_id);

        glDrawElements(GL_TRIANGLES, draw_count, GL_UNSIGNED_INT, 0);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    }

    private FloatBuffer createBuffer(float[] data)
    {
        FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
        buffer.put(data);
        buffer.flip();
        return buffer;
    }

}

希望我能得到一些帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

我解决了。四边形未渲染。在模型类中,在glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW),我不得不放置GL_ARRAY_BUFFER而不是GL_ELEMENT_ARRAY_BUFFER。感谢您的帮助!