Passing attributes to OpenGL vertex shader acts strangely

2.2k views Asked by At

The problem:

Outcome 1: I pass a vertex attribute to the shader, the program runs for 5 seconds, then the graphics driver stops responding and recovers but the program doesn't.

Outcome 2: I cap the framerate at 60 then do the same thing. The program runs fine, but when I try to pass different values for the vertex attribute in the same runtime it does not update the geometry (only the first vertex attribute passed works).

I'm using OpenGL version 3.1 with GLSL 140. (The highest my graphics card/driver will support)

What I have tried:

Internet browsing and reading tutorials. Since I do not understand OpenGL very well yet I think the problem is that I just do not know what to search for.

Note: Sorry if I have some bad OpenGL coding practices, I am still learning.

Code:

opengl2_vert.glsl

#version 140

in vec4 offset;

void main(void)
{
    // Declare a hard-coded array of positions
    vec4 vertices[3] = vec4[3](vec4( 0.25, -0.25, 0.5, 1.0),
    vec4(-0.25, -0.25, 0.5, 1.0),
    vec4( 0.25, 0.25, 0.5, 1.0));
    // Index into our array using gl_VertexID
    gl_Position = vertices[gl_VertexID] - offset;
}

Game.cpp

#include "Game.h"


void Game::init()
{
    GLuint vertex_shader = getIO().loadShader("opengl2_vert.glsl", ShaderType::VERTEX);
    GLuint fragment_shader = getIO().loadShader("opengl2_frag.glsl", ShaderType::FRAGMENT);

    // Create program, attach shaders to it, and link it
    program = glCreateProgram();
    glAttachShader(program, vertex_shader);
    glAttachShader(program, fragment_shader);
    glLinkProgram(program);

    GLint isLinked = GL_FALSE;
    glGetProgramiv(program, GL_LINK_STATUS, &isLinked);

    // Logging
    GLint maxLength = 255;
    GLint rLength = 0;
    GLchar* err = new char[maxLength];

    // Vertex shader
    glGetShaderInfoLog(vertex_shader, maxLength, &rLength, err);
    DEBUGSTR((char*)err);

    // Fragment shader
    glGetShaderInfoLog(vertex_shader, maxLength, &rLength, err);
    DEBUGSTR((char*)err);

    // Program
    glGetProgramInfoLog(vertex_shader, maxLength, &rLength, err);
    DEBUGSTR((char*)err);

    delete [] err;
    ASSERT(isLinked == GL_TRUE);



    // Delete the shaders as the program has them now
    glDeleteShader(vertex_shader);
    glDeleteShader(fragment_shader);

    glUseProgram(program);

    // VAO
    glGenVertexArrays(1, &vertex_array_object); // Creates a VAO
    glBindVertexArray(vertex_array_object); // Attaches VAO to rendering context

}

void Game::render(double dt)
{

    //DEBUG("FPS %i", static_cast<int>(1/dt));
    static bool seizure = false;
    seizure = !seizure;
    //(seizure)?getScreen().setBackgroundColor(0.333f, 0.003f, 0.082f):getScreen().setBackgroundColor(0.803f, 0.753f, 0.573f);
    static float angle = 0;
    angle += dt;
    //DEBUG("ANGLE %f %f", cos(angle)/2.0f, sin(angle)/2.0f);

    // Use the program object we created earlier for rendering
    GLfloat attrib[] = {cos(angle)/2.0f, sin(angle)/2.0f, 0.0f, 0.0f};

    glVertexAttrib4fv(glGetAttribLocation(program, "offset"), attrib);


    // Draw one point
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

void Game::shutdown()
{
    glDeleteProgram(program);
    glDeleteVertexArrays(1, &vertex_array_object);
}

IO.cpp

#include "IO.h"

GLuint IO::loadShader(const char* fileName, ShaderType shaderType)
{
    std::ifstream f(fileName, std::ifstream::binary);
    ASSERT(fileName);
    f.seekg(0, std::ios::end);   
    int length = (int)f.tellg();           
    f.seekg(0, std::ios::beg); 
    char* buffer = new char[length+1];  
    f.read(buffer, length); 
    buffer[length] = 0;
    f.close();                   

    const GLchar* in = (const GLchar *)buffer;

    GLuint shader;
    switch(shaderType)
    {
    case VERTEX:
        shader = glCreateShader(GL_VERTEX_SHADER);
        break;

    case FRAGMENT:
        shader = glCreateShader(GL_FRAGMENT_SHADER);
        break;
    }

    glShaderSource(shader, 1, &in, NULL);
    glCompileShader(shader);


    // Logging
    GLint maxLength = 255;
    GLint rLength = 0;
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);

    GLchar* err = new char[maxLength];
    glGetShaderInfoLog(shader, maxLength, &rLength, err);

    DEBUGSTR((char*)err);

    delete [] err;
    delete [] buffer;

    GLint isCompiled = 0;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
    ASSERT(isCompiled == GL_TRUE);


    return shader;
}

Entry point/Game loop

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
    // Creating dummy window
    HWND hWnd = getDummyWindow();
    HDC hdc = GetDC(hWnd);
    PIXELFORMATDESCRIPTOR pfd = getWindowsPixelFormatStruct();
    int chosenPixelFormat = ChoosePixelFormat( hdc, &pfd );
    ASSERT(chosenPixelFormat != 0);
    int result = SetPixelFormat(hdc, chosenPixelFormat, &pfd );
    ASSERT(result != NULL);

    // Creating rendering context
    HGLRC hglrc = wglCreateContext(hdc);

    wglMakeCurrent(hdc, hglrc);

    // Getting supported OpenGL version
    DEBUG("System OpenGL version: %s", (char*)glGetString(GL_VERSION));
    DEBUG("Windows pixel format: %i", chosenPixelFormat);

    GLint major_version = 1;
    GLint minor_version = 1;
    glGetIntegerv(GL_MAJOR_VERSION, &major_version);
    glGetIntegerv(GL_MINOR_VERSION, &minor_version);


    GLenum err = glewInit();
    ASSERT(GLEW_OK == err);

    // Getting OpenGL pixel format
    int nPixCount = 0;
    // Specify the important attributes we care about
    int pixAttribs[] = {
        WGL_SUPPORT_OPENGL_ARB, 1, // Must support OGL rendering
        WGL_DRAW_TO_WINDOW_ARB, 1, // pf that can run a window
        WGL_RED_BITS_ARB, 8, // At least 8 bits of red
        WGL_GREEN_BITS_ARB, 8, // At least 8 bits of green
        WGL_BLUE_BITS_ARB, 8, // At least 8 bits of blue
        WGL_DEPTH_BITS_ARB, 16, // At least 16 bits of depth
        WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, // Must be HW accelerated
        WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, // pf should be RGBA type
        WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
        0} ; // Zero termination
    // Ask OpenGL to find the most relevant format matching our attribs
    // Only get one format back.
    wglChoosePixelFormatARB(hdc,
        &pixAttribs[0],
        NULL,
        1,
        &chosenPixelFormat,
        (UINT*)&nPixCount);
    ASSERT(chosenPixelFormat != -1);
    DEBUG("OpenGL pixel format: %i", chosenPixelFormat);

    // Clean up dummies

    wglDeleteContext(hglrc);
    ReleaseDC(hWnd, hdc);
    DestroyWindow(hWnd);

    // Create window
    hWnd = getWindow(hInstance, TEXT("OpenGL Window"), iCmdShow);
    hdc = GetDC(hWnd);
    result = SetPixelFormat(hdc, chosenPixelFormat, &pfd );
    ASSERT(result != NULL);

    // Creating OpenGL rendering context


    GLint attribs[] =
    {
        WGL_CONTEXT_MAJOR_VERSION_ARB, major_version,
        WGL_CONTEXT_MINOR_VERSION_ARB, minor_version,
        WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
        0
    };
    DEBUG("Using OpenGL version %i.%i", major_version, minor_version);

    hglrc = wglCreateContextAttribsARB(hdc, 0, attribs);
    wglMakeCurrent(hdc, hglrc);

    // Getting rendering dimensions
    RECT rc_dimensions;
    GetClientRect(hWnd, &rc_dimensions);
    glViewport(0, 0, rc_dimensions.left-1, rc_dimensions.bottom-1); // set viewport


    // Setting up game
    Game game;
    game.getScreen().setWidth(rc_dimensions.left-1);
    game.getScreen().setHeight(rc_dimensions.bottom-1);
    game.getScreen().setFrameCap(59.0f);



    game.init();
    game._getGameTimer().elapsed_time();

    // Starting game loop
    MSG msg;
    float accum_time = 0.0f;
    while(true)
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if(msg.message == WM_QUIT)
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        accum_time += game._getGameTimer().elapsed_time();
        if(accum_time > game.getScreen()._getSPF())
        {
            game.render(game.getScreen()._getSPF());

            // Post render
            SwapBuffers(hdc);
            game.getScreen().clearBuffer();

            accum_time -= game.getScreen()._getSPF();
        }
    }

    // Cleaning up before shutdown
    game.shutdown();
    wglDeleteContext(hglrc);
    ReleaseDC(hWnd, hdc);
    DestroyWindow(hWnd);
    UnregisterClass(TEXT("OPENGL"), hInstance);

    return 0;
}

OpenGL support

Renderer: Intel(R) HD Graphics 3000
Vendor: Intel
Memory: 2108 MB
Version: 3.1.0 - Build 9.17.10.3347
Shading language version: 1.40 - Intel Build 9.17.10.3347


Max texture size: 8192 x 8192
Max vertex texture image units: 16
Max texture image units: 16
Max geometry texture units: 0
Max anisotropic filtering value: 16
Max viewport size: 8192 x 8192
Max Clip Distances: 6
Max samples: 4


Extensions: 129

GL_3DFX_texture_compression_FXT1
GL_ARB_color_buffer_float
GL_ARB_compatibility
GL_ARB_copy_buffer
GL_ARB_depth_buffer_float
GL_ARB_depth_clamp
GL_ARB_depth_texture
GL_ARB_draw_buffers
GL_ARB_draw_buffers_blend
GL_ARB_draw_elements_base_vertex
GL_ARB_draw_instanced
GL_ARB_explicit_attrib_location
GL_ARB_fragment_coord_conventions
GL_ARB_fragment_program
GL_ARB_fragment_program_shadow
GL_ARB_fragment_shader
GL_ARB_framebuffer_object
GL_ARB_framebuffer_sRGB
GL_ARB_half_float_pixel
GL_ARB_half_float_vertex
GL_ARB_instanced_arrays
GL_ARB_map_buffer_range
GL_ARB_multisample
GL_ARB_multitexture
GL_ARB_occlusion_query
GL_ARB_occlusion_query2
GL_ARB_pixel_buffer_object
GL_ARB_point_parameters
GL_ARB_point_sprite
GL_ARB_provoking_vertex
GL_ARB_sampler_objects
GL_ARB_seamless_cube_map
GL_ARB_shader_bit_encoding
GL_ARB_shader_objects
GL_ARB_shading_language_100
GL_ARB_shadow
GL_ARB_sync
GL_ARB_texture_border_clamp
GL_ARB_texture_buffer_object_rgb32
GL_ARB_texture_compression
GL_ARB_texture_compression_rgtc
GL_ARB_texture_cube_map
GL_ARB_texture_env_add
GL_ARB_texture_env_combine
GL_ARB_texture_env_crossbar
GL_ARB_texture_env_dot3
GL_ARB_texture_float
GL_ARB_texture_non_power_of_two
GL_ARB_texture_query_lod
GL_ARB_texture_rectangle
GL_ARB_texture_rg
GL_ARB_texture_rgb10_a2ui
GL_ARB_timer_query
GL_ARB_transpose_matrix
GL_ARB_uniform_buffer_object
GL_ARB_vertex_array_bgra
GL_ARB_vertex_array_object
GL_ARB_vertex_buffer_object
GL_ARB_vertex_program
GL_ARB_vertex_shader
GL_ARB_vertex_type_2_10_10_10_rev
GL_ARB_window_pos
GL_ATI_separate_stencil
GL_EXT_abgr
GL_EXT_bgra
GL_EXT_blend_color
GL_EXT_blend_equation_separate
GL_EXT_blend_func_separate
GL_EXT_blend_minmax
GL_EXT_blend_subtract
GL_EXT_clip_volume_hint
GL_EXT_compiled_vertex_array
GL_EXT_draw_buffers2
GL_EXT_draw_range_elements
GL_EXT_fog_coord
GL_EXT_framebuffer_blit
GL_EXT_framebuffer_multisample
GL_EXT_framebuffer_object
GL_EXT_gpu_program_parameters
GL_EXT_multi_draw_arrays
GL_EXT_packed_depth_stencil
GL_EXT_packed_float
GL_EXT_packed_pixels
GL_EXT_rescale_normal
GL_EXT_secondary_color
GL_EXT_separate_specular_color
GL_EXT_shadow_funcs
GL_EXT_stencil_two_side
GL_EXT_stencil_wrap
GL_EXT_texture3D
GL_EXT_texture_array
GL_EXT_texture_compression_s3tc
GL_EXT_texture_edge_clamp
GL_EXT_texture_env_add
GL_EXT_texture_env_combine
GL_EXT_texture_filter_anisotropic
GL_EXT_texture_integer
GL_EXT_texture_lod_bias
GL_EXT_texture_rectangle
GL_EXT_texture_shared_exponent
GL_EXT_texture_snorm
GL_EXT_texture_sRGB
GL_EXT_texture_swizzle
GL_EXT_transform_feedback
GL_IBM_texture_mirrored_repeat
GL_INTEL_map_texture
GL_INTEL_performance_queries
GL_NV_blend_square
GL_NV_conditional_render
GL_NV_primitive_restart
GL_NV_texgen_reflection
GL_SGIS_generate_mipmap
GL_SGIS_texture_edge_clamp
GL_SGIS_texture_lod
GL_WIN_swap_hint
WGL_ARB_buffer_region
WGL_ARB_create_context
WGL_ARB_extensions_string
WGL_ARB_framebuffer_sRGB
WGL_ARB_make_current_read
WGL_ARB_multisample
WGL_ARB_pbuffer
WGL_ARB_pixel_format
WGL_ARB_pixel_format_float
WGL_EXT_depth_float
WGL_EXT_extensions_string
WGL_EXT_pixel_format_packed_float
WGL_EXT_swap_control
WGL_EXT_swap_control_tear

Core features
v3.0 (100 % - 23/23)
v3.1 (100 % - 8/8)
v3.2 (70 % - 7/10)
v3.3 (70 % - 7/10)
v4.0 (21 % - 3/14)
v4.1 (0 % - 0/7)
v4.2 (0 % - 0/12)
v4.3 (0 % - 0/18)
v4.4 (0 % - 0/10)

OpenGL driver version check (Current: 3.1.0 - Build 9.17.10.3347, Latest known: 9.17.10.3347):
Outdated version of display drivers detected
According the database, you are might be not using the latest version of display drivers for your video card.

No ICD registry entry
The current OpenGL driver doesn't expose the SOFTWARE/Microsoft/Windows (NT)/CurrentVersion/OpenGLDrivers registry entry. Unable to detect the driver version, driver revision name and filename.

Extension verification: 
GL_EXT_color_subtable was not found, but has the entry point glColorSubTableEXT 
GL_EXT_paletted_texture was not found, but has the entry point glColorTableEXT 
GL_EXT_paletted_texture was not found, but has the entry point glGetColorTableEXT 
GL_EXT_paletted_texture was not found, but has the entry point glGetColorTableParameterfvEXT 
GL_EXT_paletted_texture was not found, but has the entry point glGetColorTableParameterivEXT 
1

There are 1 answers

3
Andon M. Coleman On BEST ANSWER

There are a few issues that need to be addressed, although I do not know if they will solve your problem.

There is no guarantee that in vec4 offset is going to be assigned to vertex attribute location 0. You should use glBindAttribLocation (...) to bind its location (before linking) to 0, or query the location (after linking) by name using glGetAttribLocation (...) rather than blindly using the magic number 0. Attribute locations are assigned after a program is linked and only to vertex attributes that are actually actively used when the program executes (just like uniforms). So you cannot assume that the attributes start at 0 and are assigned sequentially, you actually need to tell GL what location to give it, or query it after linking.

But here is where things get really funny... #version 140 core. The word core really did not have any significance prior to OpenGL 3.2 (GLSL 1.50). I am surprised your GLSL compiler is not complaining about this. In fact, the #version directive defined by the GLSL 1.40 spec is limited to a number and nothing else, in 1.50 an optional profile name was added. You should check the shader info log even for shaders that successfully compile because they may contain warnings such as this.

Also, while on the topic of info logs... the linker produces one as well. You are checking the compile status when you compile your shaders, but you have no code that checks the link status.

Update:

I noticed a few things wrong with the code you are using to query your shader info log as well:

  1. Assuming DEBUGSTR (...) does not delete the pointer it is passed, you are going to leak memory when your shader fails to compile.

  2. You are going to overrun your allocated memory if the compiler produces a shader info log >= 255 characters in length.

    • Rewrite this: GLchar* err = new char[255]; to this: GLchar* err = new char[maxLength];.