cmbruns / pyopenvr

Unofficial python bindings for Valve's OpenVR virtual reality SDK
BSD 3-Clause "New" or "Revised" License
254 stars 41 forks source link

Code to show object doesn't show anything. #79

Open zurgeg opened 4 years ago

zurgeg commented 4 years ago

I was making some code for SteamVR when I ran into a roadblock, my code to show the square wouldn't show anything! Here's my code:

import openvr as vr
import os
import pygame
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import socket
import time
from openvr.gl_renderer import OpenVrGlRenderer
vr_system = vr.init(vr.VRApplication_Scene)
print(dir(vr_system))
print(dir(vr))
def fh_server():
    # Run on_server_recv(data) with data as a tuple with the data recived
    ...
def on_server_recv(data):
    render_player(data)
def render_player(data):
    # Render the player at the tuple
    ...
class OBJ:
    generate_on_init = True
    @classmethod
    def loadTexture(cls, imagefile):
        surf = pygame.image.load(imagefile)
        image = pygame.image.tostring(surf, 'RGBA', 1)
        ix, iy = surf.get_rect().size
        texid = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, texid)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image)
        return texid

    @classmethod
    def loadMaterial(cls, filename):
        contents = {}
        mtl = None
        dirname = os.path.dirname(filename)

        for line in open(filename, "r"):
            if line.startswith('#'): continue
            values = line.split()
            if not values: continue
            if values[0] == 'newmtl':
                mtl = contents[values[1]] = {}
            elif mtl is None:
                raise ValueError("mtl file doesn't start with newmtl stmt")
            elif values[0] == 'map_Kd':
                # load the texture referred to by this declaration
                mtl[values[0]] = values[1]
                imagefile = os.path.join(dirname, mtl['map_Kd'])
                mtl['texture_Kd'] = cls.loadTexture(imagefile)
            else:
                mtl[values[0]] = list(map(float, values[1:]))
        return contents

    def __init__(self, filename, swapyz=False):
        """Loads a Wavefront OBJ file. """
        self.vertices = []
        self.normals = []
        self.texcoords = []
        self.faces = []
        self.gl_list = 0
        dirname = os.path.dirname(filename)

        material = None
        for line in open(filename, "r"):
            if line.startswith('#'): continue
            values = line.split()
            if not values: continue
            if values[0] == 'v':
                v = list(map(float, values[1:4]))
                if swapyz:
                    v = v[0], v[2], v[1]
                self.vertices.append(v)
            elif values[0] == 'vn':
                v = list(map(float, values[1:4]))
                if swapyz:
                    v = v[0], v[2], v[1]
                self.normals.append(v)
            elif values[0] == 'vt':
                self.texcoords.append(list(map(float, values[1:3])))
            elif values[0] in ('usemtl', 'usemat'):
                material = values[1]
            elif values[0] == 'mtllib':
                self.mtl = self.loadMaterial(os.path.join(dirname, values[1]))
            elif values[0] == 'f':
                face = []
                texcoords = []
                norms = []
                for v in values[1:]:
                    w = v.split('/')
                    face.append(int(w[0]))
                    if len(w) >= 2 and len(w[1]) > 0:
                        texcoords.append(int(w[1]))
                    else:
                        texcoords.append(0)
                    if len(w) >= 3 and len(w[2]) > 0:
                        norms.append(int(w[2]))
                    else:
                        norms.append(0)
                self.faces.append((face, norms, texcoords, material))
        if self.generate_on_init:
            self.generate()

    def generate(self):
        self.gl_list = glGenLists(1)
        glNewList(self.gl_list, GL_COMPILE)
        glEnable(GL_TEXTURE_2D)
        glFrontFace(GL_CCW)
        for face in self.faces:
            vertices, normals, texture_coords, material = face

            mtl = self.mtl[material]
            if 'texture_Kd' in mtl:
                # use diffuse texmap
                glBindTexture(GL_TEXTURE_2D, mtl['texture_Kd'])
            else:
                # just use diffuse colour
                glColor(*mtl['Kd'])

            glBegin(GL_POLYGON)
            for i in range(len(vertices)):
                if normals[i] > 0:
                    glNormal3fv(self.normals[normals[i] - 1])
                if texture_coords[i] > 0:
                    glTexCoord2fv(self.texcoords[texture_coords[i] - 1])
                glVertex3fv(self.vertices[vertices[i] - 1])
            glEnd()
        glDisable(GL_TEXTURE_2D)
        glEndList()

    def render(self):
        glCallList(self.gl_list)

    def free(self):
        glDeleteLists([self.gl_list])
#objecta = OBJ('coolthing.obj')

a = vr.VRInput().setActionManifestPath("C:\\Users\\zurgE\\OneDrive\\Documents\\3D Python programs\\newfirehawk.txt")
print(dir(vr.VRInput()))
#vr.IVRInput.setActionManifestPath(vr_system,'firehawkvrmanifest.txt')

gunshot = vr.VRInput().getActionHandle("/actions/main/in/GunShot")

recoil = vr.VRInput().getActionHandle("/actions/main/out/GunShot")

righta = vr.VRInput().getActionHandle("/actions/main/in/RightHand")

righthand = vr.VRInput().getActionHandle("/actions/main/in/RightHandAttachment")

gash = vr.VRInput().getActionSetHandle("/actions/main")

vr.VRInput().updateActionState(recoil)
vr.VRInput().updateActionState(righta)

a = vr.VRInput().getPoseActionDataForNextFrame(righta,0,False)

t = []
print(vr.VRInput().triggerHapticVibrationAction(recoil,0,1,100,1.0,1))

def square():
    # We have to declare the points in this sequence: bottom left, bottom right, top right, top left
    glBegin(GL_QUADS) # Begin the sketch
    glVertex2f(100, 100) # Coordinates for the bottom left point
    glVertex2f(200, 100) # Coordinates for the bottom right point
    glVertex2f(200, 200) # Coordinates for the top right point
    glVertex2f(100, 200) # Coordinates for the top left point
    glEnd() # Mark the end of drawing

