PDA

View Full Version : Interfacing with Stereo Cameras, Textures Not Displaying



Jciaccio89
04-27-2015, 09:19 PM
Hey Guys,

I have been working with OpenCV and OpenGl to interface two stereo cameras with an Oculus rift. The idea is to set the window up such that I can pass the window name and other information to the rift and have the rift do all of the rendering necessary for me to get it to display correctly. I must admit that I am kind of a beginner when it comes to C++ and any sort of advanced coding such as this, but I have a decent handle on the basics. OpenGL is an entirely new thing for me and I was hoping that I could pick your brains about a problem that I am having. I was able to get the window correctly, using GLUT but unfortunately mixing GLUT with the Oculus API is not easily possible. I had hoped to be able to just transfer most of the basic gl commands from GLUT to GLFW, create my own loop and be on my way. This has not been the case, I have been scouring the web for some time looking for answers but have not found any. Apologies if its out there somewhere.

My window opens to up to what seems to be the correct size, but it remains white only and just sort of hangs. After a few weeks of research trying to figure out the proper way to organize the program and resolve this issue, I believe that my structure is correct. I'm thinking there is something small that I am missing that I cannot for the life of me figure out. Thanks in advance for any help you guys can give... just using the resources on this forum has been excellent already.

Also, there is some OpenCV in there... just changing from a mat file to a texture, I do not believe that is the source of my issue. The same code was run with GLUT and it transferred the data to a texture correctly.



void Mat2Tex(VideoCapture *capture, GLuint *texture, int flip)
{
Mat image; // Defines the image

*capture >> image; // Captures a new image

glBindTexture(GL_TEXTURE_2D, *texture); // Binds the image to a texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Defines how pixels are unpacked
glTexImage2D(GL_TEXTURE_2D, // Generates the texture from the image
0,
GL_RGB,
image.cols,
image.rows,
0,
GL_BGRA_EXT,
GL_UNSIGNED_BYTE,
image.data);
}
void plane(GLfloat x, GLfloat y, GLfloat z)
{
glBegin(GL_QUADS);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexCoord2d(0.0, 1.0);
glVertex3d(0.0 + x, 0.0 + y, 0.0 + z);

glTexCoord2d(1.0, 1.0);
glVertex3d(0.5 + x, 0.0 + y, 0.0 + z);

glTexCoord2d(1.0, 0.0);
glVertex3d(0.5 + x, 1.0 + y, 0.0 + z);

glTexCoord2d(0.0, 0.0);
glVertex3d(0.0 + x, 1.0 + y, 0.0 + z);

glEnd();
}

int main(void)
{

GLFWwindow* RiftTarget;
VideoCapture CamR, CamL;
GLuint RightTex, LeftTex;

CamR;
CamR.open(0);

CamL;
CamL.open(1);

glfwSetErrorCallback(Error_Callback);

// Oculus libraries must be initialized before glfw ones

if (!glfwInit())
exit(EXIT_FAILURE);

RiftTarget = glfwCreateWindow(1280, 800, "HMD Display", NULL, NULL);

if (!RiftTarget)
{
glfwTerminate();
exit(EXIT_FAILURE);
}

glfwMakeContextCurrent(RiftTarget);
glfwSwapInterval(1);

glfwSetKeyCallback(RiftTarget, key_callback);

int width, height;
float ratio;
Mat Rframe, Lframe;

glfwGetFramebufferSize(RiftTarget, &width, &height);

glGenTextures(1, &RightTex);
glGenTextures(1, &LeftTex);

glBindTexture(GL_TEXTURE_2D, RightTex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);


glBindTexture(GL_TEXTURE_2D, LeftTex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

while (!glfwWindowShouldClose(RiftTarget));
{

if (glfwGetKey(RiftTarget, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(RiftTarget, GL_TRUE);

glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1, 0, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);

glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glEnable(GL_TEXTURE_2D);

glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
glDepthRange(0.0f, 1.0f);

Mat2Tex(&CamR, &RightTex, 0);
plane(0, 0, 0);

Mat2Tex(&CamL, &LeftTex, 0);
plane(.5, 0, 0);

glfwSwapBuffers(RiftTarget);
glfwPollEvents();

}

glfwDestroyWindow(RiftTarget);

glfwTerminate();
exit(EXIT_SUCCESS);
}





Please let me know if you need anymore info, I am more than willing to provide as... its late and I am out of ideas! Thanks again!! Also, if this should be posted in the GLFW section of this website, please let me know and I will move it, I think the issue is fundamental to OpenGL as a whole, which is why I posted here.