OpenGL Stereo ?

Alles rund um die Programmierung mit Qt
Antworten
Urkel05
Beiträge: 2
Registriert: 26. Oktober 2009 11:21

OpenGL Stereo ?

Beitrag von Urkel05 »

Hallo Leute,

ich habe ein Problem bei der Erzeugung von Stereo Bildern mit OpenGL.
Kann mir da jemand Helfen? Ich poste mal den Code wo ich einen Würfel zeichne und den in Stereo ausgeben will. Wäre nett, wenn mal jemand drüber schauen könnte:

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;

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

glEnd();
glFlush();
glDrawBuffer(GL_BACK_RIGHT);

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;

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

glEnd();
glFlush();
swapBuffers();


}

Danke im vorraus.
solarix
Beiträge: 1133
Registriert: 7. Juni 2007 19:25

Beitrag von solarix »

ich habe ein Problem bei der Erzeugung von Stereo Bildern mit OpenGL.
Was für ein Problem? Ich habe den Verdacht du kommst selbst auf die Lösung, wenn du es exakt beschreibst..
Urkel05
Beiträge: 2
Registriert: 26. Oktober 2009 11:21

Beitrag von Urkel05 »

solarix hat geschrieben:
ich habe ein Problem bei der Erzeugung von Stereo Bildern mit OpenGL.
Was für ein Problem? Ich habe den Verdacht du kommst selbst auf die Lösung, wenn du es exakt beschreibst..
Hattest recht gehabt. Hab es gelöst. Sowohl der rechte und der linke Buffer zeichnen die Szene. Aber jetzt stellt sich ein anderes Problem. Meine Mausinteraktion funktioniert nicht mehr. Vorher konnte ich die Szene per Mausklick um verschiedene Achsen rotieren lassen. Aber jetzt bleibt die Szene so wie sie ist. Jedoch erkennt man im Hintergrund, dass einer der Achsen sich bewegt. Das ist nur ganz kurz zu erkennen. Der code sieht so aus:


#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 = 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;

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;

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 += rotationDif;
}
updateGL();
}

Weiß einer woran das liegen könnte? Danke euch im vorraus.
solarix
Beiträge: 1133
Registriert: 7. Juni 2007 19:25

Beitrag von solarix »

Du traust uns bei diesem Chaos viel zu... folgende Vorschläge:

1. Räum auf!
Mach einen Backup und lösche alles, was nicht mehr benötigt wird (auskommentierter Code und überflüssige Scope-Blöcke). Schreibe wiederverwendbarer Code (z.B. drawScene(left); drawScene(right);), so dass der Code kleiner wird!

2. Verifiziere!
Nutze Debugger und/oder qDebug um deine Berechnungen zu prüfen:

Code: Alles auswählen

qDebug() << "Neue Rotation:";
for(int i = 0; i < 3; ++i) {
  m_rotation[i] += rotationDif[i];
  qDebug() << i << m_rotation[i];
}
3. Benutze Code-Tags (wie ich eben), so dass wir den Code auch lesen können!

hth...
Zonk
Beiträge: 51
Registriert: 17. September 2006 15:28
Kontaktdaten:

Beitrag von Zonk »

Wieso selber bauen wenns das schon fertig gibt?

Nutz OpenScenegraph, es besitzt auch eine Qt-Schnittstelle, und darin kannst du sowohl OSG als auch plain OpenGL zeichnen, und alle OSG Features wie Stereo sind out of the box vorhanden.

GRüße Zonk
Antworten