跳到正文

个人记录

C++ openGL

Kevin_Wa约 9 分钟
原文
文章信息与版本

文章信息

发布
2023年12月8日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 9 分钟

版本

  1. v1当前

OpenGL

前置

  • 接受参数
C++
glutInit(&argc,argv);
  • 设置显示方式
C++
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
//GLUT_SINGLE 单缓冲窗口
//GLUT_RGB 表示使用RGB颜色模式
  • 设置像素
C++
 glutInitWindowSize(500, 500);
 
  • 创建一个窗口
C++
  glutCreateWindow("Moving Car with OpenGL");
  • 设置清除颜色缓冲区时使用的颜色
C++
 glClearColor(1.0, 1.0, 1.0, 1.0);

红色(Red): 第一个参数1.0表示红色通道的最大强度。
绿色(Green): 第二个参数1.0表示绿色通道的最大强度。
蓝色(Blue): 第三个参数1.0表示蓝色通道的最大强度。
Alpha(Alpha通道/不透明度): 第四个参数1.0表示完全不透明。
  • 设置了坐标系的边界
C++
    gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
            x1   x2   y1   y2
    相对坐标,以(0,0) 为中心
    并且拉伸到像素一样
  • 显示
C++
glutDisplayFunc(display);

void display() {
    glClear(GL_COLOR_BUFFER_BIT);  //清空原来
    car->draw();
    glFlush();              //执行命令
}
  • 定时器
C++
void timer(int value) {
    (void)value;
    car->move(0.01); // Move the car a little to the right
    glutPostRedisplay(); // Redraw the scene
    glutTimerFunc(16, timer, 0); // Schedule next timer event, ~60Hz
}
glutTimerFunc(0, timer, 0); 

glutTimerFunc(0, timer, 0); 这行代码位于main函数中,它设置了一个计时器,几乎立即触发。参数解释如下:

第一个参数0指定计时器延迟的时间,单位是毫秒。这里设置为0,意味着计时器会尽可能快地触发。
第二个参数timer是当计时器触发时调用的函数。在这个例子中,它指向了你定义的timer函数。
第三个参数0是传递给timer函数的整数值。这个值可以用来传递信息,但在这个例子中没有被使用。
void timer(int value) 函数是计时器回调函数的定义。每次计时器触发时,这个函数都会被调用。函数体解释如下:

(void)value; 这行代码是对value参数的显式忽略。这样做通常是为了避免编译器警告关于未使用的参数。
car->move(0.01); 调用Car类的move方法来移动汽车。这里汽车每次向右移动0.01单位。
glutPostRedisplay(); 请求GLUT重新绘制窗口,这将导致display函数被调用。
glutTimerFunc(16, timer, 0); 再次设置计时器,以实现连续的动画效果。这里的延迟设置为16毫秒,大约对应于60Hz的刷新率(1000ms/16ms ≈ 60次/秒)。
  • 开始GLUT的主循环
C++
glutMainLoop();

画一辆车

C++
#include <GL/glut.h>
#include <cmath>

class Car {
public:
    float x,y; // Position of the car
    float width,height; // Size of the car
    Car(float x,float y,float width,float height):x(x),y(y),width(width),height(height){}
    void draw() {
        // Draw car body
        glEnable(GL_BLEND);  //模糊处理
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        glColor4f(0.57f,0.54f,0.99f,0.80f);
        glBegin(GL_POLYGON);
        glVertex2f(x,y+height);glVertex2f(x+width*0.8,y+height);
        glVertex2f(x+width*0.65,y+height*1.8);glVertex2f(x+width*0.05,y+height*1.8);
        glEnd();
        glColor4f(0.88f,0.90f,0.34f,0.70f); // Blue color
        glRectf(x,y,x+width,y+height);
        // Draw wheels
        glColor4f(0.99f,0.56f,0.55f,0.70f); // Red color
        float wheelRadius=height*0.3;
        drawCircle(x+width/4,y,wheelRadius);
        drawCircle(x+3*width/4,y,wheelRadius);
    }

    void drawCircle(float cx,float cy,float r) {
        glBegin(GL_POLYGON);
        for (int i=0;i<360;i++) {
            float theta=i*3.1415926f/180.0f;
            float x=r*cosf(theta);
            float y=r*sinf(theta);
            glVertex2f(x+cx,y+cy);
        }
        glEnd();
    }

    void move(float dx) {
        x+=dx;
        // Wrap around the screen
        if (x>1.0) x=-1.0-width;
        if (x<-1.0-width) x=1.0;
    }
};
class CarAnimator {
public:
    CarAnimator(Car* car) : carPtr(car) {}
    void display() {
        glClear(GL_COLOR_BUFFER_BIT);
        if (carPtr) {
            carPtr->draw();
        }
        glFlush();
    }
    void timer(int value) {
        if (carPtr) {
            carPtr->move(0.01);
            glutPostRedisplay();
        }
        glutTimerFunc(16, CarAnimator::timerWrapper, value);
    }
    static void setInstance(CarAnimator* instance) {
        CarAnimator::instance=instance;
    }
    static void displayWrapper() {
        if (instance) {
            instance->display();
        }
    }
    static void timerWrapper(int value) {
        if (instance) {
            instance->timer(value);
        }
    }
private:
    Car* carPtr;
    static CarAnimator* instance;
};

CarAnimator* CarAnimator::instance = nullptr;

int main(int argc, char** argv) {
    Car* car = new Car(-0.5, -0.5, 1.0, 0.25);
    CarAnimator animator(car);

    CarAnimator::setInstance(&animator);

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Moving Car with OpenGL");

    glClearColor(1.0, 1.0, 1.0, 1.0); // White background
    gluOrtho2D(-1.0, 1.0, -1.0, 1.0);

    glutDisplayFunc(CarAnimator::displayWrapper);
    glutTimerFunc(0, CarAnimator::timerWrapper, 0); // Initial call to timer
    glutMainLoop();

    delete car;

    return 0;
}
原文
存档评论1 条抓取于