def render(self): with ms.push_matrix(ms.MatrixStack.model): # rotate the triangle along the positive z axis ms.translate(ms.MatrixStack.model, math.sin(glfw.get_time()), 0, 0) ms.rotateZ(ms.MatrixStack.model, glfw.get_time()) gl.glUseProgram(self.shader) gl.glBindVertexArray(self.vao) gl.glUniformMatrix4fv( self.mvpMatrixLoc, 1, gl.GL_TRUE, np.ascontiguousarray(ms.getCurrentMatrix( ms.MatrixStack.modelviewprojection), dtype=np.float32)) gl.glDrawArrays(gl.GL_TRIANGLES, 0, self.numberOfVertices) gl.glBindVertexArray(0)
if math.fabs(float(axes_list[0][0])) > 0.19: camera.x += 10.0 * axes_list[0][0] * math.cos(camera.rot_y) camera.z -= 10.0 * axes_list[0][0] * math.sin(camera.rot_y) if math.fabs(float(axes_list[0][1])) > 0.19: camera.x += 10.0 * axes_list[0][1] * math.sin(camera.rot_y) camera.z += 10.0 * axes_list[0][1] * math.cos(camera.rot_y) if math.fabs(axes_list[0][3]) > 0.19: camera.rot_y -= 3.0 * axes_list[0][3] * 0.01 if math.fabs(axes_list[0][4]) > 0.19: camera.rot_x += axes_list[0][4] * 0.01 # note - opengl matricies use degrees ms.rotate_x(ms.MatrixStack.view, -camera.rot_x) ms.rotate_y(ms.MatrixStack.view, -camera.rot_y) ms.translate(ms.MatrixStack.view, -camera.x, -camera.y, -camera.z) with ms.push_matrix(ms.MatrixStack.model): # draw paddle 1 # Unlike in previous demos, because the transformations # are on a stack, the fns on the model stack can # be read forwards, where each operation translates/rotates/scales # the current space ms.translate( ms.MatrixStack.model, paddle1.position[0], paddle1.position[1], 0.0, ) ms.rotate_z(ms.MatrixStack.model, paddle1.rotation) paddle1.render()
nearZ=0.1, farZ=10000.0) # ms.ortho(left=-150.0, # right=150.0, # back=-150.0, # top=150.0, # near=1.0, # far=10000.0) glMatrixMode(GL_PROJECTION) # ascontiguousarray puts the array in column major order glLoadMatrixf( np.ascontiguousarray(ms.getCurrentMatrix(ms.MatrixStack.projection).T)) # note - opengl matricies use degrees ms.translate(ms.MatrixStack.view, 0.0, 0.0, -moving_camera_r) ms.rotate_x(ms.MatrixStack.view, moving_camera_rot_x) ms.rotate_y(ms.MatrixStack.view, -moving_camera_rot_y) glMatrixMode(GL_MODELVIEW) draw_ground() if animation_time < 5.0 or (animation_time > 40.0 and animation_time < 45.0): draw_axises() else: draw_axises(grayed_out=True) with ms.PushMatrix(ms.MatrixStack.model): if animation_time > 5.0: # draw paddle 1
glViewport(0, 0, width, height) glClearColor(0.0, # r 0.0, # g 0.0, # b 1.0) # a glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) handle_inputs() # note - opengl matricies use degrees ms.rotate_x(ms.MatrixStack.view, -moving_camera_rot_x) ms.rotate_y(ms.MatrixStack.view, -moving_camera_rot_y) ms.translate(ms.MatrixStack.view, -moving_camera_x, -moving_camera_y, -moving_camera_z) with ms.push_matrix(ms.MatrixStack.model): # draw paddle 1 # Unlike in previous demos, because the transformations # are on a stack, the fns on the model stack can # be read forwards, where each operation translates/rotates/scales # the current space ms.translate(ms.MatrixStack.model, paddle1.input_offset_x, paddle1.input_offset_y, 0.0) ms.translate(ms.MatrixStack.model, paddle1.initial_position[0], paddle1.initial_position[1],
def demo(): # Initialize the library if not glfw.init(): sys.exit() glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) # for osx glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE) # Create a windowed mode window and its OpenGL context window = glfw.create_window(1000, 1000, "pyNuklear demo - GLFW OpenGL3", None, None) if not window: glfw.terminate() sys.exit() # Make the window's context current glfw.make_context_current(window) ctx = nkglfw3.glfw3_init(window, nkglfw3.GLFW3_INSTALL_CALLBACKS) nuklear = nk.NuklearContext(ctx) fontAtlas = nkglfw3.FontAtlas() nkglfw3.glfw3_font_stash_begin(ctypes.byref(fontAtlas)) nkglfw3.glfw3_font_stash_end() # Install a key handler def on_key(window, key, scancode, action, mods): if key == glfw.KEY_ESCAPE and action == glfw.PRESS: glfwSetWindowShouldClose(window, 1) glfw.set_key_callback(window, on_key) gl.glClearColor(0.1, 0.18, 0.24, 1.0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glClearDepth(1.0) gl.glDepthFunc(gl.GL_LEQUAL) class Camera: def __init__(self): self.x = 0.0 self.y = 0.0 self.z = 10.0 self.rotationX = 0.0 self.rotationY = 0.0 camera = Camera() triangle = Triangle() triangle.prepareToRender() # does python have static local variables? this declaration is way too far away from use #property = ctypes.c_int(20) # Loop until the user closes the window while not glfw.window_should_close(window): # Render here # Poll for and process events glfw.poll_events() nkglfw3.glfw3_new_frame() width, height = glfw.get_framebuffer_size(window) gl.glViewport(0, 0, width, height) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) ms.setToIdentityMatrix(ms.MatrixStack.model) ms.setToIdentityMatrix(ms.MatrixStack.view) ms.setToIdentityMatrix(ms.MatrixStack.projection) # set the projection matrix to be perspective ms.perspective(fov=45.0, aspectRatio=width / height, nearZ=0.1, farZ=10000.0) # get input from keyboard for camera movement if not nuklear.item_is_any_active(): # set up Camera if glfw.get_key(window, glfw.KEY_RIGHT) == glfw.PRESS: camera.rotationY -= 0.03 if glfw.get_key(window, glfw.KEY_LEFT) == glfw.PRESS: camera.rotationY += 0.03 if glfw.get_key(window, glfw.KEY_UP) == glfw.PRESS: camera.x -= math.sin(camera.rotationY) camera.z -= math.cos(camera.rotationY) if glfw.get_key(window, glfw.KEY_DOWN) == glfw.PRESS: camera.x += math.sin(camera.rotationY) camera.z += math.cos(camera.rotationY) # move the camera to the correct position, which means # updating the view stack ms.rotateX(ms.MatrixStack.view, camera.rotationX) ms.rotateY(ms.MatrixStack.view, -camera.rotationY) ms.translate(ms.MatrixStack.view, -camera.x, -camera.y, -camera.z) # render the models triangle.render() MAX_VERTEX_BUFFER = 512 * 1024 MAX_ELEMENT_BUFFER = 128 * 1024 if (nuklear.begin(title="Demonstration", bounds=nk.Rect(10.0, 10.0, 230.0, 250.0), flags=nk.PanelFlags.WINDOW_BORDER.value | nk.PanelFlags.WINDOW_MOVABLE.value | nk.PanelFlags.WINDOW_SCALABLE.value | nk.PanelFlags.WINDOW_MINIMIZABLE.value | nk.PanelFlags.WINDOW_TITLE.value)): nuklear.layout_row_static(height=30.0, item_width=80, columns=5) if nuklear.button_label(title="button"): print('button pressed') nuklear.layout_row_dynamic(height=30.0, columns=2) try: op except Exception: op = 0 if nuklear.option_label(label="easy", active=op == 0): op = 0 if nuklear.option_label(label="hard", active=op == 1): op = 1 nuklear.layout_row_dynamic(height=25.0, columns=1) try: prop except Exception: prop = 20 prop = nuklear.property_int(name="Compression:", minV=0, val=prop, maxV=100, step=10, inc_per_pixel=1) nuklear.layout_row_dynamic(height=20.0, columns=1) nuklear.label(text="background:", alignment=nk.TextAlign.TEXT_LEFT) try: background except Exception: background = nk.ColorF(r=0.10, g=0.18, b=0.24, a=1.0) nuklear.layout_row_dynamic(height=25.0, columns=1) if nuklear.combo_begin_color(color=nuklear.rgb_cf(background), size=nk.Vec2(nuklear.widget_width(), 400)): nuklear.layout_row_dynamic(height=120.0, columns=1) background = nuklear.color_picker(color=background, format=nk.ColorFormat.RGBA) nuklear.layout_row_dynamic(height=25.0, columns=1) background.r = nuklear.propertyf(name="#R:", minVal=0.0, val=background.r, maxVal=1.0, step=0.01, inc_per_pixel=0.005) background.g = nuklear.propertyf(name="#G:", minVal=0.0, val=background.g, maxVal=1.0, step=0.01, inc_per_pixel=0.005) background.b = nuklear.propertyf(name="#B:", minVal=0.0, val=background.b, maxVal=1.0, step=0.01, inc_per_pixel=0.005) background.a = nuklear.propertyf(name="#A:", minVal=0.0, val=background.a, maxVal=1.0, step=0.01, inc_per_pixel=0.005) gl.glClearColor(background.r, background.g, background.b, background.a) nuklear.combo_end() nuklear.end() overview(nuklear) nkglfw3.glfw3_render(nk.AntiAliasing.ON.value, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER) # done with frame, flush and swap buffers # Swap front and back buffers glfw.swap_buffers(window) glfw.terminate()
ms.setToIdentityMatrix(ms.MatrixStack.view) ms.setToIdentityMatrix(ms.MatrixStack.projection) # set the projection matrix to be ortho if NDC: ms.ortho(left=-1.0, right=1.0, back=-1.0, top=1.0, near=0.0, far=550.0) else: ms.ortho(left=-100.0, right=100.0, back=-100.0, top=100.0, near=0.0, far=550.0) # note - opengl matricies use degrees ms.translate(ms.MatrixStack.view, 0.0, 0.0, -camera.r) ms.rotate_x(ms.MatrixStack.view, camera.rot_x) ms.rotate_y(ms.MatrixStack.view, -camera.rot_y) # draw NDC in global space, so that we can see the camera space # go to NDC with ms.PushMatrix(ms.MatrixStack.model): cube.render(animation_time) with ms.PushMatrix(ms.MatrixStack.model): ms.rotate_y(ms.MatrixStack.model, math.radians(90.0)) ms.rotate_z(ms.MatrixStack.model, math.radians(90.0)) ground.render(animation_time) if animation_time > 60.0: ms.translate( ms.MatrixStack.model,
def handle_inputs() -> None: global rotation_around_paddle1 if glfw.get_key(window, glfw.KEY_E) == glfw.PRESS: rotation_around_paddle1 += 0.1 global square_rotation if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS: square_rotation += 0.1 global camera move_multiple = 15.0 if glfw.get_key(window, glfw.KEY_RIGHT) == glfw.PRESS: camera.rot_y -= 0.03 if glfw.get_key(window, glfw.KEY_LEFT) == glfw.PRESS: camera.rot_y += 0.03 if glfw.get_key(window, glfw.KEY_PAGE_UP) == glfw.PRESS: camera.rot_x += 0.03 if glfw.get_key(window, glfw.KEY_PAGE_DOWN) == glfw.PRESS: camera.rot_x -= 0.03 # NEW -- reason about moving in 3D space just like placing # objects. If the up button is pressed if glfw.get_key(window, glfw.KEY_UP) == glfw.PRESS: # describe the vector to move forwards. # -1 unit in the z direction is forwards # # Although we have not covered linear algebra and # matrix multiplication in depth, all coordinates # in OpenGL are in 4D space, (x,y,z,w), # where to convert to NDC, use (x/w,y/w,z/w) # # For most purposes, by setting w to 1, we can # think in normal 3D space # forwards = np.array([0.0, 0.0, -1.0, 1.0]) # push matrix on the view stack, as we are going # to use the view matrix to determine the new position, # but then will reset the value of the view matrix with ms.PushMatrix(ms.MatrixStack.view): ms.translate(ms.MatrixStack.view, camera.x, camera.y, camera.z) ms.rotate_y(ms.MatrixStack.view, camera.rot_y) ms.scale(ms.MatrixStack.view, 5.0, 5.0, 5.0) camera.x, camera.y, camera.z, _ = ( ms.getCurrentMatrix(ms.MatrixStack.view) @ forwards) if glfw.get_key(window, glfw.KEY_DOWN) == glfw.PRESS: backwards = np.array([0.0, 0.0, 1.0, 1.0]) with ms.PushMatrix(ms.MatrixStack.view): ms.translate(ms.MatrixStack.view, camera.x, camera.y, camera.z) ms.rotate_y(ms.MatrixStack.view, camera.rot_y) ms.scale(ms.MatrixStack.view, 5.0, 5.0, 5.0) camera.x, camera.y, camera.z, _ = ( ms.getCurrentMatrix(ms.MatrixStack.view) @ backwards) global paddle1, paddle2 if glfw.get_key(window, glfw.KEY_S) == glfw.PRESS: paddle1.position[1] -= 10.0 if glfw.get_key(window, glfw.KEY_W) == glfw.PRESS: paddle1.position[1] += 10.0 if glfw.get_key(window, glfw.KEY_K) == glfw.PRESS: paddle2.position[1] -= 10.0 if glfw.get_key(window, glfw.KEY_I) == glfw.PRESS: paddle2.position[1] += 10.0 global paddle_1_rotation, paddle_2_rotation if glfw.get_key(window, glfw.KEY_A) == glfw.PRESS: paddle1.rotation += 0.1 if glfw.get_key(window, glfw.KEY_D) == glfw.PRESS: paddle1.rotation -= 0.1 if glfw.get_key(window, glfw.KEY_J) == glfw.PRESS: paddle2.rotation += 0.1 if glfw.get_key(window, glfw.KEY_L) == glfw.PRESS: paddle2.rotation -= 0.1
# render scene handle_inputs() ms.setToIdentityMatrix(ms.MatrixStack.model) ms.setToIdentityMatrix(ms.MatrixStack.view) ms.setToIdentityMatrix(ms.MatrixStack.projection) # set the projection matrix to be perspective ms.perspective( fov=45.0, aspectRatio=float(width) / float(height), nearZ=0.1, farZ=10000.0 ) # note - opengl matricies use degrees if view_ndc: ms.translate(ms.MatrixStack.view, 0.0, 0.0, -camera.r) ms.rotate_x(ms.MatrixStack.view, camera.rot_x) ms.rotate_y(ms.MatrixStack.view, -camera.rot_y) if view_paddle1 or view_square: if view_square: ms.rotate_z( ms.MatrixStack.model, -square_rotation, ) ms.translate( ms.MatrixStack.model, -15.0, 0.0, 0.0, )
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # render scene handle_inputs() ms.setToIdentityMatrix(ms.MatrixStack.model) ms.setToIdentityMatrix(ms.MatrixStack.view) ms.setToIdentityMatrix(ms.MatrixStack.projection) # set the projection matrix to be perspective ms.perspective( fov=45.0, aspectRatio=float(width) / float(height), nearZ=0.1, farZ=10000.0 ) # note - opengl matricies use degrees ms.translate(ms.MatrixStack.view, 0.0, 0.0, -camera.r) ms.rotate_x(ms.MatrixStack.view, camera.rot_x) ms.rotate_y(ms.MatrixStack.view, -camera.rot_y) # draw NDC in global space, so that we can see the camera space # go to NDC with ms.PushMatrix(ms.MatrixStack.model): cube.render(animation_time) ground.render(animation_time) if animation_time < 5.0 or animation_time > 95: axis.render(animation_time) else: axis.render(animation_time, grayed_out=True) with ms.PushMatrix(ms.MatrixStack.model):
if glfw.glfwGetKey(window, glfw.GLFW_KEY_UP) == glfw.GLFW_PRESS: camera.x -= math.sin(camera.rotationY) camera.z -= math.cos(camera.rotationY) if glfw.glfwGetKey(window, glfw.GLFW_KEY_DOWN) == glfw.GLFW_PRESS: camera.x += math.sin(camera.rotationY) camera.z += math.cos(camera.rotationY) # move the camera to the correct position, which means # updating the view stack ms.rotateX(ms.MatrixStack.view, camera.rotationX) ms.rotateY(ms.MatrixStack.view, -camera.rotationY) ms.translate(ms.MatrixStack.view, -camera.x, -camera.y, -camera.z) # render the models triangle.render() MAX_VERTEX_BUFFER = 512 * 1024 MAX_ELEMENT_BUFFER = 128 * 1024 if(nuklear.begin(title="Demonstration", bounds=nk.Rect(10.0,10.0,230.0,250.0), flags=nk.WINDOW_BORDER |nk.WINDOW_MOVABLE |nk.WINDOW_SCALABLE