How do I make 3D work in pyglet?

I was trying to create using OpenGL, Python and pyglet, a flat triangle in a 3D space, I saw some tutorials on the internet, some videos on YouTube, and in the end I wrote this code down, the problem is that it did not work as I expected, I thought that if I tried to rotate, I would see the flat triangle rotating, and when I moved away from the scenario the Triangle did not have to decrease?

import pyglet
from pyglet.gl import *

config = Config(sample_buffers=1, samples=8)
tela = pyglet.window.Window(height=500, width=500, config=config)

glViewport(0,0,500,500)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(35,1,0.1,1000)
glMatrixMode(GL_MODELVIEW)

@tela.event
def on_draw():
    glBegin(GL_POLYGON)
    glVertex3f(10,10,0)
    glVertex3f(100,10,0)
    glVertex3f(50,100,0)
    glEnd()
    glFlush()

@tela.event
def on_key_press(s,m):
    tela.clear()
    if s == pyglet.window.key.W:
        glTranslatef(0,0,1)
    if s == pyglet.window.key.S:
        glTranslatef(0,0,-1)
    if s == pyglet.window.key.A:
        glRotatef(1,0,1,0)
    if s == pyglet.window.key.D:
        glRotatef(-1,0,1,0)

pyglet.app.run()

When I shave the code appears this:

insert the description of the image here

And when I try to rotate the scenario it happens:

insert the description of the image here

Does anyone know where I'm going wrong?

Author: Arthur Sally, 2019-01-08

1 answers

Solved, the problem is in pyglet, every time the page updates it goes through the function on_draw(), but the problem is that pyglet already defines a series of things when it goes through the function to draw the screen, which means that even if at the beginning of the code I put the commands:

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(35, 1, 0.1, 1000)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

Simply will not work, because the first time the screen is redesigned the pyglet will transform the image in 2D again(which sucks), ie the solution is that I have that put the codes for definition of 3D inside the function to redraw the triangle, so it sets the program to 3D every time and the 3D starts working, they only need to be included before the function draws the objects of the scenario.

And the function glViewport() is useless, it is only useful when I change the window size which I did not at any time so it was just a waste.

 0
Author: Arthur Sally, 2019-03-30 00:36:31