Ejemplo n.º 1
0
 def init_gl(self):
     vertex_shader = compileShader(
         shader_string("""
         layout(location = 0) in vec3 in_Position;
         layout(location = 1) in vec3 in_Normal;
         layout(location = 2) in vec2 in_TexCoord;
         
         layout(location = 0) uniform mat4 projection = mat4(1);
         layout(location = 4) uniform mat4 model_view = mat4(1);
         layout(location = 8) uniform mat4 normal_matrix = mat4(1);
         
         out vec3 color;
         out vec2 fragTexCoord;
         
         void main() {
           gl_Position = projection * model_view * vec4(in_Position, 1.0);
           vec3 normal = normalize((normal_matrix * vec4(in_Normal, 0)).xyz);
           color = (normal + vec3(1,1,1)) * 0.5; // color by normal
           fragTexCoord = in_TexCoord;
           // color = vec3(in_TexCoord, 0.5); // color by texture coordinate
         }
         """),
         GL.GL_VERTEX_SHADER)
     fragment_shader = compileShader(
         shader_string("""
         uniform sampler2D diffuse;
         in vec3 color;
         in vec2 fragTexCoord;
         out vec4 fragColor;
         
         void main() {
           // fragColor = vec4(color, 1.0);
           fragColor = texture(diffuse, fragTexCoord);
         }
         """),
         GL.GL_FRAGMENT_SHADER)
     self.shader = compileProgram(vertex_shader, fragment_shader)
     self._check_devices()
     GL.glEnable(GL.GL_DEPTH_TEST)
