cancel
Showing results for 
Search instead for 
Did you mean: 

OpenGL example code not working in MAC Xcode

sixunone
Honored Guest
Hi,

I have been trying to run this code I got from a previous post "Simple, one page, OpenGL example (Oculus SDK 0.3.2 Preview)" on my Mac Xcode 6.0.1 with OpenGL:2.1, GLFW3.1 and GLEW1.11.0. I can build the code without any error and a window gets created fine when I run it, but it only shows black screen and nothing else. I cannot find any problem with the code. I am thinking that it might be something to do with the Project setting or i am using the wrong OpenGL version or sth. Please can someone help me out. 😞 Thanks very much!

Kind regards,

Matt

Please see the code below:

main.cpp

#include "main.h"
int main(int argc, const char * argv[])
{
//Initialise Oculus rift library
OculusInit();
//Initialise GLFW
GLint glfwStatus =glfwInit();
if (glfwStatus!=GL_TRUE) {
cout<<"GLFW failed to initialise!"<<endl;
glfwTerminate();
}
else
{
cout<<"GLFW initialised!"<<endl;
}

//glfwWindowHint(GLFW_SAMPLES, 0);

GLFWwindow*window=glfwCreateWindow(Res.w, Res.h, "testing", NULL, NULL);
if (window==NULL) {
cout<<"Window failed to create!"<<endl;
glfwTerminate();
}
else
{
cout<<"Window created!"<<endl;
}
glfwMakeContextCurrent(window);


//Initialise GLEW
glewExperimental = GL_TRUE;
GLenum l_Result =glewInit();
if (l_Result!=GLEW_OK)
{
cout<<"GLEW failed to initialise!"<<endl;
exit(EXIT_FAILURE);
}
else
{
cout<<"GLEW initialised!"<<endl;
}
int l_Major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
int l_Minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
int l_Profile = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
cout<<"Using OpenGL:"<<l_Major<<"."<<l_Minor<<endl;
//Set up Opengl State lighting,materials etc.
SetupGL();

//Get FOV texture size
Sizei recommendTex0Size = ovrHmd_GetFovTextureSize(l_Hmd, ovrEye_Left, l_HmdDesc.DefaultEyeFov[0], 1.0f);
Sizei recommendTex1Size = ovrHmd_GetFovTextureSize(l_Hmd, ovrEye_Right, l_HmdDesc.DefaultEyeFov[1], 1.0f);
Sizei renderTargetSize;
renderTargetSize.w = recommendTex0Size.w +recommendTex1Size.w;
renderTargetSize.h = max(recommendTex0Size.h, recommendTex1Size.h);

cout<<"test point1- before Create FBO"<<endl;

// Create FBO...

GLuint l_FBOId;
glGenFramebuffersEXT(1, &l_FBOId);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, l_FBOId);

// The texture we're going to render to...
GLuint l_TextureId;
glGenTextures(1, &l_TextureId);
// "Bind" the newly created texture : all future texture functions will modify this texture...
glBindTexture(GL_TEXTURE_2D, l_TextureId);
// Give an empty image to OpenGL (the last "0")
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, renderTargetSize.w, renderTargetSize.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
// Linear filtering...
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// Create Depth Buffer...
GLuint l_DepthBufferId;
glGenRenderbuffersEXT(1, &l_DepthBufferId);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, l_DepthBufferId);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, renderTargetSize.w, renderTargetSize.h);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, l_DepthBufferId);

// Set the texture as our colour attachment #0...
glFramebufferTextureEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0, l_TextureId, 0);

// Set the list of draw buffers...
GLenum l_GLDrawBuffers[1] = { GL_COLOR_ATTACHMENT0_EXT };
glDrawBuffers(1, l_GLDrawBuffers); // "1" is the size of DrawBuffers

//Check
GLenum FBO_state = glCheckFramebufferStatusEXT(GL_DRAW_FRAMEBUFFER_EXT);
if (FBO_state!=GL_FRAMEBUFFER_COMPLETE_EXT)
{
cout<<"There is a problem with the FBO."<<endl;
cout<<FBO_state<<endl;
exit(EXIT_FAILURE);
}
else
{
cout<<"FBO checked fine!"<<endl;
}
// Unbind...
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
cout<<"test point- after Create FBO"<<endl;
//Configre OpenGL

