2 Replies - 1421 Views - Last Post: 21 April 2013 - 07:28 PM Rate Topic: -----

#1 sillygod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-January 13

draw a cube using pyglet--hava a problem

Posted 21 April 2013 - 08:45 AM

I am an openGL and pyglet newbie. As the topic, I have no idea why I can't see anything but a black background window. Anyone can teach me were is the error.

here is my code. code link

the following is alse my code. Read my code in the way you like :)/>/>

import pyglet
from pyglet.gl import *
from pyglet import clock, window


def vector(type, *args):
    '''
        return a ctype array
        GLfloat
        GLuint
        ...
    '''
    return (type*len(args))(*args)



class model:
    def __init__(self, vertices, colorMatrix, indice):
        self.vertices = vertices
        self.colorMatrix = colorMatrix
        self.indice = indice

    def draw(self):
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        glEnableClientState(GL_COLOR_ARRAY)
        glEnableClientState(GL_VERTEX_ARRAY)
        
        glColorPointer(3, GL_FLOAT, 0, vector(GLfloat, *self.colorMatrix))
        glVertexPointer(3, GL_FLOAT, 0, vector(GLfloat, *self.vertices))
        glDrawElements(GL_QUADS, len(self.indice), GL_UNSIGNED_INT, vector(GLuint, *self.indice))

        glDisableClientState(GL_COLOR_ARRAY)
        glDisableClientState(GL_VERTEX_ARRAY)



class world:
    def __init__(self):
        self.element = []

    def addModel(self, model):
        self.element.append(model)

    def draw(self):
        for obj in self.element:
            obj.draw()


        


class camera:
    def __init__(self, win):
        self.win = win

    def worldProjection(self):
        glViewport(0, 0, self.win.width, self.win.height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(60.0, self.win.width/self.win.height, 0.1, 1000.0)
        glMatrixMode(GL_MODELVIEW)


class App:
    def __init__(self):
        self.world = world()

        self.win = window.Window(fullscreen=False, vsync=True)
        self.camera = camera(self.win)
        clock.set_fps_limit(30)

    def setup(self):
        #glClearColor(0.2, 0.2, 0.2, 0.8)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)


        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)

        cube = (
            1, 1, 1, #0
            -1, 1, 1, #1
            -1, -1, 1, #2
            1, -1, 1, #3
            1, 1, -1, #4
            -1, 1, -1, #5
            -1, -1, -1, #6
            1, -1, -1 #7
        )

        cube = [x*100 for x in cube ]

        color = (
            1, 1, 1,
            0, 1, 1,
            0, 0, 1,
            1, 0, 1,
            1, 0, 0,
            0, 0, 0,
            0, 1, 0,
            1, 1, 0
        )

        indice = (
            0, 1, 2, 3, # front face
            4, 5, 1, 0, # top face
            0, 3, 7, 4, # right face
            1, 5, 6, 2, # left face
            2, 3, 7, 6, # bottom face
            4, 5, 6, 7 # back face 
        )

        obj = model(cube, color, indice)

        self.world.addModel(obj)

    def run(self):
        while not self.win.has_exit:
            #TODO set cube and draw
            self.win.dispatch_events()
            # go to search what's this
            self.camera.worldProjection() 
            self.world.draw()
            clock.tick()
            self.win.flip()

app = App()
app.setup()
app.run()




Is This A Good Question/Topic? 0
  • +

Replies To: draw a cube using pyglet--hava a problem

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: draw a cube using pyglet--hava a problem

Posted 21 April 2013 - 09:27 AM

I am fairly new to OpenGL myself, and I use pygame+pyOpenGL rather than pyglet. That said, from what I understand glEnableClientState/glDisableClientState are part of the fixed-function pipeline and should really be avoided. Where are you getting your code from?

I recommend you take a look at Learning Modern 3D Graphics Programming. All the code is given in C++ which may or may not be a problem, but the content is still very valuable. By the time you finish the first chapter you should be convinced of the evils of the fixed-function pipeline.

I have been working to convert the tutorials into python as best I can in my attempt to teach myself OpenGL. I have so far done chapters 1-6. As said, they use pygame/pyopengl but converting them to pyglet should be no problem.
GLtut for Python

-Mek
Was This Post Helpful? 0
  • +
  • -

#3 sillygod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-January 13

Re: draw a cube using pyglet--hava a problem

Posted 21 April 2013 - 07:28 PM

View PostMekire, on 21 April 2013 - 09:27 AM, said:

I am fairly new to OpenGL myself, and I use pygame+pyOpenGL rather than pyglet. That said, from what I understand glEnableClientState/glDisableClientState are part of the fixed-function pipeline and should really be avoided. Where are you getting your code from?

I recommend you take a look at Learning Modern 3D Graphics Programming. All the code is given in C++ which may or may not be a problem, but the content is still very valuable. By the time you finish the first chapter you should be convinced of the evils of the fixed-function pipeline.

I have been working to convert the tutorials into python as best I can in my attempt to teach myself OpenGL. I have so far done chapters 1-6. As said, they use pygame/pyopengl but converting them to pyglet should be no problem.
GLtut for Python

-Mek


Hello Mekire.
Thanks for your reply. I write this code by myself but I read this site as my reference. About your recommendation, I will take a look at it :)
However, I still wonder why I can't see the correct picture :(

Regards,
sillygod

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1