为什么OpenGL不绘图?

时间:2016-07-15 13:45:11

标签: c++ opengl sdl

这段代码应该是绘制三角形的代码,但我得到的只是黑屏。为什么我没有得到任何东西?

其次,在我的顶点数组中,如果我放了一个' f'在我的坐标之后,就像我总是在教程中看到的那样,我得到一个关于八进制中无效数字的错误。为什么其他人都会使用' f'他们的数字之后而不是我?

我在OSX Yosemite上使用openGL 4.1。

  #include <iostream>
    //Using SDL and standard IO
    #include <SDL2/SDL.h>
    //#define GL_GLEXT_PROTOTYPES 1
    //#include <SDL2/SDL_opengl.h>
    #include <GLUT/glut.h>
    #include <stdio.h>
    #include <OpenGL/gl3.h>
    #include <glm/glm.hpp>
    #include <glm/gtc/matrix_transform.hpp>
    #include <string.h>

using namespace std;


//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;



bool SetOpenGLAttributes()
{
    // Set our OpenGL version.
    // SDL_GL_CONTEXT_CORE gives us only the newer version, deprecated functions are disabled
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    return true;
}



int main( int argc, char* args[] )
{
    //The window we'll be rendering to
    SDL_Window* window = NULL;

    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
    }
    else
    {
        SetOpenGLAttributes();
        //Create window
        window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL );
        if( window == NULL )
        {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        }
        else
        {
            //creating new context
            SDL_GL_CreateContext(window);

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


            printf("%s", "This is your version");
            printf("%s\n", glGetString(GL_VERSION));
            printf("%s", glGetString(GL_RENDERER));

            SDL_GL_SetSwapInterval(1);
            glEnable(GL_DEPTH_TEST);


            float r = 0.5;
            static const GLfloat cubeV[] = {
              -.5, 0, 0,
                .5, 0, 0,
                0, .5, 0

            };

            GLuint vertexBuffer;
            glGenBuffers(1, &vertexBuffer);
            glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
            glBufferData(GL_ARRAY_BUFFER, sizeof(cubeV), cubeV, GL_STATIC_DRAW);





            SDL_GL_SwapWindow(window);

            bool running = true;
            while(running){
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glEnableVertexAttribArray(0);
                glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
                glVertexAttribPointer(
                                      0,                  // attribute 0. No particular reason for 0, but   must match the layout in the shader.
                                      3,                  // size
                                      GL_FLOAT,           // type
                                      GL_FALSE,           // normalized?
                                      0,                  // stride
                                      (void*)0            // array buffer offset
                                      );

                glDrawArrays(GL_TRIANGLES, 0, 3);
                glDisableVertexAttribArray(0);

                glFlush();
                SDL_GL_SwapWindow(window);
                SDL_Delay(17);

            }


        }
    }


    //Destroy window
    //SDL_DestroyWindow( window );

    //Quit SDL subsystems
    //SDL_Quit();

    return 0;
}

0 个答案:

没有答案