Steward
分享是一種喜悅、更是一種幸福
程式語言 - Qt - v6.0 - OpenGL 1.0 - Translate Position
參考資訊:
https://openhome.cc/Gossip/Qt4Gossip/FirstQt.html
https://stackoverflow.com/questions/8995399/error-qapplication-no-such-file-or-directory
main.cpp
#include <math.h>
#include <QApplication>
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QSurfaceFormat>
#include <QOpenGLContext>
#include <GL/gl.h>
#define W 320
#define H 240
class MyGL : public QOpenGLWidget {
protected:
void initializeGL() override {
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);
glMatrixMode(GL_PROJECTION);
glFrustum(-right, right, -top, top, nearPlane, farPlane);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void resizeGL(int w, int h) override {
glViewport(0, 0, w, h);
}
void paintGL() override {
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();
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
setenv("MESA_GL_VERSION_OVERRIDE", "3.2COMPAT", 1);
QSurfaceFormat format;
format.setVersion(3, 2);
format.setProfile(QSurfaceFormat::CompatibilityProfile);
QSurfaceFormat::setDefaultFormat(format);
MyGL window;
window.resize(W, H);
window.show();
return app.exec();
}
編譯、執行
$ g++ main.cpp -o main $(pkg-config --cflags --libs Qt6Widgets Qt6OpenGLWidgets) -lGL $ ./main