Added all necessary stuff for the homework assignment (environment mapping + triangulated meshes)

homework_assignment
Dmitry V. Sokolov 2019-01-20 13:30:16 +01:00
parent 5e0da1f09f
commit d7e1447ee1
11 changed files with 951 additions and 7 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "lib/stb"]
path = lib/stb
url = https://github.com/nothings/stb

View File

@ -1,8 +1,10 @@
cmake_minimum_required (VERSION 2.8)
project (tinyraytracer)
include(CheckCXXCompilerFlag)
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib")
include(CheckCXXCompilerFlag)
function(enable_cxx_compiler_flag_if_supported flag)
string(FIND "${CMAKE_CXX_FLAGS}" "${flag}" flag_already_set)
if(flag_already_set EQUAL -1)
@ -21,7 +23,18 @@ enable_cxx_compiler_flag_if_supported("-std=c++11")
enable_cxx_compiler_flag_if_supported("-O3")
enable_cxx_compiler_flag_if_supported("-fopenmp")
file(GLOB SOURCES *.h *.cpp)
file(GLOB SOURCES
"${SRC_DIR}/*.h"
"${SRC_DIR}/*.cpp"
)
# Executable definition and properties
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE "${SRC_DIR}")
# STB
set(STB_DIR "${LIB_DIR}/stb")
target_include_directories(${PROJECT_NAME} PRIVATE "${STB_DIR}")

765
duck.obj Normal file
View File

@ -0,0 +1,765 @@
v 4.73086 0.46518 -8.0423
v 5.29815 0.36796 -8.25698
v 5.04326 0.0245349 -8.00362
v 5.05325 0.805965 -8.45088
v 4.60896 0.887375 -8.36949
v 4.21674 0.676275 -8.19915
v 4.15834 0.259045 -7.97398
v 4.56604 -0.10482 -7.87416
v 5.42822 -0.18652 -8.28961
v 5.47088 0.62491 -8.66851
v 5.67455 0.15282 -8.66292
v 4.94576 -0.48033 -8.02983
v 5.17405 0.919035 -8.9961
v 4.65837 1.09546 -8.85537
v 4.16751 1.03009 -8.64284
v 3.77129 0.594815 -8.40534
v 3.68079 0.04882 -8.10217
v 4.07963 -0.28395 -7.88547
v 4.40456 -0.681365 -7.97489
v 5.28065 -0.67956 -8.4183
v 5.64018 -0.38089 -8.76388
v 5.62248 0.51977 -9.10695
v 5.72872 -0.0496099 -9.17313
v 4.82301 -0.942835 -8.28263
v 5.24053 0.731915 -9.48595
v 4.72937 1.01484 -9.35713
v 4.16365 1.13106 -9.15962
v 3.81609 0.913525 -8.90061
v 3.47751 0.49208 -8.92954
v 3.42633 0.252695 -8.43192
v 3.2866 -0.22748 -8.27816
v 3.70107 -0.496375 -8.08205
v 3.97208 -0.984675 -8.15359
v 4.40815 -1.23919 -8.2
v 5.10805 -1.16999 -8.71148
v 5.40549 -0.82018 -8.90338
v 5.52945 -0.56112 -9.3131
v 5.54149 0.28154 -9.52767
v 5.45157 -0.20777 -9.67913
v 4.87083 -1.4538 -8.38652
v 5.17667 0.291655 -9.86108
v 4.79573 0.71261 -9.7864
v 4.24571 0.875515 -9.5845
v 3.74985 0.72268 -9.36946
v 3.42024 0.260025 -9.55346
v 3.08767 0.31998 -9.19979
v 2.99937 0.26802 -8.66608
v 2.98539 -0.19963 -8.47114
v 3.18423 -0.557965 -8.61936
v 3.55182 -0.819145 -8.41918
v 3.62565 -1.32065 -8.48518
v 4.00972 -1.58088 -8.15194
v 4.54114 -1.73548 -8.05045
v 5.295 -1.42483 -9.08879
v 5.14771 -1.03764 -9.30435
v 5.42576 -1.62268 -8.61067
v 5.13863 -0.70158 -9.70008
v 5.04058 -0.24089 -9.99476
v 5.15405 -1.81472 -8.17039
v 4.71096 0.2169 -10.0514
v 4.33064 0.53512 -9.93755
v 3.83851 0.460905 -9.77882
v 3.65753 -0.06024 -9.91206
v 3.28231 -0.22347 -9.71336
v 3.0016 -0.224945 -9.51575
v 2.76469 0.11869 -9.43287
v 2.64172 0.321575 -9.04039
v 2.70744 0.129975 -8.64895
v 2.75862 -0.25132 -8.80845
v 3.04713 -0.52997 -9.09408
v 3.44965 -0.90518 -8.93336
v 3.4246 -1.39266 -8.9681
v 3.21189 -1.77376 -8.64345
v 3.51313 -1.78655 -8.21659
v 3.90199 -1.97125 -7.77647
v 4.38943 -2.02199 -7.64505
v 4.90472 -2.05715 -7.72261
v 5.73942 -1.65145 -9.13432
v 5.44588 -1.73204 -9.56785
v 5.0132 -1.45906 -9.51554
v 4.69418 -1.1591 -9.69059
v 5.76032 -1.95079 -8.22071
v 5.98038 -1.71589 -8.68525
v 4.67565 -0.690135 -9.96393
v 4.52868 -0.255315 -10.1344
v 5.41225 -2.14493 -7.86533
v 4.13035 0.08094 -10.0769
v 4.06306 -0.45545 -10.0865
v 3.69022 -0.674 -9.83589
v 3.33199 -0.63931 -9.46933
v 2.76811 -0.2159 -9.17518
v 3.58272 -1.062 -9.40796
v 3.10972 -1.83569 -9.17212
v 3.46451 -1.54336 -9.46943
v 2.85205 -2.19934 -8.85482
v 3.02488 -2.17497 -8.30702
v 3.38865 -2.13446 -7.88579
v 3.6456 -2.39758 -7.47915
v 4.16366 -2.38561 -7.3633
v 4.71672 -2.40031 -7.33657
v 5.2751 -2.41253 -7.4466
v 6.25366 -1.66046 -9.20996
v 5.98697 -1.86784 -9.63828
v 5.41582 -2.01653 -9.97185
v 4.88965 -1.76969 -9.88708
v 4.34553 -1.56478 -9.85728
v 4.22404 -0.953025 -9.91084
v 5.82841 -2.38759 -7.71398
v 6.24876 -2.20501 -7.9989
v 6.35293 -1.84113 -8.34632
v 6.53295 -1.57871 -8.76999
v 3.89118 -1.25358 -9.73755
v 2.85693 -2.27615 -9.42464
v 3.21645 -1.96218 -9.69672
v 3.74847 -1.71935 -9.85475
v 2.65536 -2.69701 -9.14235
v 2.69986 -2.64457 -8.60673
v 2.86741 -2.63656 -8.12494
v 3.18235 -2.54486 -7.73967
v 3.93464 -2.85077 -7.24739
v 3.40353 -2.90492 -7.4264
v 4.46083 -2.87147 -7.17581
v 5.0288 -2.828 -7.17883
v 5.55818 -2.80843 -7.27242
v 6.75936 -1.4552 -9.22664
v 6.58273 -1.78638 -9.62934
v 5.91105 -2.19595 -10.0351
v 6.42957 -2.16274 -9.94658
v 5.56706 -2.38319 -10.3881
v 5.05255 -2.14887 -10.3379
v 4.55057 -1.96567 -10.2591
v 4.02929 -1.92999 -10.2039
v 6.01841 -2.76553 -7.43505
v 6.42278 -2.61409 -7.69738
v 6.8125 -2.04889 -8.19398
v 6.78708 -2.47344 -7.97812
v 6.92999 -1.62095 -8.46587
v 7.00533 -1.22421 -8.78329
v 3.02922 -2.40853 -9.96916
v 2.74997 -2.76198 -9.68068
v 3.46432 -2.10239 -10.1417
v 2.63668 -3.16655 -8.84962
v 2.67276 -3.23238 -9.37412
v 2.73764 -3.12615 -8.30655
v 3.01367 -3.04891 -7.80421
v 4.23141 -3.41357 -7.269
v 3.72617 -3.30988 -7.32665
v 3.33728 -3.44423 -7.60104
v 4.80301 -3.30828 -7.21125
v 5.34753 -3.31196 -7.2404
v 5.8824 -3.25767 -7.32599
v 7.14819 -1.14868 -9.23183
v 7.10819 -1.6303 -9.52727
v 7.00123 -2.13592 -9.78634
v 6.12931 -2.54438 -10.3625
v 6.63756 -2.57242 -10.1547
v 5.61677 -2.80208 -10.7094
v 5.15684 -2.58339 -10.7119
v 4.68122 -2.39363 -10.6612
v 4.2137 -2.2759 -10.5749
v 3.74044 -2.31125 -10.504
v 6.39634 -3.10057 -7.49973
v 6.86366 -2.98159 -7.84436
v 7.30167 -2.00911 -8.41057
v 7.22337 -2.56337 -8.20525
v 7.42344 -1.52518 -8.66669
v 7.37126 -1.11313 -8.91702
v 3.00555 -2.9312 -10.1832
v 3.31222 -2.59488 -10.4186
v 2.82911 -3.25767 -9.86054
v 2.81198 -3.59116 -8.59143
v 2.83442 -3.67759 -9.12959
v 2.96257 -3.69269 -9.66149
v 2.9971 -3.51537 -8.03563
v 3.83868 -3.7663 -7.52351
v 4.28704 -3.97365 -7.62049
v 4.62891 -3.75617 -7.36866
v 3.46184 -3.84864 -7.91839
v 5.153 -3.80035 -7.41228
v 5.67219 -3.76582 -7.48214
v 6.17465 -3.63989 -7.58611
v 7.50702 -1.49859 -9.20417
v 7.46367 -2.01277 -9.43303
v 7.40421 -2.52667 -9.58957
v 7.07452 -2.64686 -9.9293
v 6.05081 -2.97213 -10.6305
v 6.50984 -3.00829 -10.4318
v 6.90654 -3.10612 -10.1185
v 5.194 -3.1534 -10.8084
v 5.66559 -3.35615 -10.6978
v 4.73832 -2.91758 -10.8384
v 4.22523 -2.75709 -10.787
v 3.72744 -2.79058 -10.6953
v 6.63422 -3.43713 -7.73075
v 6.92049 -3.54183 -8.10289
v 7.22072 -3.09217 -8.22217
v 7.50566 -2.41509 -8.60083
v 7.5823 -1.95582 -8.89946
v 7.44988 -2.92972 -8.67105
v 3.36494 -3.16384 -10.534
v 3.16647 -3.50701 -10.1818
v 3.18895 -3.96606 -8.84609
v 3.15945 -3.86838 -8.34358
v 3.28401 -3.99224 -9.37575
v 3.41296 -3.90802 -9.92028
v 3.93422 -4.07189 -8.02353
v 4.46704 -4.12651 -8.14604
v 4.8346 -4.06576 -7.73317
v 3.60128 -4.08295 -8.45059
v 5.39896 -4.07732 -7.83656
v 5.92489 -4.01764 -7.93108
v 6.4474 -3.84826 -8.01581
v 7.5604 -2.48135 -9.09683
v 7.45224 -2.97866 -9.25432
v 7.25683 -3.10353 -9.71764
v 6.15801 -3.44917 -10.5244
v 6.59787 -3.51638 -10.2421
v 6.93614 -3.58615 -9.83553
v 4.75426 -3.52278 -10.7252
v 5.24472 -3.71425 -10.6249
v 5.71891 -3.85072 -10.4411
v 4.33301 -3.26975 -10.778
v 3.84958 -3.30919 -10.7018
v 7.18108 -3.52964 -8.52017
v 6.74359 -3.91732 -8.46726
v 7.33916 -3.37225 -8.94364
v 3.63622 -3.69325 -10.4332
v 3.70214 -4.11911 -9.00383
v 3.80248 -4.11932 -9.55321
v 3.90814 -4.02348 -10.0971
v 4.10044 -4.13434 -8.57733
v 5.01651 -4.13367 -8.23746
v 4.61501 -4.13437 -8.7093
v 5.59387 -4.13313 -8.41035
v 6.18565 -4.0851 -8.42695
v 7.20373 -3.53653 -9.39448
v 6.16822 -3.84176 -10.2203
v 6.47017 -3.9165 -9.84105
v 6.79397 -3.92575 -9.40752
v 4.80411 -3.96909 -10.4566
v 4.20685 -3.77625 -10.5775
v 5.30721 -4.07236 -10.2045
v 5.83732 -4.08327 -9.91842
v 7.0105 -3.83009 -8.93855
v 6.49504 -4.07143 -8.92538
v 4.24903 -4.13437 -9.1333
v 4.34088 -4.13352 -9.6612
v 4.39586 -4.06448 -10.1966
v 5.1215 -4.13437 -8.73046
v 4.82537 -4.13437 -9.27749
v 5.40142 -4.13437 -9.08313
v 5.92419 -4.1328 -8.8891
v 6.25659 -4.09374 -9.41461
v 4.83818 -4.12797 -9.88817
v 5.29839 -4.13423 -9.64229
v 5.77463 -4.13305 -9.41484
f 1 3 2
f 2 4 1
f 4 5 1
f 5 6 1
f 6 7 1
f 7 8 1
f 1 8 3
f 3 9 2
f 2 10 4
f 2 11 10
f 9 11 2
f 8 12 3
f 3 12 9
f 10 13 4
f 13 14 4
f 4 14 5
f 14 15 5
f 5 15 6
f 15 16 6
f 6 16 7
f 16 17 7
f 17 18 7
f 7 18 8
f 8 19 12
f 18 19 8
f 12 20 9
f 20 21 9
f 9 21 11
f 11 22 10
f 10 22 13
f 21 23 11
f 11 23 22
f 19 24 12
f 12 24 20
f 22 25 13
f 25 26 13
f 13 26 14
f 26 27 14
f 14 27 15
f 27 28 15
f 15 28 16
f 28 29 16
f 29 30 16
f 16 30 17
f 30 31 17
f 31 32 17
f 17 32 18
f 18 32 19
f 32 33 19
f 33 34 19
f 19 34 24
f 24 35 20
f 35 36 20
f 20 36 21
f 21 37 23
f 36 37 21
f 23 38 22
f 22 38 25
f 37 39 23
f 23 39 38
f 34 40 24
f 24 40 35
f 38 41 25
f 39 41 38
f 41 42 25
f 25 42 26
f 42 43 26
f 26 43 27
f 28 44 29
f 27 44 28
f 43 44 27
f 44 45 29
f 45 46 29
f 46 47 29
f 29 47 30
f 47 48 30
f 30 48 31
f 48 49 31
f 49 50 31
f 32 50 33
f 31 50 32
f 50 51 33
f 51 52 33
f 33 52 34
f 52 53 34
f 34 53 40
f 35 55 36
f 54 55 35
f 36 55 37
f 35 56 54
f 40 56 35
f 55 57 37
f 37 57 39
f 57 58 39
f 39 58 41
f 53 59 40
f 40 59 56
f 58 60 41
f 41 60 42
f 60 61 42
f 42 61 43
f 61 62 43
f 44 62 45
f 43 62 44
f 62 63 45
f 63 64 45
f 64 65 45
f 65 66 45
f 45 66 46
f 66 67 46
f 46 67 47
f 67 68 47
f 47 68 48
f 68 69 48
f 48 69 49
f 69 70 49
f 70 71 49
f 50 71 51
f 49 71 50
f 71 72 51
f 72 73 51
f 73 74 51
f 51 74 52
f 74 75 52
f 52 75 53
f 75 76 53
f 76 77 53
f 53 77 59
f 56 78 54
f 78 79 54
f 79 80 54
f 54 80 55
f 80 81 55
f 55 81 57
f 59 82 56
f 82 83 56
f 56 83 78
f 81 84 57
f 57 84 58
f 58 85 60
f 84 85 58
f 77 86 59
f 59 86 82
f 60 87 61
f 85 87 60
f 61 87 62
f 62 87 63
f 87 88 63
f 85 88 87
f 84 88 85
f 88 89 63
f 63 89 64
f 89 90 64
f 64 90 65
f 65 90 70
f 70 90 71
f 70 91 65
f 69 91 70
f 68 91 69
f 67 91 68
f 66 91 67
f 65 91 66
f 89 92 90
f 90 92 71
f 71 92 72
f 72 93 73
f 72 94 93
f 92 94 72
f 93 95 73
f 95 96 73
f 73 96 74
f 96 97 74
f 74 97 75
f 97 98 75
f 98 99 75
f 75 99 76
f 99 100 76
f 76 100 77
f 100 101 77
f 77 101 86
f 83 102 78
f 102 103 78
f 78 103 79
f 103 104 79
f 104 105 79
f 80 105 81
f 79 105 80
f 105 106 81
f 106 107 81
f 88 107 89
f 81 107 84
f 84 107 88
f 86 108 82
f 101 108 86
f 108 109 82
f 109 110 82
f 82 110 83
f 110 111 83
f 83 111 102
f 107 112 89
f 106 112 107
f 92 112 94
f 89 112 92
f 93 113 95
f 93 114 113
f 94 114 93
f 112 115 94
f 106 115 112
f 94 115 114
f 113 116 95
f 116 117 95
f 95 117 96
f 117 118 96
f 118 119 96
f 96 119 97
f 97 119 98
f 98 120 99
f 98 121 120
f 119 121 98
f 120 122 99
f 99 122 100
f 100 123 101
f 122 123 100
f 123 124 101
f 101 124 108
f 111 125 102
f 125 126 102
f 102 126 103
f 103 127 104
f 103 128 127
f 126 128 103
f 127 129 104
f 129 130 104
f 104 130 105
f 105 131 106
f 130 131 105
f 106 132 115
f 131 132 106
f 124 133 108
f 133 134 108
f 108 134 109
f 109 135 110
f 109 136 135
f 134 136 109
f 110 137 111
f 135 137 110
f 137 138 111
f 111 138 125
f 114 139 113
f 113 140 116
f 139 140 113
f 115 141 114
f 114 141 139
f 132 141 115
f 116 142 117
f 116 143 142
f 140 143 116
f 142 144 117
f 117 144 118
f 144 145 118
f 119 145 121
f 118 145 119
f 120 146 122
f 120 147 146
f 121 147 120
f 145 148 121
f 121 148 147
f 122 149 123
f 146 149 122
f 149 150 123
f 123 150 124
f 150 151 124
f 124 151 133
f 138 152 125
f 125 153 126
f 152 153 125
f 126 154 128
f 153 154 126
f 128 155 127
f 127 155 129
f 154 156 128
f 128 156 155
f 155 157 129
f 157 158 129
f 129 158 130
f 158 159 130
f 130 159 131
f 159 160 131
f 131 160 132
f 160 161 132
f 132 161 141
f 133 162 134
f 151 162 133
f 162 163 134
f 134 163 136
f 135 164 137
f 136 165 135
f 135 165 164
f 163 165 136
f 164 166 137
f 137 166 138
f 166 167 138
f 138 167 152
f 139 168 140
f 139 169 168
f 141 169 139
f 161 169 141
f 168 170 140
f 140 170 143
f 142 171 144
f 142 172 171
f 143 172 142
f 143 173 172
f 170 173 143
f 171 174 144
f 145 174 148
f 144 174 145
f 147 175 146
f 148 175 147
f 175 176 146
f 176 177 146
f 146 177 149
f 174 178 148
f 148 178 175
f 177 179 149
f 149 179 150
f 150 180 151
f 179 180 150
f 180 181 151
f 151 181 162
f 152 182 153
f 167 182 152
f 166 182 167
f 182 183 153
f 153 183 154
f 183 184 154
f 184 185 154
f 154 185 156
f 155 186 157
f 155 187 186
f 156 187 155
f 185 188 156
f 156 188 187
f 157 189 158
f 157 190 189
f 186 190 157
f 189 191 158
f 158 191 159
f 191 192 159
f 159 192 160
f 160 192 161
f 192 193 161
f 161 193 169
f 162 194 163
f 181 194 162
f 194 195 163
f 195 196 163
f 163 196 165
f 165 197 164
f 164 198 166
f 197 198 164
f 182 198 183
f 166 198 182
f 165 199 197
f 196 199 165
f 169 200 168
f 193 200 169
f 200 201 168
f 170 201 173
f 168 201 170
f 172 202 171
f 202 203 171
f 171 203 174
f 174 203 178
f 173 204 172
f 172 204 202
f 201 205 173
f 173 205 204
f 178 206 175
f 175 206 176
f 206 207 176
f 207 208 176
f 177 208 179
f 176 208 177
f 203 209 178
f 202 209 203
f 178 209 206
f 208 210 179
f 179 210 180
f 210 211 180
f 180 211 181
f 211 212 181
f 194 212 195
f 181 212 194
f 198 213 183
f 197 213 198
f 199 213 197
f 183 213 184
f 213 214 184
f 199 214 213
f 214 215 184
f 185 215 188
f 184 215 185
f 187 216 186
f 186 216 190
f 188 217 187
f 187 217 216
f 215 218 188
f 188 218 217
f 189 219 191
f 189 220 219
f 190 220 189
f 190 221 220
f 216 221 190
f 219 222 191
f 191 222 192
f 192 223 193
f 222 223 192
f 193 223 200
f 196 224 199
f 195 224 196
f 212 225 195
f 195 225 224
f 199 226 214
f 224 226 199
f 223 227 200
f 201 227 205
f 200 227 201
f 202 228 209
f 204 228 202
f 205 229 204
f 204 229 228
f 227 230 205
f 205 230 229
f 209 231 206
f 228 231 209
f 206 231 207
f 208 232 210
f 207 232 208
f 207 233 232
f 231 233 207
f 232 234 210
f 210 234 211
f 234 235 211
f 211 235 212
f 212 235 225
f 226 236 214
f 215 236 218
f 214 236 215
f 217 237 216
f 216 237 221
f 218 238 217
f 217 238 237
f 236 239 218
f 218 239 238
f 220 240 219
f 240 241 219
f 227 241 230
f 223 241 227
f 222 241 223
f 219 241 222
f 221 242 220
f 220 242 240
f 237 243 221
f 238 243 237
f 221 243 242
f 225 244 224
f 236 244 239
f 226 244 236
f 224 244 226
f 235 245 225
f 225 245 244
f 244 245 239
f 231 246 233
f 229 246 228
f 228 246 231
f 230 247 229
f 229 247 246
f 241 248 230
f 230 248 247
f 240 248 241
f 233 249 232
f 232 249 234
f 246 250 233
f 233 250 249
f 247 250 246
f 249 251 234
f 250 251 249
f 234 252 235
f 251 252 234
f 235 252 245
f 239 253 238
f 245 253 239
f 238 253 243
f 252 253 245
f 242 254 240
f 248 254 247
f 247 254 250
f 240 254 248
f 243 255 242
f 250 255 251
f 254 255 250
f 242 255 254
f 253 256 243
f 251 256 252
f 252 256 253
f 255 256 251
f 243 256 255