class FireHawkVR:
    def __init__(self):

        self.renderer = OpenVrGlRenderer(multisample=4)
        self.vr_system = vr_system

        glutInit()
        glutInitDisplayMode(GLUT_RGBA)
        glutInitWindowSize(400,400)
        glutInitWindowPosition(50,50)
        win = glutCreateWindow(b"FIREHAWK VR")
        self.coolthing = OBJ('cube.obj')
        glutDisplayFunc(self.display)
        glutIdleFunc(self.display)
        #glutReshapeFunc(self.render.resize_gl)
        glutKeyboardFunc(self.key_press)
        self.vr_width, self.vr_height = self.vr_system.getRecommendedRenderTargetSize()
        self.compositor = vr.VRCompositor()
        if self.compositor is None:
            raise Exception("Unable to create compositor")
        poses_t = vr.TrackedDevicePose_t * vr.k_unMaxTrackedDeviceCount
        self.poses = poses_t()
        self.fb = glGenFramebuffers(1)
        glBindFramebuffer(GL_FRAMEBUFFER, self.fb)
        self.depth_buffer = glGenRenderbuffers(1)
        glBindRenderbuffer(GL_RENDERBUFFER, self.depth_buffer)
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, self.vr_width, self.vr_height)
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, self.depth_buffer)
        self.texture_id = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, self.texture_id)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, self.vr_width, self.vr_height, 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)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)  
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.texture_id, 0)
        status = glCheckFramebufferStatus(GL_FRAMEBUFFER)
        if status != GL_FRAMEBUFFER_COMPLETE:
            glBindFramebuffer(GL_FRAMEBUFFER, 0)
            raise Exception("Incomplete framebuffer")
        glBindFramebuffer(GL_FRAMEBUFFER, 0)
        # OpenVR texture data
        self.texture = vr.Texture_t()
        self.texture.handle = self.texture_id
        self.texture.eType = vr.TextureType_OpenGL
        self.texture.eColorSpace = vr.ColorSpace_Gamma
        self.angle = 0
    def __enter__(self):
        "For lexically scoped cleanup, using the 'with' keyword"
        return self

    def __exit__(self, type_arg, value, traceback):
        "For lexically scoped cleanup, using the 'with' keyword"
        if self.vr_system is not None:
            vr.shutdown
            self.vr_system = None
        glDeleteTextures([self.texture_id])
        glDeleteRenderbuffers([self.depth_buffer])
        glDeleteFramebuffers([self.fb])
        self.fb = 0
    def display(self):
        self.compositor.waitGetPoses(self.poses, None)
        vr.VRInput().updateActionState(righta)
        t.append(vr.VRInput().getPoseActionDataForNextFrame(righta,0,False))
        try:
            #self.coolthing.render()
            square()
            ...
        except:
            print('Error')
        if True:
            glClearColor(0.8, 0.4, 0.4, 0)
            glScale(100,100,100)
            glTranslatef(209,240,0)
            square()
            #glClearColor(0.8, 0.4, 0.4, 0) # Pink background
            glClear(GL_COLOR_BUFFER_BIT)
            # glutSwapBuffers()
            glFlush() # Single buffer
        glBindFramebuffer(GL_FRAMEBUFFER, self.fb)
        glClearColor(0.8, 0.4, 0.4, 0) # Pink background
        glScale(100,100,100)
        glTranslatef(209,240,0)
        square()
        glClear(GL_COLOR_BUFFER_BIT)
        glBindFramebuffer(GL_FRAMEBUFFER, 0)
        #
        # TODO: use different textures for each eye
        self.compositor.submit(vr.Eye_Left, self.texture)
        self.compositor.submit(vr.Eye_Right, self.texture)
        glBindFramebuffer(GL_FRAMEBUFFER, 0)
    def key_press(self,key,x,y):
        print('BRUH')
        if ord(key) == 27:
            # print "Escape!"
            if bool(glutLeaveMainLoop):
                glutLeaveMainLoop()
            else:
                raise Exception("Application quit")
with FireHawkVR() as fhvr:
    glutMainLoop()

Can anyone help? Thanks in advance!

zurgeg commented 4 years ago

System Specs are: 1050 Ti i7 8700 16GB of ram

zurgeg commented 4 years ago

