コード例 #1
0
ファイル: shader.py プロジェクト: springtangent/ld34
    def __init__(self, vertex_shader_source, fragment_shader_source, geometry_shader_source=None):
        super(Shader, self).__init__()
        vertex_shader = create_shader(gl.VERTEX_SHADER, [vertex_shader_source])
        fragment_shader = create_shader(gl.FRAGMENT_SHADER, [fragment_shader_source])
        if geometry_shader_source:
            geometry_shader = create_shader(gl.GEOMETRY_SHADER, [geometry_shader_source])
        else:
            geometry_shader = None

        self.program_id = gl.createProgram()
        gl.attachShader(self.program_id, vertex_shader)
        gl.attachShader(self.program_id, fragment_shader)
        if geometry_shader:
            gl.attachShader(self.program_id, geometry_shader)
        gl.linkProgram(self.program_id)

        success = gl.getProgramiv(self.program_id, gl.LINK_STATUS)
        print("SHADER PROGRAM LINK STATUS:", success)
        if not success:
            infoLog = gl.getProgramInfoLog(self.program_id)
            raise Exception(infoLog)

        assert gl.isProgram(self.program_id)

        gl.deleteShader(vertex_shader)
        gl.deleteShader(fragment_shader)
        if geometry_shader:
            gl.deleteShader(geometry_shader)

        self.uniforms = ProgramUniforms(self)
        self.uniforms.load()
コード例 #2
0
ファイル: graphics.py プロジェクト: springtangent/ld34
    def __init__(self, vertex_shader_source, fragment_shader_source, geometry_shader_source=None):
        super(ShaderProgram, self).__init__()

        # first we save off the source for future reference
        self.vertex_shader_source = vertex_shader_source
        self.fragment_shader_source = fragment_shader_source
        self.geometry_shader_source = geometry_shader_source

        # next we create and compile the shaders. If there's a problem we raise an execption which the caller
        # has to deal with.
        vertex_shader = create_shader(gl.VERTEX_SHADER, [self.vertex_shader_source])
        fragment_shader = create_shader(gl.FRAGMENT_SHADER, [self.fragment_shader_source])
        if self.geometry_shader_source:
            geometry_shader = create_shader(gl.GEOMETRY_SHADER, [self.geometry_shader_source])
        else:
            geometry_shader = None

        # create the rogram, attach the shaders, link the program.
        self.program_id = gl.createProgram()

        gl.attachShader(self.program_id, vertex_shader)
        gl.attachShader(self.program_id, fragment_shader)
        if geometry_shader:
            gl.attachShader(self.program_id, geometry_shader)

        gl.linkProgram(self.program_id)

        # make sure there weren't any errors
        success = gl.getProgramiv(self.program_id, gl.LINK_STATUS)
        if not success:
            infoLog = gl.getProgramInfoLog(self.program_id)
            raise ShaderLinkError(infoLog)

        # grab attribute info about the shader
        self.attributes = ShaderAttributes(self)

        # next we grab the uniform information from the shader program handle.
        # we store it so the user can set their values later.
        self.uniforms = ShaderUniforms(self)
コード例 #3
0
ファイル: hellotriangle.py プロジェクト: springtangent/ld34
def main():
	glfw.setErrorCallback(error_callback)

	glfw.init()
	glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3)
	glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3)
	glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
	# glfw.windowHint(glfw.RESIZABLE, gl.FALSE)
	glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1)

	window = glfw.createWindow(800, 600, "LearnOpenGL")

	if window is None:
		print('could not open window.')
		glfw.terminate()
		sys.exit()

	window.makeContextCurrent()
	window.setKeyCallback(key_callback)

	gl.init()
	err = gl.getError()
	print(gl)
	if err:
		print("WINDOW OPEN ERROR:", err)

	vertexShader = gl.createShader(gl.VERTEX_SHADER)
	gl.shaderSource(vertexShader, [VERTEX_SHADER_SOURCE]);
	gl.compileShader(vertexShader)

	success = gl.getShaderiv(vertexShader, gl.COMPILE_STATUS)
	print("VERTEX SHADER COMPILE STATUS:",success)
	if not success:
		infoLog = gl.getShaderInfoLog(vertexShader)
		print("ERROR COMPILING SHADER:", infoLog)
		sys.exit()

	fragmentShader = gl.createShader(gl.FRAGMENT_SHADER)
	gl.shaderSource(fragmentShader, [FRAGMENT_SHADER_SOURCE]);
	gl.compileShader(fragmentShader)

	success = gl.getShaderiv(fragmentShader, gl.COMPILE_STATUS)
	print("FRAGMENT SHADER COMPILE STATUS:",success)
	if not success:
		infoLog = gl.getShaderInfoLog(fragmentShader)
		print("ERROR COMPILING SHADER:", infoLog)
		sys.exit()

	shaderProgram = gl.createProgram()
	gl.attachShader(shaderProgram, vertexShader)
	gl.attachShader(shaderProgram, fragmentShader)
	gl.linkProgram(shaderProgram)

	gl.getProgramiv(shaderProgram, gl.LINK_STATUS)
	print("SHADER PROGRAM LINK STATUS:",success)
	if not success:
		infoLog = gl.getProgramInfoLog(shaderProgram)
		print("ERROR LINKING SHADER PROGRAM:", infoLog)
		sys.exit()

	assert(gl.isProgram(shaderProgram))
	gl.useProgram(shaderProgram)
	#gl.deleteShader(vertexShader)
	#gl.deleteShader(fragmentShader)

	vao = gl.genVertexArrays(1)[0]
	vbo, ebo = gl.genBuffers(2)
	print(vao, vbo, ebo)

	# 1. Bind Vertex Array Object
	gl.bindVertexArray(vao)

    # 2. Copy our vertices array in a buffer for OpenGL to use
	gl.bindBuffer(gl.ARRAY_BUFFER, vbo)
	gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)

    # 2. Copy our index array in a buffer for OpenGL to use
	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo)
	gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW)

    # 3. Then set our vertex attributes pointers
	gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 3 * sizeof(gl.GLfloat), None)
	gl.enableVertexAttribArray(0)

	# 4. unbind the vbo
	gl.bindBuffer(gl.ARRAY_BUFFER, 0)

	# 4. Unbind the VAO
	gl.bindVertexArray(0)

	while not window.shouldClose():
		glfw.pollEvents()

		framebuffer_width, framebuffer_height = window.getFramebufferSize()
		gl.viewport(0, 0, framebuffer_width, framebuffer_height)
		gl.clearColor(0.2, 0.3, 0.3, 1.0)
		gl.clear(gl.COLOR_BUFFER_BIT)

		gl.useProgram(shaderProgram)
		gl.bindVertexArray(vao)
		gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0)
		# gl.drawArrays(gl.TRIANGLES, 0, 3); 
		gl.bindVertexArray(0)

		window.swapBuffers()

	gl.deleteVertexArrays([vao])
	gl.deleteBuffers([vbo, ebo])