簡體   English   中英

紋理未加載 opengl 拱門 linux C++

[英]Texture not loading opengl Arch linux C++

我試圖在 opengl 2.1 上使用 linux 中的 glsl 120 顯示紋理。但它顯示錯誤。我的 main.cpp

#include "GL/glew.h"
#include "GL/glu.h"
#include "GLFW/glfw3.h"
#include "stb_image/stb_image.h"
#include <iostream>

unsigned char* texture;
unsigned int render_ID;

const char* vertexcode = {
    "#version 120\n"
    "\n"
    "attribute vec3 coord;\n"
    "attribute vec2 texCoord;\n"
    "\n"
    "varying vec2 UV;\n"
    "\n"
    "void main(){\n"    
        "gl_Position = vec4(coord.x, coord.y, coord.z, 1.0);\n"
        "UV = texCoord;\n"
    "}\n"
};

const char* fragmentcode = {
    "#version 120\n"
    "uniform sampler2D tex;\n"
    "varying vec2 UV;\n"
    "void main(){\n"
        "gl_FragColor.rgb = texture2D(tex, UV).rgb;\n"
        "gl_FragColor.a = 1.0;\n"
    "}"
};

float quadCoords[] = {
        -0.5f, -0.5f,
        0.5f, -0.5f, 
        0.5f, 0.5f, 
        0.5f, 0.5f, 
        -0.5f, 0.5f, 
        -0.5f, -0.5f,};

const float texCoords[] = {
        0.0f, 0.0f, 
        1.0f, 0.0f, 
        1.0f, 1.0f, 
        1.0f, 1.0f, 
        0.0f, 1.0f,
        0.0f, 0.0f};

unsigned int Texture()
{
    texture = stbi_load("cubes.png",0,0,0,4);
    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1,&render_ID);
    glBindTexture(GL_TEXTURE_2D,render_ID);
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0,0, 0, GL_RGBA, GL_UNSIGNED_BYTE,texture);

}

int main()
{
    glfwInit();
    GLFWwindow* window = glfwCreateWindow(900,600,"ada",NULL,NULL);
    if (window == NULL)
    {
        std::cout << "no glfw" << std::endl;
        glfwTerminate();
    }
    glfwMakeContextCurrent(window);

    unsigned int buffer;
    unsigned int texture_buffer;
    unsigned int shader = glCreateProgram();

    unsigned int vs = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vs,1,&vertexcode,nullptr);
    glCompileShader(vs);

    unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fs,1,&fragmentcode,nullptr);
    glCompileShader(fs);

    glAttachShader(shader,vs);
    glAttachShader(shader,fs);
    glLinkProgram(shader);

    glGenBuffers(1,&buffer);
    glBindBuffer(GL_ARRAY_BUFFER,buffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(quadCoords) * sizeof(float),quadCoords,GL_STATIC_DRAW);

    glGenBuffers(1,&texture_buffer);
    glBindBuffer(GL_ARRAY_BUFFER,texture_buffer);
    glBufferData(GL_ARRAY_BUFFER,sizeof(texCoords) * sizeof(float),texCoords,GL_STATIC_DRAW);
    
    glUseProgram(shader);
    Texture();
    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glfwPollEvents();

        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER,buffer);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3* sizeof(GLfloat), nullptr);

        glEnableVertexAttribArray(1);
        glBindBuffer(GL_ARRAY_BUFFER,texture_buffer);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,2 * sizeof(GLfloat), nullptr);

        glDrawArrays(GL_TRIANGLES,0,6);

        glfwSwapBuffers(window);
    }
    glfwTerminate();
    return 0;
}

stb_image.h header 只是一個讓我們從圖片中讀取數據的庫: 在此處輸入圖像描述

但不是 window 打開然后立即關閉:

 fish: Job 1, './raw' terminated by signal SIGSEGV (Address boundary error) 

我也在使用魚 shell。我的編譯器設置:

g++  main.cpp stb_image/stb_image.cpp  -o raw -lGL -lX11 -lglfw -lGLU -lGLEW

stbi_load在output個參數中返回紋理的大小(正因如此,這arguments個是指針)。 您需要獲取大小並將其與glTexImage2D一起使用:

int cx, cy, ch;
texture = stbi_load("cubes.png", &cx, &cy, &ch, 4);

// [...]

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cx, cy, 0, GL_RGBA, GL_UNSIGNED_BYTE,texture);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM