How do I make 3D work on pyglet?

Asked

Viewed 214 times

0

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 there, the problem is that it didn’t work as I expected, I thought if I tried to spin, I could see the flat triangle spinning, and when I moved the scenario away the triangle didn’t 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 spin the code this appears:

inserir a descrição da imagem aqui

And when I try to spin the scenery it happens:

inserir a descrição da imagem aqui

Does anyone know where I’m going wrong?

  • add code as text, not as image. This makes it easier to help you.

1 answer

0


I 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 number of things when it goes through the function to draw the screen, which means that even at the beginning of the code I put the commands:

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

It’s just not going to work, because the first moment the screen is redesigned the pyglet will turn the image into 2D again (which sucks), so the solution is that I have to put the codes for 3D definition inside the function to redesign the triangle, so he sets the program to 3D every time and the 3D starts working, they just need to be included before the function draws the objects of the scenario.

And the function glViewport() It’s useless, it’s only useful when I change the window size which I didn’t do at any time so it was just a waste.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.