BIN
envmap.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 MiB

1
lib/stb Submodule

@ -0,0 +1 @@
Subproject commit e6afb9cbae4064da8c3e69af3ff5c4629579c1d2

110
model.cpp Normal file
View File

@ -0,0 +1,110 @@
#include <iostream>
#include <cassert>
#include <fstream>
#include <sstream>
#include "model.h"
// fills verts and faces arrays, supposes .obj file to have "f " entries without slashes
Model::Model(const char *filename) : verts(), faces() {
std::ifstream in;
in.open (filename, std::ifstream::in);
if (in.fail()) {
std::cerr << "Failed to open " << filename << std::endl;
return;
}
std::string line;
while (!in.eof()) {
std::getline(in, line);
std::istringstream iss(line.c_str());
char trash;
if (!line.compare(0, 2, "v ")) {
iss >> trash;
Vec3f v;
for (int i=0;i<3;i++) iss >> v[i];
verts.push_back(v);
} else if (!line.compare(0, 2, "f ")) {
Vec3i f;
int idx, cnt=0;
iss >> trash;
while (iss >> idx) {
idx--; // in wavefront obj all indices start at 1, not zero
f[cnt++] = idx;
}
if (3==cnt) faces.push_back(f);
}
}
std::cerr << "# v# " << verts.size() << " f# " << faces.size() << std::endl;
Vec3f min, max;
get_bbox(min, max);
}
// Moller and Trumbore
bool Model::ray_triangle_intersect(const int &fi, const Vec3f &orig, const Vec3f &dir, float &tnear) {
Vec3f edge1 = point(vert(fi,1)) - point(vert(fi,0));
Vec3f edge2 = point(vert(fi,2)) - point(vert(fi,0));
Vec3f pvec = cross(dir, edge2);
float det = edge1*pvec;
if (det<1e-5) return false;
Vec3f tvec = orig - point(vert(fi,0));
float u = tvec*pvec;
if (u < 0 || u > det) return false;
Vec3f qvec = cross(tvec, edge1);
float v = dir*qvec;
if (v < 0 || u + v > det) return false;
tnear = edge2*qvec * (1./det);
return tnear>1e-5;
}
int Model::nverts() const {
return (int)verts.size();
}
int Model::nfaces() const {
return (int)faces.size();
}
void Model::get_bbox(Vec3f &min, Vec3f &max) {
min = max = verts[0];
for (int i=1; i<(int)verts.size(); ++i) {
for (int j=0; j<3; j++) {
min[j] = std::min(min[j], verts[i][j]);
max[j] = std::max(max[j], verts[i][j]);
}
}
std::cerr << "bbox: [" << min << " : " << max << "]" << std::endl;
}
const Vec3f &Model::point(int i) const {
assert(i>=0 && i<nverts());
return verts[i];
}
Vec3f &Model::point(int i) {
assert(i>=0 && i<nverts());
return verts[i];
}
int Model::vert(int fi, int li) const {
assert(fi>=0 && fi<nfaces() && li>=0 && li<3);
return faces[fi][li];
}
std::ostream& operator<<(std::ostream& out, Model &m) {
for (int i=0; i<m.nverts(); i++) {
out << "v " << m.point(i) << std::endl;
}
for (int i=0; i<m.nfaces(); i++) {
out << "f ";
for (int k=0; k<3; k++) {
out << (m.vert(i,k)+1) << " ";
}
out << std::endl;
}
return out;
}

