C++与Opengl交互 Python与Opnegl交互(使用鼠标函数)
2017-11-13 20:02
441 查看
先放一个C++与Opengl交互的代码:
再来看一段Python实现同样功能的代码:
二者代码几乎一致,最后的结果却有点不一致,用Python实现的结果,只能显示一个点,第二个点直接就消失了,不知道是什么具体原因。
#include<GL/glut.h> GLsizei winWidth = 400, winHeight = 300; //display-window size void init(void) { glClearColor(0, 0, 1, 1); //设置显示的颜色为蓝色 glMatrixMode(GL_PROJECTION); //三维显示模式 gluOrtho2D(0,200,0,150); //二维显示框大小 } void displayFcn(void) { glClear(GL_COLOR_BUFFER_BIT); //将颜色显示在屏幕上 glColor3f(1, 0, 0); //设置点的颜色为红色 glPointSize(3); } void winReshapeFcn(GLint newWidth,GLint newHeight) { glViewport(0, 0, newWidth, newHeight); glMatrixMode(GL_PROJECTION); //三维显示模式 glLoadIdentity(); gluOrtho2D(0, GLdouble(newWidth), 0, GLdouble(newHeight)); winHeight = newHeight; winWidth = newWidth; } void plotPoint(GLint x, GLint y) { glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); } void mousePtPlot(GLint button, GLint action, GLint xMouse, GLint yMouse) { if (button==GLUT_LEFT_BUTTON&&action==GLUT_DOWN) { plotPoint(xMouse, winHeight - yMouse); } glFlush(); //强制清空所有缓存来执行OpenGL函数 } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(100, 100); glutInitWindowSize(winWidth, winHeight); glutCreateWindow("Mouse Plot Points"); init(); glutDisplayFunc(displayFcn); glutReshapeFunc(winReshapeFcn); glutMouseFunc(mousePtPlot); glutMainLoop(); }
再来看一段Python实现同样功能的代码:
from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * winWidth = 400 winHeight = 300 #display-window size def init(): glClearColor(0, 0, 1, 1) #设置显示的颜色为蓝色 glMatrixMode(GL_PROJECTION) #三维显示模式 gluOrtho2D(0,200,0,150) #二维显示框大小 def displayFcn(): glClear(GL_COLOR_BUFFER_BIT) #将颜色显示在屏幕上 glColor3f(1, 0, 0) #设置点的颜色为红色 glPointSize(3) def winReshapeFcn(newWidth,newHeight): glViewport(0, 0, newWidth, newHeight) glMatrixMode(GL_PROJECTION) #三维显示模式 glLoadIdentity() gluOrtho2D(0, newWidth, 0, newHeight) winHeight = newHeight winWidth = newWidth def plotPoint(x, y): glBegin(GL_POINTS) glVertex2i(x, y) glEnd() def mousePtPlot(button,action,xMouse,yMouse): if button==GLUT_LEFT_BUTTON and action==GLUT_DOWN: plotPoint(xMouse, winHeight - yMouse) glFlush() #强制清空所有缓存来执行OpenGL函数 def main(): glutInit() glutInitDisplayMode(GLUT_SINGLE or GLUT_RGB) glutInitWindowPosition(100, 100); glutInitWindowSize(winWidth, winHeight) glutCreateWindow("Mouse Plot Points".encode()) init() glutDisplayFunc(displayFcn) glutReshapeFunc(winReshapeFcn) glutMouseFunc(mousePtPlot) glutMainLoop(); main()
二者代码几乎一致,最后的结果却有点不一致,用Python实现的结果,只能显示一个点,第二个点直接就消失了,不知道是什么具体原因。
相关文章推荐
- python ctypes 探究 ---- python 与 c 的交互 近几天使用 python 与 c/c++ 程序交互,网上有推荐swig但效果都不理想,所以琢磨琢磨了 python 的 ct
- c++ python交互之boost.python 简集之普通函数
- OpenGL: 多边形分格化 和 gluTessCallback() 函数在c++中的使用方法
- c++调用python函数时,使用PyArray_SimpleNewFromData(nd, dims, typenum, data)函数时出现内存错误的问题
- c++ python交互之boost.python 简集之Vector(map)混合使用
- OpenGL: 多边形分格化 和 gluTessCallback() 函数在c++中的使用方法
- Python调用C++函数(SWIG,VS2013使用numpy.i完成Numpy与C++数组转换)
- c++ python交互之boost.python 简集之普通函数
- c++ python交互之boost.python 简集之Vector(map)混合使用
- 如何在C++中使用类似python的字符串split()函数
- Python、C++中编写函数以及不使用临时变量交换两变量值的比较
- C++中嵌入python程序——使用API接口,从函数到类
- c++中函数指针的使用方法
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- [C++] 使用const提高函数的健壮性
- C++ qsort example.C++ 快排函数使用示例
- 让C++和Python使用同一个枚举
- 使用OpenGL实现三维坐标的鼠标拣选(转)
- C++常用字符串处理函数及使用示例
- 在c++中qsort()排序函数的使用qsort函数应用大全