OpenGL unable to draw triangle with VBO

I recently started with OpenGL programming and found this tutorial site called "open.gl". The second tutorial explains the process of drawing polygons with "modern" OpenGL, but after closely following the tutorial I cannot see my triangle on the screen. I have been searching around trying to see what I am missing, but so far have no idea why my triangle doesn't get drawn on the screen. Have I missed something really simple here?

I use SDL to create the window and GLEW to check the OpenGL functions on the hardware like on the tutorial. I also did do a check to see that the OpenGL context version is "3.2".

#include <stdio.h>
#include "glew.h"
#include <SDL.h>
#include <SDL_opengl.h>

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;

const GLchar* vertexSource =
"#version 150 coren"
"in vec2 position;"
"void main() {"
"   gl_Position = vec4(position, 0.0, 1.0);"
"}";

const GLchar* fragmentSource =
"#version 150 coren"
"out vec4 outColor;"
"void main() {"
"   outColor = vec4(1.0, 1.0, 1.0, 1.0,);"
"}";


GLfloat vertices[] =
{
    0.0f, 0.5f,
    0.5f, -0.5f,
    -0.5f, -0.5f
};

int main( int argc, char* argv[] )
{
    SDL_Init( SDL_INIT_VIDEO );

    // OpenGL core profile - deprecated functions are disabled
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK,
                         SDL_GL_CONTEXT_PROFILE_CORE );
    // OpenGL version 3.2
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 2 );

    SDL_Window* window = SDL_CreateWindow( "SDL OpenGL",
                                           SDL_WINDOWPOS_CENTERED,
                                           SDL_WINDOWPOS_CENTERED,
                                           SCREEN_WIDTH,
                                           SCREEN_HEIGHT,
                                           SDL_WINDOW_OPENGL );

    SDL_GLContext context = SDL_GL_CreateContext( window );

    glewExperimental = GL_TRUE;
    if( glewInit() != GLEW_OK )
    {
        printf( "Failed to init glew!n" );
        return -1;
    }

    glClearColor( 0.0f, 0.0f, 0.3f, 1.0f );

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

    GLuint vbo;
    glGenBuffers( 1, &vbo );

    glBindBuffer( GL_ARRAY_BUFFER, vbo );
    glBufferData( GL_ARRAY_BUFFER,
                  sizeof(vertices),
                  vertices,
                  GL_STATIC_DRAW );

    GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER );
    glShaderSource( vertexShader, 1, &vertexSource, NULL );
    glCompileShader( vertexShader );

    GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
    glShaderSource( fragmentShader, 1, &fragmentSource, NULL );
    glCompileShader( fragmentShader );

    GLuint shaderProgram = glCreateProgram();
    glAttachShader( shaderProgram, vertexShader );
    glAttachShader( shaderProgram, fragmentShader );
    glBindFragDataLocation( shaderProgram, 0, "outColor" );
    glLinkProgram( shaderProgram );
    glUseProgram( shaderProgram );

    GLuint posAttrib = glGetAttribLocation( shaderProgram, "position" );
    glEnableVertexAttribArray( posAttrib );
    glVertexAttribPointer( posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0 );

    SDL_Event windowEvent;
    while( true )
    {
        if( SDL_PollEvent( &windowEvent ) )
        {
            if( windowEvent.type == SDL_QUIT )
                break;

            if( windowEvent.type == SDL_KEYUP &&
                windowEvent.key.keysym.sym == SDLK_ESCAPE )
                break;
        }

        glClear( GL_COLOR_BUFFER_BIT );

        glDrawArrays( GL_TRIANGLES, 0, 3 );

        SDL_GL_SwapWindow( window );
    }

    glDeleteProgram( shaderProgram );
    glDeleteShader( fragmentShader );
    glDeleteShader( vertexShader );

    glDeleteBuffers( 1, &vbo );
    glDeleteVertexArrays( 1, &vao );

    SDL_GL_DeleteContext( context );
    SDL_Quit();
    return 0;
}
链接地址: http://www.djcxy.com/p/38458.html

上一篇: SDL无法通过2.1 Mac OSX Yosemite加载OpenGL上下文

下一篇: OpenGL无法与VBO绘制三角形