28
model.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef __MODEL_H__
#define __MODEL_H__
#include <vector>
#include <string>
#include "geometry.h"
class Model {
private:
std::vector<Vec3f> verts;
std::vector<Vec3i> faces;
public:
Model(const char *filename);
int nverts() const; // number of vertices
int nfaces() const; // number of triangles
bool ray_triangle_intersect(const int &fi, const Vec3f &orig, const Vec3f &dir, float &tnear);
const Vec3f &point(int i) const; // coordinates of the vertex i
Vec3f &point(int i); // coordinates of the vertex i
int vert(int fi, int li) const; // index of the vertex for the triangle fi and local index li
void get_bbox(Vec3f &min, Vec3f &max); // bounding box for all the vertices, including isolated ones
};
std::ostream& operator<<(std::ostream& out, Model &m);
#endif //__MODEL_H__

BIN
out-envmap-duck.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 820 KiB

BIN
out-envmap.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 822 KiB

BIN
out.jpg

Binary file not shown.

Before

Width:  |  Height:  |  Size: 104 KiB

After

Width:  |  Height:  |  Size: 259 KiB

View File

@ -3,8 +3,18 @@
#include <iostream>
#include <fstream>
#include <vector>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "model.h"
#include "geometry.h"
int envmap_width, envmap_height;
std::vector<Vec3f> envmap;
Model duck("../duck.obj");
struct Light {
Light(const Vec3f &p, const float &i) : position(p), intensity(i) {}
Vec3f position;
@ -58,6 +68,8 @@ Vec3f refract(const Vec3f &I, const Vec3f &N, const float &refractive_index) { /
return k < 0 ? Vec3f(0,0,0) : I*eta + n*(eta * cosi - sqrtf(k));
}
bool scene_intersect(const Vec3f &orig, const Vec3f &dir, const std::vector<Sphere> &spheres, Vec3f &hit, Vec3f &N, Material &material) {
float spheres_dist = std::numeric_limits<float>::max();
for (size_t i=0; i < spheres.size(); i++) {
@ -133,21 +145,33 @@ void render(const std::vector<Sphere> &spheres, const std::vector<Light> &lights
}
}
std::ofstream ofs; // save the framebuffer to file
ofs.open("./out.ppm");
ofs << "P6\n" << width << " " << height << "\n255\n";
std::vector<unsigned char> pixmap(width*height*3);
for (size_t i = 0; i < height*width; ++i) {
Vec3f &c = framebuffer[i];
float max = std::max(c[0], std::max(c[1], c[2]));
if (max>1) c = c*(1./max);
for (size_t j = 0; j<3; j++) {
ofs << (char)(255 * std::max(0.f, std::min(1.f, framebuffer[i][j])));
pixmap[i*3+j] = (unsigned char)(255 * std::max(0.f, std::min(1.f, framebuffer[i][j])));
}
}
ofs.close();
stbi_write_jpg("out.jpg", width, height, 3, pixmap.data(), 100);
}
int main() {
int n = -1;
unsigned char *pixmap = stbi_load("../envmap.jpg", &envmap_width, &envmap_height, &n, 0);
if (!pixmap || 3!=n) {
std::cerr << "Error: can not load the environment map" << std::endl;
return -1;
}
envmap = std::vector<Vec3f>(envmap_width*envmap_height);
for (int j = envmap_height-1; j>=0 ; j--) {
for (int i = 0; i<envmap_width; i++) {
envmap[i+j*envmap_width] = Vec3f(pixmap[(i+j*envmap_width)*3+0], pixmap[(i+j*envmap_width)*3+1], pixmap[(i+j*envmap_width)*3+2])*(1/255.);
}
}
stbi_image_free(pixmap);
Material ivory(1.0, Vec4f(0.6, 0.3, 0.1, 0.0), Vec3f(0.4, 0.4, 0.3), 50.);
Material glass(1.5, Vec4f(0.0, 0.5, 0.1, 0.8), Vec3f(0.6, 0.7, 0.8), 125.);
Material red_rubber(1.0, Vec4f(0.9, 0.1, 0.0, 0.0), Vec3f(0.3, 0.1, 0.1), 10.);