您的位置:首页 > 编程语言 > C语言/C++

C++与Opengl交互 Python与Opnegl交互(使用鼠标函数)

2017-11-13 20:02 441 查看
先放一个C++与Opengl交互的代码:

#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 opengl