2013-05-16 32 views
1

我剛開始實行librocket一個要求是RenderInterface(從HTML生成網狀的UI LIB)。該基本上發送你的類繼承RenderInterface生成的網格,並希望你保存網格,然後它希望你呈現這個生成的網格。聽起來不錯,因爲你有機會爲第三方庫實現你自己的渲染系統。但是我的問題可能與lib無關。渲染與glDrawElements有一個怪異的結果

正如我告訴的lib發送網格上我的課:當庫送我網

GLuint m_BufferID[2]; 
//... 
glGenBuffers(2, m_BufferID); 
//.. 
glBindBuffer(GL_ARRAY_BUFFER, m_BufferID[0]); 
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_BufferID[1]); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); 

那基本上發生。實際上它也會加載紋理等,但目前這並不重要。 (注意:數據是一個指針,指向ROCKET_LIB ::頂點數組)

glEnableVertexAttribArray(0); 
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(ROCKET_CORE::Vertex), (GLvoid*)offsetof(Rocket::Core::Vertex, position)); 
glEnableVertexAttribArray(1); 
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ROCKET_CORE::Vertex), (GLvoid*)offsetof(Rocket::Core::Vertex, colour)); 
glEnableVertexAttribArray(2); 
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(ROCKET_CORE::Vertex), (GLvoid*)offsetof(Rocket::Core::Vertex, tex_coord)); 

glUniformMatrix4fv(0, 1, GL_FALSE, &m_TransMatrix[0][0]); 
glUniform2f(1, translation.x, translation.y); 

glActiveTexture(GL_TEXTURE0); 

glBindBuffer(GL_ARRAY_BUFFER, m_BufferID[0]); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_BufferID[1]); 

glDrawElements(GL_TRIANGLES, m_indices_count, GL_UNSIGNED_INT, NULL); 

glDisableVertexAttribArray(0); 
glDisableVertexAttribArray(1); 
glDisableVertexAttribArray(2); 

網格被上傳到GPU我與上部代碼使其後。要完成這個問題,這裏是我的着色器:

頂點着色器:

#version 330 

layout(location = 0) in vec2 inPosition; 
layout(location = 1) in vec4 inColor; 
layout(location = 2) in vec2 inTexCoord; 

out vec4 fragColor; 
out vec2 fragTexCoord; 

layout(location = 0) uniform mat4 m_Ortho; 
layout(location = 1) uniform vec2 m_Translation; 


void main() 
{ 
    // Setting out values 
    fragColor = inColor; 
    fragTexCoord = inTexCoord; 

    // Settings transformed position 
    gl_Position = vec4(inPosition + m_Translation, 1.0f, 1.0f) * m_Ortho; 
} 

片段着色器:

#version 330 

in vec4 fragColor; 
in vec2 fragTexCoord; 

out vec4 outColor; 

layout (location = 2) uniform sampler2D m_TextureSampler; 

void main() 
{ 
    // Just for testing purpose show the text coord as color 
    outColor = vec4(fragTexCoord, 0.0f, 1.0f); 
} 

哦,我只是生成矩陣:

glm::ortho(0.0f, static_cast<float>(m_Width), static_cast<float>(m_Height), 0.0f, -1.0f, 1.0f); 

從DirectX我知道你必須轉置矩陣,我也嘗試過在opengl中,但最終結果都很奇怪。這是一個截圖,顯示某些東西肯定是不正確的。 (線框被激活) enter image description here

回答

1

我不能說這肯定是你的實際問題,但它一個問題:

#version 330 

layout(location = 0) uniform mat4 m_Ortho; 
layout(location = 1) uniform vec2 m_Translation; 

這兩件事情不走在一起。 explicit assignment of uniform locations是OpenGL 4.3的一個特性。而GLSL 3.30版對應於OpenGL 3.3。

現在,可能3.3的驅動程序將公開該功能。但它會作爲擴展被暴露。所以,你需要activate it like an extension

#version 330 
#extension ARB_explicit_uniform_location : require 

顯然,這種着色器將僅當此擴展可用編譯。

+0

感謝您的回答我很幸運,即使沒有明確的設置位置,它有位置0和1 ..現在我得到的位置glGetUniformLocation不幸的是結果是相同的 – Aurus

+0

以及我接受你的答案,在結束我有另一個問題,但在一個非提供的代碼..你幫了很多。再次感謝。 – Aurus