2016-09-24 92 views
1

我試圖在OpenGL中加載一個紋理,但我得到的只是黑色。我已驗證着色器在編譯時正在返回GL_TRUE。所以,我不認爲這是問題...爲什麼我無法在OpenGL中顯示紋理?

需要注意以下幾點:

我已經採取和https://open.gl/content/code/c3_multitexture.txt修改後的代碼。這是一個教程從https://open.gl/textures

我試圖加載的紋理是從一個640 x 400 PGM文件。您可以假設加載器pgmImage正確加載文件(我已經在GDB中對其進行了驗證)。 PGM是8位B & W數據。在這裏,我試圖將其用作紅色通道,儘管我希望能夠最終將其顯示爲B & W。

我哪裏錯了?

謝謝!代碼編譯時使用:-lglut -lGLU -lGL -lm -lGLEW在g ++中的標誌(有些不是必須的,我知道)。

#include <stdio.h> 
#include <stdlib.h> 
#include <stdarg.h> 
#include <math.h> 

// Include GLEW 
#include <GL/glew.h> 

//Glut 
#include <GL/glut.h> 

#include "shader.hpp" 
#include "PGM.hpp" 

GLuint programID; 
GLuint output_image; 

// Shader sources 
const GLchar* vertexSource = 
    "#version 450 core\n" 
    "in vec2 position;" 
    "in vec2 texcoord;" 
    "out vec2 Texcoord;" 
    "void main()" 
    "{" 
    " Texcoord = texcoord;" 
    " gl_Position = vec4(position, 0.0, 1.0);" 
    "}"; 
const GLchar* fragmentSource = 
    "#version 450 core\n" 
    "in vec2 Texcoord;" 
    "out vec4 outColor;" 
    "uniform sampler2D texData;" 
    "void main()" 
    "{" 
    " outColor = texture(texData, Texcoord);" 
    "}"; 



void display() 
{ 

    glClearColor(0.0f, 0.0f, 1.0f, 0.0f); 

    glClear(GL_COLOR_BUFFER_BIT); 

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 

    glFlush(); 
    glutSwapBuffers(); 

}  


void reshape(int width,int height) 
{ 
    double w2h = (height>0) ? (double)width/height : 1; 
    // Set viewport as entire window 
    glViewport(0,0, width,height); 

} 



int main(int argc, char** argv) 
{ 
    // Image setup 

    PGM pgmImage; 
    pgmImage.ReadFile("test.pgm"); 

    // Window Setup 

    glutInitWindowSize(640, 400); 
    glutInitWindowPosition (140, 140); 
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 
    glutInit(&argc, argv); 

    glutCreateWindow("OpenGL Application"); 
    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 

    glewExperimental = true; // Needed for core profile 
    if (glewInit() != GLEW_OK) { 
     fprintf(stderr, "Failed to initialize GLEW\n"); 
     return -1; 
    } 

    // Vertices & texture init 

    GLuint vao; 
    glGenVertexArrays(1, &vao); 
    glBindVertexArray(vao); 

    GLuint vbo; 
    glGenBuffers(1, &vbo); 

    GLfloat vertices[] = { 
     // X Y  S T 
     -0.5f, 0.5f, 0.0f, 0.0f, // Top-left 
     0.5f, 0.5f, 1.0f, 0.0f, // Top-right 
     0.5f, -0.5f, 1.0f, 1.0f, // Bottom-right 
     -0.5f, -0.5f, 0.0f, 1.0f // Bottom-left 
    }; 

    glBindBuffer(GL_ARRAY_BUFFER, vbo); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 

    GLuint ebo; 
    glGenBuffers(1, &ebo); 

    GLuint elements[] = { 
     0, 1, 2, 
     2, 3, 0 
    }; 

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); 

    // Create shaders 

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); 
    glShaderSource(vertexShader, 1, &vertexSource, NULL); 
    glCompileShader(vertexShader); 


    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); 
    glShaderSource(fragmentShader, 1, &fragmentSource, NULL); 
    glCompileShader(fragmentShader); 

    GLuint shaderProgram = glCreateProgram(); 
    glAttachShader(shaderProgram, vertexShader); 
    glAttachShader(shaderProgram, fragmentShader); 
    glBindFragDataLocation(shaderProgram, 0, "outColor"); 
    glLinkProgram(shaderProgram); 
    glUseProgram(shaderProgram); 

    // Vertex data specification 
    GLint posAttrib = glGetAttribLocation(shaderProgram, "position"); 
    glEnableVertexAttribArray(posAttrib); 
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0); 

    GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord"); 
    glEnableVertexAttribArray(texAttrib); 
    glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void *)(2 * sizeof(GLfloat))); 

    // Load Textures 

    //TODO: don't really need 2 textures, but just following along with example source code for now... 
    GLuint textures[2]; 
    unsigned char* image; 

    int width, height; 

    width = pgmImage.GetWidth(); 
    height = pgmImage.GetHeight(); 
    image = (unsigned char *)pgmImage.GetData(); 

    glGenTextures(2, textures); 

    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, textures[0]); 

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, image); 


    glUniform1i(glGetUniformLocation(shaderProgram, "texData"), 0); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 

    // Start program 

    glutMainLoop(); 

    // Teardown 

    glDeleteTextures(2, textures); 

    glDeleteProgram(shaderProgram); 
    glDeleteShader(fragmentShader); 
    glDeleteShader(vertexShader); 

    glDeleteBuffers(1, &ebo); 
    glDeleteBuffers(1, &vbo); 

    glDeleteVertexArrays(1, &vao); 

    return 0; 
} 
+0

在某些時候,紋理尺寸必須是2的冪;不確定這在今天的某些情況下是否仍然相關。你有沒有嘗試在glTexImage2D之後檢查glGetError?您是否嘗試使用常量顏色片段着色器來檢查屏幕上的座標? –

+0

感謝您的回覆!座標在屏幕上。我的四邊形是黑色方框,背景是藍色。你對2的權力是正確的,但我讀到這在OpenGL 4.5中並不重要(可能是錯誤的)。我會看看glError說什麼,然後回覆。 – Maxthecat

+0

glTexImage2D調用中存在無效操作。需要匹配的格式和內部格式參數。我將格式設置爲GL_R8UI,該格式可以消除該錯誤,但我仍然得到一個黑色四邊形(沒有紋理)。 – Maxthecat

回答

4

正如你已經找到了,internalFormat格式glTexImage2D()調用的是不相容的:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, image); 

GL_RED_INTEGER是整數的紋理類型。對於規範化的紋理單組分紋理數據,正確的格式是GL_RED

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, image); 

對於新的代碼,我總是建議使用容量內部類型,即使未施膠內部類型,如GL_RGB仍然是有效的向後兼容性。因此,對於標準化的紋理3 8位分量:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, image); 

您在使用GL_R8UI作爲內部格式的嘗試失敗,因爲使用整數紋理需要額外的變化,特別是在着色器代碼,你將需要使用usampler2D作爲採樣器類型,並在採樣紋理時處理獲取整數值。但就你而言,首先這不是你想要的。