程式語言 - Simple DirectMedia Layer (SDL) - v1.2 - C/C++ - OpenGL 1.0 - Apply Scissor Test



參考資訊:
https://github.com/gamedev-net/nehe-opengl

main.c

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <GL/gl.h>

int main(int argc, char *argv[])
{
    const int W = 320;
    const int H = 240;

    GLdouble nearPlane = 0.1;
    GLdouble farPlane = 100.0;
    GLdouble top = nearPlane * tan(45.0 * 3.14159 / 360.0);
    GLdouble right = top * ((GLfloat)W / (GLfloat)H);

    SDL_Surface *screen = NULL;

    SDL_Init(SDL_INIT_VIDEO);
    screen = SDL_SetVideoMode(W, H, 16, SDL_SWSURFACE | SDL_OPENGL);

    glViewport(0, 0, W, H);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-right, right, -top, top, nearPlane, farPlane);

    glMatrixMode(GL_MODELVIEW);

    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_SCISSOR_TEST);

    glScissor(0, 120, 160, 120);
    glClearColor(1, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glScissor(160, 120, 160, 120);
    glClearColor(0, 1, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glScissor(0, 0, 160, 120);
    glClearColor(0, 0, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glScissor(160, 0, 160, 120);
    glClearColor(1, 1, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glDisable(GL_SCISSOR_TEST);

    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -5.0f);

    glBegin(GL_TRIANGLES);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(0.0f, 0.5f, 0.0f);

        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-0.5f, -0.5f, 0.0f);

        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(0.5f, -0.5f, 0.0f);
    glEnd();

    SDL_GL_SwapBuffers();
    SDL_Delay(3000);

    SDL_Quit();

    return 0;
}

編譯、執行

$ gcc main.c -I/usr/include/SDL -o main -lSDL -lGL -lGLU
$ ./main