雖然課程有提供配好環境的虛擬機,但我電腦運行起來比較慢...所以在開始學習前配了半天環境XD
環境配置參考,我用的是vs2019,這里記錄下遇到的幾個問題。
問題
安裝OpenCV時報錯
mingw32-make[1]: CMakeFiles\Makefile2: No such file or directory
mingw32-make[1]: *** No rule to make target 'CMakeFiles\Makefile2'.  Stop.
mingw32-make: *** [Makefile:179: all] Error 2
 
        解決方法:更改了下目標文件夾解決問題,就很玄學...猜測是因為路徑里不能有括號之類的特殊符號?
編譯文件時提示 fatal error: Eigen3/Core: No such file or directory
解決方法:在 CMakeLists.txt 里添加
include_directories("你的eigen3路徑/Eigen3/include/eigen3")
 
        引入eigen3庫用
#include<Eigen/Eigen>
 
        使用opencv相關函數時報錯如下
嚴重性    代碼    說明    項目    文件    行    禁止顯示狀態
錯誤    LNK2019    無法解析的外部符號 "void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPEAX@Z),函數 "public: __cdecl cv::Mat::~Mat(void)" (??1Mat@cv@@QEAA@XZ) 中引用了該符號    D:\code\graphics\Assignment1\frame\out\build\x64-Debug (默認值)\frame    D:\code\graphics\Assignment1\frame\out\build\x64-Debug (默認值)\main.cpp.obj    1    
嚴重性    代碼    說明    項目    文件    行    禁止顯示狀態
錯誤    LNK2019    無法解析的外部符號 "void __cdecl cv::error(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,char const *,char const *,int)" (?error@cv@@YAXHAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEBD1H@Z),函數 "public: int const & __cdecl cv::MatSize::operator[](int)const " (??AMatSize@cv@@QEBAAEBHH@Z) 中引用了該符號    D:\code\graphics\Assignment1\frame\out\build\x64-Debug (默認值)\frame    D:\code\graphics\Assignment1\frame\out\build\x64-Debug (默認值)\main.cpp.obj    1    
 
        解決方法:出錯原因大約是沒找到鏈接庫,網上翻了半天都是c++項目的解法,明顯不能用於cmake項目
所以是時候學一波cmake語法了~可以參考下面的CMakeLists.txt,把庫鏈接進來
cmake_minimum_required(VERSION 3.10)
project(Rasterizer)
find_package(OpenCV REQUIRED)
set(CMAKE_CXX_STANDARD 17)
include_directories("C:/Program Files (x86)/Eigen3/include/eigen3")
include_directories( ${OpenCV_INCLUDE_DIRS} )
#指定要引用的dll的頭文件所在路徑
include_directories("D:/opencv/opencv/build/include")
#指定該dll的lib所在路徑
link_directories("D:/opencv/opencv/build/x64/vc15/lib")
# 將源代碼添加到此項目的可執行文件
add_executable(Rasterizer main.cpp rasterizer.hpp rasterizer.cpp Triangle.hpp Triangle.cpp)
target_link_libraries(Rasterizer ${OpenCV_LIBRARIES})
#指定鏈接庫的名字,即該dll
# opencv_world450d.lib在..\opencv\build\x64\vc15\lib目錄下,名字可能會稍有差別
target_link_libraries(Rasterizer opencv_world450d)
 
        GAMES101 作業0
題目:給定一個點 P=(2,1), 將該點繞原點先逆時針旋轉 45◦,再平移 (1,2), 計算出 變換后點的坐標(要求用齊次坐標進行計算)
關於齊次坐標,我的理解是將二維坐標三維化,可以將原本是矩陣加法的平移轉化為矩陣乘法,方便進行旋轉平移縮放等一系列運算。
代碼:(注意浮點數的表示)
#include<cmath>
#include<Eigen/Core>
#include<Eigen/Dense>
#include<iostream>
using namespace std;
using namespace Eigen;
#define PI acos(-1)
int main(){
    Vector3d P(2.0f, 1.0f, 1.0f);
    Matrix3d rot, tra;
    double ang = 45.0/180.0 * PI;
    rot << 
        cos(ang), -sin(ang), 0,
        sin(ang), cos(ang), 0,
        0, 0, 1;
    tra <<
        1, 0, 1,
        0, 1, 2,
        0, 0, 1;
    cout << tra * rot * P << endl;
    return 0;
}
 
        加油!
