當前位置: 首頁>>代碼示例>>Python>>正文


Python Program.validate方法代碼示例

本文整理匯總了Python中program.Program.validate方法的典型用法代碼示例。如果您正苦於以下問題:Python Program.validate方法的具體用法?Python Program.validate怎麽用?Python Program.validate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在program.Program的用法示例。


在下文中一共展示了Program.validate方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: from program import Program [as 別名]
# 或者: from program.Program import validate [as 別名]
class GameObject:
    def __init__(self, *args, **kwargs):
        self.program = Program()
        self.vao = GLuint(0)
        self.vbo = GLuint(0)
        self.ebo = GLuint(0)

        # Create Vertex Array Object
        glGenVertexArrays(1, self.vao)

        # Create a Vertex Buffer Object
        glGenBuffers(1, self.vbo)

        # Create an element array
        glGenBuffers(1, self.ebo)

        # use the vao
        glBindVertexArray(self.vao)

        width = float(100)
        height = float(100)
        r = 1.0
        g = 0.0
        b = 0.0

        # self.vertices = [
        #     0.0, 0.0, r, g, b, # Top-left
        #     0.0 + width, 0.0, r, g, b, # Top-right
        #     0.0 + width, 0.0 + height, r, g, b, # Bottom-right
        #     0.0, 0.0 + height, r, g, b  # Bottom-left
        # ]
        # self.vertices_gl = (GLfloat * len(self.vertices))(*self.vertices)

        self.vertices = [
            0.0, 0.0,
            0.0 + width, 0.0,
            0.0 + width, 0.0 + height,
            0.0, 0.0 + height,
        ]
        self.vertices_gl = (GLfloat * len(self.vertices))(*self.vertices)

        glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
        glBufferData(GL_ARRAY_BUFFER, sizeof(self.vertices_gl), self.vertices_gl, GL_STATIC_DRAW)

        self.elements = [
            0, 1, 2,
            2, 3, 0
        ]
        self.elements_gl = (GLuint * len(self.elements))(*self.elements)

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ebo)
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(self.elements_gl), self.elements_gl, GL_STATIC_DRAW)

        self.program.compile_shader_from_string(
            """
            layout (location = 0) in vec2 position;
            uniform mat4 proj;
            uniform mat4 view;
            uniform mat4 model;

            void main()
            {
                gl_Position = proj * view * model * vec4(position, 0.0, 1.0);
            }
            """, 'vertex')

        self.program.compile_shader_from_string(
            """
            void main()
            {
                gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
            }
            """, 'fragment')

        if not self.program.link():
            return

        if not self.program.validate():
            return

        self.program.bind()

        # Specify the layout of the vertex data
        position_attribute = glGetAttribLocation(self.program.get_handle(), "position")
        if position_attribute > -1:
            glEnableVertexAttribArray(position_attribute)
            glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0)

        # color_attribute = glGetAttribLocation(self.program.get_handle(), "color")
        # if color_attribute > -1:
        #     glEnableVertexAttribArray(color_attribute)
        #     glVertexAttribPointer(color_attribute, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 2 * sizeof(GLfloat))

        # stop the vao
        glBindVertexArray(0)

    # def new_look_at(self, eye, at, up):
    #     z = (eye - at).normalized()
    #     x = up.cross(z).normalized()
    #     y = z.cross(x)
#.........這裏部分代碼省略.........
開發者ID:davidejones,項目名稱:spaceinvaders,代碼行數:103,代碼來源:_gameobject.py

示例2: Mesh

# 需要導入模塊: from program import Program [as 別名]
# 或者: from program.Program import validate [as 別名]
class Mesh(GameObject):

    def __init__(self):
        self.program = Program()
        self.vao = GLuint(0)
        self.vbuf = None
        self.ibuf = None
        self.vertices = []
        self.normals = []
        self.indices = []
        self.uvs = []
        # we should really be getting the camera not creating a new instance..
        self.camera = Camera(800, 600)
        GameObject.__init__(self)

    def set_data(self, *args, **kwargs):
        self.vertices = kwargs.get('vertices', [])
        self.normals = kwargs.get('normals', [])
        self.indices = kwargs.get('indices', [])
        self.uvs = kwargs.get('uvs', [])
        self.colors = kwargs.get('colors', [])

        # create a vao
        glGenVertexArrays(1, self.vao)
        # use it
        glBindVertexArray(self.vao)
        # create a vertexbuffer
        if self.vertices:
            self.vbuf = VertexBuffer(self.vertices)
            #self.vbuf.set_attribute(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0)
            #self.vbuf.set_attribute(1, 2, GL_FLOAT, GL_FALSE, 2, 0)
        # create an indexbuffer
        if self.indices:
            self.ibuf = IndexBuffer(self.indices)

        self.program.compile_shader_from_string(
            """
            layout (location = 0) in vec2 position;
            layout (location = 1) in vec3 color;
            uniform mat4 proj;
            uniform mat4 view;
            uniform mat4 model;
            smooth out vec3 theColor;

            void main()
            {
                theColor = color;
                gl_Position = proj * view * model * vec4(position, 0.0, 1.0);
            }
            """, 'vertex')

        self.program.compile_shader_from_string(
            """
            smooth in vec3 theColor;
            void main()
            {
                gl_FragColor = vec4(theColor, 1.0);
            }
            """, 'fragment')
        self.program.link()
        self.program.validate()
        self.program.bind()

        #self.vbuf.set_attribute(self.program.get_handle(), "position", 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0)
        #self.vbuf.set_attribute(self.program.get_handle(), "color", 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0)

        # Specify the layout of the vertex data
        position_attribute = glGetAttribLocation(self.program.get_handle(), "position")
        if position_attribute > -1:
            glEnableVertexAttribArray(position_attribute)
            glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0)

        color_attribute = glGetAttribLocation(self.program.get_handle(), "color")
        if color_attribute > -1:
            glEnableVertexAttribArray(color_attribute)
            glVertexAttribPointer(color_attribute, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 2 * sizeof(GLfloat))

        # stop the vao
        glBindVertexArray(0)

    def render(self):
        GameObject.render(self)

        self.program.bind()

        view_location = glGetUniformLocation(self.program.get_handle(), "view")
        if view_location > -1:
            v = self.camera.view
            v = v[:]
            v_ctype = (GLfloat * len(v))(*v)
            glUniformMatrix4fv(view_location, 1, GL_FALSE, v_ctype)

        proj_location = glGetUniformLocation(self.program.get_handle(), "proj")
        if proj_location > -1:
            p = self.camera.projection
            p = p[:]
            p_ctype = (GLfloat * len(p))(*p)
            glUniformMatrix4fv(proj_location, 1, GL_FALSE, p_ctype)

        model_location = glGetUniformLocation(self.program.get_handle(), "model")
#.........這裏部分代碼省略.........
開發者ID:davidejones,項目名稱:spaceinvaders,代碼行數:103,代碼來源:mesh.py


注:本文中的program.Program.validate方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。