예제 #1
0
def create_shader(enum, sources):
    shader = gl.createShader(enum)

    gl.shaderSource(shader, sources)
    gl.compileShader(shader)

    success = gl.getShaderiv(shader, gl.COMPILE_STATUS)
    if not success:
        infoLog = gl.getShaderInfoLog(shader)
        raise Exception(infoLog)

    return shader
예제 #2
0
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])