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:
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){};
};
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){};
};
To copy to clipboard, switch view to plain text mode
Triangle constructor and its data:
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
Shaders preparation:
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
Not working drawing function: (with shaders)
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);
}
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);
}
To copy to clipboard, switch view to plain text mode
Working drawing function (without shaders):
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);
}
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);
}
To copy to clipboard, switch view to plain text mode
Shaders:
Vertex shader:
#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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
Fragment shader:
#version 410 compatibility
in vec3 pass_Color;
out vec4 out_Color;
void main(void)
{
out_Color = vec4(pass_Color, 1.0);
}
#version 410 compatibility
in vec3 pass_Color;
out vec4 out_Color;
void main(void)
{
out_Color = vec4(pass_Color, 1.0);
}
To copy to clipboard, switch view to plain text mode
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: QGLTriangle02VOBSL.zip
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.
Bookmarks