Skip to content

Commit

Permalink
fixup! ovr: Add HMDCamera.
Browse files Browse the repository at this point in the history
  • Loading branch information
Squareys committed Jun 5, 2015
1 parent fd78343 commit c638343
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
91 changes: 88 additions & 3 deletions src/ovr/HMDCamera.cpp
Expand Up @@ -24,22 +24,107 @@
*/
#include "HMDCamera.h"

#include <Magnum/Math/Vector.h>
#include <Magnum/Texture.h>
#include <Magnum/Framebuffer.h>
#include <Magnum/Math/Matrix.h>
#include <Magnum/Math/Matrix3.h>
#include <Magnum/Math/Matrix4.h>
#include <Magnum/TextureFormat.h>
#include <Magnum/ColorFormat.h>
#include <Magnum/Extensions.h>
#include <Magnum/Context.h>
#include <Magnum/Image.h>

#include "OVR/OVR_CAPI_GL.h"
#undef near

namespace Magnum { namespace Examples {

HMDCamera::HMDCamera(SceneGraph::AbstractObject3D& object): SceneGraph::Camera3D(object) {
HMDCamera::HMDCamera(ovrHmd hmd, ovrEyeType eye, SceneGraph::AbstractObject3D& object): SceneGraph::Camera3D(object), _hmd(hmd) {
ovrSizei _ovrTextureSize = ovrHmd_GetFovTextureSize(_hmd, eye, _hmd->DefaultEyeFov[eye], 1);
_textureSize = Vector2i::from(reinterpret_cast<const int*>(&_ovrTextureSize));

createEyeRenderTexture(_textureSize);
// setViewport(_textureSize);

// const Float near = 0.01f;
// const ovrFovPort fov = _hmd->DefaultEyeFov[eye];
// const Float xyScale = (fov.RightTan + fov.LeftTan) * near;
// setPerspective({xyScale, xyScale / _textureSize.aspectRatio()}, near, 100.0f);
setPerspective(Deg(90.0f), _textureSize.aspectRatio(), 0.01f, 100.0f); // temporary, until framebuffers do what they are supposed to
}

HMDCamera::~HMDCamera() {
ovrHmd_DestroySwapTextureSet(_hmd, _textureSet);
}

void HMDCamera::createEyeRenderTexture(Vector2i size) {
ovrHmd_CreateSwapTextureSetGL(_hmd, GL_RGBA, size.x(), size.y(), &_textureSet);

// wrap the texture set for magnum
_eyeRenderTextures = new Texture2D*[_textureSet->TextureCount];

for(int i = 0; i < _textureSet->TextureCount; ++i) {
ovrGLTexture* tex = (ovrGLTexture*)&_textureSet->Textures[i];

_eyeRenderTextures[i] = new Texture2D(Texture2D::wrap(tex->OGL.TexId));
_eyeRenderTextures[i]->setMinificationFilter(Sampler::Filter::Linear)
.setMagnificationFilter(Sampler::Filter::Linear)
.setWrapping(Sampler::Wrapping::ClampToEdge);
}

// create the framebuffer which will be used to render to the current texture
// of the texture set later
_framebuffer = new Framebuffer(Range2Di({}, size));
_framebuffer->mapForDraw(Framebuffer::ColorAttachment(0));

// setup depth attachment
ColorType type = ColorType::UnsignedInt;
TextureFormat format = TextureFormat::DepthComponent24;

if(Context::current()->isExtensionSupported<Extensions::GL::ARB::depth_buffer_float>()) {
format = TextureFormat::DepthComponent32F;
type = ColorType::Float;
}

Image2D image(ColorFormat::DepthComponent, type, size, nullptr);

_depth = new Texture2D();
_depth->setMinificationFilter(Sampler::Filter::Linear)
.setMagnificationFilter(Sampler::Filter::Linear)
.setWrapping(Sampler::Wrapping::ClampToEdge)
.setStorage(1, format, size)
.subImage(1, {{}, size}, image);
}

void HMDCamera::draw(SceneGraph::DrawableGroup3D& group) {
// increment to use next texture
_textureSet->CurrentIndex = (_textureSet->CurrentIndex + 1) % _textureSet->TextureCount;

// switch to eye render target and bind render textures
_framebuffer->bind()
.attachTexture(Framebuffer::ColorAttachment(0), *(_eyeRenderTextures[_textureSet->CurrentIndex]), 0)
.attachTexture(Framebuffer::BufferAttachment::Depth, *_depth, 0)
.setViewport(Range2Di::fromSize({}, _textureSize))
// clear with the standard grey so that at least that will be visible in case the scene is
// not correctly set up.
.clear(FramebufferClear::Color | FramebufferClear::Depth);

// render scene
SceneGraph::Camera3D::draw(group);

// code for stereo rendering here...
// Reasoning for the next two lines, taken from the Oculus SDK examples code:
// Without this, during the next while loop iteration [... of this method ...]
// would bind a framebuffer with an invalid COLOR_ATTACHMENT0 because the texture ID
// associated with COLOR_ATTACHMENT0 had been unlocked by calling wglDXUnlockObjectsNV.

// Should match:
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);

// possibly some other code for distortion here...
// _framebuffer->attachTexture(Framebuffer::ColorAttachment(0), ?, 0);
// _framebuffer->attachTexture(Framebuffer::BufferAttachment::Depth, ?, 0);
}

}}
27 changes: 26 additions & 1 deletion src/ovr/HMDCamera.h
Expand Up @@ -30,16 +30,41 @@

#include "Types.h"

#define OVR_DLL_IMPORT
#include "OVR/OVR_CAPI_0_6_0.h"

namespace Magnum { namespace Examples {

class HMDCamera: public SceneGraph::Camera3D {
public:
explicit HMDCamera(SceneGraph::AbstractObject3D& object);
explicit HMDCamera(ovrHmd hmd, ovrEyeType eye, SceneGraph::AbstractObject3D& object);

~HMDCamera();

void draw(SceneGraph::DrawableGroup3D& group) override;

ovrSwapTextureSet* getTextureSet() const {
return _textureSet;
}

ovrRecti getViewport() const {
return ovrRecti{{0, 0}, _ovrTextureSize};
}

private:

void createEyeRenderTexture(Vector2i size);

Texture2D** _eyeRenderTextures;
Texture2D* _depth;

ovrHmd _hmd;
ovrSizei _ovrTextureSize;
ovrRecti _viewport;
ovrSwapTextureSet* _textureSet;

Vector2i _textureSize;
Framebuffer* _framebuffer;
};

}}
Expand Down

0 comments on commit c638343

Please sign in to comment.