/* --------------------------------------------------------------------- --- Author : Ahmet Özlü --- Mail : ahmetozlu93@gmail.com --- Date : 1st August 2017 --- Version : 1.0 --- OpenCV Version : 2.4.10 --- Demo Video : https://www.youtube.com/watch?v=nPfR5ACrqu0 --------------------------------------------------------------------- */ // File includes: #include #include "ARDrawingContext.hpp" // Include standard headers #include #include #include // Include GLEW #include // Include GLFW #include GLFWwindow* window; // Include GLM #include #include using namespace glm; // Standard includes: #include #include #include "objloader.hpp" #include "texture.hpp" GLuint VertexArrayID; std::vector vertices; std::vector uvs; std::vector normals; // Won't be used at the moment. bool res; glm::vec3 a; glm::vec2 b; GLuint Texture; void ARDrawingContextDrawCallback(void* param) { ARDrawingContext * ctx = static_cast(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); // Initialise GLFW if (!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW\n"); } glewExperimental = true; // Needed for core profile if (glewInit() != GLEW_OK) { fprintf(stderr, "Failed to initialize GLEW\n"); } glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); // Load .bmp file as texture Texture=loadBMP_custom("C:/Users/ErArGe-5/Documents/Visual Studio 2013/Projects/ARProject/Debug/preview.bmp"); // Load .dds file as texture (uvmap) //Texture=loadDDS("C:/Users/ErArGe-5/Documents/Visual Studio 2013/Projects/ARProject/Debug/uvmap.dds"); // load(parse) .obj file res = loadOBJ("C:/Users/ErArGe-5/Documents/Visual Studio 2013/Projects/ARProject/Debug/untitled.obj", vertices, uvs, normals); // Scale 3D Model scale3DModel(0.1f); // Analyze size of the vertices and uvs /*std::cout << vertices.size(); std::cout <(&glMatrix.data[0])); // Render model //drawCoordinateAxis(); draw3DModel(); } } 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::draw3DModel() { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, Texture); //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glBegin(GL_TRIANGLES); for (int i = 0; i < vertices.size(); i += 1) { a = vertices[i]; b = uvs[i]; glNormal3f(a.x, a.y, a.z); glTexCoord2d(b.x, b.y); glVertex3f(a.x, a.y, a.z); } glEnd();//end drawing of line loop glDisable(GL_TEXTURE_2D); } void ARDrawingContext::scale3DModel(float scaleFactor) { for (int i = 0; i < vertices.size(); i += 1) { vertices[i] = vertices[i] * vec3(scaleFactor * 1.0f, scaleFactor * 1.0f, scaleFactor * 1.0f); } for (int i = 0; i < normals.size(); i += 1) { normals[i] = normals[i] * vec3(scaleFactor * 1.0f, scaleFactor * 1.0f, scaleFactor * 1.0f); } for (int i = 0; i < uvs.size(); i += 1) { uvs[i] = uvs[i] * vec2(scaleFactor * 1.0f, scaleFactor * 1.0f); } }