2017-05-13 45 views
-1

對於我的2D時,OpenGL/SDL遊戲我目前正在運行的打印出以下openGL的程序屬性OpenGL的日誌文件:openGL的統一屬性位置無效

GL_LINK_STATUS = SUCCESS 
GL_ATTACHED_SHADERS = 0 
GL_ACTIVE_ATTRIBUTES = 2 
- 0) type:vec2 name:textCoord location 1 
- 1) type:vec3 name:vertexPosition location 0 
GL_ACTIVE_UNIFORMS = 1 
- 0) type:sampler2D name:basicTexture location -1 

正如你所看到的,我的位置和紋理座標屬性是活動和工作,但我的1統一屬性顯示的位置-1(我假設意味着無效的位置)。我試圖讓我的第一個紋理正確加載。在嘗試顯示紋理之前,我可以在屏幕上顯示一個彩色框,所以假設所有其他的opengl代碼都在函數中正確工作。這是爲什麼我設置了我的紋理特性:

的main.cpp

int main(int agrc, char** argv) 
{ 
    window.Initialize(); 
    playerSprite.Init(-1.0f, -1.0f, 1.0f, 1.0f); 

    GameState gamestate{ GameState::PLAY }; 

    SDL_Event evnt; 

    int32 x, y, currentChannels; 
    int32 forceChannels = 4; 
    uchar8* imageData = 0; 
    imageData = stbi_load("CharImage.png", &x, &y, &currentChannels, forceChannels); 

    if (imageData == nullptr) 
    { 
     LOG("ERROR: Could not load image file!"); 
    }; 

    Blz::OpenGL::ShaderProgram colorShaderProgram; 
    colorShaderProgram.Compile(); 
    colorShaderProgram.Link(); 
    colorShaderProgram.Bind(); 

    GLuint texture; 
    glGenTextures(1, &texture); 
    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, texture); 

    GLuint uniformLocation = glGetUniformLocation(colorShaderProgram.programID, "basicTexture"); 
    glUniform1i(uniformLocation, 0); 

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); 
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 

    //Where I am logging to the openGL file 
    Blz::OpenGL::LogShaderProgramProperties(colorShaderProgram.programID); 

    while (gamestate != GameState::EXIT) 
    { 
     unsigned int startTime = SDL_GetTicks(); 

     //Game Logic Update 
     while (SDL_PollEvent(&evnt)) 
     { 
      switch (evnt.type) 
      { 
      case SDL_QUIT: 
       gamestate = GameState::EXIT; 

      default: 
       break; 
      } 
     } 

     window.ClearBuffers(); 

     playerSprite.Draw(); 

     window.SwapBuffers(); 
    } 

    return 0; 
} 

下面是我使用的着色器設置統一的變量:

Veretx着色

#version 430 

layout(location=0) in vec3 vertexPosition; 
layout(location=1) in vec2 textCoord; 

out vec2 TextureCoord; 

void main() 
{ 
    gl_Position = vec4(vertexPosition, 1.0f); 
    TextureCoord = textCoord; 
}; 

片段着色器

#version 430 

out vec4 daColor; 
in vec2 TextureCoord; 

uniform sampler2D basicTexture; 

void main() 
{ 
    vec4 texel = texture(basicTexture, TextureCoord); 
    daColor = texel; 
}; 

從上面的代碼是否有任何問題會導致我的統一變量位置在日誌文件中打印-1?

編輯:

這裏是用來打印日誌信息的代碼:

void LogShaderProgramProperties(GLuint shaderProgramID) 
     { 
      //Header 
      OpenGL::LogToFile("-------------------------------------------------\n"); 
      OpenGL::LogToFile("Shader program %i\n", shaderProgramID); 
      OpenGL::LogToFile("-------------------------------------------------\n\n"); 

      int32 result = -1; 
      glGetProgramiv(shaderProgramID, GL_LINK_STATUS, &result); 
      OpenGL::LogToFile("GL_LINK_STATUS = %s\n", (result == GL_TRUE) ? "SUCCESS" : "FAILURE"); 

      glGetProgramiv(shaderProgramID, GL_ATTACHED_SHADERS, &result); 
      OpenGL::LogToFile("GL_ATTACHED_SHADERS = %i\n", result); 

      glGetProgramiv(shaderProgramID, GL_ACTIVE_ATTRIBUTES, &result); 
      OpenGL::LogToFile("GL_ACTIVE_ATTRIBUTES = %i\n", result); 

      //Will log all current active attributes for program/shader 
      for (GLuint i = 0; i < (GLuint)result; ++i) 
      { 
       char8 name[64]; 
       int32 maxLength = 64; 
       int32 actualLength = 0; 
       int32 size = 0; 
       GLenum type; 

       glGetActiveAttrib(shaderProgramID, i, maxLength, &actualLength, &size, &type, name); 
       if (size > 1) 
       { 
        //Sometimes an attribute will contain an array of other attributes for which this 
        //loop will catch and print all contained variables 
        for (int j = 0; j < size; j++) 
        { 
         char8 longName[64]; 
         sprintf(longName, "%s[%i]", name, j); 
         int32 location = glGetAttribLocation(shaderProgramID, longName); 
         OpenGL::LogToFile(" - %i) type:%s name:%s location: %i\n", i, GLTypeToString(type), name, location); 
        } 
       } 
       else 
       { 
        //Just print single attribute information 
        int32 location = glGetAttribLocation(shaderProgramID, name); 
        OpenGL::LogToFile(" - %i) type:%s name:%s location %i\n", i, GLTypeToString(type), name, location); 
       } 
      } 

      glGetProgramiv(shaderProgramID, GL_ACTIVE_UNIFORMS, &result); 
      LogToFile("GL_ACTIVE_UNIFORMS = %i\n", result); 

      //Will log all current active attributes for program/shader 
      for (GLuint i = 0; i < (GLuint)result; ++i) 
      { 
       char8 name[64]; 
       int32 maxLength = 64; 
       int32 actualLength = 0; 
       int32 size = 0; 
       GLenum type; 

       glGetActiveUniform(shaderProgramID, i, maxLength, &actualLength, &size, &type, name); 
       if (size > 1) 
       { 
        //In case a uniform contains an array of other variables/uniforms 
        for (int j = 0; j < size; j++) 
        { 
         char8 longName[64]; 
         sprintf(longName, "%s[%i]", name, j); 
         int32 location = glGetUniformLocation(shaderProgramID, longName); 
         OpenGL::LogToFile(" - %i) type:%s name:%s location: %i\n", i, GLTypeToString(type), longName, location); 
        } 
       } 
       else 
       { 
        //Just print single uniform variable 
        int32 location = glGetAttribLocation(shaderProgramID, name); 
        OpenGL::LogToFile(" - %i) type:%s name:%s location %i\n", i, GLTypeToString(type), name, location); 
       } 
      } 

      int32 maxLength = 2048; 
      int32 actualLength = 0; 
      char8 log[2048]; 
      glGetProgramInfoLog(shaderProgramID, maxLength, &actualLength, log); 
      OpenGL::LogToFile("Program info log for GL index %u:\n%s", shaderProgramID, log); 
     } 
+0

沒有「統一屬性」這樣的東西。有「屬性」(頂點着色器輸入)和「制服」。一個變量不能是* both *。 –

+0

此外,打印所有這些東西的代碼在哪裏?確定'basicTexture'具有-1的位置的代碼? –

+0

在[mcve]中編輯。 – genpfault

回答

1

您嘗試通過glGetAttribLocation查詢均勻位置,當然還有沒有屬性與該名稱:

  else 
      { 
       //Just print single uniform variable 
       int32 location = glGetAttribLocation(shaderProgramID, name); 
       OpenGL::LogToFile(" - %i) type:%s name:%s location %i\n", i, GLTypeToString(type), name, location); 
      } 
     } 

無論你的紋理有什麼問題,它都不是統一的位置。

+0

非常感謝你。這至少有助於縮小我的問題 – Jason