1 Attachment(s)
OpengGL with Shaders. I'm stuck. Triangle with Buffer + not working shader.
Hi.
The rendering with shaders doesnt work (nothing on screen). But without it everthing is OK. I use simple custom vertex structure, buffers.
Vertex structure:
Code:
struct MyVertex
{
GLfloat x,y,z;
GLfloat r,g,b;
MyVertex() : x(0), y(0), z(0), r(0), g(0), b(0){};
MyVertex(GLfloat x, GLfloat y, GLfloat z, GLfloat r, GLfloat g, GLfloat b) : x(x), y(y), z(z), r(r), g(g), b(b){};
};
Triangle constructor and its data:
Code:
MyTriangle::MyTriangle() : mVertexVBO(QGLBuffer::VertexBuffer), mIndexVBO(QGLBuffer::IndexBuffer), mVertices(3), mIndices(3)
{
mVertices[0]=MyVertex( 0.0f, 1.0f,-2.0f, 1.0f,0.0f,0.0f);
mVertices[1]=MyVertex(-1.0f,-1.0f,-2.0f, 0.0f,1.0f,0.0f);
mVertices[2]=MyVertex( 1.0f,-1.0f,-2.0f, 0.0f,0.0f,1.0f);
mIndices[0]=0;
mIndices[1]=1;
mIndices[2]=2;
}
Shaders preparation:
Code:
bool MyQGLWidget::prepareShaders()
{
QGLShader shaderVert(QGLShader::Vertex);
QGLShader shaderFrag(QGLShader::Fragment);
bool isCompiledVert
= shaderVert.
compileSourceFile(QString("../QGLTriangle02VOBSL/shader_vert.glsl"));
bool isCompiledFrag
= shaderFrag.
compileSourceFile(QString("../QGLTriangle02VOBSL/shader_frag.glsl"));
if(isCompiledVert && isCompiledFrag)
{
bool isAddVert = mShaderProgram.addShader(&shaderVert);
bool isAddFrag = mShaderProgram.addShader(&shaderFrag);
if(isAddVert && isAddFrag)
{
bool isLink = mShaderProgram.link();
if(isLink)
{
return mShaderProgram.bind();
}
}
}
return false;
}
Not working drawing function: (with shaders)
Code:
void MyTriangle::drawBuffers(QGLShaderProgram& program)
{
int vertexLocation = program.attributeLocation("in_Position");
int colorLocation = program.attributeLocation("in_Color");
program.enableAttributeArray(vertexLocation);
program.enableAttributeArray(colorLocation);
program.setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, sizeof(MyVertex));
program.setAttributeBuffer(colorLocation, GL_FLOAT, 12, 3, sizeof(MyVertex));
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
program.disableAttributeArray(vertexLocation);
program.disableAttributeArray(colorLocation);
}
Working drawing function (without shaders):
Code:
void MyTriangle::drawBuffers()
{
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(MyVertex), BUFFER_OFFSET(0));
glColorPointer(3, GL_FLOAT, sizeof(MyVertex), BUFFER_OFFSET(12));
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
Shaders:
Vertex shader:
Code:
#version 410 compatibility
in vec3 in_Position;
in vec3 in_Color;
out vec3 pass_Color;
void main(void)
{
gl_Position = vec4(in_Position, 1.0);
pass_Color = in_Color;
}
Fragment shader:
Code:
#version 410 compatibility
in vec3 pass_Color;
out vec4 out_Color;
void main(void)
{
out_Color = vec4(pass_Color, 1.0);
}
I have:
GL_VERSION: 4.1.0
GL_SHADING_LANGUAGE_VERSION: 4.10 NVIDIA via Cg compiler
What is wrong?
I attached fully function project made in QtCreator: Attachment 5643
I think that I will do the same in pure OpenGL without Qt. To look for mistakes. But if you can provide some answers or clues. That will be awesome.
Re: OpengGL with Shaders. I'm stuck. Triangle with Buffer + not working shader.
Solved. With code is everything fine. I should provide projection, view and model matrix to GLSL vertex shaders (gl_Position = in_Matrix * vec4(in_Position, 1.0);), Because that stuf was deprecated (glOrtho, glProjection, glTranslate, etc...).