Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Bug in code.

  1. #1
    Newbie Newbie
    Join Date
    Aug 2014
    Posts
    2

    Question Bug in code.

    Hi,
    I am trying to learn OpenGL from "OpenGL SuperBible Sixth Edition". I was able to compile sb6source and example from book. But I get a bug. If I try to run program sometimes it draw point and sometimes do not draw. Point are drawn about once in 10 tries. And I can't figure out why it happens. Can someone find a bug or explain why it happens? Here are example source.

    Code :
    #include "sb6.h"
     
    GLuint compile_shaders();
     
    class App : public sb6::application
    {
      void render(double currentTime)
      {
        const GLfloat color[] = { 0.8, 0.8, 0.0f, 1.0f };
        glClearBufferfv(GL_COLOR, 0, color);
     
        glUseProgram(m_program);
        glPointSize(40.0f);
        glDrawArrays(GL_POINTS, 0, 1);
      }
     
      void startup()
      {
        m_program = compile_shaders();
        glGenVertexArrays(1, &m_vao);
        glBindVertexArray(m_vao);
      }
     
      void shutdown()
      {
        glDeleteVertexArrays(1, &m_vao);
        glDeleteProgram(m_program);
      }
     
      private:
        GLuint m_program;
        GLuint m_vao;
    };
     
    GLuint compile_shaders()
    {
      GLuint vs;
      GLuint fs;
      GLuint program;
     
      static const GLchar * vs_source[] =
      {
        "#version 430 core"
        "void main(void)"
        "{"
        "gl_Position = vec4(0.0, 0.0, 0.0, 1.0);"
        "}"
     
      };
     
      static const GLchar * fs_source[] =
      {
        "#version 430 core"
        "out vec4 color;"
        "void main(void)"
        "{"
        "color = vec4(0.0, 0.8, 1.0, 1.0);"
        "}"
      };
     
      // Create and compile vertex shader
      vs = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vs, 1, vs_source, NULL);
      glCompileShader(vs);
     
      // Create and compile fragment shader
      fs = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(fs, 1, fs_source, NULL);
      glCompileShader(fs);
     
      // Create program, attach shaders to it and link it
      program = glCreateProgram();
      glAttachShader(program, vs);
      glAttachShader(program, fs);
      glLinkProgram(program);
     
      // Delete the shaders as the program has them now
      glDeleteShader(vs);
      glDeleteShader(fs);
     
      return program;
     
    }
     
    DECLARE_MAIN(App);

  2. #2
    Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    356
    You are drawing one primitive, but without any buffer attached, any without any data.
    Cause the vertex shader will transform all the input into gl_position 0, 0, 0, 1 sometime you are lucky and you get some byte of memory that is valid.
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

  3. #3
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    867
    I think the program really wants to just draw a point at the origin of clip space and since the vertex shader consumes no inputs it should also not read random memory. Hard to say for me what goes wrong. Perhaps the application requests a depth buffer, but it is never cleared and the single point sometimes fails the depth test? Might also be a driver bug, given that rendering without any vertex attributes is perhaps not a 'traditional' use case...

  4. #4
    Newbie Newbie
    Join Date
    Aug 2014
    Posts
    2
    Thanks for helping.
    Buffers will be later in the book and this code works without it. I had found a bug in the code. It was error in shader. You need "\n" in all lines of shader code, like this

    Code :
      static const char * vs_source[] =
      {
        "#version 430 core \n"
        "void main(void) \n"
        "{ \n"
        "gl_Position = vec4(0.0, 0.0, 0.0, 1.0); \n"
        "} \n"
     
      };
     
      static const char * fs_source[] =
      {
        "#version 430 core \n"
        "out vec4 color; \n"
        "void main(void) \n"
        "{ \n"
        "color = vec4(0.0, 0.8, 1.0, 1.0); \n"
        "} \n"
      };

    I guess shader compiler seen my shader as one long line until ";" "#version 430 core void main(void) { gl_Position = vec4(0.0, 0.0, 0.0, 1.0);" And could not compile. But I do not understand why sometimes a point appeared running program. And point color was black, but fs shader color is not black.

  5. #5
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    457
    Unfortunately #version (and other preprocessor directives) are terminated by line endings, so you need to include it for those lines. Most (or all?) other statements are okay to include on the same line because they are terminated by semi-colons/keywords.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •