GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — code/Chapter3_MarkerlessAR/src/ARDrawingContext.cpp at master · bigjun/code Expand file tree/ Copy pathARDrawingContext.cppMore file actions executable file ·317 lines (264 loc) · 11 KB / Copy pathARDrawingContext.cppFile metadata and controlsexecutable file ·317 lines (264 loc) · 11 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 /***************************************************************************** * Markerless AR desktop application. ****************************************************************************** * by Khvedchenia Ievgen, 5th Dec 2012 * http://computer-vision-talks.com ****************************************************************************** * Ch3 of the book "Mastering OpenCV with Practical Computer Vision Projects" * Copyright Packt Publishing 2012. * http://www.packtpub.com/cool-projects-with-opencv/book *****************************************************************************/ //////////////////////////////////////////////////////////////////// // File includes: #include "ARDrawingContext.hpp" //////////////////////////////////////////////////////////////////// // Standard includes: #include <gl/gl.h> #include <gl/glu.h> void ARDrawingContextDrawCallback(void* param) { ARDrawingContext * ctx = static_cast<ARDrawingContext*>(param); if (ctx) { ctx->draw(); } } ARDrawingContext::ARDrawingContext(std::string windowName, cv::Size frameSize, const CameraCalibration& c) : m_isTextureInitialized(false) , m_calibration(c) , m_windowName(windowName) { // Create window with OpenGL support cv::namedWindow(windowName, cv::WINDOW_OPENGL); // Resize it exactly to video size cv::resizeWindow(windowName, frameSize.width, frameSize.height); // Initialize OpenGL draw callback: cv::setOpenGlContext(windowName); cv::setOpenGlDrawCallback(windowName, ARDrawingContextDrawCallback, this); } ARDrawingContext::~ARDrawingContext() { cv::setOpenGlDrawCallback(m_windowName, 0, 0); } void ARDrawingContext::updateBackground(const cv::Mat& frame) { frame.copyTo(m_backgroundImage); } void ARDrawingContext::updateWindow() { cv::updateWindow(m_windowName); } void ARDrawingContext::draw() { glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); // Clear entire screen: drawCameraFrame(); // Render background drawAugmentedScene(); // Draw AR glFlush(); } void ARDrawingContext::drawCameraFrame() { // Initialize texture for background image if (!m_isTextureInitialized) { glGenTextures(1, &m_backgroundTextureId); glBindTexture(GL_TEXTURE_2D, m_backgroundTextureId); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); m_isTextureInitialized = true; } int w = m_backgroundImage.cols; int h = m_backgroundImage.rows; glPixelStorei(GL_PACK_ALIGNMENT, 1); glBindTexture(GL_TEXTURE_2D, m_backgroundTextureId); // Upload new texture data: if (m_backgroundImage.channels() == 3) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, m_backgroundImage.data); else if(m_backgroundImage.channels() == 4) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, m_backgroundImage.data); else if (m_backgroundImage.channels()==1) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, m_backgroundImage.data); const GLfloat bgTextureVertices[] = { 0, 0, w, 0, 0, h, w, h }; const GLfloat bgTextureCoords[] = { 1, 0, 1, 1, 0, 0, 0, 1 }; const GLfloat proj[] = { 0, -2.f/w, 0, 0, -2.f/h, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1 }; glMatrixMode(GL_PROJECTION); glLoadMatrixf(proj); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, m_backgroundTextureId); // Update attribute values. glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(2, GL_FLOAT, 0, bgTextureVertices); glTexCoordPointer(2, GL_FLOAT, 0, bgTextureCoords); glColor4f(1,1,1,1); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisable(GL_TEXTURE_2D); } void ARDrawingContext::drawAugmentedScene() { // Init augmentation projection Matrix44 projectionMatrix; int w = m_backgroundImage.cols; int h = m_backgroundImage.rows; buildProjectionMatrix(m_calibration, w, h, projectionMatrix); glMatrixMode(GL_PROJECTION); glLoadMatrixf(projectionMatrix.data); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if (isPatternPresent) { // Set the pattern transformation Matrix44 glMatrix = patternPose.getMat44(); glLoadMatrixf(reinterpret_cast<const GLfloat*>(&glMatrix.data[0])); // Render model drawCoordinateAxis(); drawCubeModel(); } } void ARDrawingContext::buildProjectionMatrix(const CameraCalibration& calibration, int screen_width, int screen_height, Matrix44& projectionMatrix) { float nearPlane = 0.01f; // Near clipping distance float farPlane = 100.0f; // Far clipping distance // Camera parameters float f_x = calibration.fx(); // Focal length in x axis float f_y = calibration.fy(); // Focal length in y axis (usually the same?) float c_x = calibration.cx(); // Camera primary point x float c_y = calibration.cy(); // Camera primary point y projectionMatrix.data[0] = -2.0f * f_x / screen_width; projectionMatrix.data[1] = 0.0f; projectionMatrix.data[2] = 0.0f; projectionMatrix.data[3] = 0.0f; projectionMatrix.data[4] = 0.0f; projectionMatrix.data[5] = 2.0f * f_y / screen_height; projectionMatrix.data[6] = 0.0f; projectionMatrix.data[7] = 0.0f; projectionMatrix.data[8] = 2.0f * c_x / screen_width - 1.0f; projectionMatrix.data[9] = 2.0f * c_y / screen_height - 1.0f; projectionMatrix.data[10] = -( farPlane + nearPlane) / ( farPlane - nearPlane ); projectionMatrix.data[11] = -1.0f; projectionMatrix.data[12] = 0.0f; projectionMatrix.data[13] = 0.0f; projectionMatrix.data[14] = -2.0f * farPlane * nearPlane / ( farPlane - nearPlane ); projectionMatrix.data[15] = 0.0f; } void ARDrawingContext::drawCoordinateAxis() { static float lineX[] = {0,0,0,1,0,0}; static float lineY[] = {0,0,0,0,1,0}; static float lineZ[] = {0,0,0,0,0,1}; glLineWidth(2); glBegin(GL_LINES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3fv(lineX); glVertex3fv(lineX + 3); glColor3f(0.0f, 1.0f, 0.0f); glVertex3fv(lineY); glVertex3fv(lineY + 3); glColor3f(0.0f, 0.0f, 1.0f); glVertex3fv(lineZ); glVertex3fv(lineZ + 3); glEnd(); } void ARDrawingContext::drawCubeModel() { static const GLfloat LightAmbient[]= { 0.25f, 0.25f, 0.25f, 1.0f }; // Ambient Light Values static const GLfloat LightDiffuse[]= { 0.1f, 0.1f, 0.1f, 1.0f }; // Diffuse Light Values static const GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f }; // Light Position glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT); glColor4f(0.2f,0.35f,0.3f,0.75f); // Full Brightness, 50% Alpha ( NEW ) glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA); // Blending Function For Translucency Based On Source Alpha glEnable(GL_BLEND); glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glDisable(GL_LIGHT0); glEnable(GL_LIGHT1); glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); glLightfv(GL_LIGHT1, GL_POSITION, LightPosition); glEnable(GL_COLOR_MATERIAL); glScalef(0.25,0.25, 0.25); glTranslatef(0,0, 1); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glBegin(GL_QUADS); // Front Face glNormal3f( 0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer glVertex3f(-1.0f, -1.0f, 1.0f); // Point 1 (Front) glVertex3f( 1.0f, -1.0f, 1.0f); // Point 2 (Front) glVertex3f( 1.0f, 1.0f, 1.0f); // Point 3 (Front) glVertex3f(-1.0f, 1.0f, 1.0f); // Point 4 (Front) // Back Face glNormal3f( 0.0f, 0.0f,-1.0f); // Normal Pointing Away From Viewer glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Back) glVertex3f(-1.0f, 1.0f, -1.0f); // Point 2 (Back) glVertex3f( 1.0f, 1.0f, -1.0f); // Point 3 (Back) glVertex3f( 1.0f, -1.0f, -1.0f); // Point 4 (Back) // Top Face glNormal3f( 0.0f, 1.0f, 0.0f); // Normal Pointing Up glVertex3f(-1.0f, 1.0f, -1.0f); // Point 1 (Top) glVertex3f(-1.0f, 1.0f, 1.0f); // Point 2 (Top) glVertex3f( 1.0f, 1.0f, 1.0f); // Point 3 (Top) glVertex3f( 1.0f, 1.0f, -1.0f); // Point 4 (Top) // Bottom Face glNormal3f( 0.0f,-1.0f, 0.0f); // Normal Pointing Down glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Bottom) glVertex3f( 1.0f, -1.0f, -1.0f); // Point 2 (Bottom) glVertex3f( 1.0f, -1.0f, 1.0f); // Point 3 (Bottom) glVertex3f(-1.0f, -1.0f, 1.0f); // Point 4 (Bottom) // Right face glNormal3f( 1.0f, 0.0f, 0.0f); // Normal Pointing Right glVertex3f( 1.0f, -1.0f, -1.0f); // Point 1 (Right) glVertex3f( 1.0f, 1.0f, -1.0f); // Point 2 (Right) glVertex3f( 1.0f, 1.0f, 1.0f); // Point 3 (Right) glVertex3f( 1.0f, -1.0f, 1.0f); // Point 4 (Right) // Left Face glNormal3f(-1.0f, 0.0f, 0.0f); // Normal Pointing Left glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Left) glVertex3f(-1.0f, -1.0f, 1.0f); // Point 2 (Left) glVertex3f(-1.0f, 1.0f, 1.0f); // Point 3 (Left) glVertex3f(-1.0f, 1.0f, -1.0f); // Point 4 (Left) glEnd(); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glColor4f(0.2f,0.65f,0.3f,0.35f); // Full Brightness, 50% Alpha ( NEW ) glBegin(GL_QUADS); // Front Face glNormal3f( 0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer glVertex3f(-1.0f, -1.0f, 1.0f); // Point 1 (Front) glVertex3f( 1.0f, -1.0f, 1.0f); // Point 2 (Front) glVertex3f( 1.0f, 1.0f, 1.0f); // Point 3 (Front) glVertex3f(-1.0f, 1.0f, 1.0f); // Point 4 (Front) // Back Face glNormal3f( 0.0f, 0.0f,-1.0f); // Normal Pointing Away From Viewer glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Back) glVertex3f(-1.0f, 1.0f, -1.0f); // Point 2 (Back) glVertex3f( 1.0f, 1.0f, -1.0f); // Point 3 (Back) glVertex3f( 1.0f, -1.0f, -1.0f); // Point 4 (Back) // Top Face glNormal3f( 0.0f, 1.0f, 0.0f); // Normal Pointing Up glVertex3f(-1.0f, 1.0f, -1.0f); // Point 1 (Top) glVertex3f(-1.0f, 1.0f, 1.0f); // Point 2 (Top) glVertex3f( 1.0f, 1.0f, 1.0f); // Point 3 (Top) glVertex3f( 1.0f, 1.0f, -1.0f); // Point 4 (Top) // Bottom Face glNormal3f( 0.0f,-1.0f, 0.0f); // Normal Pointing Down glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Bottom) glVertex3f( 1.0f, -1.0f, -1.0f); // Point 2 (Bottom) glVertex3f( 1.0f, -1.0f, 1.0f); // Point 3 (Bottom) glVertex3f(-1.0f, -1.0f, 1.0f); // Point 4 (Bottom) // Right face glNormal3f( 1.0f, 0.0f, 0.0f); // Normal Pointing Right glVertex3f( 1.0f, -1.0f, -1.0f); // Point 1 (Right) glVertex3f( 1.0f, 1.0f, -1.0f); // Point 2 (Right) glVertex3f( 1.0f, 1.0f, 1.0f); // Point 3 (Right) glVertex3f( 1.0f, -1.0f, 1.0f); // Point 4 (Right) // Left Face glNormal3f(-1.0f, 0.0f, 0.0f); // Normal Pointing Left glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Left) glVertex3f(-1.0f, -1.0f, 1.0f); // Point 2 (Left) glVertex3f(-1.0f, 1.0f, 1.0f); // Point 3 (Left) glVertex3f(-1.0f, 1.0f, -1.0f); // Point 4 (Left) glEnd(); glPopAttrib(); } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |