0
\$\begingroup\$

I am going through an OpenGL book trying to get the examples to work. I am using Glew, Glfw, and that's it beyond Opengl 4.3, which my I know my graphics card supports completely via the OpenGL Extension viewer.

The problem is that the triangle displays in the window, but it flashes at a fluctuating rate between black/background and completely white. I am drawing the triangle with a red, green, and a blue point. My thoughts were that the positionData were too close to the camera (floating point errors/clipping). What could be the source of this error. I've never successfully gotten modern ( 3.X or greater ) OpenGL to work. The setup is really intense.

Here is the main.cpp:

#include "shaders\tools.h"
//#define GLEW_STATIC
#define GLFW_DLL                    //Required for linking
#define GLFW_INCLUDE_NONE           //Load our own opengl
#include "glfw\glfw3.h"


using namespace std;



int main() {

    //Use glfw to create window
    if (!glfwInit())
    {
        cout << "Error: could not init glfw" << endl;
        return 1;
    }

    GLFWwindow* window = glfwCreateWindow(640, 480, "Shader Demo", nullptr, nullptr);
    if (window == nullptr)
    {
        cout << "failure creating window!" << endl;
        glfwTerminate();
        return 1;
    }

    glfwMakeContextCurrent(window);

    GLenum glew_err = glewInit();
    if (glew_err != GLEW_OK)
    {
        cout << "Glew Error: " << glewGetErrorString(glew_err) << endl;
        return 1;
    }

    GLuint vertShader_i;
    GLuint fragShader_i;
    bool shader_compile_error = compileShaders( &vertShader_i, &fragShader_i, "basic.vert", "basic.frag");

    if (shader_compile_error)
    {
        cout << "shader compile error!" << endl;
        return 1;
    }

    GLuint glslProgram_i;
    bool shader_link_error = linkShaders( &glslProgram_i, vertShader_i, fragShader_i );

    if (shader_link_error)
    {
        cout << "shader link error!" << endl;
        return 1;
    }
    else
    {
        glUseProgram( glslProgram_i );
    };


    float positionData[] =
    {
       -.8f,  -.8f,  0.f,
        .8f,  -.8f,  0.f,
        .0f,   .8f,  0.f
    };

    float colorData[] =
    {
        1.f, 1.f, 1.f,
        0.f, 1.f, 0.f,
        0.f, 0.f, 1.f
    };

        //Create buffer objects
    GLuint vboHandles[2];
    glGenBuffers(2, vboHandles);
    GLuint positionBufferHandle = vboHandles[0];
    GLuint colorBufferHandle = vboHandles[1];

    //Populate the position buffer
    glBindBuffer( GL_ARRAY_BUFFER, positionBufferHandle );
    glBufferData( GL_ARRAY_BUFFER, sizeof(positionData), positionData, GL_STATIC_DRAW );

    // color buffer
    glBindBuffer( GL_ARRAY_BUFFER, colorBufferHandle );
    glBufferData( GL_ARRAY_BUFFER, sizeof(colorData), colorData, GL_STATIC_DRAW );

    GLuint vaoHandle;
    glGenVertexArrays( 1, &vaoHandle );
    glBindVertexArray( vaoHandle );


    //Enable vertex attribute arrays
    glEnableVertexAttribArray(0); //vertexPosition
    glEnableVertexAttribArray(1); //vertexColor

    //map index 0 to position buffer
    glBindBuffer( GL_ARRAY_BUFFER, positionBufferHandle );
    glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);

    // map index 1 to color buffer
    glBindBuffer( GL_ARRAY_BUFFER, colorBufferHandle );
    glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);

    glBindVertexArray( vaoHandle );
    glDrawArrays( GL_TRIANGLES, 0, 3);

    //show version of opengl
//  glVersionData gl_version_data;
//  getGLVersion( &gl_version_data );
//  glDisplayVersionData( gl_version_data );

    while (!glfwWindowShouldClose(window))
    {
        // Keep running
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

basic.vert:

#version 430

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexColor;

out vec3 Color;
void main()
{
    Color = VertexColor;
    gl_Position = vec4( VertexPosition, 1.0 );
}

and basic.frag:

#version 430

in vec3 Color;

out vec4 FragColor;

void main()
{
    Color = vec4( Color, 1.0 );
}

I can include the "shaders\tool.h" which defines loadShaderCode, compileShaders, and linkShaders if necessary, but I don't want to post too much code to break the flow of my question. They do what you'd expect, and I don't get any errors returned. They load the codes, link them attach them to a program, compile the program and spit out the glsl programHandle.

I'm on windows x64 using g++ 4.8.1. with C++ 11.

EDIT: here is the shader loader function:

const GLchar * loadShaderCode(const char * shaderFilename)
{
    fstream shaderFile( shaderFilename, std::ios::in | std::ios::ate);

    streampos end = shaderFile.tellg();
    shaderFile.seekg(0, std::ios::beg );
    streampos beg = shaderFile.tellg();

    string code;
    code.reserve(end-beg);
    shaderFile.read(&code[0],end-beg);
    shaderFile.close();

    return code.c_str();
}

WHen I load both shaders, there is a marking at the end that doesn't make sense:

#version 430

in vec3 Color;

out vec4 FragColor;

void main()
{
        FragColor = vec4( Color, 1.0 );
}o↨☺  // <--- What is this marking???

The Vert Shader is very similar. I tried to delete the last character from the returned string containing this code and it changed the closing brace at the end of main. I also tried setting it to zero - then the triangle doesn't display at all, even though the code looks perfect then. What is this character and how do I get rid of it?

THank you @Boreal for the help! Here is the (beautiful result): 1

\$\endgroup\$
2
\$\begingroup\$

There's an error in basic.frag. Instead of setting FragColor, you're setting Color, which is an input variable. I don't know why that's not being reported as an error because it is certainly not permitted.

As for the flashing problem, I have a feeling it might be due to a ping-ponging double-buffer setup. Since you only draw the scene once, every other swap is showing a blank buffer. Try moving your draw calls to the main loop.

\$\endgroup\$
  • \$\begingroup\$ You were right about the flashing. The frag error looks right and makes sense, but something else is going on. I printed out the shaders, and there's a weird set of 2-3 characters at the end of each shader. Perhaps eol or some utf-8 character marking? I will post the shader loader code. \$\endgroup\$ – user27886 Jan 28 '15 at 7:44
  • \$\begingroup\$ One issue I can see is that you're using the internal pointer of a std::string that gets deallocated as soon as the function returns. This could have something to do with the garbage (which you see commonly with a string that for some reason isn't null-terminated). \$\endgroup\$ – jmegaffin Jan 28 '15 at 19:29
  • \$\begingroup\$ Ah that's a good point. I'll try fixing that. \$\endgroup\$ – user27886 Jan 28 '15 at 20:16
  • \$\begingroup\$ Thank you! You were right on. Those were some sloppy mistakes on my part. Thanks again. I included the picture for kicks just cause the colors were so vibrant (and stable too). :) \$\endgroup\$ – user27886 Jan 29 '15 at 0:19
  • \$\begingroup\$ @Boreal Shader compilers are pretty terrible, I guess the developers were lazy and instead of doing proper error checking, they just allow people to use incorrect shaders. It's super annoying, because if you change one thing tons of other unrelated stuff breaks. \$\endgroup\$ – BWG Jan 29 '15 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.