首页 > 解决方案 > 使用 org.lwjgl.stb 加载纹理

问题描述

我正在尝试加载一个简单的纹理并将其应用于矩形,但它似乎不起作用。这是 org.lwjgl.stb/文件加载的错误吗(我对 opengl 不是很有经验):

这是我的 main.java 中的代码:

float vertices[] = {
                // positions          // colors           // texture coords
                 0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // top right
                 0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // bottom right
                -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // bottom left
                -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // top left 
            };
            int indices[] = {  
                0, 1, 3, // first triangle
                1, 2, 3  // second triangle
            };
            int VBO = glGenBuffers(), VAO = glGenVertexArrays(), EBO = glGenBuffers();

            glBindVertexArray(VAO);

            glBindBuffer(GL_ARRAY_BUFFER, VBO);
            glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);

            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW);

            // position attribute
            glVertexAttribPointer(0, 3, GL_FLOAT, false, 8*4, 0);
            glEnableVertexAttribArray(0);
            // color attribute
            glVertexAttribPointer(1, 3, GL_FLOAT, false, 8*4, 2*4);
            glEnableVertexAttribArray(1);
            // texture coord attribute
            glVertexAttribPointer(2, 2, GL_FLOAT, false, 8*4, 6*4);
            glEnableVertexAttribArray(2);
            
            
            int texture = glGenTextures();
            glBindTexture(GL_TEXTURE_2D, texture);
            
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            
            
            
            int[] width = new int[1], height = new int[1], nrChannels = new int[1];
            ByteBuffer data = stbi_load("C:/Users/Lion/Images/biene.png", width, height, nrChannels, 0);
            if(data.hasRemaining()) System.out.println(data);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width[0], height[0], 0, GL_RGB, GL_UNSIGNED_BYTE, data);
            glGenerateMipmap(GL_TEXTURE_2D);
        while (!glfwWindowShouldClose(window))
            {
            
                

                glClear(GL_COLOR_BUFFER_BIT);
                
                if(mainProgram != null) {
                    mainProgram.use();
                }
                glBindVertexArray(VAO); 
                glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

这是我的顶点着色器的代码:

#version 330 core
layout (location = 0) in vec3 aPos;
layout(location = 1) in vec3 aColor;
layout(location = 2) in vec2 aTexCoord;

out vec3 vertexColor;
out vec2 texCoord;

void main()
{
    gl_Position = vec4(aPos, 1.0);
    vertexColor = aColor;
    texCoord = aTexCoord;
}

这是我的片段着色器:

#version 330 core
   in vec3 vertexColor;
   in vec2 texCoord;

   out vec4 FragColor;

  uniform sampler2D aTexture;

   void main()
    {
       FragColor = texture(aTexture, texCoord);
    };

标签: javaopengltextureslwjgl

解决方案


glTexImage2D在 using 中使用之前尝试翻转像素缓冲区data.flip()

此外,在加载纹理时,使用 3 个通道加载它,因为您在glTexImage2D. 通过更改stbi_load(..., 0)为 来做到这一点stbi_load(..., 3)


推荐阅读