2017-06-29 61 views
-1

我正在嘗試將紋理映射到二維到三角形。但是目前,我只能得到一個沒有任何紋理的漸變色的三角形。這意味着,glsl中的紋理函數總是返回vec4(1,1,1,1),我的textCoords正在工作。我應該如何解決它?任何建議將有助於嘗試!OpenGL GLSL紋理函數總是返回vec4(1,1,1,1),白色三角形

順便說一句,已經在這工作了3天。

構造:

Texture::Texture(const std::string& fileName){ 

    int width, height, numComponents; 
    //float* imageData = stbi_loadf(fileName.c_str(), &width, &height, &numComponents, 4); 
    unsigned char* imageData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4); 
    if(imageData == NULL){ 
     cerr << "Texture loading failed for "<<fileName<< endl; 
    } 

    glGenTextures(1, &m_texture); 
    glBindTexture(GL_TEXTURE_2D, m_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); 

    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); 


    stbi_image_free(imageData); 
} 

紋理結合功能:

void Texture::Bind(){ 
    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, m_texture); 
} 

以我的main.cpp:

m_shader.generateProgramObject(); 
m_shader.attachVertexShader(getAssetFilePath("VertexShader.vs").c_str()); 
m_shader.attachFragmentShader(getAssetFilePath("FragmentShader.fs").c_str() ); 
m_shader.link(); 

// texture created here 
texture = Texture("Assets/bricks.jpg"); 


// enable vertex attribute indices 
glGenVertexArrays(1, &m_vao_triangle); 
glBindVertexArray(m_vao_triangle); 

// Enable the attribute index location for "position" when rendering. 
GLint positionAttribLocation = m_shader.getAttribLocation("position"); 
glEnableVertexAttribArray(positionAttribLocation); 

GLint textCoordLocation = m_shader.getAttribLocation("atextCoord"); 
glEnableVertexAttribArray(textCoordLocation); 


// Restore defaults 
glBindVertexArray(0); 


CHECK_GL_ERRORS; 

uploade三角形數據緩衝

紋理類

vec3 triangleVertices[] = { 
    // Construct equalaterial triangle 
    vec3(0.0f, 0.0f, 0.0f), 
    vec3(0.25f, 1.0f, 0.0), 
    vec3(0.5f, 0.0f, 0.0f) 
}; 

vec2 textCoords[] = { 
     vec2(1.0f, 0.0f), 
     vec2(0.25f, 1.0f), 
     vec2(0.5f, 0.0f)}; 



// Generate a vertex buffer object to hold the triangle's vertex data. 
glGenBuffers(1, &m_vbo_triangle); 

//-- Upload triangle vertex data to the vertex buffer: 
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_triangle); 
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, 
     GL_STATIC_DRAW); 

glBindBuffer(GL_ARRAY_BUFFER, 0); 
CHECK_GL_ERRORS; 




//====generate buffer for holding texture coordinates==== 
glGenBuffers(1, &m_uv_triangle); 
glBindBuffer(GL_ARRAY_BUFFER, m_uv_triangle); 
glBufferData(GL_ARRAY_BUFFER, sizeof(textCoords), textCoords, 
      GL_STATIC_DRAW); 

// Unbind the target GL_ARRAY_BUFFER, now that we are finished using it. 
glBindBuffer(GL_ARRAY_BUFFER, 0); 

CHECK_GL_ERRORS; 

圖緩存數據着色器

glBindVertexArray(m_vao_triangle); 

glBindBuffer(GL_ARRAY_BUFFER, m_vbo_triangle); 
GLint positionAttribLocation = m_shader.getAttribLocation("position"); 
glVertexAttribPointer(positionAttribLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr); 



glBindBuffer(GL_ARRAY_BUFFER, m_uv_triangle); 
GLint textCoordLocation = m_shader.getAttribLocation("atextCoord"); 
glVertexAttribPointer(textCoordLocation,2, GL_FLOAT, GL_FALSE, 0, nullptr); 


//-- Unbind target, and restore default values: 
glBindBuffer(GL_ARRAY_BUFFER, 0); 
glBindVertexArray(0); 

CHECK_GL_ERRORS; 

上傳統一着色器

m_shader.enable(); 

    ... 

    GLint uniformLocation_diffuse = m_shader.getUniformLocation("diffuse"); 
    glUniform1i(uniformLocation_diffuse, 0); 
    CHECK_GL_ERRORS; 

    m_shader.disable(); 

    CHECK_GL_ERRORS; 

在我的繪製函數:

glBindVertexArray(m_vao_triangle); 
// below I tried, but didn't work 
// glClear(GL_STENCIL_BUFFER_BIT); 
// glEnable(GL_BLEND); 
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
// glEnable(GL_DEPTH_TEST); 

texture.Bind(); 
// do these below: 
// glActiveTexture(GL_TEXTURE0); 
// glBindTexture(GL_TEXTURE_2D, texture.m_texture); 

m_shader.enable(); 

glDrawArrays(GL_TRIANGLES, 0, 3); 


m_shader.disable(); 


// Restore defaults 
glBindVertexArray(0); 

CHECK_GL_ERRORS; 

在這裏,我也附上我的着色器 verte X着色器:

#version 330 

in vec3 position; 
in vec2 atextCoord; 

uniform mat4 transform; 
out vec2 textCoord; 

void main() { 
    gl_Position = transform * vec4(position, 1.0); 
    textCoord = atextCoord; 
} 

我的片段着色器:

#version 330 

uniform sampler2D diffuse; 

out vec4 fragColor; 
in vec2 textCoord; 

void main() { 

    fragColor = vec4(texture(diffuse, textCoord).rgb,1.0) * vec4(textCoord,0.0,1.0); 
    // texture(...) shows vec4(1,1,1,1) 
    // radiant colour can only prove my textCoord is working 
    // fragColor = texture(diffuse, textCoord); <- only shows a default texture 
} 

here is the running result

Here is the texture image

+0

膠所有的拼湊和編輯的產生[MCVE。 – genpfault

+0

您是否嘗試將內部紋理格式從「GL_RGBA32F_ARB」更改爲「GL_RGBA8」? – Rabbid76

+0

嗨,剛試過,但沒有工作。 @ Rabbid76 – Lalaphoon

回答

0

我找到了一個方法,使其工作。

我複製Texture構造函數中的每一行,並將其粘貼到draw(),而不是調用texture.Bind()。 看起來就像在我準備繪製幾何圖形之前製作一個紋理,並且這種方式正在工作。

但我仍然必須知道爲什麼會發生這種情況。對於編碼風格,我仍然需要將我的代碼放在Texture類中。你介意提供一個解決之前發生的事情嗎?

it looks like this right now

+0

等一下。這裏發生了一些魔術。我很奇怪。這裏的另一個解決方案:我將我的構造函數更改爲一個名爲setUp(fileName)的函數。現在,我的構造函數是空的。我沒有在上面的代碼中初始化一個實例,而是調用了texture.setUp(「bricks.jpg」),並且紋理仍然存在,我甚至不需要在我的draw中調用texture.Bind()。 – Lalaphoon