我剛開始實行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中,但最終結果都很奇怪。這是一個截圖,顯示某些東西肯定是不正確的。 (線框被激活)
感謝您的回答我很幸運,即使沒有明確的設置位置,它有位置0和1 ..現在我得到的位置glGetUniformLocation不幸的是結果是相同的 – Aurus
以及我接受你的答案,在結束我有另一個問題,但在一個非提供的代碼..你幫了很多。再次感謝。 – Aurus