Skip to content
Snippets Groups Projects
Commit 9e214973 authored by Philip Trettner's avatar Philip Trettner
Browse files

live coding 3

parent 9a10a476
No related branches found
No related tags found
No related merge requests found
*.txt.user
imgui.ini
.vscode/
bin/
......@@ -4,7 +4,7 @@
#include <GLFW/glfw3.h>
#include <typed-geometry/tg.hh>
#include <typed-geometry/tg-std.hh>
#include <glow/common/scoped_gl.hh>
......@@ -13,11 +13,54 @@ void LiveApp::init()
setGui(Gui::ImGui);
GlfwApp::init();
mMeshShader = glow::Program::createFromFile("mesh");
// init terrain
{
tg::rng rng;
mHeightMap.resize(mMapSize * mMapSize);
for (auto z = 0; z < mMapSize; ++z)
for (auto x = 0; x < mMapSize; ++x)
mHeightMap[idxOf(x, z)] = uniform(rng, -.8f, .8f);
for (auto z = 0; z < mMapSize - 1; ++z)
for (auto x = 0; x < mMapSize - 1; ++x)
{
auto make_pos = [&](int x, int z) {
auto h = mHeightMap[idxOf(x, z)];
return tg::pos3(x - mMapSize / 2, h, z - mMapSize / 2);
};
auto p00 = make_pos(x + 0, z + 0);
auto p01 = make_pos(x + 0, z + 1);
auto p10 = make_pos(x + 1, z + 0);
auto p11 = make_pos(x + 1, z + 1);
debugMesh.add_triangle(p00, p11, p10, tg::color3::green);
debugMesh.add_triangle(p00, p01, p11, tg::color3::green);
}
}
debugMesh.add_triangle({1, 1, 0}, {7, 1, -1}, {4, 1, -3}, tg::color3::red);
debugMesh.add_triangle({3, 0, -2}, {1, 0, -3}, {-2, 0, 5}, tg::color3::blue);
debugMesh.upload();
}
void LiveApp::update(float elapsedSeconds)
{
// TODO
auto const speed = 10.f;
if (mKeyDownW)
cam.pos += cam.dir * elapsedSeconds * speed;
if (mKeyDownS)
cam.pos -= cam.dir * elapsedSeconds * speed;
if (mKeyDownD)
cam.pos += cam.right() * elapsedSeconds * speed;
if (mKeyDownA)
cam.pos -= cam.right() * elapsedSeconds * speed;
}
void LiveApp::render(float elapsedSeconds)
......@@ -25,12 +68,26 @@ void LiveApp::render(float elapsedSeconds)
auto const w = getWindowWidth();
auto const h = getWindowHeight();
cam.aspect_ratio = w / float(h);
// clear screen
GLOW_SCOPED(clearColor, tg::color3::black);
GLOW_SCOPED(enable, GL_DEPTH_TEST);
GLOW_SCOPED(enable, GL_CULL_FACE);
glViewport(0, 0, w, h);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// TODO
// camera matrices
auto proj = cam.proj();
auto view = cam.view();
{
auto shader = mMeshShader->use();
shader["uProj"] = proj;
shader["uView"] = view;
debugMesh.vertexArray->bind().draw();
}
}
void LiveApp::onGui()
......@@ -38,6 +95,27 @@ void LiveApp::onGui()
// TODO
}
bool LiveApp::onMousePosition(double x, double y)
{
static double lastX = x;
static double lastY = y;
auto dx = float(x - lastX);
auto dy = float(y - lastY);
auto cam_speed = -360_deg / 2000;
auto rotX = tg::rotation_around(cam_speed * dx, cam.up);
cam.dir = normalize(rotX * cam.dir);
auto rotY = tg::rotation_around(cam_speed * dy, cam.right());
cam.dir = normalize(rotY * cam.dir);
lastX = x;
lastY = y;
return true;
}
bool LiveApp::onKey(int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_A && action == GLFW_PRESS)
......@@ -67,3 +145,14 @@ bool LiveApp::onKey(int key, int scancode, int action, int mods)
return false;
}
void mesh::upload()
{
arrayBuffer = glow::ArrayBuffer::create();
arrayBuffer->defineAttribute(&vertex::pos, "aPosition");
arrayBuffer->defineAttribute(&vertex::normal, "aNormal");
arrayBuffer->defineAttribute(&vertex::color, "aColor");
arrayBuffer->bind().setData(vertices);
vertexArray = glow::VertexArray::create(arrayBuffer, GL_TRIANGLES);
}
......@@ -4,8 +4,62 @@
#include <typed-geometry/tg-lean.hh>
#include <glow/objects/ArrayBuffer.hh>
#include <glow/objects/Program.hh>
#include <glow/objects/VertexArray.hh>
#include <vector>
struct vertex
{
tg::pos3 pos;
tg::vec3 normal;
tg::color3 color;
};
struct mesh
{
std::vector<vertex> vertices;
glow::SharedArrayBuffer arrayBuffer; // vertex data
glow::SharedVertexArray vertexArray; // mesh config
void add_triangle(tg::pos3 v0, tg::pos3 v1, tg::pos3 v2, tg::color3 c)
{
auto n = normalize(cross(v1 - v0, v2 - v0));
vertices.push_back({v0, n, c});
vertices.push_back({v1, n, c});
vertices.push_back({v2, n, c});
}
void upload();
};
struct camera
{
float aspect_ratio = 1.f;
tg::pos3 pos = {8, 3, 8};
tg::dir3 dir = normalize(tg::pos3::zero - pos);
tg::dir3 up = {0, 1, 0};
tg::dir3 right() const { return normalize(cross(dir, up)); }
tg::mat4 proj() const { return tg::perspective_opengl(70_deg, aspect_ratio, 0.1f, 100.f); }
tg::mat4 view() const { return tg::look_at(pos, dir, tg::vec3::unit_y); }
};
class LiveApp : public glow::glfw::GlfwApp
{
mesh debugMesh;
camera cam;
glow::SharedProgram mMeshShader;
int mMapSize = 128;
std::vector<float> mHeightMap;
int idxOf(int x, int z) const { return z * mMapSize + x; }
bool mKeyDownW = false;
bool mKeyDownA = false;
bool mKeyDownS = false;
......@@ -20,5 +74,6 @@ public:
void onGui() override;
bool onMousePosition(double x, double y) override;
bool onKey(int key, int scancode, int action, int mods) override;
};
in vec3 vNormal;
in vec3 vColor;
out vec3 fColor;
void main()
{
fColor = vColor * max(0, vNormal.y);
}
uniform mat4 uView;
uniform mat4 uProj;
in vec3 aPosition;
in vec3 aNormal;
in vec3 aColor;
out vec3 vColor;
out vec3 vNormal;
void main()
{
vColor = aColor;
vNormal = aNormal;
gl_Position = uProj * uView * vec4(aPosition, 1);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment