How integrate opengl hello world in panda3d

Hello, i view that i can to add in my python code a opengl code.
But i no found basic example, i want to integrate for example this opengl code in panda3d python code :

#include <GL/glut.h>

void displayMe(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.5, 0.0, 0.0);
        glVertex3f(0.5, 0.5, 0.0);
        glVertex3f(0.0, 0.5, 0.0);
    glEnd();
    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(300, 300);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Hello world :D");
    glutDisplayFunc(displayMe);
    glutMainLoop();
    return 0;
}

and this is my basic python code :

from math import sqrt
from panda3d.ode import OdeWorld, OdeSimpleSpace, OdeJointGroup
from panda3d.ode import OdeBody, OdeMass, OdeBoxGeom, OdePlaneGeom
from panda3d.core import BitMask32, CardMaker, Vec4, Quat
from random import randint, random
import threading
import time

from direct.showbase.ShowBase import ShowBase

class MyApp(ShowBase):
 
    def __init__(self):
        ShowBase.__init__(self)
 
if __name__ == '__main__':
    app = MyApp()
    app.run()

how can i make this ? i use glut library

I’m not entirely sure what you’re asking. Do you wish to convert this code to pure Panda code? Or do you wish to inject custom (py)OpenGL code into a Panda application?

For the latter, you can use draw callbacks. A sample program for them is in this post:

i want to injected opengl code in panda3d

for example i want to draw tringle like this in opengl : de-brauwer.be/wiki/wikka.php … Sierpinski
in my panda3d game

i view your link and i think that it’s i want. But i don’t understand how to draw my opengl code in panda3d window ?

That is shown in the sample.

Here is that sample adapted with the sierpinski code you linked:

from panda3d.core import *

from direct.directbase.DirectStart import *

from OpenGL.GL import *
import random
import sys


# Make sure we're running OpenGL.
if base.pipe.getInterfaceName() != 'OpenGL':
    print("This program requires OpenGL.")
    sys.exit(1)


def displayFun(cbdata):
    # Reset the transformation.
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glBegin(GL_POINTS)

    x=[0.0, 1.0, 0.5]
    y=[0.0, 0.0, 0.75]

    curx=0
    cury=0.5
    glVertex2f(curx,cury)
    for i in range(0,5000):
        idx=random.randint(0,2)
        curx=(curx+x[idx])/2.0
        cury=(cury+y[idx])/2.0
        glVertex2f(curx,cury)
    glEnd()

    # We have to upcall to the normal DisplayRegion handling, or
    # anything parented to render2d won't be drawn.
    cbdata.upcall()

# Set up a callback on the render2d DisplayRegion to draw the sierpinski.
base.cam2d.node().getDisplayRegion(0).setDrawCallback(displayFun)

base.run()

All I changed in the displayFun function was:

  1. I removed glFlush - it is not necessary and slows down rendering.
  2. I removed glClear otherwise anything else Panda renders would be blanked.
  3. I added code to reset the transformation from whatever Panda might have set it to.
  4. I changed the coordinates to be from 0-1 rather than in pixel coordinates to match Panda’s conventions.
  5. I reduced the number of iterations in the loop to make it less slow.

thank you very much for your example !
your code is simpler and I understand how to do it now!