Also as a debugging step put the square displaying code in Pink World, that didn't work. Also, does the object need to be a GL_TRIANGLE? Could that cause any problems? If so do you have example GL commands that I could try? Thanks! I'm trying the following instructions:


    glBegin(GL_TRIANGLES)                    # Start Drawing The Pyramid

    glColor3f(1.0,0.0,0.0)            # Red
    glVertex3f( 0.0, 1.0, 0.0)        # Top Of Triangle (Front)
    glColor3f(0.0,1.0,0.0)            # Green
    glVertex3f(-1.0,-1.0, 1.0)        # Left Of Triangle (Front)
    glColor3f(0.0,0.0,1.0)            # Blue
    glVertex3f( 1.0,-1.0, 1.0)

    glColor3f(1.0,0.0,0.0)            # Red
    glVertex3f( 0.0, 1.0, 0.0)        # Top Of Triangle (Right)
    glColor3f(0.0,0.0,1.0)            # Blue
    glVertex3f( 1.0,-1.0, 1.0)        # Left Of Triangle (Right)
    glColor3f(0.0,1.0,0.0)            # Green
    glVertex3f( 1.0,-1.0, -1.0)        # Right

    glColor3f(1.0,0.0,0.0)            # Red
    glVertex3f( 0.0, 1.0, 0.0)        # Top Of Triangle (Back)
    glColor3f(0.0,1.0,0.0)            # Green
    glVertex3f( 1.0,-1.0, -1.0)        # Left Of Triangle (Back)
    glColor3f(0.0,0.0,1.0)            # Blue
    glVertex3f(-1.0,-1.0, -1.0)        # Right Of

    glColor3f(1.0,0.0,0.0)            # Red
    glVertex3f( 0.0, 1.0, 0.0)        # Top Of Triangle (Left)
    glColor3f(0.0,0.0,1.0)            # Blue
    glVertex3f(-1.0,-1.0,-1.0)        # Left Of Triangle (Left)
    glColor3f(0.0,1.0,0.0)            # Green
    glVertex3f(-1.0,-1.0, 1.0)        # Right Of Triangle (Left)
    glEnd()

    glLoadIdentity()
    glTranslatef(1.5,0.0,-7.0)        # Move Right And Into The Screen
    glRotatef(rquad,1.0,1.0,1.0)        # Rotate The Cube On X, Y & Z
    glBegin(GL_QUADS)            # Start Drawing The Cube

    glColor3f(0.0,1.0,0.0)            # Set The Color To Blue
    glVertex3f( 1.0, 1.0,-1.0)        # Top Right Of The Quad (Top)
    glVertex3f(-1.0, 1.0,-1.0)        # Top Left Of The Quad (Top)
    glVertex3f(-1.0, 1.0, 1.0)        # Bottom Left Of The Quad (Top)
    glVertex3f( 1.0, 1.0, 1.0)        # Bottom Right Of The Quad (Top)

    glColor3f(1.0,0.5,0.0)            # Set The Color To Orange
    glVertex3f( 1.0,-1.0, 1.0)        # Top Right Of The Quad (Bottom)
    glVertex3f(-1.0,-1.0, 1.0)        # Top Left Of The Quad (Bottom)
    glVertex3f(-1.0,-1.0,-1.0)        # Bottom Left Of The Quad (Bottom)
    glVertex3f( 1.0,-1.0,-1.0)        # Bottom Right Of The Quad (Bottom)

    glColor3f(1.0,0.0,0.0)            # Set The Color To Red
    glVertex3f( 1.0, 1.0, 1.0)        # Top Right Of The Quad (Front)
    glVertex3f(-1.0, 1.0, 1.0)        # Top Left Of The Quad (Front)
    glVertex3f(-1.0,-1.0, 1.0)        # Bottom Left Of The Quad (Front)
    glVertex3f( 1.0,-1.0, 1.0)        # Bottom Right Of The Quad (Front)

    glColor3f(1.0,1.0,0.0)            # Set The Color To Yellow
    glVertex3f( 1.0,-1.0,-1.0)        # Bottom Left Of The Quad (Back)
    glVertex3f(-1.0,-1.0,-1.0)        # Bottom Right Of The Quad (Back)
    glVertex3f(-1.0, 1.0,-1.0)        # Top Right Of The Quad (Back)
    glVertex3f( 1.0, 1.0,-1.0)        # Top Left Of The Quad (Back)

    glColor3f(0.0,0.0,1.0)            # Set The Color To Blue
    glVertex3f(-1.0, 1.0, 1.0)        # Top Right Of The Quad (Left)
    glVertex3f(-1.0, 1.0,-1.0)        # Top Left Of The Quad (Left)
    glVertex3f(-1.0,-1.0,-1.0)        # Bottom Left Of The Quad (Left)
    glVertex3f(-1.0,-1.0, 1.0)        # Bottom Right Of The Quad (Left)

    glColor3f(1.0,0.0,1.0)            # Set The Color To Violet
    glVertex3f( 1.0, 1.0,-1.0)        # Top Right Of The Quad (Right)
    glVertex3f( 1.0, 1.0, 1.0)        # Top Left Of The Quad (Right)
    glVertex3f( 1.0,-1.0, 1.0)        # Bottom Left Of The Quad (Right)
    glVertex3f( 1.0,-1.0,-1.0)        # Bottom Right Of The Quad (Right)
    glEnd()                # Done Drawing The Quad