linux

linux下sdl2&opengl绘制四棱锥

2020-05-09  本文已影响0人  岁月静好忄

linux下使用sdl2和opengl简单绘制出四棱椎

头文件

#ifndef APPLICATION_H_
#define APPLICATION_H_

#include <GL/glew.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_video.h>

class Application{

public:
    bool quit;
    SDL_Window* window;
    SDL_GLContext glContext;
    SDL_Event sdlEvent;
    GLfloat     rtri;

public:
    Application();
    bool Run();
    void Draw();

};


#endif /* APPLICATION_H_ */

cpp文件

#include "Application.h"

#include <GL/gl.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_timer.h>
#include <cstdio>

Application::Application()
{
       quit = false;
       window = NULL;
       glContext = NULL;
       rtri = 0;
}


bool Application::Run()
{
        //Use OpenGL 3.1 core
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,24);
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

        // Initialize video subsystem
        if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
        {
            // Display error message
            printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
            return false;
        }
        else
        {
            // Create window
            window = SDL_CreateWindow("SDL+OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,400,300,SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
            if( window == NULL )
            {
                // Display error message
                printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
                return false;
            }
            else
            {
                // Create OpenGL context
                glContext = SDL_GL_CreateContext(window);

                if( glContext == NULL )
                {
                    // Display error message
                    printf( "OpenGL context could not be created! SDL Error: %s\n", SDL_GetError() );
                    return false;
                }
                else
                {
                    SDL_GL_SetSwapInterval(1);
                    // Initialize glew
                    glewInit();
                }
            }
        }

        // Game loop
        while (!quit)
        {
            while(SDL_PollEvent(&sdlEvent))
            {
                if(sdlEvent.type == SDL_QUIT)
                {
                    quit = true;
                }
            }
            Draw();
            SDL_Delay(16);
        }

        //Destroy window
        SDL_DestroyWindow(window);
        window = NULL;

        //Quit SDL subsystems
        SDL_Quit();

        return true;
}

void Application::Draw()
{

     rtri++;
     if(rtri==360)
         rtri=0;
     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
     glLoadIdentity();
     glScaled(1.0f/10,1.0f/10,1.0f/10);
     glRotatef(rtri,0.0f,1.0f,0.0f);
     glBegin(GL_TRIANGLES);
     glColor3f(1.0f,0.0f,0.0f);
     glVertex3f( 0.0f, 4.0f, 0.0f);
     glColor3f(0.0f,1.0f,0.0f);
     glVertex3f(-4.0f,-4.0f, 4.0f);
     glColor3f(0.0f,0.0f,1.0f);
     glVertex3f( 4.0f,-4.0f, 4.0f);
     glColor3f(1.0f,0.0f,0.0f);
     glVertex3f( 0.0f, 4.0f, 0.0f);
     glColor3f(0.0f,0.0f,1.0f);
     glVertex3f( 4.0f,-4.0f, 4.0f);
     glColor3f(0.0f,1.0f,0.0f);
     glVertex3f( 4.0f,-4.0f, -4.0f);
     glColor3f(1.0f,0.0f,0.0f);
     glVertex3f( 0.0f, 4.0f, 0.0f);
     glColor3f(0.0f,1.0f,0.0f);
     glVertex3f( 4.0f,-4.0f, -4.0f);
     glColor3f(0.0f,0.0f,1.0f);
     glVertex3f(-4.0f,-4.0f, -4.0f);
     glColor3f(1.0f,0.0f,0.0f);
     glVertex3f( 0.0f, 4.0f, 0.0f);
     glColor3f(0.0f,0.0f,1.0f);
     glVertex3f(-4.0f,-4.0f,-4.0f);
     glColor3f(0.0f,1.0f,0.0f);
     glVertex3f(-4.0f,-4.0f, 4.0f);
     glEnd();

     SDL_GL_SwapWindow(window);
}

main函数

#include "Application.h"

int main(int argc, char *argv[])
{
    Application* app = new Application();
    app->Run();
    return 0;
}

运行结果

图片.png
上一篇下一篇

猜你喜欢

热点阅读