l_Cfg.OGL.Header.API = ovrRenderAPI_OpenGL;
l_Cfg.OGL.Header.RTSize.w = Res.w;
l_Cfg.OGL.Header.RTSize.h = Res.h;
l_Cfg.OGL.Header.Multisample = 0;

cout<<"test point- before ovrHmd_ConfigureRendering"<<endl;

ovrHmd_ConfigureRendering(l_Hmd, &l_Cfg.Config, l_HmdDesc.DistortionCaps, l_HmdDesc.DefaultEyeFov, l_Eyes);
cout<<"test point- after ovrHmd_ConfigureRendering"<<endl;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//Left eye
ovrTexture ovr_EyeTexture[2];
ovr_EyeTexture[0].Header.API = ovrRenderAPI_OpenGL;
ovr_EyeTexture[0].Header.TextureSize.w= renderTargetSize.w;
ovr_EyeTexture[0].Header.TextureSize.h= renderTargetSize.h;
ovr_EyeTexture[0].Header.RenderViewport.Pos.x=0;
ovr_EyeTexture[0].Header.RenderViewport.Pos.y=0;
ovr_EyeTexture[0].Header.RenderViewport.Size.w=renderTargetSize.w/2;
ovr_EyeTexture[0].Header.RenderViewport.Size.h=renderTargetSize.h;
((ovrGLTexture*)(&ovr_EyeTexture[0]))->OGL.TexId =l_TextureId;

//Right eye
ovr_EyeTexture[1]=ovr_EyeTexture[0];
ovr_EyeTexture[1].Header.RenderViewport.Pos.x=(renderTargetSize.w+1)/2;

cout<<"test point2- After Eyes configuration"<<endl;
glfwSetKeyCallback(window, KeyCallback);
glfwSetWindowSizeCallback(window, WindowSizeCallback);

GLfloat l_SpinX;
GLfloat l_SpinY;
//main loop
while (!glfwWindowShouldClose(window))
{

l_SpinX = 30.0f;
l_SpinY = 40.0f;

ovrFrameTiming l_HmdFrameTiming = ovrHmd_BeginFrame(l_Hmd, 0);
// Bind the FBO...
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, l_FBOId);
// Clear...
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

ovrPosef l_HeadPose[2];
for (int l_EyeIndex=0; l_EyeIndex<ovrEye_Count; l_EyeIndex++)
{
ovrEyeType l_Eye = l_HmdDesc.EyeRenderOrder[l_EyeIndex];
l_HeadPose[l_Eye] = ovrHmd_GetEyePose(l_Hmd, l_Eye);
glViewport(
ovr_EyeTexture[l_Eye].Header.RenderViewport.Pos.x,
ovr_EyeTexture[l_Eye].Header.RenderViewport.Pos.y,
ovr_EyeTexture[l_Eye].Header.RenderViewport.Size.w,
ovr_EyeTexture[l_Eye].Header.RenderViewport.Size.h
);
// Get Projection and ModelView matrici from the device...
Matrix4f l_ProjectionMatrix = ovrMatrix4f_Projection(l_Eyes[l_Eye].Fov, 0.3f, 100.0f, true);
Quatf l_Orientation = Quatf(l_HeadPose[l_Eye].Orientation);
Matrix4f l_ModelViewMatrix = Matrix4f(l_Orientation.Inverted());

// Pass matrici on to OpenGL...
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMultMatrixf(&(l_ProjectionMatrix.Transposed().M[0][0]));
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Translate for specific eye based on IPD...
glTranslatef(l_Eyes[l_Eye].ViewAdjust.x,
l_Eyes[l_Eye].ViewAdjust.y,
l_Eyes[l_Eye].ViewAdjust.z);

// Multiply with orientation retrieved from sensor...
glMultMatrixf(&(l_ModelViewMatrix.Transposed().M[0][0]));
// Move back a bit to show scene in front of us...
glTranslatef(0.0f, 0.0f, -2.0f);

glRotatef(l_SpinX, 1.0f, 0.0f, 0.0f);
glRotatef(l_SpinY, 0.0f, 1.0f, 0.0f);
//render
//RenderCubeFixedFunction();
RenderCubeVertexArrays();

}
ovrHmd_EndFrame(l_Hmd);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

