刚才发现了glut的一个小细节!

博客提到点击鼠标时,glutPassiveMotionFunc()似乎比glutMouseFunc()先执行,博主刚得出此结论,但不确定是否正确,围绕信息技术中鼠标事件函数执行顺序展开探讨。
当你点击鼠标的时候,glutPassiveMotionFunc()居然比glutMouseFunc()先执行!刚得出的结论,不知对不对。
12.cpp 定义控制台应用程序的入口点。 #include stdafx.h #include GLglut.h void DrawLine(int x0,int y0,int x1,int y1,double color_r,double color_g,double color_b)声明了7个变量 { 绘制实线 glColor3f(color_r,color_g,color_b);设置颜色,且分别用刚才的三个字段代表 glBegin(GL_LINES);声明要绘制的是【线】 glVertex2f(x0,y0);指定了一个点的坐标 glVertex2f(x1,y1); glEnd(); 绘制虚线 glEnable(GL_LINE_STIPPLE);开始定义点画模式 glLineStipple(1,0xF0F0);【前】是重复因子;【后】是由1和0组成的16位序列,如果模型中对应的位是1,则绘制这个像素,否则不绘制。 glBegin(GL_LINES); glVertex2f(x0,y0+50);y偏移+50 glVertex2f(x1,y1+50); glEnd(); glDisable(GL_LINE_STIPPLE);结束此模式 绘制点划线 glEnable(GL_LINE_STIPPLE); glLineStipple(1,0xFE10); glBegin(GL_LINES); glVertex2f(x0,y0-50);y偏移-50 glVertex2f(x1,y1-50); glEnd(); glDisable(GL_LINE_STIPPLE); } void Display() { glClear(GL_COLOR_BUFFER_BIT);通过glClear现实背景颜色 DrawLine(50,150,200,150,0.9921875f,0.26171875f,0.39453125f);【50,150】初始的坐标,【200,150】结束的坐标,后面三个为颜色 glFlush();强制刷新 } void ChangeSize(GLsizei w,GLsizei h) { if(h==0) { h=1; } glViewport(0,0,w,h);设置窗口的宽度为【w】,长度为【H】 glMatrixMode(GL_PROJECTION);设定为投影矩阵 glLoadIdentity();重置当前矩阵为单位矩阵 if (w=h) { glOrtho(0.0f,250.f,0.0f,250.0fhw,1.0f,-1.0f); } else { glOrtho(0.0f,250.0fwh,0.0f,250.0f,1.0f,-1.0f); } glMatrixMode(GL_MODELVIEW);设定为模型视图矩阵 glLoadIdentity();重置当前矩阵为单位矩阵 } void Setup() { glClearColor(1.0f,1.0f,1.0f,1.0f);指定颜色为不透明的白色 } int _tmain(int argc, _TCHAR argv[]) { glutInitDisplayMode(GLUT_SINGLEGLUT_RGB);//设置显示模式为单缓冲和RGB颜色模式 glutCreateWindow(实线、虚线、点划线);//窗口的名字为【实线、虚线、点划线】 glutDisplayFunc(Display);//调用Display函数 glutReshapeFunc(ChangeSize);//当窗口大小变化时,调用ChangeSize函数 Setup(); glutMainLoop(); return 0; } 帮我分析一下这段代码的,目的,要求,存在的问题和感想(用初学者的视角用大白话总结几句话)
11-16
使用OpenGL和GLUT库用C++编写一个简易计算器,可按以下步骤进行: ### 1. 初始化GLUT和OpenGL 首先,要初始化GLUT库并创建一个窗口,同时对OpenGL进行必要的设置。 ```cpp #include <GL/glut.h> #include <iostream> #include <string> #include <sstream> #include <cmath> // 全局变量 std::string input = ""; double num1 = 0, num2 = 0; char op = '\0'; bool waitingForSecondNum = false; // 初始化OpenGL设置 void init() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); gluOrtho2D(0, 400, 0, 400); } ``` ### 2. 绘制计算器界面 绘制计算器的按钮和显示区域。 ```cpp // 绘制文本 void drawText(float x, float y, const std::string& text) { glRasterPos2f(x, y); for (char c : text) { glutBitmapCharacter(GLUT_BITMAP_8_BY_13, c); } } // 绘制按钮 void drawButton(int x, int y, int width, int height, const std::string& label) { glBegin(GL_QUADS); glColor3f(0.5f, 0.5f, 0.5f); glVertex2i(x, y); glVertex2i(x + width, y); glVertex2i(x + width, y + height); glVertex2i(x, y + height); glEnd(); drawText(x + width / 2 - 5, y + height / 2 - 5, label); } // 绘制计算器界面 void display() { glClear(GL_COLOR_BUFFER_BIT); // 绘制显示区域 glBegin(GL_QUADS); glColor3f(1.0f, 1.0f, 1.0f); glVertex2i(50, 300); glVertex2i(350, 300); glVertex2i(350, 350); glVertex2i(50, 350); glEnd(); // 显示输入内容 drawText(60, 310, input); // 绘制按钮 drawButton(50, 200, 50, 50, "7"); drawButton(110, 200, 50, 50, "8"); drawButton(170, 200, 50, 50, "9"); drawButton(230, 200, 50, 50, "/"); drawButton(50, 140, 50, 50, "4"); drawButton(110, 140, 50, 50, "5"); drawButton(170, 140, 50, 50, "6"); drawButton(230, 140, 50, 50, "*"); drawButton(50, 80, 50, 50, "1"); drawButton(110, 80, 50, 50, "2"); drawButton(170, 80, 50, 50, "3"); drawButton(230, 80, 50, 50, "-"); drawButton(50, 20, 50, 50, "0"); drawButton(110, 20, 50, 50, "."); drawButton(170, 20, 50, 50, "="); drawButton(230, 20, 50, 50, "+"); glutSwapBuffers(); } ``` ### 3. 处理鼠标点击事件 处理用户的鼠标点击事件,根据点击的按钮更新输入内容。 ```cpp // 处理鼠标点击事件 void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { y = 400 - y; // 转换坐标系 // 检查点击的按钮 if (x >= 50 && x <= 100 && y >= 200 && y <= 250) input += "7"; if (x >= 110 && x <= 160 && y >= 200 && y <= 250) input += "8"; if (x >= 170 && x <= 220 && y >= 200 && y <= 250) input += "9"; if (x >= 230 && x <= 280 && y >= 200 && y <= 250) { if (!input.empty()) { num1 = std::stod(input); op = '/'; waitingForSecondNum = true; input = ""; } } if (x >= 50 && x <= 100 && y >= 140 && y <= 190) input += "4"; if (x >= 110 && x <= 160 && y >= 140 && y <= 190) input += "5"; if (x >= 170 && x <= 220 && y >= 140 && y <= 190) input += "6"; if (x >= 230 && x <= 280 && y >= 140 && y <= 190) { if (!input.empty()) { num1 = std::stod(input); op = '*'; waitingForSecondNum = true; input = ""; } } if (x >= 50 && x <= 100 && y >= 80 && y <= 130) input += "1"; if (x >= 110 && x <= 160 && y >= 80 && y <= 130) input += "2"; if (x >= 170 && x <= 220 && y >= 80 && y <= 130) input += "3"; if (x >= 230 && x <= 280 && y >= 80 && y <= 130) { if (!input.empty()) { num1 = std::stod(input); op = '-'; waitingForSecondNum = true; input = ""; } } if (x >= 50 && x <= 100 && y >= 20 && y <= 70) input += "0"; if (x >= 110 && x <= 160 && y >= 20 && y <= 70) input += "."; if (x >= 170 && x <= 220 && y >= 20 && y <= 70) { if (!input.empty() && waitingForSecondNum) { num2 = std::stod(input); switch (op) { case '+': input = std::to_string(num1 + num2); break; case '-': input = std::to_string(num1 - num2); break; case '*': input = std::to_string(num1 * num2); break; case '/': if (num2 != 0) input = std::to_string(num1 / num2); else input = "Error"; break; } waitingForSecondNum = false; } } if (x >= 230 && x <= 280 && y >= 20 && y <= 70) { if (!input.empty()) { num1 = std::stod(input); op = '+'; waitingForSecondNum = true; input = ""; } } glutPostRedisplay(); } } ``` ### 4. 主函数 在主函数中初始化GLUT和OpenGL,并进入主循环。 ```cpp // 主函数 int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(400, 400); glutCreateWindow("Simple Calculator"); init(); glutDisplayFunc(display); glutMouseFunc(mouse); glutMainLoop(); return 0; } ``` ### 代码解释 - `init()`函数:对OpenGL的清屏颜色和投影模式进行设置。 - `display()`函数:负责绘制计算器的界面,包含显示区域和按钮。 - `mouse()`函数:处理鼠标点击事件,依据点击的按钮更新输入内容,并且在用户点击“=”时进行计算。 - `main()`函数:初始化GLUT和OpenGL,注册回调函数,然后进入主循环。 ### 编译和运行 把上述代码保存为一个`.cpp`文件,例如`calculator.cpp`,接着使用以下命令进行编译: ```sh g++ calculator.cpp -o calculator -lGL -lGLU -lglut ``` 运行生成的可执行文件: ```sh ./calculator ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值