Results 1 to 2 of 2

Thread: OpengGL with Shaders. I'm stuck. Triangle with Buffer + not working shader.

  1. #1
    Join Date
    Dec 2010
    Location
    Poland. Jelenia Góra.
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. struct MyVertex
    2. {
    3. GLfloat x,y,z;
    4. GLfloat r,g,b;
    5. MyVertex() : x(0), y(0), z(0), r(0), g(0), b(0){};
    6. 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){};
    7. };
    To copy to clipboard, switch view to plain text mode 
    Triangle constructor and its data:
    Qt Code:
    1. MyTriangle::MyTriangle() : mVertexVBO(QGLBuffer::VertexBuffer), mIndexVBO(QGLBuffer::IndexBuffer), mVertices(3), mIndices(3)
    2. {
    3. mVertices[0]=MyVertex( 0.0f, 1.0f,-2.0f, 1.0f,0.0f,0.0f);
    4. mVertices[1]=MyVertex(-1.0f,-1.0f,-2.0f, 0.0f,1.0f,0.0f);
    5. mVertices[2]=MyVertex( 1.0f,-1.0f,-2.0f, 0.0f,0.0f,1.0f);
    6.  
    7. mIndices[0]=0;
    8. mIndices[1]=1;
    9. mIndices[2]=2;
    10. }
    To copy to clipboard, switch view to plain text mode 
    Shaders preparation:
    Qt Code:
    1. bool MyQGLWidget::prepareShaders()
    2. {
    3. QGLShader shaderVert(QGLShader::Vertex);
    4. QGLShader shaderFrag(QGLShader::Fragment);
    5.  
    6. bool isCompiledVert = shaderVert.compileSourceFile(QString("../QGLTriangle02VOBSL/shader_vert.glsl"));
    7. bool isCompiledFrag = shaderFrag.compileSourceFile(QString("../QGLTriangle02VOBSL/shader_frag.glsl"));
    8.  
    9. if(isCompiledVert && isCompiledFrag)
    10. {
    11. bool isAddVert = mShaderProgram.addShader(&shaderVert);
    12. bool isAddFrag = mShaderProgram.addShader(&shaderFrag);
    13.  
    14. if(isAddVert && isAddFrag)
    15. {
    16. bool isLink = mShaderProgram.link();
    17. if(isLink)
    18. {
    19. return mShaderProgram.bind();
    20. }
    21. }
    22. }
    23. return false;
    24. }
    To copy to clipboard, switch view to plain text mode 
    Not working drawing function: (with shaders)
    Qt Code:
    1. void MyTriangle::drawBuffers(QGLShaderProgram& program)
    2. {
    3. int vertexLocation = program.attributeLocation("in_Position");
    4. int colorLocation = program.attributeLocation("in_Color");
    5.  
    6. program.enableAttributeArray(vertexLocation);
    7. program.enableAttributeArray(colorLocation);
    8. program.setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, sizeof(MyVertex));
    9. program.setAttributeBuffer(colorLocation, GL_FLOAT, 12, 3, sizeof(MyVertex));
    10.  
    11. glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
    12.  
    13. program.disableAttributeArray(vertexLocation);
    14. program.disableAttributeArray(colorLocation);
    15. }
    To copy to clipboard, switch view to plain text mode 
    Working drawing function (without shaders):
    Qt Code:
    1. void MyTriangle::drawBuffers()
    2. {
    3. glEnableClientState(GL_COLOR_ARRAY);
    4. glEnableClientState(GL_VERTEX_ARRAY);
    5.  
    6. glVertexPointer(3, GL_FLOAT, sizeof(MyVertex), BUFFER_OFFSET(0));
    7. glColorPointer(3, GL_FLOAT, sizeof(MyVertex), BUFFER_OFFSET(12));
    8.  
    9. glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
    10.  
    11. glDisableClientState(GL_VERTEX_ARRAY);
    12. glDisableClientState(GL_COLOR_ARRAY);
    13. }
    To copy to clipboard, switch view to plain text mode 
    Shaders:
    Vertex shader:
    Qt Code:
    1. #version 410 compatibility
    2. in vec3 in_Position;
    3. in vec3 in_Color;
    4. out vec3 pass_Color;
    5. void main(void)
    6. {
    7. gl_Position = vec4(in_Position, 1.0);
    8. pass_Color = in_Color;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Fragment shader:
    Qt Code:
    1. #version 410 compatibility
    2. in vec3 pass_Color;
    3. out vec4 out_Color;
    4. void main(void)
    5. {
    6. out_Color = vec4(pass_Color, 1.0);
    7. }
    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.
    Last edited by bartosz.kwasniewski; 23rd December 2010 at 19:48.

  2. #2
    Join Date
    Dec 2010
    Location
    Poland. Jelenia Góra.
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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...).

Similar Threads

  1. Geometry shader with QT4.7
    By saraksh in forum General Programming
    Replies: 3
    Last Post: 28th March 2011, 16:40
  2. Fragment shader with 2d
    By krzat in forum Newbie
    Replies: 1
    Last Post: 20th December 2010, 21:37
  3. Replies: 0
    Last Post: 10th November 2010, 13:56
  4. Node Shader Editor
    By AMDx64BT in forum Newbie
    Replies: 1
    Last Post: 6th December 2009, 22:30
  5. How to draw fast rectangle/triangle/circle
    By nileshsince1980 in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2008, 11:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.