57 #include <OpenGL/gl.h>
58 #include <OpenGL/glu.h>
59 #include <OpenGL/glext.h>
84 static PFNGLGENBUFFERSARBPROC glGenBuffers = 0;
85 static PFNGLBINDBUFFERARBPROC glBindBuffer = 0;
86 static PFNGLBUFFERDATAARBPROC glBufferData = 0;
87 static PFNGLMAPBUFFERARBPROC glMapBuffer = 0;
88 static PFNGLUNMAPBUFFERARBPROC glUnmapBuffer = 0;
112 m_bMatrixOnStack =
false;
118 gluDeleteQuadric(m_pQuadric);
145 glMatrixMode(GL_PROJECTION);
153 glMatrixMode(GL_MODELVIEW);
155 glRotatef(180, 1, 0, 0);
176 const float w_factor = width / 640.0f;
177 const float h_factor = height / 480.0f;
178 Math3d::SetMat(K, 580 * w_factor, 0, 320 * w_factor, 0, 580 * h_factor, 240 * h_factor, 0, 0, 1);
181 glMatrixMode(GL_PROJECTION);
189 glMatrixMode(GL_MODELVIEW);
191 glRotatef(180, 1, 0, 0);
196 if (bActivateShading)
198 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
199 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
201 glDepthFunc(GL_LESS);
202 glEnable(GL_DEPTH_TEST);
203 glShadeModel(GL_SMOOTH);
204 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
205 glEnable(GL_LIGHTING);
207 glEnable(GL_NORMALIZE);
208 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
213 glDisable(GL_LIGHTING);
214 glDisable(GL_DEPTH_TEST);
215 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
225 glViewport(0, 0, nWidth, nHeight);
228 m_pQuadric = gluNewQuadric();
231 if (!InitExtension())
250 glViewport(0, 0, width, height);
253 m_pQuadric = gluNewQuadric();
256 if (!InitExtension())
271 if (pDestinationImage->
width != width || pDestinationImage->
height != height)
273 printf(
"error: destination image does not match current mode\n");
281 const int nBytes = width * height *pDestinationImage->
bytesPerPixel;
282 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB,
bufferID);
283 glBufferData(GL_PIXEL_PACK_BUFFER_ARB, nBytes, NULL, GL_STREAM_READ);
286 glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, 0);
288 glReadPixels(0, 0, width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, 0);
290 void *pData = glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
291 memcpy(pDestinationImage->
pixels, pData, nBytes);
292 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
293 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
300 glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pDestinationImage->
pixels);
302 glReadPixels(0, 0, width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, pDestinationImage->
pixels);
310 if (pDestinationMatrix->
columns != width || pDestinationMatrix->
rows != height)
312 printf(
"error: destination matrix does not match current mode\n");
316 glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, pDestinationMatrix->
data);
323 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
328 glMatrixMode(GL_MODELVIEW);
330 if (m_bMatrixOnStack)
336 m_bMatrixOnStack =
true;
344 glDisable(GL_LIGHTING);
350 glEnable(GL_LIGHTING);
358 glDisable(GL_LIGHTING);
362 const int c = (int) points.size();
363 for (
int i = 0; i < c; i++)
365 const Vec3d &v = points.at(i);
366 glVertex3f(v.
x, v.
y, v.
z);
377 glEnable(GL_LIGHTING);
384 glTranslated(point.
x, point.
y, point.
z);
385 if (pColor) glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pColor);
386 gluSphere(m_pQuadric, radius, 50, 50);
406 glTranslated(point1.
x, point1.
y, point1.
z);
407 glRotatef(acosf(u.
z) * 180.0f /
FLOAT_PI, -u.
y, u.
x, 0.0f);
408 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
409 gluCylinder(m_pQuadric, radius1, radius2, length, 20, 20);
426 resultMatrix[10] = transformation.
rotation.
r9;
427 resultMatrix[11] = 0;
431 resultMatrix[15] = 1;
436 const int nTriangles = pMatrix->
rows / 3;
437 const float *data = pMatrix->
data;
445 for (
int i = 0, offset = 0; i < nTriangles; i++, offset += 18)
447 glBegin(GL_TRIANGLES);
448 glNormal3f(data[offset], data[offset + 1], data[offset + 2]);
449 glVertex3f(data[offset + 3], data[offset + 4], data[offset + 5]);
450 glNormal3f(data[offset + 6], data[offset + 7], data[offset + 8]);
451 glVertex3f(data[offset + 9], data[offset + 10], data[offset + 11]);
452 glNormal3f(data[offset + 12], data[offset + 13], data[offset + 14]);
453 glVertex3f(data[offset + 15], data[offset + 16], data[offset + 17]);
464 m[0] = (2.0f * K.
r1 / width);
468 m[4] = (2.0f * K.
r2 / width);
469 m[5] = (2.0f * K.
r5 / height);
472 m[8] = -((2.0f * K.
r3 / width) - 1.0f);
473 m[9] = ((2.0f * K.
r6 / height) - 1.0f);
474 m[10] = -(gfar + gnear) / (gfar - gnear);
478 m[14] = -2.0f * gfar * gnear / (gfar - gnear);
485 m[0] = (2.0f * K.
r1 / width);
489 m[4] = (2.0f * K.
r2 / width);
490 m[5] = (2.0f * K.
r5 / height);
495 m[10] = -(gfar + gnear) / (gfar - gnear);
499 m[14] = -2.0f * gfar * gnear / (gfar - gnear);
503 bool COpenGLVisualizer::InitExtension()
509 glGenBuffers = (PFNGLGENBUFFERSARBPROC) wglGetProcAddress(
"glGenBuffersARB");
510 glBindBuffer = (PFNGLBINDBUFFERARBPROC) wglGetProcAddress(
"glBindBufferARB");
511 glBufferData = (PFNGLBUFFERDATAARBPROC) wglGetProcAddress(
"glBufferDataARB");
512 glMapBuffer = (PFNGLMAPBUFFERARBPROC) wglGetProcAddress(
"glMapBufferARB");
513 glUnmapBuffer = (PFNGLUNMAPBUFFERARBPROC) wglGetProcAddress(
"glUnmapBufferARB");
514 bExtensionInitialized = glGenBuffers && glBindBuffer && glBufferData && glMapBuffer && glUnmapBuffer;
void SetProjectionMatrix(const CCalibration *pCalibration)
void DrawObject(const CFloatMatrix *pMatrix, const Transformation3d &transformation)
static const float blue[3]
bool InitByCalibration(const CCalibration *pCalibration, bool bActivateShading=true)
void DrawCylinder(const Vec3d &point1, const Vec3d &point2, float radius1, float radius2, const float color[3])
static void CalculateOpenGLProjectionMatrix(const Mat3d &K, int width, int height, float gnear, float gfar, float *m)
static const float green[3]
static const float yellow[3]
float Length(const Vec3d &vec)
const CCameraParameters & GetCameraParameters() const
Gives access to the camera parameters.
void NormalizeVec(Vec3d &vec)
void SubtractVecVec(const Vec3d &vector1, const Vec3d &vector2, Vec3d &result)
ImageType type
The type of the image.
Data structure for the representation of a matrix of values of the data type float.
int width
The width of the image in pixels.
int height
The height of the image in pixels.
void ActivateShading(bool bActivateShading)
void DrawPoints(Vec3dList &points, const float *pColor=0)
Data structure for the representation of a 3D vector.
bool GetImage(CByteImage *pDestinationImage)
void SetMat(Mat3d &matrix, float r1, float r2, float r3, float r4, float r5, float r6, float r7, float r8, float r9)
static bool bExtensionInitialized
Camera model parameters and functions for a single camera.
static const float red[3]
unsigned char * pixels
The pointer to the the pixels.
void DrawPoint(float x, float y, float z, const float *pColor=0)
bool Init(int width=640, int height=480, bool bActivateShading=true)
std::vector< Vec3d > Vec3dList
void DrawSphere(const Vec3d &point, float radius, const float *pColor=0)
void AddToVec(Vec3d &vec, const Vec3d &vectorToAdd)
Data structure for the representation of a 3x3 matrix.
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
void MulVecScalar(const Vec3d &vec, float scalar, Vec3d &result)
bool GetDepthMatrix(CFloatMatrix *pDestinationMatrix)
static void CalculateOpenGLProjectionMatrixDefaultPrincipalPoint(const Mat3d &K, int width, int height, float gnear, float gfar, float *m)
void GetCalibrationMatrix(Mat3d &K) const
Sets up the calibration matrix K.
int bytesPerPixel
The number of bytes used for encoding one pixel.
void SetVec(Vec3d &vec, float x, float y, float z)
static void ConvertToOpenGLMatrix(const Transformation3d &transformation, float resultMatrix[16])