OpenGL/SDL - 无法进行线路加载

OpenGL/SDL - Can't make line load

我一直在尝试制作一个代码,以便在 window 上出现一条线。我一直在使用 c 和 opengl/sdl。但出于某种原因,代码没有在 window 中生成一行。我不知道我做错了什么:

#include <SDL.h> 
#include <stdio.h>
#include <GL/glew.h> 
#include <SDL_opengl.h>  


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

    SDL_Window* window;


    SDL_Init(SDL_INIT_EVERYTHING);


    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
    SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);;


    window = SDL_CreateWindow("Breakout!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 400, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);

    SDL_GLContext context = SDL_GL_CreateContext(window);
    SDL_GL_MakeCurrent(window, context);


    SDL_GL_SetSwapInterval(1);

    glClearColor(1.f, 1.f, 1.f, 1.f);

    glViewport(0, 0, 800, 400);
    
    glShadeModel(GL_SMOOTH);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    glDisable(GL_DEPTH_TEST);

    

    // Check that the window was successfully created
    if (window == NULL) {
        // In the case window was not made
        printf("Could not create window: %s\n", SDL_GetError());
        return 1;
    }


    SDL_Event windowEvent;

    while (1)
    {
        
        

        // Get the next event
        SDL_Event event;


        if (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
            {
                // Break out of the loop on quit
                break;
            }

            
        }



        //Rendering to the screen
        glClear(GL_COLOR_BUFFER_BIT);


        glOrtho(0.0, 800, 400, 0.0, -1, 1);   // setup a 10x10x2 viewing world

        glColor4ub(255, 0, 0, 255);

        glBegin(GL_LINES);

        glVertex2f(0, 0);
        glVertex2f(800, 400);
    
        glEnd();

        //Render
        SDL_GL_SwapWindow(window);

    }

    // Close and destroy the window
    SDL_DestroyWindow(window);
    SDL_GL_DeleteContext(window);


    // Clean up
    SDL_Quit();  
    return 0;
}

glOrtho 不只是设置矩阵。该函数创建一个正交投影矩阵并将当前矩阵与新矩阵相乘。

如果你在应用程序循环中调用glOrtho,你必须先用glLoadIdentity加载单位矩阵:

glLoadIdentity();
glOrtho(0.0, 800, 400, 0.0, -1, 1);