Ejemplo n.º 2
0
 def init_gl(self):
     vertex_shader = compileShader(
         shader_string("""
         layout(location = 0) in vec3 in_Position;
         layout(location = 1) in vec3 in_Normal;
         layout(location = 2) in vec2 in_TexCoord;
         
         layout(location = 0) uniform mat4 projection = mat4(1);
         layout(location = 4) uniform mat4 model_view = mat4(1);
         layout(location = 8) uniform mat4 normal_matrix = mat4(1);
         
         out vec3 color;
         out vec2 fragTexCoord;
         
         void main() {
           gl_Position = projection * model_view * vec4(in_Position, 1.0);
           vec3 normal = normalize((normal_matrix * vec4(in_Normal, 0)).xyz);
           color = (normal + vec3(1,1,1)) * 0.5; // color by normal
           fragTexCoord = in_TexCoord;
           // color = vec3(in_TexCoord, 0.5); // color by texture coordinate
         }
         """),
         GL.GL_VERTEX_SHADER)
     fragment_shader = compileShader(
         shader_string("""
         uniform sampler2D diffuse;
         in vec3 color;
         in vec2 fragTexCoord;
         out vec4 fragColor;
         
         void main() {
           // fragColor = vec4(color, 1.0);
           fragColor = texture(diffuse, fragTexCoord);
         }
         """),
         GL.GL_FRAGMENT_SHADER)
     self.shader = compileProgram(vertex_shader, fragment_shader)
     self._check_devices()
     GL.glEnable(GL.GL_DEPTH_TEST)
    def __init__(s):
        s.vr_system = openvr.init(openvr.VRApplication_Scene)
        s.vr_compositor = openvr.VRCompositor()
        poses_t = openvr.TrackedDevicePose_t * openvr.k_unMaxTrackedDeviceCount
        s.poses = poses_t()
        s.w, s.h = s.vr_system.getRecommendedRenderTargetSize()
        SDL_Init(SDL_INIT_VIDEO)
        s.window = SDL_CreateWindow(b"test", SDL_WINDOWPOS_CENTERED,
                                    SDL_WINDOWPOS_CENTERED, 100, 100,
                                    SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL)
        s.context = SDL_GL_CreateContext(s.window)
        SDL_GL_MakeCurrent(s.window, s.context)
        s.depth_buffer = glGenRenderbuffers(1)
        s.frame_buffers = glGenFramebuffers(2)
        s.texture_ids = glGenTextures(2)
        s.textures = [None] * 2
        s.eyes = [openvr.Eye_Left, openvr.Eye_Right]
        s.cameraToProjection = [None] * 2
        s.headToCamera = [None] * 2
        s.col3 = [0, 0, 0, 1]

        vertexShader = compileShader(
            shader_string("""
          layout (location=0) uniform mat4 cameraToProjection;
          layout (location=1) uniform mat4 modelToCamera;
          void main() {
            float angle = gl_VertexID * (3.14159*2/3);
            vec4 modelPos = vec4(cos(angle), sin(angle), -2, 1);
            gl_Position = cameraToProjection * (modelToCamera * modelPos);
          }
        """), GL_VERTEX_SHADER)
        fragmentShader = compileShader(
            shader_string("""
          out vec4 colour;
          void main() {
            colour = vec4(1, 0, 0, 1);
          }
        """), GL_FRAGMENT_SHADER)
        s.program = compileProgram(vertexShader, fragmentShader)
        s.vertexBuffer = glGenVertexArrays(1)

        for eye in range(2):
            glBindFramebuffer(GL_FRAMEBUFFER, s.frame_buffers[eye])
            glBindRenderbuffer(GL_RENDERBUFFER, s.depth_buffer)
            glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, s.w,
                                  s.h)
            glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                                      GL_DEPTH_STENCIL_ATTACHMENT,
                                      GL_RENDERBUFFER, s.depth_buffer)
            glBindTexture(GL_TEXTURE_2D, s.texture_ids[eye])
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, s.w, s.h, 0, GL_RGBA,
                         GL_UNSIGNED_BYTE, None)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                   GL_TEXTURE_2D, s.texture_ids[eye], 0)
            texture = openvr.Texture_t()
            texture.handle = int(s.texture_ids[eye])
            texture.eType = openvr.TextureType_OpenGL
            texture.eColorSpace = openvr.ColorSpace_Gamma
            s.textures[eye] = texture
            proj = s.vr_system.getProjectionMatrix(s.eyes[eye], 0.2, 500.0)
            s.cameraToProjection[eye] = numpy.matrix(
                [[proj.m[i][j] for i in range(4)] for j in range(4)],
                numpy.float32)
            camToHead = s.vr_system.getEyeToHeadTransform(s.eyes[eye])
            s.headToCamera[eye] = numpy.matrix(
                [[camToHead.m[i][j] for i in range(3)] + [s.col3[j]]
                 for j in range(4)], numpy.float32).I
 def init_gl(self):
     vertex_shader = compileShader(
         shader_string("""
         // Adapted from @jherico's RiftDemo.py in pyovr
         
         layout(location = 0) uniform mat4 Projection = mat4(1);
         layout(location = 4) uniform mat4 ModelView = mat4(1);
         layout(location = 8) uniform float Size = 0.3;
         
         // Minimum Y value is zero, so cube sits on the floor in room scale
         const vec3 UNIT_CUBE[8] = vec3[8](
           vec3(-1.0, -0.0, -1.0), // 0: lower left rear
           vec3(+1.0, -0.0, -1.0), // 1: lower right rear
           vec3(-1.0, +2.0, -1.0), // 2: upper left rear
           vec3(+1.0, +2.0, -1.0), // 3: upper right rear
           vec3(-1.0, -0.0, +1.0), // 4: lower left front
           vec3(+1.0, -0.0, +1.0), // 5: lower right front
           vec3(-1.0, +2.0, +1.0), // 6: upper left front
           vec3(+1.0, +2.0, +1.0)  // 7: upper right front
         );
         
         const vec3 UNIT_CUBE_NORMALS[6] = vec3[6](
           vec3(0.0, 0.0, -1.0),
           vec3(0.0, 0.0, 1.0),
           vec3(1.0, 0.0, 0.0),
           vec3(-1.0, 0.0, 0.0),
           vec3(0.0, 1.0, 0.0),
           vec3(0.0, -1.0, 0.0)
         );
         
         const int CUBE_INDICES[36] = int[36](
           0, 1, 2, 2, 1, 3, // front
           4, 6, 5, 6, 5, 7, // back
           0, 2, 4, 4, 2, 6, // left
           1, 3, 5, 5, 3, 7, // right
           2, 6, 3, 6, 3, 7, // top
           0, 1, 4, 4, 1, 5  // bottom
         );
         
         out vec3 _color;
         
         void main() {
           _color = vec3(1.0, 0.0, 0.0);
           int vertexIndex = CUBE_INDICES[gl_VertexID];
           int normalIndex = gl_VertexID / 6;
           
           _color = UNIT_CUBE_NORMALS[normalIndex];
           if (any(lessThan(_color, vec3(0.0)))) {
               _color = vec3(1.0) + _color;
           }
         
           gl_Position = Projection * ModelView * vec4(UNIT_CUBE[vertexIndex] * Size, 1.0);
         }
         """), GL_VERTEX_SHADER)
     fragment_shader = compileShader(
         shader_string("""
         in vec3 _color;
         out vec4 FragColor;
         
         void main() {
           FragColor = vec4(_color, 1.0);
         }
         """), GL_FRAGMENT_SHADER)
     self.shader = compileProgram(vertex_shader, fragment_shader)
     #
     self.vao = glGenVertexArrays(1)
     glBindVertexArray(self.vao)
     glEnable(GL_DEPTH_TEST)