PDA

View Full Version : Bug in code.



azaravicius
08-10-2014, 08:07 AM
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);

Rosario Leonardi
08-11-2014, 03:15 PM
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.

carsten neumann
08-12-2014, 02:16 AM
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...

azaravicius
08-12-2014, 08:25 AM
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


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.

Dan Bartlett
08-12-2014, 09:40 AM
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.