スポンサーリンク
オブジェクトをobject_3dから継承させ、intersectをoverrideする。それ以外はほぼ変わらない
#pragma once #include "Grid.hpp" #include <array> #include <vector> #include <memory>
////////////////////////////////////////////// //////////////////////////////////////////////
// 三次元オブジェクトのスーパークラス class object_3d { protected: glm::vec3 _color; public: object_3d(const glm::vec3& color_) :_color(color_) {} object_3d() {} virtual bool intersect( glm::vec3* pos_, glm::vec3* color_, glm::vec3* normal_, float* distance_, const std::array<glm::vec3, 2>& ray_)const = 0; const glm::vec3& color()const { return _color; } glm::vec3& color() { return _color; } };
// 球オブジェクト class object_sphere:public object_3d { glm::vec3 _center; float _r; public: object_sphere(glm::vec3 center_, glm::vec3 color_,float radius_) : _center(center_),_r(radius_), object_3d(color_) {} object_sphere() {} virtual bool intersect( glm::vec3* pos_, glm::vec3* color_, glm::vec3* normal_, float* distance_, const std::array<glm::vec3, 2>& ray_)const override; };
// 三角形オブジェクト class object_triangle :public object_3d { std::array<glm::vec3,3> _points; public: object_triangle( std::array<glm::vec3, 3> points_, glm::vec3 color_) : _points(points_), object_3d(color_){} object_triangle() {} bool intersect( glm::vec3* pos_, glm::vec3* color_, glm::vec3* normal_, float* distance_, const std::array<glm::vec3, 2>& ray_)const override; };
////////////////////////////////////////////// ////////////////////////////////////////////// // ポイントライト class light_point { glm::vec3 _position; glm::vec3 _diffuse_color; glm::vec3 _ambient_color; glm::vec3 _specular_color; public: light_point( const glm::vec3& position_, const glm::vec3& diffuse_color_, const glm::vec3& ambient_color_, const glm::vec3& specular_color_ ) : _position(position_), _diffuse_color(diffuse_color_), _ambient_color(ambient_color_), _specular_color(specular_color_) {} light_point() {} glm::vec3 calc_vector_point_to_light(const glm::vec3& point_)const { // from -> _position return glm::normalize(_position - point_); } const glm::vec3& position()const {return _position;} glm::vec3& position() { return _position; } const glm::vec3& diffuse_color()const {return _diffuse_color;} glm::vec3& diffuse_color() { return _diffuse_color; } const glm::vec3& ambient_color()const { return _ambient_color; } glm::vec3& ambient_color() { return _ambient_color; } const glm::vec3& specular_color()const { return _specular_color; } glm::vec3& specular_color() { return _specular_color; } }; struct hit_pixel_t { glm::vec3 _position; glm::vec3 _color; hit_pixel_t(const glm::vec3& position_, const glm::vec3& color_) : _position(position_), _color(color_) {} }; // レイキャスト用のクラス class my_raycast { grid g[2]; float _startZ; float _endZ; int _pxwidth; int _pxheight; public: glm::vec3 get_eye_position()const { float a = g[0].ywidth() / 2.0; float b = g[1].ywidth() / 2.0; float d = glm::length(g[1].center() - g[0].center()); float c = a * d / (b - a); // 始点 → 終点 glm::vec3 veceye = g[1].center() - g[0].center(); veceye = glm::normalize(veceye); return g[0].center() - veceye * c; } my_raycast() {} int width()const { return _pxwidth; } int height()const { return _pxheight; } // レイの開始点となるグリッド、終点となるグリッドを作成する void set(const float startZ_, const float endZ_, const int pxwidth_, const int pxheight_); //グリッド取得 const grid& get_grid(const size_t index)const { return g[index]; } //グリッドから作成するレイの数を取得 int ray_count() { return g[0].cellCount(); } //グリッドから作成するレイを取得 std::array<glm::vec3, 2> get_ray(const size_t index)const; //始点グリッド、終点グリッドの中間の位置を取得(表示用) float lookatZ()const { return (_startZ + _endZ) / 2.f; } ////////////////////////////////////////////// ////////////////////////////////////////////// private: // オブジェクト一覧 std::vector< std::shared_ptr<object_3d> > _objlist; std::vector< hit_pixel_t > _hits; public: void raycast(); std::vector< hit_pixel_t >& get_intersects() {return _hits;} void set_object(const object_sphere& obj_); void set_object(const object_triangle& obj_); ////////////////////////////// private: light_point _light; public: void set_light(const light_point& light_); const light_point& get_light()const { return _light; } }; // objectNormal 面法線 // lightDirection そのポイント→光源へのベクトル // lightColor ライトの色 inline glm::vec3 calc_diffuse( const glm::vec3& lightDiffuse, const glm::vec3& MaterialDiffuse, const glm::vec3& lightIncidence, const glm::vec3& objectNormal) { // 拡散反射は「そのポイント→光源」と面法線の内積を取る float sDotN = glm::max(glm::dot(-lightIncidence, objectNormal), 0.f); return lightDiffuse * MaterialDiffuse *sDotN; } inline glm::vec3 calc_ambient( const glm::vec3& LightAmbient, const glm::vec3& MaterialAmbient) { return LightAmbient * MaterialAmbient; } // https://araramistudio.jimdo.com/2017/10/02/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0-directx-11%E3%81%A7%E9%8F%A1%E9%9D%A2%E5%8F%8D%E5%B0%84-specular-reflection/ // https://amengol.github.io/game_physics/using-glm-reflect-to-react/ // https://learnopengl.com/Lighting/Basic-Lighting inline glm::vec3 calc_specular( const glm::vec3& eyePosition, const glm::vec3& lightSpecularColor, const glm::vec3& materialSpecularColor, const glm::vec3& lightIncidence, // 入射 const float materialShininess, const glm::vec3& objectNormal, const glm::vec3& objectPosition ) { glm::vec3 VertexToEye = glm::normalize(eyePosition - objectPosition); glm::vec3 LightReflect = glm::normalize(glm::reflect(lightIncidence, objectNormal)); float SpecularFactor = glm::pow(glm::max(glm::dot(VertexToEye, LightReflect),0.f), materialShininess); glm::vec3 SpecularColor(0, 0, 0); SpecularColor = SpecularFactor * lightSpecularColor * materialSpecularColor; return SpecularColor; }
#include "raycast.hpp" #include <glm/gtx/intersect.hpp> #include <glm/gtx/normal.hpp > #include <memory> bool object_sphere::intersect( glm::vec3* pos_, glm::vec3* color_, glm::vec3* normal_, float* distance_, const std::array<glm::vec3, 2>& ray_) const{ glm::vec3 nray = glm::normalize(ray_[1] - ray_[0]); float distance; bool valid = glm::intersectRaySphere(ray_[0], nray, _center, _r*_r, distance); //レイが衝突した点の座標 *pos_ = ray_[0] + nray * distance; //色の設定 *color_ = _color; //面法線 // 球中心 → 衝突点 *normal_ = glm::normalize(*pos_ - _center); *distance_ = distance; return valid; } bool object_triangle::intersect( glm::vec3* pos_, glm::vec3* color_, glm::vec3* normal_, float* distance_, const std::array<glm::vec3, 2>& ray_)const { glm::vec3 nray = glm::normalize(ray_[1] - ray_[0]); float distance; glm::vec2 bary; bool valid = glm::intersectRayTriangle( ray_[0], nray, _points[0], _points[1], _points[2], bary, distance ); //レイが衝突した点の座標 *pos_ = ray_[0] + nray * distance; //色の設定 *color_ = _color; //面法線 *normal_ = glm::triangleNormal( _points[0], _points[1], _points[2] ); // normalize済 *distance_ = distance; return valid; } void my_raycast::set(const float startZ_, const float endZ_, const int pxwidth_, const int pxheight_) { _startZ = startZ_; _endZ = endZ_; _pxwidth = pxwidth_; _pxheight = pxheight_; glm::vec3 vecx(1, 0, 0); glm::vec3 vecy(0, 1, 0); g[0] = grid( vecx, vecy, glm::vec3(0, 0, _startZ), 1.f, 1.f, pxwidth_, pxheight_ ); g[1] = grid( vecx, vecy, glm::vec3(0, 0, _endZ), 2.f, 2.f, pxwidth_, pxheight_ ); } std::array<glm::vec3, 2> my_raycast::get_ray(const size_t index)const { return std::array<glm::vec3, 2>{ g[0][index].center(), g[1][index].center() }; } void my_raycast::set_object(const object_sphere& obj_) { _objlist.push_back(std::make_shared<object_sphere>(obj_)); } void my_raycast::set_object(const object_triangle& obj_) { _objlist.push_back(std::make_shared<object_triangle>(obj_)); } void my_raycast::set_light(const light_point& light_) { _light = light_; } void my_raycast::raycast() { // 結果の初期化 float inf = std::numeric_limits<float>::infinity(); _hits.clear(); _hits.resize(_pxwidth * _pxheight, hit_pixel_t( glm::vec3(inf, inf, inf), glm::vec3(0, 0, 0) ) ); for (size_t i = 0; i < _pxwidth * _pxheight; i++) { auto ray = get_ray(i); glm::vec3 p; // レイがヒットした座標 glm::vec3 c; // レイがヒットした位置の色 glm::vec3 n; // レイがヒットした位置の法線 float distance = (std::numeric_limits<float>::max)(); bool hit = false; for (auto _obj : _objlist) { float _tmpdist; glm::vec3 tmp_p; glm::vec3 tmp_c; glm::vec3 tmp_n;
if (_obj->intersect(&tmp_p, &tmp_c, &tmp_n, &_tmpdist, ray) == true) { if (_tmpdist < distance) { p = tmp_p; c = tmp_c; n = tmp_n; } hit = true; };
if (hit) { _hits[i]._position = p; // ライトの入射ベクトル glm::vec3 lightIncidence = -_light.calc_vector_point_to_light(p); glm::vec3 diffuse = calc_diffuse(_light.diffuse_color(), c, lightIncidence, n); glm::vec3 specular = calc_specular( get_eye_position(), _light.specular_color(), c, lightIncidence, 80, n, p); glm::vec3 ambient = calc_ambient( _light.ambient_color(), c ); _hits[i]._color = ambient + diffuse + specular; }
} } }
#include <iostream> #include <array> #include<GL/freeglut.h> #include<gl/GL.h> #include "raycast.hpp" #include<glm/gtc/type_ptr.hpp> #include<glm/gtc/matrix_transform.hpp> my_raycast mydata; //! @brief グリッドを表示 //! @param [in] g グリッドオブジェクト //! @param [in] dispcenter セルの中央を表示するか void drawGrid(const grid& g, const bool dispcenter) { glLineWidth(1); glColor3d(1, 1, 1); for (size_t i = 0; i < g.cellCount(); i++) { glBegin(GL_LINE_LOOP); glVertex3fv(glm::value_ptr(g.p0(i))); glVertex3fv(glm::value_ptr(g.p1(i))); glVertex3fv(glm::value_ptr(g.p2(i))); glVertex3fv(glm::value_ptr(g.p3(i))); glEnd(); } if (dispcenter) { glPointSize(1); glColor3d(1, 1, 1); glBegin(GL_POINTS); for (size_t i = 0; i < g.cellCount(); i++) { glVertex3fv( glm::value_ptr(g[i].center())); } glEnd(); } } void drawXYZ() { glLineWidth(3); glBegin(GL_LINES); glColor3f(1, 0, 0); glVertex3f(0, 0, 0); glVertex3f(1, 0, 0); glColor3f(0, 1, 0); glVertex3f(0, 0, 0); glVertex3f(0, 1, 0); glColor3f(0, 0, 1); glVertex3f(0, 0, 0); glVertex3f(0, 0, 1); glEnd(); glLineWidth(1); } void drawRays() { glBegin(GL_LINES); for (size_t i = 0; i < mydata.ray_count(); i++) { auto ray = mydata.get_ray(i); glColor3f(1.0, 1.0, 1.0); glVertex3fv(glm::value_ptr(ray[0])); glColor3f(0.0, 0.0, 1.0); glVertex3fv(glm::value_ptr(ray[1])); } glEnd(); } //ウィンドウの幅と高さ int width, height; //描画関数 void disp(void) { glViewport(0, 0, width, height); glClearColor(0.2, 0.2, 0.2, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glm::mat4 proj = glm::perspectiveFov(glm::radians(45.f), (float)width, (float)height, 0.1f, 50.f); glLoadMatrixf(glm::value_ptr(proj)); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); ////////////////////////////////////////// ////////////////////////////////////////// // カメラの設定 float z = mydata.lookatZ(); glm::mat4 look = glm::lookAt( glm::vec3(5, 4, z-5 ),// eye glm::vec3(0, 0, z ), // lookat glm::vec3(0,1, 0) // up ); glLoadMatrixf(glm::value_ptr(look)); ////////////////////////////////////////// // グリッドを表示 drawGrid(mydata.get_grid(0), false);//from drawGrid(mydata.get_grid(1), false);//to ////////////////////////////////////////// // 作成したレイを表示 //drawRays(); ////////////////////////////////////////// glPointSize(3); glBegin(GL_POINTS); for (size_t i = 0; i < mydata.get_intersects().size(); i++) { glColor3fv( glm::value_ptr( mydata.get_intersects()[i]._color ) ); glVertex3fv( glm::value_ptr(mydata.get_intersects()[i]._position) ); } glEnd(); glPointSize(1); drawXYZ(); glPointSize(3); glBegin(GL_POINTS); for (size_t i = 0; i < mydata.get_intersects().size(); i++) { if (isinf(mydata.get_intersects()[i]._position.x) == false) { glColor3fv( glm::value_ptr( mydata.get_intersects()[i]._color ) ); glVertex3fv( glm::value_ptr( mydata.get_grid(0)[i].center() ) ); } } glEnd(); glPointSize(1); glColor3d(1, 1, 0); glPointSize(10); glBegin(GL_POINTS); glVertex3fv(glm::value_ptr(mydata.get_light().position())); glEnd(); glPointSize(1); glColor3d(1, 0, 0); glColor3d(0, 0, 1); glPointSize(35); glBegin(GL_POINTS); glm::vec3 eyepos = mydata.get_eye_position(); glVertex3fv(glm::value_ptr(eyepos)); glEnd(); glPointSize(1); glFlush(); } //ウィンドウサイズの変化時に呼び出される void reshape(int w, int h) { width = w; height = h; disp(); } //エントリポイント int main(int argc, char** argv) { glutInit(&argc, argv); glutInitWindowPosition(100, 50); glutInitWindowSize(500, 500); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); mydata.set(0.f, 3.f,100,100); /////////////////////////////////// mydata.set_object( // 球の登録 object_sphere( glm::vec3(0.0, 0.0, 2), // 球の中心 glm::vec3(1.0, 0, 0), // 球の色 0.5 ) ); mydata.set_object( // 三角形の登録 object_triangle( std::array< glm::vec3, 3>{ glm::vec3(0.916876, -0.840909, 1.58713), glm::vec3(-0.633301, -0.922765, 0.585358), glm::vec3(-0.513684, 0.453145, 1.330333) }, glm::vec3(1, 1, 0) ) ); mydata.set_light( light_point( glm::vec3(1.0, 1.0, 0.5), // ライトの位置 glm::vec3(0.5, 0.5, 0.5), // diffuse色 glm::vec3(0.2, 0.2, 0.2), // ambient色 glm::vec3(1.0, 1.0, 1.0) // specular色 ) ); mydata.raycast(); glutCreateWindow("sample"); glutDisplayFunc(disp); glutReshapeFunc(reshape); glutMainLoop(); return 0; }