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.

#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);

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 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…

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 "
" in all lines of shader code, like this

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

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.

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.