程式語言 - Fox Toolkit - CPP - OpenGL 1.0 - Translate Position



參考資訊:
https://fox-toolkit.org

main.cpp

#include <fx.h>
#include <FXGLCanvas.h>
#include <FXGLVisual.h>
#include <GL/gl.h>
 
class MyGL : public FXMainWindow {
    FXDECLARE(MyGL)
 
    FXGLCanvas* canvas;
    bool is_inited;

public:
    enum {
        ID_PAINT = FXMainWindow::ID_LAST
    };
 
    MyGL() : canvas(nullptr) {
    }
 
    MyGL(FXApp* app) : FXMainWindow(app, "main", nullptr, nullptr, DECOR_ALL, 0, 0, 320, 240), is_inited(false) {
        FXGLVisual* visual = new FXGLVisual(app, VISUAL_DOUBLEBUFFER);
        canvas = new FXGLCanvas(this, visual, this, ID_PAINT, LAYOUT_FILL_X | LAYOUT_FILL_Y);
    }
 
    long onPaint(FXObject*, FXSelector, void*) {
        canvas->makeCurrent();

        if (is_inited == false) {
            GLdouble nearPlane = 0.1;
            GLdouble farPlane = 100.0;
            GLdouble top = nearPlane * tan(45.0 * 3.14159 / 360.0);
            GLdouble right = top * ((GLfloat)canvas->getWidth() / (GLfloat)canvas->getHeight());

            is_inited = true;
            glViewport(0, 0, canvas->getWidth(), canvas->getHeight());
            glMatrixMode(GL_PROJECTION);
            glFrustum(-right, right, -top, top, nearPlane, farPlane);
           
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
        }
 
        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();
 
        canvas->swapBuffers();
        return 1;
    }
 
    void create() override {
        FXMainWindow::create();
        show(PLACEMENT_SCREEN);
    }
};
 
FXDEFMAP(MyGL) map[] = {
    FXMAPFUNC(SEL_PAINT, MyGL::ID_PAINT, MyGL::onPaint)
};
 
FXIMPLEMENT(MyGL, FXMainWindow, map, ARRAYNUMBER(map))
 
int main(int argc, char* argv[]) {
    FXApp app("fox", "main");
 
    app.init(argc, argv);
    MyGL* window = new MyGL(&app);
    app.create();
 
    window->create();
 
    return app.run();
}

編譯、執行

$ g++ main.cpp -o main $(fox-config --cflags --libs) -lGL
$ ./main