Is there any way to call OpenGL code writing by c++ in python via pybind11?

Viewed 82

I've writed a toy example using OpenGL and c++

#include <GL/glut.h>

void init(void)
{
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glMatrixMode(GL_PROJECTION);
  glOrtho(-5, 5, -5, 5, 5, 15);
  glMatrixMode(GL_MODELVIEW);
  gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
  return;
}

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f(1.0, 0, 0);
  glutWireTeapot(3);
  glFlush();
  return;
}

void test()
{
  int argc = 0;
  char **argv=NULL;
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(300, 300);
  glutCreateWindow("OpenGL 3D View");
  init();
  glutDisplayFunc(display);
  glutMainLoop();
}

And then build the code using

gcc -o test test.c -lGL -lGLU -lglut

It works fine. But I want to call this function in Python, than I've tried

# include <pybind11/pybind11.h>
...

PYBIND11_MODULE(py2cpp, m)
{
  m.doc() = "opengl test";
  m.def("display", &test, "test opengl");
}

Build code above using

g++ -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` -lGL -lGLU -lglut test.cpp -o py2cpp`python3-config --extension-suffix` -I /home/xiaohanbao/anaconda3/envs/ros/include/python3.8

I compiled the code successfully. But when I import the library using import py2cpp, I got

Import Error: /path/py2cpp.cpython-38-x86_64-linux-gnu.so:  undefined symbol: glutInitWindowPosition

Is there any way to fix it?

0 Answers
Related