Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "src\base.h"
- #include "src\render.h"
- #include "src\glCommands.h"
- #include "src\render\meshGens.h"
- #include "src\render\assetLoader.h"
- #include "src\render\shaderFac.h"
- #include <iostream>
- #include <vector>
- #include "src\matrix\vec4.h"
- #include "src\matrix\vec3.h"
- #include "src\ext\math.h"
- #include "src\ext\handlerMap.h"
- class Block {
- private:
- typedef engine::Model Model;
- Model model;
- matrix::Vec3 pos = {};
- matrix::Vec3 vel = {};
- matrix::Vec3 rotAxis = {};
- f32 rotRate = 0.0;
- f32 alive = 0.0;
- public:
- Block() : model(createModel()) {
- using namespace ext::math;
- vel[0] = randFloat(-1.0f, 1.0f);
- vel[1] = randFloat(-1.0f, 1.0f);
- vel[2] = randFloat(-1.0f, 1.0f);
- //vel *= 5.0f;
- rotAxis[0] = randFloat(-1.0f, 1.0f);
- rotAxis[1] = randFloat(-1.0f, 1.0f);
- rotAxis[2] = randFloat(-1.0f, 1.0f);
- rotAxis.normalize();
- //std::cout << rotAxis.length() << "\n";
- rotRate = randFloat(0.1f, 500.0f);
- alive = randFloat(-10.0f, 0.0f);
- //alive = 0.0f;
- }
- void update(number dt) {
- //if (alive > 10.0f) {
- // model.clear();
- //}
- alive += dt;
- model.pos += vel * dt;
- //std::cout << rotRate * alive << "\n";
- model.rot.axisAngle(rotAxis, rotRate * alive);
- model.scl.setTo(0.1, 0.1, 0.1);
- }
- void draw(const engine::BasicCamera& camera) {
- model.drawModel(camera);
- }
- private:
- static Model createModel() {
- static auto layout = engine::AttrLayout{} //TODO: automagically generate chunk layout based on shader inputs
- .attrPointer("a_pos", 3, DataType::F32)
- .attrPointer("a_vertexColor", 4, DataType::U8, true)
- .attrPointer("a_uvs", 2, DataType::U16, true)
- ;
- static engine::DrawBatch batch { layout };
- engine::Model model{ batch };
- model.attachMesh(
- [] {
- auto mesh = engine::createMesh::Plane();
- mesh.colors = {
- 255, 0, 0, 255,
- 0, 255, 0, 255,
- 0, 0, 255, 255,
- 255, 255, 0, 255,
- };
- return mesh;
- }(),
- [&] {
- engine::Material mat{};
- //mat.useTexture(engine::mat::TEXTURE_ADD, tex);
- mat.vertexColor = engine::mat::VERTEX_COLOR_ADD;
- return mat;
- }()
- );
- return model;
- }
- };
- int main(int argc, char *argv[])
- {
- engine::Window window{};
- engine::MainLoop loop{};
- engine::BasicCamera camera{ window };
- camera.getProjection().perspective(ext::math::toRadians(90.0), 640.0 / 480.0, 0.01, 1000.0);
- camera.getView().setTranslation(0.0, 0.0, -5.0);
- camera.madeChanges();
- engine::AssetLoader loader{};
- auto tex = loader.loadImg(R"(assets\smug.jpg)");
- engine::TextureObj tex_4Pixels{}; // testing a texture constructed by hand...
- {
- u8 arr[16] = {
- 196, 0, 0, 255,
- 0, 196, 0, 255,
- 0, 0, 196, 255,
- 196, 0, 196, 255
- };
- tex_4Pixels.asColorTexture(arr, 2, 2, 4);
- tex_4Pixels.setMagFilter(GL_NEAREST);
- tex_4Pixels.setMinFilter(GL_NEAREST);
- }
- auto layout = engine::AttrLayout{} //TODO: automagically generate chunk layout based on shader inputs
- .attrPointer("a_pos", 3, DataType::F32)
- .attrPointer("a_vertexColor", 4, DataType::U8, true)
- .attrPointer("a_uvs", 2, DataType::U16, true)
- ;
- auto batch = engine::DrawBatch{ layout };
- engine::Model model{ batch };
- model.attachMesh(
- []{return engine::createMesh::Plane();}(),
- [&]{
- engine::Material mat{};
- mat.useTexture(engine::mat::TEXTURE_ADD, tex_4Pixels);
- return mat;
- }()
- );
- engine::Model model2{ batch };
- model2.attachMesh(
- [] {
- auto mesh = engine::createMesh::Cube();
- mesh.colors = {
- 255, 0, 0, 255,
- 0, 255, 0, 255,
- 0, 0, 255, 255,
- 255, 255, 0, 255,
- 255, 0, 0, 255,
- 0, 255, 0, 255,
- 0, 0, 255, 255,
- 255, 255, 0, 255,
- 255, 0, 0, 255,
- 0, 255, 0, 255,
- 0, 0, 255, 255,
- 255, 255, 0, 255,
- 255, 0, 0, 255,
- 0, 255, 0, 255,
- 0, 0, 255, 255,
- 255, 255, 0, 255,
- 255, 0, 0, 255,
- 0, 255, 0, 255,
- 0, 0, 255, 255,
- 255, 255, 0, 255,
- 255, 0, 0, 255,
- 0, 255, 0, 255,
- 0, 0, 255, 255,
- 255, 255, 0, 255
- };
- return mesh;
- }(),
- [&] {
- engine::Material mat{};
- mat.useTexture(engine::mat::TEXTURE_ADD, tex);
- mat.vertexColor = engine::mat::VERTEX_COLOR_ADD;
- return mat;
- }()
- );
- std::vector<Block> blocks{};
- loop.attachMouseDownCallback([&](){
- auto amt = 1000;
- for (auto i = 0; i < amt; ++i) {
- //auto h = blocks.acquire();
- blocks.emplace_back();
- }
- std::cout << "# of blocks: " << blocks.size() << "\n";
- });
- glEnable(GL_DEPTH_TEST);
- auto time = 0.0f;
- loop.flexStep([&](auto dt, auto ts) {
- glClearColor(0.5, 0.5, 0.5, 1.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- model.rot.axisAngle({ 0.0f, 1.0f, 0.0f }, time * 2.0f);
- model.drawModel(camera);
- model2.pos[0] = (std::sin(time*0.5f));
- model2.rot.axisAngle({ 0.0, 0.71f, 0.71f }, time * 5.0f);
- model2.scl = matrix::Vec3{ 1.0f, 1.0f, 1.0f } * (std::sin(time)+1.0f);
- model2.drawModel(camera);
- for (auto& b : blocks) {
- b.update(dt);
- b.draw(camera);
- }
- window.swapBuffer();
- time += static_cast<float>(dt);
- });
- loop.start();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement