コード例 #1
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(WIDTH, HEIGHT, "LearnOpenGL")

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

	window.makeContextCurrent()
	window.setKeyCallback(key_callback)

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

	gl.enable(gl.DEPTH_TEST)

	shaderProgram = Shader(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE)
	shaderProgram.use()

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

	# 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)

	# Position attribute
	gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset)
	gl.enableVertexAttribArray(0)
	# Color attribute
	# gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.color.offset)
	# gl.enableVertexAttribArray(1)
	# TexCoord attribute
	gl.vertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.texcoord.offset)
	gl.enableVertexAttribArray(2)

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

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

	# create the texture object, load the texture
    # Load and create a texture 
	texture1, texture2 = gl.genTextures(2)

    # Texture 1
	load_texture(texture1, 'learningopengl/resources/textures/container.jpg')

	# Texture 2
	load_texture(texture2, 'learningopengl/resources/textures/awesomeface.png')

	sb = True
	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.DEPTH_BUFFER_BIT)

		shaderProgram.use()
		shaderProgram.uniforms.ourTexture1 = texture1
		shaderProgram.uniforms.ourTexture2 = texture2

		# create transformations
		view = Mat4.Translation(Vec3(0.0, 0.0, -3.0))
		projection = Mat4.Perspective(45.0, float(WIDTH) / float(HEIGHT), 0.1, 100.0)

		# assign the transformations
		shaderProgram.uniforms.view = view
		shaderProgram.uniforms.projection = projection

		gl.bindVertexArray(vao)

		for i, position in enumerate(cubePositions):
            # Calculate the model matrix for each object and pass it to shader before drawing
			model = Mat4.Translation(position)
			angle = 20.0 * i;
			model = model * Mat4.Rotation(angle, Vec3(1.0, 0.3, 0.5))
			shaderProgram.uniforms.model = model
			gl.drawArrays(gl.TRIANGLES, 0, 36)

		gl.bindVertexArray(0)

		window.swapBuffers()

	shaderProgram.delete()
	gl.deleteVertexArrays([vao])
	gl.deleteBuffers([vbo, ebo])
コード例 #2
0
ファイル: materials.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(WIDTH, HEIGHT, "LearnOpenGL")

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


	inputMap = InputMap()

	window.makeContextCurrent()
	window.setKeyCallback(inputMap.key_callback)
	window.setCursorPosCallback(inputMap.mouse_callback)
	#window.setScrollCallback(inputMap.scroll_callback)
	window.setInputMode(glfw.CURSOR, glfw.CURSOR_DISABLED)

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

	camera = Camera(position=Vec3(0.0, 0.0, 3.0))
	lastX = 400
	lastY = 300
	firstMouse = True
	deltaTime = 0.0
	lastFrame = 0.0

	gl.enable(gl.DEPTH_TEST)

	lampShaderProgram = Shader(LAMP_VERTEX_SHADER_SOURCE, LAMP_FRAGMENT_SHADER_SOURCE)
	lightingShaderProgram = Shader(LIGHTING_VERTEX_SHADER_SOURCE, LIGHTING_FRAGMENT_SHADER_SOURCE)
	lightingShaderProgram.use()

	lightVAO, containerVAO = gl.genVertexArrays(2)
	vbo = gl.genBuffers(1)[0]

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

    # 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)

	# Position attribute
	gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset)
	gl.enableVertexAttribArray(0)

	# Normal attribute
	gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.normal.offset)
	gl.enableVertexAttribArray(1)

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


	# 1. Bind Vertex Array Object
	gl.bindVertexArray(lightVAO)
	
	# Position attribute
	gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset)
	gl.enableVertexAttribArray(0)

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

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

	lightPos = Vec3(1.2, 1.0, 2.0)

	lastFrame = time.perf_counter()
	while not window.shouldClose():
		inputMap.begin_frame()
		glfw.pollEvents()
		currentFrame = time.perf_counter()
		deltaTime = currentFrame - lastFrame
		lastFrame = currentFrame
		input = inputMap.get_input()
		camera.processInput(input, 1.0/30.0)

		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.DEPTH_BUFFER_BIT)

		lightingShaderProgram.use()
		lightingShaderProgram.uniforms.light.position = lightPos
		lightingShaderProgram.uniforms.viewPos = camera.position


		lightColor = Vec3(math.sin(currentFrame) * 2.0, math.sin(currentFrame) * 0.7, math.sin(currentFrame) * 1.3)
		diffuseColor = lightColor * 0.3
		ambientColor = diffuseColor * 0.2
		lightingShaderProgram.uniforms.light.ambient = ambientColor
		lightingShaderProgram.uniforms.light.diffuse = diffuseColor
		lightingShaderProgram.uniforms.light.specular = Vec3(1.0, 1.0, 1.0)

		lightingShaderProgram.uniforms.material.ambient = Vec3(1.0, 0.5, 0.31)
		lightingShaderProgram.uniforms.material.diffuse = Vec3(1.0, 0.5, 0.31)
		lightingShaderProgram.uniforms.material.specular = Vec3(0.5, 0.5, 0.5) # Specular doesn't have full effect on this object's material
		lightingShaderProgram.uniforms.material.shininess = 32.0

		# create transformations
		view = camera.getViewMatrix()
		projection = Mat4.Perspective(45.0, float(WIDTH) / float(HEIGHT), 0.1, 100.0)

		# assign the transformations
		lightingShaderProgram.uniforms.view = view
		lightingShaderProgram.uniforms.projection = projection

		# draw the container
		gl.bindVertexArray(containerVAO)
		model = Mat4.Identity()

		lightingShaderProgram.uniforms.model = model
		gl.drawArrays(gl.TRIANGLES, 0, 36)
		gl.bindVertexArray(0)

		# draw the lamp
		lampShaderProgram.use()
		
		lampShaderProgram.uniforms.view = view
		lampShaderProgram.uniforms.projection = projection

		model = Mat4.Translation(lightPos) * Mat4.Scale(0.2)

		lampShaderProgram.uniforms.model = model

		gl.bindVertexArray(lightVAO)
		gl.drawArrays(gl.TRIANGLES, 0, 36)
		gl.bindVertexArray(0)

		window.swapBuffers()

	lampShaderProgram.delete()
	lightingShaderProgram.delete()

	gl.deleteVertexArrays([containerVAO, lightVAO])
	gl.deleteBuffers([vbo])
コード例 #3
0
ファイル: cubeobj.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(WIDTH, HEIGHT, "LearnOpenGL")

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


	inputMap = InputMap()

	window.makeContextCurrent()
	window.setKeyCallback(inputMap.key_callback)
	window.setCursorPosCallback(inputMap.mouse_callback)
	# window.setScrollCallback(inputMap.scroll_callback)
	window.setInputMode(glfw.CURSOR, glfw.CURSOR_DISABLED)

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

	camera = Camera(Vec3(0.0, 0.0, 3.0))
	lastX = 400
	lastY = 300
	firstMouse = True
	deltaTime = 0.0
	lastFrame = 0.0

	gl.enable(gl.DEPTH_TEST)

	shaderProgram = Shader(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE)
	shaderProgram.use()

	obj_model = generate_model(load_obj_file(io.StringIO(CUBE_OBJ), ""))
	vao, vbo, ebo = load_model_data(obj_model)

	# create the texture object, load the texture
    # Load and create a texture 
	texture1, texture2 = gl.genTextures(2)

    # Texture 1
	load_texture(texture1, 'learningopengl/resources/textures/container.jpg')

	# Texture 2
	load_texture(texture2, 'learningopengl/resources/textures/awesomeface.png')

	while not window.shouldClose():
		inputMap.begin_frame()
		glfw.pollEvents()
		input = inputMap.get_input()
		camera.processInput(input, 1.0/30.0)

		# TODO: see http://www.glfw.org/docs/latest/window.html#window_fbsize
		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.DEPTH_BUFFER_BIT)

		shaderProgram.use()
		shaderProgram.uniforms.ourTexture1 = texture1
		shaderProgram.uniforms.ourTexture2 = texture2

		# create transformations
		# view = Mat4.Translation(Vec3(0.0, 0.0, -3.0))
		view = camera.getViewMatrix()
		projection = Mat4.Perspective(45.0, float(WIDTH) / float(HEIGHT), 0.1, 100.0)

		# assign the transformations
		shaderProgram.uniforms.view = view
		shaderProgram.uniforms.projection = projection

		gl.bindVertexArray(vao)
		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo)

		for i, position in enumerate(cubePositions):
            # Calculate the model matrix for each object and pass it to shader before drawing
			model = Mat4.Translation(position)
			angle = 20.0 * i;
			model = model * Mat4.Rotation(angle, Vec3(1.0, 0.3, 0.5))
			shaderProgram.uniforms.model = model
			dc = obj_model['meshes'][0]['draw_call']
			gl.drawElements(gl.TRIANGLES, dc['count'], gl.UNSIGNED_INT, dc['start'] * obj_model['vertex_buffer']['stride'])

		gl.bindVertexArray(0)

		window.swapBuffers()

	shaderProgram.delete()
	gl.deleteVertexArrays([vao])
	gl.deleteBuffers([vbo, ebo])
コード例 #4
0
ファイル: transformations.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)

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

	shaderProgram = Shader(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE)
	shaderProgram.use()

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

	# 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)

	# Position attribute
	gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset)
	gl.enableVertexAttribArray(0)
	# Color attribute
	gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.color.offset)
	gl.enableVertexAttribArray(1)
	# TexCoord attribute
	gl.vertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.texcoord.offset)
	gl.enableVertexAttribArray(2)

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

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

	# create the texture object, load the texture
    # Load and create a texture 
	texture1, texture2 = gl.genTextures(2)

    # Texture 1
	load_texture(texture1, 'learningopengl/resources/textures/container.jpg')

	# Texture 2
	load_texture(texture2, 'learningopengl/resources/textures/awesomeface.png')

	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)

		shaderProgram.use()

		t = time.perf_counter()
		translation = Mat4.Translation(Vec3(0.5, -0.5, 0.0))
		rotation = Mat4.Rotation(t, Vec3(0.0, 0.0, 1.0))
		trans = translation * rotation

		shaderProgram.uniforms.transform = trans
		shaderProgram.uniforms.ourTexture1 = texture1
		shaderProgram.uniforms.ourTexture2 = texture2

		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])
コード例 #5
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)

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

	shaderProgram = Shader(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE)
	shaderProgram.use()

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

	# 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)

	# Position attribute
	gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset)
	gl.enableVertexAttribArray(0)
	# Color attribute
	gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.color.offset)
	gl.enableVertexAttribArray(1)
	# TexCoord attribute
	gl.vertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.texcoord.offset)
	gl.enableVertexAttribArray(2)

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

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

	# create the texture object, load the texture
    # Load and create a texture 
	texture1, texture2 = gl.genTextures(2)
    # ====================
    # Texture 1
    # ====================
 
	gl.bindTexture(gl.TEXTURE_2D, texture1) # All upcoming GL_TEXTURE_2D operations now have effect on our texture object
    
	# Set our texture parameters
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)	# Set texture wrapping to GL_REPEAT
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)

	# Set texture filtering
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
	# Load, create texture and generate mipmaps

	img = PIL.Image.open('learningopengl/resources/textures/container.jpg')
	img = PIL.ImageOps.flip(img)
	width, height = img.size
	data = img.convert("RGBA").tobytes("raw", "RGBA")
    # unsigned char* image = SOIL_load_image(FileSystem::getPath("resources/textures/container.jpg").c_str(), &width, &height, 0, SOIL_LOAD_RGB);

	gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data)
	gl.generateMipmap(gl.TEXTURE_2D)
	gl.bindTexture(gl.TEXTURE_2D, 0)  # Unbind texture when done, so we won't accidentily mess up our texture.
	
	# ===================
	# Texture 2
	# ===================
	gl.bindTexture(gl.TEXTURE_2D, texture2)

	# Set our texture parameters
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)

	# Set texture filtering
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
	gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)

	# Load, create texture and generate mipmaps
	img = PIL.Image.open('learningopengl/resources/textures/awesomeface.png')
	img = PIL.ImageOps.flip(img)
	width, height = img.size
	data = img.convert("RGBA").tobytes("raw", "RGBA")

	gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data)
	gl.generateMipmap(gl.TEXTURE_2D)
	# SOIL_free_image_data(image);
	gl.bindTexture(gl.TEXTURE_2D, 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)

		shaderProgram.use()

		# Bind Textures using texture units
		gl.activeTexture(gl.TEXTURE0)
		gl.bindTexture(gl.TEXTURE_2D, texture1);
		gl.uniform1i(shaderProgram.getUniformLocation("ourTexture1"), 0);
		gl.activeTexture(gl.TEXTURE1);
		gl.bindTexture(gl.TEXTURE_2D, texture2);
		gl.uniform1i(shaderProgram.getUniformLocation("ourTexture2"), 1);

		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])
