程式語言 - X Window (X11) - C/C++ - OpenGL 1.0 - Translate Position



參考資訊:
https://www.winehq.org/
https://github.com/gamedev-net/nehe-opengl
https://wiki.winehq.org/Winelib_User%27s_Guide

main.c

#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glu.h>

int main(int argc, char *argv[])
{
    const int width = 320;
    const int height = 240;
    int screen = 0;
    Window win = { 0 };
    Colormap cmap = { 0 };
    GLXContext ctx = { 0 };
    XVisualInfo *vi = NULL;
    Display *display = NULL;
    XSetWindowAttributes attr = { 0 };
    int visual[] = {
        GLX_RGBA,
        GLX_DOUBLEBUFFER, 
        GLX_RED_SIZE, 8, 
        GLX_GREEN_SIZE, 8, 
        GLX_BLUE_SIZE, 8, 
        GLX_ALPHA_SIZE, 8,
        None
    };
  
    display = XOpenDisplay(NULL);
    screen = DefaultScreen(display);
    vi = glXChooseVisual(display, screen, visual);
    ctx = glXCreateContext(display, vi, 0, GL_TRUE);
    cmap = XCreateColormap(display, RootWindow(display, vi->screen), vi->visual, AllocNone);

    attr.border_pixel = 0;
    attr.colormap = cmap;
    attr.event_mask = ExposureMask;
    win = XCreateWindow(display, RootWindow(display, vi->screen), 0, 0, width, height, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel | CWColormap, &attr);
    XStoreName(display, win, "main");
    XMapWindow(display, win);
    XSync(display, False);
    glXMakeCurrent(display, win, ctx);

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

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glFrustum(-right, right, -top, top, nearPlane, farPlane);
    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity(); 
    glTranslatef(-1.5f, 0.0f, -10.0f);
    glBegin(GL_TRIANGLES);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();
   
    glLoadIdentity(); 
    glTranslatef(1.5f, 0.0f, -5.0f);
    glBegin(GL_TRIANGLES);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 0.0f);
    glEnd();

    glBegin(GL_TRIANGLES);
        glVertex3f(0.0f, 0.5f, 0.0f);
        glVertex3f(-0.5f, -0.5f, 0.0f);
        glVertex3f(0.5f, -0.5f, 0.0f);
    glEnd();

    glXSwapBuffers(display, win);
    sleep(3);
 
    glXMakeCurrent(display, None, NULL);
    glXDestroyContext(display, ctx);
    XDestroyWindow(display, win);
    XFreeColormap(display, cmap);
    XFree(vi);
    XCloseDisplay(display);
  
    return 0;
}

編譯、執行

$ gcc main.c -o main -lX11 -lGL -lGLU
$ ./main