git.haldean.org dotmatrix / fdbbd06
initial commit: makes a sphere Haldean Brown 2 years ago
4 changed file(s) with 115 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 dotmatrix
1 .tup
2 output.png
0 : main.cpp perspective.hpp |> g++ -ggdb -I/usr/include/eigen3 -std=gnu++11 -lpng main.cpp -o dotmatrix |> dotmatrix
1 : dotmatrix |> ./dotmatrix |> output.png
0 #include <iostream>
1 #include <vector>
2 #include <Eigen/Dense>
3 #include <cstdint>
4 #include <png++/png.hpp>
5 #include "perspective.hpp"
6
7 #define IMGSIZE 600
8
9 static Eigen::Matrix<uint16_t, Eigen::Dynamic, Eigen::Dynamic> img;
10 typedef png::basic_rgb_pixel<uint8_t> pixel;
11
12 int main() {
13 img.resize(IMGSIZE, IMGSIZE);
14 img.setZero();
15
16 Eigen::Vector3f r3;
17 Eigen::Vector4f p;
18 size_t i, j;
19
20 Eigen::Matrix4f view = lookAt<float>(
21 Eigen::Vector3f(3, 10, 5),
22 Eigen::Vector3f(0, 0, 0),
23 Eigen::Vector3f(0, 0, 1));
24 Eigen::Matrix4f proj = perspective<float>(M_PI / 3, 1, 0.1, 100);
25 Eigen::Matrix4f mvp = proj * view;
26
27 for (float u = 0; u < 2 * M_PI; u += 0.05)
28 {
29 for (float v = 0; v < M_PI; v += 0.05)
30 {
31 Eigen::Vector4f h;
32 h.block(0, 0, 3, 1) =
33 Eigen::AngleAxisf(u, Eigen::Vector3f::UnitZ()) *
34 Eigen::AngleAxisf(v, Eigen::Vector3f::UnitY()) *
35 Eigen::Vector3f::UnitZ();
36 h[3] = 1;
37 p = mvp * h;
38 i = size_t(p[0] * 100) + IMGSIZE / 2;
39 j = size_t(p[1] * 100) + IMGSIZE / 2;
40 img(i, j) += 1;
41 }
42 }
43 png::image<pixel> out(IMGSIZE, IMGSIZE);
44 for (i = 0; i < IMGSIZE; i++)
45 {
46 for (j = 0; j < IMGSIZE; j++)
47 {
48 if (img(i, j))
49 out.set_pixel(i, j, pixel(0, 0, 0));
50 else
51 out.set_pixel(i, j, pixel(0xFF, 0xFF, 0xFF));
52 }
53 }
54 out.write("output.png");
55 return 0;
56 }
0 #include <Eigen/Core>
1
2 /* taken from
3 * http://spointeau.blogspot.com/2013/12/hello-i-am-looking-at-opengl-3.html */
4
5 template<class T>
6 Eigen::Matrix<T,4,4> perspective
7 (
8 double radf,
9 double aspect,
10 double zNear,
11 double zFar
12 )
13 {
14 typedef Eigen::Matrix<T,4,4> Matrix4;
15
16 assert(aspect > 0);
17 assert(zFar > zNear);
18
19 double tanHalfFovy = tan(radf / 2.0);
20 Matrix4 res = Matrix4::Zero();
21 res(0,0) = 1.0 / (aspect * tanHalfFovy);
22 res(1,1) = 1.0 / (tanHalfFovy);
23 res(2,2) = - (zFar + zNear) / (zFar - zNear);
24 res(3,2) = - 1.0;
25 res(2,3) = - (2.0 * zFar * zNear) / (zFar - zNear);
26 return res;
27 }
28
29 template<class T>
30 Eigen::Matrix<T,4,4> lookAt
31 (
32 Eigen::Matrix<T,3,1> const & eye,
33 Eigen::Matrix<T,3,1> const & center,
34 Eigen::Matrix<T,3,1> const & up
35 )
36 {
37 typedef Eigen::Matrix<T,4,4> Matrix4;
38 typedef Eigen::Matrix<T,3,1> Vector3;
39
40 Vector3 f = (center - eye).normalized();
41 Vector3 u = up.normalized();
42 Vector3 s = f.cross(u).normalized();
43 u = s.cross(f);
44
45 Matrix4 res;
46 res << s.x(),s.y(),s.z(),-s.dot(eye),
47 u.x(),u.y(),u.z(),-u.dot(eye),
48 -f.x(),-f.y(),-f.z(),f.dot(eye),
49 0,0,0,1;
50
51 return res;
52 }