Question

Within my project, I've been having trouble getting an triangle to display within OnRender(), but for some reason, nothing other than the background color (green) is visible.

int main(int argc, char **argv)
{
    if (!OnInit())
    return -1;
    SDL_Event Event;

    while (_isRunning)
    {
        while (SDL_PollEvent(&Event))
            OnEvent(&Event);

        OnRender();
        OnLoop();
        SDL_GL_SwapWindow(_screen);
    }

    OnCleanup();

    return 0;
}

void generalSetup()
{
    // Initialize SDL2
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
        sdldie("Failed to initial SDL2.");
    else
    {
        /* Request OpenGL 3.2 */
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

        // Create window
        _screen = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);        

        // Create Context
        _mainContext = SDL_GL_CreateContext(_screen);

        // Create Surface
        _surface = SDL_GetWindowSurface(_screen);
        SDL_FillRect(_surface, NULL, SDL_MapRGB(_surface->format, 0xCC, 0x20, 0x20));
        SDL_UpdateWindowSurface(_screen);

        /* swap synchronized */
        SDL_GL_SetSwapInterval(1);

        // Initialize GLew 1.10
        glewExperimental = GL_TRUE;
        GLenum error = glewInit();

        if (error != GLEW_OK)
            printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
        else
            std::cout << "GLew Initialized" << std::endl;


        glClearColor(0, 1, 0, 0);

        glViewport(0, 0, 800, 600);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        gluPerspective(45, 800 / 600, 1, 1000);
        gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
}


bool OnInit()
{
    generalSetup();
    return true;
}

void OnEvent(SDL_Event* Event)
{
    if (Event->type == SDL_QUIT)
        _isRunning = false;
}

void OnLoop()
{

}

void OnRender()
{
    glClearColor(1.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glTranslatef(0.f, 0.f, -10.f);
    glBegin(GL_TRIANGLES);
    glColor3f(0.1, 0.2, 0.3);
    glVertex3f(0, 0, 0);
    glVertex3f(1, 0, 0);
    glVertex3f(0, 1, 0);
    glEnd();
    glPopMatrix();
}

void OnCleanup()
{
    SDL_GL_DeleteContext(_mainContext);
    SDL_DestroyWindow(_screen);
    SDL_Quit();
}
Was it helpful?

Solution

You requested a Core context. None of your immediate-mode (matrix stack, glBegin(), etc.) code will work.

Drop back to a compatibility context (SDL_GL_CONTEXT_PROFILE_COMPATIBILITY) or supply all the necessary shaders, vertex buffers, etc. that Core requires.

OTHER TIPS

You are trying to render through the immediate mode, but it is not supported by the OpenGL 3.2. Try using the version 2.0 or 2.1, which support both shaders (if you are intending to use them) and the immediate mode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top