//GLint WindowWidth, WindowHeight;
//glfwGetWindowSize(window, &WindowWidth, &WindowHeight);
//glViewport(0, 0, WindowWidth, WindowHeight);
glfwPollEvents();
//glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
ovrHmd_Destroy(l_Hmd);
ovr_Shutdown();

glfwTerminate();
return 0;
}


main.h

// main.h
#ifndef __Oculus_video_tool__main__
#define __Oculus_video_tool__main__
#define GLFW_INCLUDE_GLCOREAPB
#include <GL/glew.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <GLUT/GLUT.h>
#include <ApplicationServices/ApplicationServices.h>
#include <stdlib.h>
#include <stdio.h>
#include <GLFW/glfw3.h>
//#include <GLFW/glfw3native.h>
//#include <glm/glm.hpp>
#include <OVR.h>
#include <OVR_CAPI.h>
#include <OVR_CAPI_GL.h>
#include "CAPI_GL_DistortionRenderer.h"
#include <iostream>
#include <fstream>
#include <streambuf>
#include <string>
#include <math.h>
using namespace std;
using namespace OVR;

ovrHmd l_Hmd;
ovrHmdDesc l_HmdDesc;
ovrEyeRenderDesc l_Eyes[2];
ovrGLConfig l_Cfg;
ovrSizei Res;
const bool l_MultiSampling = false;

GLfloat l_VAPoints[] =
{
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f,-0.5f, 0.5f,
0.5f,-0.5f, 0.5f,
-0.5f,-0.5f,-0.5f,
-0.5f, 0.5f,-0.5f,
0.5f, 0.5f,-0.5f,
0.5f,-0.5f,-0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f,-0.5f,
-0.5f, 0.5f,-0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f,-0.5f,-0.5f,
0.5f,-0.5f,-0.5f,
0.5f,-0.5f, 0.5f,
-0.5f,-0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f,-0.5f, 0.5f,
0.5f,-0.5f,-0.5f,
0.5f, 0.5f,-0.5f,
-0.5f,-0.5f,-0.5f,
-0.5f,-0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f,-0.5f
};

GLfloat l_VANormals[] =
{
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f,-1.0f,
0.0f, 0.0f,-1.0f,
0.0f, 0.0f,-1.0f,
0.0f, 0.0f,-1.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f,-1.0f, 0.0f,
0.0f,-1.0f, 0.0f,
0.0f,-1.0f, 0.0f,
0.0f,-1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, 0.0f
};

GLuint l_VAIndici[] =
{
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15,
16, 17, 18, 19,
20, 21, 22, 23
};


void OculusInit()
{
ovr_Initialize();
l_Hmd = ovrHmd_Create(0);
if (l_Hmd==NULL)
{
ovrHmd_CreateDebug(ovrHmd_DK1);
cout<<"Oculus Rift not connected!"<<endl;
Res.w=1200;
Res.h=800;
}
else
{
cout<<"Oculus Rift connected!"<<endl;
ovrHmd_GetDesc(l_Hmd, &l_HmdDesc);
//Starting sensor
ovrHmd_StartSensor(l_Hmd, ovrSensorCap_Orientation | ovrSensorCap_YawCorrection |ovrSensorCap_Position, ovrSensorCap_Orientation);
Res.w=l_HmdDesc.Resolution.w;
Res.h=l_HmdDesc.Resolution.h;
}

}

static void KeyCallback(GLFWwindow* p_Window, int p_Key, int p_Scancode, int p_Action, int p_Mods)
{
if (p_Action==GLFW_PRESS)
{
if (p_Key==GLFW_KEY_ESCAPE)
{
glfwSetWindowShouldClose(p_Window, GL_TRUE);
}
else
if (p_Key==GLFW_KEY_R)
{
ovrHmd_ResetSensor(l_Hmd);
}
}
}

