PDA

View Full Version : OpenGl Stereo



Urkel05
26th October 2009, 10:31
Hi everybody,

i need your help. I have to program a 3D-Scene in Stereo. But it doesn't work. I tried so much, but i don't know where the problem is. Here is the Code. Can somebody look if he can find anything. I give you the part where the Scene is getting drawn:

void Cube_test::paintGL()
{
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
const float camPos[3] = { 10.0f, 10.0f, 10.0f };
glTranslatef(-camPos[0], -camPos[1], -camPos[2]);
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
};

// draw Cube_test
glDrawBuffer(GL_BACK_LEFT);
//glClearColor(0,0,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// set left-eye perspective
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// render objects in scene for left-eye view
glMatrixMode(GL_MODELVIEW);
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();
glFlush();
glDrawBuffer(GL_BACK_RIGHT);
// clear separate draw buffer for right eye rendering
//glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// set right-eye perspective
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// render scene again for right-eye view
glMatrixMode(GL_MODELVIEW);
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();
glFlush();
swapBuffers();


}

i hope somebody can help me. Thank you.

high_flyer
26th October 2009, 15:29
But it doesn't work.
Can you explain WHAT is no working exactly?

Urkel05
26th October 2009, 15:45
The Cube is not getting drawn. I only see ONE line of the grid() function.
Can you explain me the procedure, how to show scenes in stereo? Must i initialize the Stereo function of OpenGL? Is it enough to draw the Scene in BACK_LEFT and BACK_RIGHT Buffer?
How is it handeled with the MatrixMode?

high_flyer
26th October 2009, 16:00
Do you have at least one cube drawn correctly, or ALL you see is just one line?
If he later is the correct answer, then I will stop working on the stereo issue until you have a correctly drawn cube in one perspective.
Then you can start working on the other perspective, knowing that the drawing of the cube it self it ok.

Urkel05
26th October 2009, 16:10
All i saw was the line. But ichanged my implementation. Now i see the Cube drawn from both buffers. The left and the right Buffer are drawing the cube (success :D). But it does not look like Stereo i think. Also my cam Rotation does not work anymore. The scene never changes when i use the mouse. :
The complete Source off the .cpp


#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);
// QGLFormat::setDefaultFormat(fmt);
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::paintGL()
{
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]);
// qglColor(Qt::red);
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 && 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();
}

high_flyer
26th October 2009, 16:43
[Now i see the Cube drawn from both buffers. The left and the right Buffer are drawing the cube (success ). /QUOTE]
Great!:)

[QUOTE]But it does not look like Stereo i think. Also my cam Rotation does not work anymore.
This is not in the scope of this forum, if there are very openGL competent people reading this they might help you, but I would try this more in openGL forums.

However we should be able to help you with making sure the mouse event is called.
Can you set a break point in the mouseMoveEvent() and see if it gets called(or a debug outpout)?

Also, please post code in code tags, its very hard to read code like that.

Urkel05
27th October 2009, 11:32
It is getting called. In the background of the Scene you can see one of the Axis rotating for a second. But the scene stands still. Nothing changes.
Sorry this is the first time that i use a forum. So i don't know well how to post the code.


#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 && 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();
}