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 3 of 3

Thread: OpenGL 2.1 VBO not rendering, blank screen.

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2018
    Posts
    12

    Post [Solved] OpenGL 2.1 VBO not rendering, blank screen.

    I'm trying to draw a simple triangle with VBOs, however I can't get it to work, it's a blank screen.
    What is the issue?

    Code :
    #include<SDL2/SDL.h>
    #include<GL/glew.h>
    #include<GL/gl.h>
     
    const char* vertshaderSource = R"(
        #version 120
        attribute vec3 pos;
     
        void main() {
            gl_Position = vec4(pos.xyz, 1);
        }
    )";
    const char* fragshaderSource = R"(
        #version 120
     
        void main() {
            gl_FragColor = vec4(1, 0, 0, 1);
        }
    )";
     
    struct vert {
        float x, y, z;
    };
     
    int main(int argc, char** argv) {
        SDL_Window* window = SDL_CreateWindow("A", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL);
     
        SDL_GLContext glctx = SDL_GL_CreateContext(window);
     
        glewExperimental = false;
        glewInit();
     
        SDL_ShowWindow(window);
     
        glViewport(0, 0, 640, 480);
     
        GLint vertshader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertshader, 1, &vertshaderSource, 0);
        glCompileShader(vertshader);
     
        GLint fragshader = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragshader, 1, &fragshaderSource, 0);
        glCompileShader(fragshader);
     
        GLint prog = glCreateProgram();
        glAttachShader(prog, vertshader);
        glAttachShader(prog, fragshader);
        glLinkProgram(prog);
     
        glEnable(GL_VERTEX_ARRAY);
        vert tri[3] = {{0, 0, 0}, {1, 0, 0}, {0.5f, 1, 0}};
        GLuint trivbo;
        glGenBuffers(1, &trivbo);
        glBindBuffer(GL_ARRAY_BUFFER, trivbo);
        glBufferData(GL_ARRAY_BUFFER, sizeof(tri), tri, GL_DYNAMIC_DRAW);
     
        glClearColor(0, 1, 0.5f, 1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
        glUseProgram(prog);
        glBindBuffer(GL_ARRAY_BUFFER, trivbo);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (void*) 0);
        glDrawArrays(GL_TRIANGLES, 0, 3);
     
        SDL_GL_SwapWindow(window);
     
        SDL_Delay(5000);
     
        SDL_GL_DeleteContext(glctx);
        SDL_DestroyWindow(window);
     
        return 0;
    }
    Last edited by Midnightas; 07-09-2018 at 02:48 AM.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,859
    Quote Originally Posted by Midnightas View Post
    Code :
        glEnable(GL_VERTEX_ARRAY);
    This shouldn't be here.

    Quote Originally Posted by Midnightas View Post
    Code :
        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (void*) 0);
    The second parameter should be 3.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2018
    Posts
    12
    Oh wow, I mistook it for the size of a float, sorry.
    Thank you so much for this!

Posting Permissions

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