# Copyright (c) Meta Platforms, Inc. and affiliates. # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from animated_drawings.model.transform import Transform import numpy as np import numpy.typing as npt import OpenGL.GL as GL import ctypes class TransformWidget(Transform): def __init__(self, shader_name: str = 'color_shader'): super().__init__() self.points: npt.NDArray[np.float32] = np.array([ [0.0, 0.0, 0.0, 1.0, 0.0, 0.0], [1.0, 0.0, 0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0, 0.0, 1.0], ], np.float32) self.shader_name: str = shader_name self._is_opengl_initialized: bool = False def _initialize_opengl_resources(self): self.vao = GL.glGenVertexArrays(1) self.vbo = GL.glGenBuffers(1) GL.glBindVertexArray(self.vao) # buffer vertex data GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.vbo) GL.glBufferData(GL.GL_ARRAY_BUFFER, self.points, GL.GL_STATIC_DRAW) vert_bytes = 4 * self.points.shape[1] # 4 is byte size of np.float32 pos_offset = 4 * 0 color_offset = 4 * 3 # position attributes GL.glVertexAttribPointer( 0, 3, GL.GL_FLOAT, False, vert_bytes, ctypes.c_void_p(pos_offset)) GL.glEnableVertexAttribArray(0) # color attributes GL.glVertexAttribPointer( 1, 3, GL.GL_FLOAT, False, vert_bytes, ctypes.c_void_p(color_offset)) GL.glEnableVertexAttribArray(1) GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0) GL.glBindVertexArray(0) self._is_opengl_initialized = True def _draw(self, **kwargs): if not self._is_opengl_initialized: self._initialize_opengl_resources() GL.glUseProgram(kwargs['shader_ids'][self.shader_name]) model_loc = GL.glGetUniformLocation( kwargs['shader_ids'][self.shader_name], "model") GL.glUniformMatrix4fv(model_loc, 1, GL.GL_FALSE, self._world_transform.T) GL.glBindVertexArray(self.vao) GL.glDrawArrays(GL.GL_LINES, 0, len(self.points)) GL.glBindVertexArray(0)