zoukankan      html  css  js  c++  java
  • SDL2+OpenGL (2)绘制立体图形

    绘制正四面体,即从四个面依次绘制三角形,其他立体图形方法类似

     1  #include <GL/glew.h>
     2  #include <SDL2/SDL.h>
     3  #include <SDL2/SDL_opengl.h>
     4  
     5  bool quit;
     6  SDL_Window* window;
     7  SDL_GLContext glContext;
     8  SDL_Event sdlEvent;
     9  GLfloat     rtri=0;
    10  void Init()
    11  {
    12      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    13      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    14      SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,24);
    15      SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    16      SDL_Init(SDL_INIT_EVERYTHING);
    17      window = SDL_CreateWindow("SDL+OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,400,300,SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
    18      glContext = SDL_GL_CreateContext(window);
    19      SDL_GL_SetSwapInterval(1);
    20      glewInit();
    21  }
    22  void Draw()
    23  {
    24      rtri++;
    25      if(rtri==360)
    26          rtri=0;
    27      glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    28      glLoadIdentity();
    29      glScaled(1.0f/10,1.0f/10,1.0f/10);
    30  
    31      glRotatef(rtri,0.0f,1.0f,0.0f);
    32      glBegin(GL_TRIANGLES);
    33          glColor3f(1.0f,0.0f,0.0f);
    34          glVertex3f( 0.0f, 4.0f, 0.0f);
    35          glColor3f(0.0f,1.0f,0.0f);
    36          glVertex3f(-4.0f,-4.0f, 4.0f);
    37          glColor3f(0.0f,0.0f,1.0f);
    38          glVertex3f( 4.0f,-4.0f, 4.0f);
    39          glColor3f(1.0f,0.0f,0.0f);
    40          glVertex3f( 0.0f, 4.0f, 0.0f);
    41          glColor3f(0.0f,0.0f,1.0f);
    42          glVertex3f( 4.0f,-4.0f, 4.0f);
    43          glColor3f(0.0f,1.0f,0.0f);
    44          glVertex3f( 4.0f,-4.0f, -4.0f);
    45          glColor3f(1.0f,0.0f,0.0f);
    46          glVertex3f( 0.0f, 4.0f, 0.0f);
    47          glColor3f(0.0f,1.0f,0.0f);
    48          glVertex3f( 4.0f,-4.0f, -4.0f);
    49          glColor3f(0.0f,0.0f,1.0f);
    50          glVertex3f(-4.0f,-4.0f, -4.0f);
    51          glColor3f(1.0f,0.0f,0.0f);
    52          glVertex3f( 0.0f, 4.0f, 0.0f);
    53          glColor3f(0.0f,0.0f,1.0f);
    54          glVertex3f(-4.0f,-4.0f,-4.0f);
    55          glColor3f(0.0f,1.0f,0.0f);
    56          glVertex3f(-4.0f,-4.0f, 4.0f);
    57      glEnd();
    58  
    59      SDL_GL_SwapWindow(window);
    60  }
    61  int main(int argc, char *argv[])
    62  {
    63      quit = false;
    64      Init();
    65      while (!quit)
    66      {
    67          while(SDL_PollEvent(&sdlEvent))
    68          {
    69              if(sdlEvent.type == SDL_QUIT)
    70              {
    71                  quit = true;
    72              }
    73          }
    74          Draw();
    75          SDL_Delay(16);
    76      }
    77      SDL_DestroyWindow(window);
    78      window = NULL;
    79      SDL_Quit();
    80      return 0;
    81  }
    
  • 相关阅读:
    Vector成员为指针时要注意的问题
    (转)C++中的static关键字的总结
    (转)内联(inline)函数与虚函数(virtual)的讨论
    基类类型和派生类型之间的转换以及虚函数
    (转) vector的reserve和resize
    关于函数返回值的一些见解
    (转)感受异或的神奇
    一个mysql不解的问题
    获取本机TCP连接状态信息
    (转)socket的SYN_RECV和ESTABLISHED状态
  • 原文地址:https://www.cnblogs.com/CodeMIRACLE/p/5517209.html
Copyright © 2011-2022 走看看