// =============================================================================

static void WindowSizeCallback(GLFWwindow* p_Window, int p_Width, int p_Height)
{
l_Cfg.OGL.Header.RTSize.w = p_Width;
l_Cfg.OGL.Header.RTSize.h = p_Height;

ovrHmd_ConfigureRendering(l_Hmd, &l_Cfg.Config, l_HmdDesc.DistortionCaps, l_HmdDesc.DefaultEyeFov, l_Eyes);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void RenderCubeFixedFunction(void)
{
// Obsolete, remains as a fall back for the vertex arrays version...
glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f,-0.5f, 0.5f);
glVertex3f( 0.5f,-0.5f, 0.5f);

glNormal3f( 0.0f, 0.0f,-1.0f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f, 0.5f,-0.5f);
glVertex3f( 0.5f, 0.5f,-0.5f);
glVertex3f( 0.5f,-0.5f,-0.5f);

glNormal3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f( 0.5f, 0.5f,-0.5f);
glVertex3f(-0.5f, 0.5f,-0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);

glNormal3f( 0.0f,-1.0f, 0.0f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f( 0.5f,-0.5f,-0.5f);
glVertex3f( 0.5f,-0.5f, 0.5f);
glVertex3f(-0.5f,-0.5f, 0.5f);

glNormal3f( 1.0f, 0.0f, 0.0f);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f( 0.5f,-0.5f, 0.5f);
glVertex3f( 0.5f,-0.5f,-0.5f);
glVertex3f( 0.5f, 0.5f,-0.5f);

glNormal3f(-1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f,-0.5f);
glEnd();
}

void RenderCubeVertexArrays(void)
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, l_VAPoints);

glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, l_VANormals);

glDrawElements(GL_QUADS, 6*4, GL_UNSIGNED_INT, l_VAIndici);

glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}

static void SetupGL(void)
{
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if (l_MultiSampling)
{
glEnable(GL_MULTISAMPLE);
}
glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
// Some (stationary) lights...
GLfloat l_Light0Position[] = { 5.0f, 6.0f, 3.0f, 0.0f };
GLfloat l_Light0Diffuse[] = { 1.0f, 0.8f, 0.6f, 1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, l_Light0Position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, l_Light0Diffuse);
glEnable(GL_LIGHT0);

GLfloat l_Light1Position[] = { -5.0f, -6.0f, 5.0f, 0.0f };
GLfloat l_Light1Diffuse[] = { 0.6f, 0.8f, 1.0f, 1.0f };
glLightfv(GL_LIGHT1, GL_POSITION, l_Light1Position);
glLightfv(GL_LIGHT1, GL_DIFFUSE, l_Light1Diffuse);
glEnable(GL_LIGHT1);

// Material...
GLfloat l_MaterialSpecular[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat l_MaterialShininess[] = { 10.0f };
glMaterialfv(GL_FRONT, GL_SPECULAR, l_MaterialSpecular);
glMaterialfv(GL_FRONT, GL_SHININESS, l_MaterialShininess);
}

#endif /* defined(__Oculus_video_tool__main__) */



Could it be something wrong I did with the ovrHmd_ConfigureRendering() ?

ovrHmd_ConfigureRendering(l_Hmd, &l_Cfg.Config, l_HmdDesc.DistortionCaps, l_HmdDesc.DefaultEyeFov, l_Eyes);
2 REPLIES 2

sixunone
Honored Guest
fixed it.

I change the sdk from 3.0.2 to 4.0.2 and update the ovrHmd_ConfigureRendering(); to
ovrHmd_ConfigureRendering(l_Hmd, &l_Cfg.Config, l_Hmd->DistortionCaps & ~ovrDistortionCap_FlipInput, l_Hmd->DefaultEyeFov, l_Eyes)

jherico
Adventurer
That's not fixing it. That's just correcting the current bug. You shouldn't be passing in l_Hmd->DistortionCaps to the configure rendering function. You should specifically be creating a value ORd from the specific distortion caps you want.