PDA

View Full Version : Stereo Problem



Urkel05
10-27-2009, 03:34 AM
Hi everybody,

i hav to implement a 3D Cube in Stereo. I tried so long to realize it with the BACK_LEFT and BACK_RIGHT Buffers. After a long Time both Buffers are drawing the Cube. But it does not look like real Stereo. I will post my Code now. If somebody can look at the Code it would help me a lot. Did I everything right?

#include "cube.hpp"
#include <QtOpenGL>
#include <QtGui>
#include <qdebug>
#define DTR 0.0174532925
Cube_test::Cube_test(QWidget *pParent) :
QGLWidget(pParent)
{
glEnable(GL_STEREO);
QGLFormat fmt;
fmt.setAlpha(true);
fmt.setStereo(true);
for(int i = 0; i < 3; ++i)
{
m_rotation[i] = 0.0f;
}
depthZ=-10;
fovy=45;
nearZ=3.0;
farZ=30.0;
screenZ = 10.0;
IOD = 0.5;
}

Cube_test::~Cube_test()
{

}

void Cube_test::initializeGL()
{
qglClearColor(Qt::black);
glShadeModel(GL_SMOOTH);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

}

void Cube_test::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
aspect=double(width)/double(height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float x = static_cast<float> (width) / static_cast<float> (height);
glFrustum(-x, +x, -1.0f, +1.0f, 4.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
setFrustum();
}
void Cube_test::setFrustum()
{
double top=nearZ*tan(DTR*fovy/2);
double right=aspect*top;
double frustumshift=(IOD/2)*nearZ/screenZ;

leftCam.topfrustum=top;
leftCam.bottomfrustum=-top;
leftCam.leftfrustum=-right+frustumshift;
leftCam.rightfrustum=right+frustumshift;
leftCam.modeltranslation=IOD/2;

rightCam.topfrustum=top;
rightCam.bottomfrustum=-top;
rightCam.leftfrustum=-right-frustumshift;
rightCam.rightfrustum=right-frustumshift;
rightCam.modeltranslation=-IOD/2;
}
void Cube_test:aintGL()
{
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

const float camPos[3] = { 10.0f, 10.0f, 10.0f };

glRotatef(35.0f, 1.0f, 0.0f, 0.0f);
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(-camPos[0], -camPos[1], -camPos[2]);

// user cam rotation
glRotatef(m_rotation[0], 1.0f, 0.0f, 0.0f);
glRotatef(m_rotation[1], 0.0f, 1.0f, 0.0f);
glRotatef(m_rotation[2], 0.0f, 0.0f, 1.0f);

const int numIndices = 24;
const int numVertices = 8;

const int indices[numIndices] = { 3, 2, 1, 0, 2, 6, 5, 1, 2, 3, 7, 6, 4, 7, 3, 0, 5, 4, 0, 1, 7, 4, 5, 6};

const float vertices[numVertices][3] =
{
{-1.0f, -1.0f, -1.0f},
{-1.0f, -1.0f, 1.0f},
{1.0f, -1.0f, 1.0f},
{1.0f, -1.0f, -1.0f},

{-1.0f, 1.0f, -1.0f},
{-1.0f, 1.0f, 1.0f},
{1.0f, 1.0f, 1.0f},
{1.0f, 1.0f, -1.0f},
};

const QColor vertexColors[numVertices] =
{
Qt::red,
Qt::green,
Qt::blue,
Qt::white,
Qt::black,
Qt::yellow,
Qt::darkRed,
Qt::darkGreen
};


glDrawBuffer(GL_BACK_LEFT);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();
glFrustum(leftCam.leftfrustum,leftCam.rightfrustum ,leftCam.bottomfrustum,leftCam.topfrustum,nearZ,fa rZ); // move rightward for left eye view
glTranslatef(leftCam.modeltranslation,0.0,0.0);
glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
//glPushMatrix();
{
// glTranslatef(0.0,0.0,depthZ);
drawGrid();
drawAxis();

glBegin(GL_QUADS);

for(int i = 0; i < numIndices; ++i)
{
const int currentIndex = indices[i];

qglColor(vertexColors[currentIndex]);
glVertex3f(vertices[currentIndex][0], vertices[currentIndex][1], vertices[currentIndex][2]);
}

glEnd();
}
glPopMatrix();

glDrawBuffer(GL_BACK_RIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(rightCam.leftfrustum,rightCam.rightfrust um,rightCam.bottomfrustum,rightCam.topfrustum,near Z,farZ);
glTranslatef(rightCam.modeltranslation,0.0,0.0);

glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
//glPushMatrix();
{
// glTranslatef(0.0,0.0,depthZ);
drawGrid();
drawAxis();
glBegin(GL_QUADS);

for(int i = 0; i < numIndices; ++i)
{
const int currentIndex = indices[i];

qglColor(vertexColors[currentIndex]);
glVertex3f(vertices[currentIndex][0], vertices[currentIndex][1], vertices[currentIndex][2]);
}

glEnd();
}

// glPopMatrix();
swapBuffers();
}

void Cube_test::drawAxis()
{
const float axisLength = 100.0f;

glBegin(GL_LINES);

glLineWidth(5);

qglColor(Qt::yellow);
glVertex3f(-axisLength, 0.0f, 0.0f);
glVertex3f(axisLength, 0.0f, 0.0f);

qglColor(Qt::green);
glVertex3f(0.0f, -axisLength, 0.0f);
glVertex3f(0.0f, axisLength, 0.0f);

qglColor(Qt::red);
glVertex3f(0.0f, 0.0f, -axisLength);
glVertex3f(0.0f, 0.0f, axisLength);

glEnd();
}

void Cube_test::drawGrid()
{
glBegin(GL_LINES);

qglColor(Qt::gray);

const float gridLineLength = 1000.0f * 0.5f;

glLineWidth(5);

for(float pos=-100.0f; pos<100.0f; pos += 0.5f)
{
if(pos == 0.0f) // do not render Axis here
{
continue;
}

glVertex3f(-gridLineLength, 0.0f, pos);
glVertex3f(gridLineLength, 0.0f, pos);
glVertex3f(pos, 0.0f, -gridLineLength);
glVertex3f(pos, 0.0f, gridLineLength);
}
glEnd();
}

void Cube_test::mousePressEvent(QMouseEvent *pEvent)
{
m_lastMousePos = pEvent->pos();
}

void Cube_test::mouseMoveEvent(QMouseEvent *pEvent)
{
const bool leftBtnPressed = (pEvent->buttons() & Qt::LeftButton) != 0;
const bool rightBtnPressed = (pEvent->buttons() & Qt::RightButton) != 0;

if(leftBtnPressed == false &amp;&amp; rightBtnPressed == false)
{
m_lastMousePos = pEvent->pos();
return;
}

const float rotDx = 180.0f * static_cast<float> (pEvent->x() - m_lastMousePos.x()) / static_cast<float> (width());
const float rotDy = 180.0f * static_cast<float> (pEvent->y() - m_lastMousePos.y()) / static_cast<float> (height());

m_lastMousePos = pEvent->pos();

float rotationDif[3] = { rotDy, 0.0f, 0.0f };

if(leftBtnPressed == true)
{
rotationDif[1] = rotDx;
}
else
{
rotationDif[2] = rotDx;
}

for(int i = 0; i < 3; ++i)
{
m_rotation[i] += rotationDif[i];
}
updateGL();
}




I hope you can help me.Thank you

knackered
10-28-2009, 12:14 PM
firstly, glEnable(GL_STEREO) is not a valid call. Even if it was, it looks like you're doing it before an opengl context has been created.
secondly, i've no idea about how Qt sets up pixel formats, so I'll take your word for it that you're doing it correctly.
thirdly, are you using an nvidia quadro, or other card that supports quad buffered stereo?
fourthy, if you are using a quadro, have you enabled stereo in the nvidia control panel?

dukey
10-30-2009, 06:35 AM
You need a card that supports stereo pixel formats
eg a quadro card

Then you need to setup a quad buffer pixel format

Jan
10-30-2009, 09:18 AM
With Qt setting up a stereo context is indeed that easy (setStereo (true)), he is doing that correct.

Most problems with stereo, that i had, were that stereo was not properly activated on a PC. Even if it worked yesterday, the PCs i used took every reason to disable stereo again. It is a pain in the ass. I suggest you first start ANOTHER app with stereo (that should work), and check whether THAT properly renders. If it doesn't, you need to fix the OS'es settings first.

Other than that your code looks good (except that your "paintGL" function is called "aintGL" ??)

Jan.