コード例 #6
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)

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

	shaderProgram = Shader(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE)
	shaderProgram.use()

	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)

	# Position attribute
	gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset)
	gl.enableVertexAttribArray(0)
	# Color attribute
	gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.color.offset)
	gl.enableVertexAttribArray(1)

	# 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)

		shaderProgram.use()
		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])
コード例 #7
0
ファイル: colors.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(WIDTH, HEIGHT, "LearnOpenGL")

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


	inputMap = InputMap()

	window.makeContextCurrent()
	window.setKeyCallback(inputMap.key_callback)
	window.setCursorPosCallback(inputMap.mouse_callback)
	#window.setScrollCallback(inputMap.scroll_callback)
	window.setInputMode(glfw.CURSOR, glfw.CURSOR_DISABLED)

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

	camera = Camera(position=Vec3(0.0, 0.0, 3.0))
	lastX = 400
	lastY = 300
	firstMouse = True
	deltaTime = 0.0
	lastFrame = 0.0

	gl.enable(gl.DEPTH_TEST)

	lampShaderProgram = Shader(LAMP_VERTEX_SHADER_SOURCE, LAMP_FRAGMENT_SHADER_SOURCE)
	colorShaderProgram = Shader(COLORS_VERTEX_SHADER_SOURCE, COLORS_FRAGMENT_SHADER_SOURCE)
	colorShaderProgram.use()

	lightVAO, containerVAO = gl.genVertexArrays(2)
	vbo, ebo = gl.genBuffers(2)

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

    # 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)

	# Position attribute
	gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset)
	gl.enableVertexAttribArray(0)

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


	# 1. Bind Vertex Array Object
	gl.bindVertexArray(lightVAO)
	
	# Position attribute
	gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, sizeof(Vertex), Vertex.position.offset)
	gl.enableVertexAttribArray(0)

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

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



	# create the texture object, load the texture
    # Load and create a texture 
	# texture1, texture2 = gl.genTextures(2)

    # Texture 1
	# load_texture(texture1, 'learningopengl/resources/textures/container.jpg')

	# Texture 2
	# load_texture(texture2, 'learningopengl/resources/textures/awesomeface.png')

	

	# window.setFrameBufferSizeCallback(lambda: )
	# glfwSetFramebufferSizeCallback(window, framebuffer_size_callback)

	sb = True
	while not window.shouldClose():
		inputMap.begin_frame()
		glfw.pollEvents()
		input = inputMap.get_input()
		camera.processInput(input, 1.0/30.0)

		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.DEPTH_BUFFER_BIT)

		colorShaderProgram.use()
		colorShaderProgram.uniforms.objectColor = Vec3(1.0, 0.5, 0.31)
		colorShaderProgram.uniforms.lightColor = Vec3(1.0, 0.5, 1.0)

		# create transformations
		# view = Mat4.Translation(Vec3(0.0, 0.0, -3.0))
		view = camera.getViewMatrix()
		projection = Mat4.Perspective(45.0, float(WIDTH) / float(HEIGHT), 0.1, 100.0)

		# assign the transformations
		colorShaderProgram.uniforms.view = view
		colorShaderProgram.uniforms.projection = projection

		# draw the container
		gl.bindVertexArray(containerVAO)
		model = Mat4.Identity()

		colorShaderProgram.uniforms.model = model
		gl.drawArrays(gl.TRIANGLES, 0, 36)
		gl.bindVertexArray(0)

		# draw the lamp
		lampShaderProgram.use()
		
		lampShaderProgram.uniforms.view = view
		lampShaderProgram.uniforms.projection = projection

		lightPos = Vec3(1.2, 1.0, 2.0)
		model = Mat4.Translation(lightPos) * Mat4.Scale(0.2)

		lampShaderProgram.uniforms.model = model

		gl.bindVertexArray(lightVAO)
		gl.drawArrays(gl.TRIANGLES, 0, 36)
		gl.bindVertexArray(0)

		window.swapBuffers()

	lampShaderProgram.delete()
	colorShaderProgram.delete()

	gl.deleteVertexArrays([containerVAO, lightVAO	])
	gl.deleteBuffers([vbo, ebo])