#include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtx/string_cast.hpp> #include<glm/gtc/type_ptr.hpp> #include <iostream> #include <vector> #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" using point3 = glm::vec3; using vec3 = glm::vec3; struct ray { const point3 orig; const vec3 dir; ray(const point3& o, const point3& d) : orig(o), dir(d) { } point3 at(float t) {return orig + t*dir;} }; void save_fig(const int& width, const int& height, const std::string& fname) { const int channel_num = 3; std::vector<uint8_t> pixels(width*height*channel_num, 0); int index=0; for(int j = height-1; j>=0; j--) { for(int i = 0; i< width; i++) { float r = (1.f*i)/width; float g = (1.f*j)/height; float b = 0.3; int ir = int(255.99 * r); int ig = int(255.99 * g); int ib = int(255.99 * b); pixels[index++]= ir; pixels[index++]= ig; pixels[index++]= ib; } } stbi_write_png(fname.c_str(), width, height, channel_num, pixels.data(), width*channel_num); } int main() { point3 eye = point3(0); vec3 dir = vec3(1, 1, 0); ray r(eye, dir); std::cout << glm::to_string(r.orig) << "\n"; std::cout << glm::to_string(r.at(2)) << "\n"; glm::vec4 pos00(0, 1, 0, 0); std::cout << glm::to_string(pos00) <<"\n"; glm::vec3 pos01(2.5); std::cout << glm::to_string(pos01) <<"\n"; glm::vec4 pos02(pos01, 1); std::cout << glm::to_string(pos02) <<"\n"; glm::mat4 model = glm::mat4(1); std::cout << glm::to_string(model) <<"\n"; glm::vec4 t1 = pos02 * model; std::cout << glm::to_string(t1) <<"\n"; float* pm = glm::value_ptr(model); save_fig(200, 100, "slika_00.png"); return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}