X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgraphics%2Fshader.cpp;h=b74d225f8d49b34a8d749fa5c127f8f9dbc381eb;hb=a19fdf3d9f0d7ecbf6eeeec817856d85049a8336;hp=0890dfdc8d308bf84f1de0c47db381bc35fca1e5;hpb=2ab70a92ae39cebc6166ef15545ebcbd31a31c38;p=blobs.git diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 0890dfd..b74d225 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -190,18 +190,21 @@ PlanetSurface::PlanetSurface() "#version 330 core\n" "layout(location = 0) in vec3 vtx_position;\n" - "layout(location = 1) in vec3 vtx_tex_uv;\n" + "layout(location = 1) in vec3 vtx_normal;\n" + "layout(location = 2) in vec3 vtx_tex_uv;\n" "uniform mat4 M;\n" "uniform mat4 MV;\n" "uniform mat4 MVP;\n" - "out vec3 frag_tex_uv;\n" "out vec3 vtx_viewspace;\n" + "out vec3 nrm_viewspace;\n" + "out vec3 frag_tex_uv;\n" "void main() {\n" - "gl_Position = MVP * vec4(vtx_position, 1);\n" - "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n" + "gl_Position = MVP * vec4(vtx_position, 1.0);\n" + "vtx_viewspace = (MV * vec4(vtx_position, 1.0)).xyz;\n" + "nrm_viewspace = (MV * vec4(vtx_position, 0.0)).xyz;\n" "frag_tex_uv = vtx_tex_uv;\n" "}\n" ); @@ -216,16 +219,17 @@ PlanetSurface::PlanetSurface() "};\n" "in vec3 vtx_viewspace;\n" + "in vec3 nrm_viewspace;\n" "in vec3 frag_tex_uv;\n" "uniform sampler2DArray tex_sampler;\n" - "uniform vec3 normal;\n" "uniform int num_lights;\n" "uniform LightSource light[8];\n" "out vec3 color;\n" "void main() {\n" + "vec3 normal = normalize(nrm_viewspace);\n" "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n" "vec3 total_light = tex_color * vec3(0.1, 0.1, 0.1);\n" "for (int i = 0; i < num_lights; ++i) {\n" @@ -253,7 +257,6 @@ PlanetSurface::PlanetSurface() mv_handle = prog.UniformLocation("MV"); mvp_handle = prog.UniformLocation("MVP"); sampler_handle = prog.UniformLocation("tex_sampler"); - normal_handle = prog.UniformLocation("normal"); num_lights_handle = prog.UniformLocation("num_lights"); for (int i = 0; i < MAX_LIGHTS; ++i) { light_handle[3 * i + 0] = prog.UniformLocation("light[" + std::to_string(i) + "].position"); @@ -310,10 +313,6 @@ void PlanetSurface::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm:: prog.Uniform(mvp_handle, mvp); } -void PlanetSurface::SetNormal(const glm::vec3 &n) noexcept { - prog.Uniform(normal_handle, n); -} - void PlanetSurface::SetTexture(ArrayTexture &tex) noexcept { glActiveTexture(GL_TEXTURE0); tex.Bind(); @@ -380,68 +379,95 @@ SunSurface::SunSurface() light_color_handle = prog.UniformLocation("light_color"); light_strength_handle = prog.UniformLocation("light_strength"); + // "resolution" of sphere + constexpr int size = 10; + vao.Bind(); vao.BindAttributes(); vao.EnableAttribute(0); vao.AttributePointer(0, false, offsetof(Attributes, position)); - vao.ReserveAttributes(8, GL_STATIC_DRAW); + vao.ReserveAttributes(4 * 6 * size * size, GL_STATIC_DRAW); { auto attrib = vao.MapAttributes(GL_WRITE_ONLY); - attrib[0].position = glm::vec3(-1.0f, -1.0f, -1.0f); - attrib[1].position = glm::vec3(-1.0f, -1.0f, 1.0f); - attrib[2].position = glm::vec3(-1.0f, 1.0f, -1.0f); - attrib[3].position = glm::vec3(-1.0f, 1.0f, 1.0f); - attrib[4].position = glm::vec3( 1.0f, -1.0f, -1.0f); - attrib[5].position = glm::vec3( 1.0f, -1.0f, 1.0f); - attrib[6].position = glm::vec3( 1.0f, 1.0f, -1.0f); - attrib[7].position = glm::vec3( 1.0f, 1.0f, 1.0f); + + constexpr float radius = float(size) * 0.5f; + int index = 0; + for (int surface = 0; surface < 3; ++surface) { + for (int y = 0; y < size; ++y) { + for (int x = 0; x < size; ++x, ++index) { + glm::vec3 pos[4]; + pos[0][(surface + 0) % 3] = float(x + 0) - radius; + pos[0][(surface + 1) % 3] = float(y + 0) - radius; + pos[0][(surface + 2) % 3] = radius; + pos[1][(surface + 0) % 3] = float(x + 0) - radius; + pos[1][(surface + 1) % 3] = float(y + 1) - radius; + pos[1][(surface + 2) % 3] = radius; + pos[2][(surface + 0) % 3] = float(x + 1) - radius; + pos[2][(surface + 1) % 3] = float(y + 0) - radius; + pos[2][(surface + 2) % 3] = radius; + pos[3][(surface + 0) % 3] = float(x + 1) - radius; + pos[3][(surface + 1) % 3] = float(y + 1) - radius; + pos[3][(surface + 2) % 3] = radius; + attrib[4 * index + 0].position = glm::normalize(pos[0]); + attrib[4 * index + 1].position = glm::normalize(pos[1]); + attrib[4 * index + 2].position = glm::normalize(pos[2]); + attrib[4 * index + 3].position = glm::normalize(pos[3]); + } + } + } + for (int surface = 3; surface < 6; ++surface) { + for (int y = 0; y < size; ++y) { + for (int x = 0; x < size; ++x, ++index) { + glm::vec3 pos[4]; + pos[0][(surface + 0) % 3] = float(x + 0) - radius; + pos[0][(surface + 1) % 3] = float(y + 0) - radius; + pos[0][(surface + 2) % 3] = radius; + pos[1][(surface + 0) % 3] = float(x + 0) - radius; + pos[1][(surface + 1) % 3] = float(y + 1) - radius; + pos[1][(surface + 2) % 3] = radius; + pos[2][(surface + 0) % 3] = float(x + 1) - radius; + pos[2][(surface + 1) % 3] = float(y + 0) - radius; + pos[2][(surface + 2) % 3] = radius; + pos[3][(surface + 0) % 3] = float(x + 1) - radius; + pos[3][(surface + 1) % 3] = float(y + 1) - radius; + pos[3][(surface + 2) % 3] = radius; + attrib[4 * index + 0].position = glm::normalize(pos[0]) * -1.0f; + attrib[4 * index + 1].position = glm::normalize(pos[1]) * -1.0f; + attrib[4 * index + 2].position = glm::normalize(pos[2]) * -1.0f; + attrib[4 * index + 3].position = glm::normalize(pos[3]) * -1.0f; + } + } + } } vao.BindElements(); - vao.ReserveElements(36, GL_STATIC_DRAW); + vao.ReserveElements(6 * 6 * size * size, GL_STATIC_DRAW); { auto element = vao.MapElements(GL_WRITE_ONLY); - // -X - element[ 0] = 0; - element[ 1] = 1; - element[ 2] = 2; - element[ 3] = 2; - element[ 4] = 1; - element[ 5] = 3; - // -Y - element[ 6] = 0; - element[ 7] = 4; - element[ 8] = 1; - element[ 9] = 1; - element[10] = 4; - element[11] = 5; - // -Z - element[12] = 0; - element[13] = 2; - element[14] = 4; - element[15] = 4; - element[16] = 2; - element[17] = 6; - // +Z - element[18] = 1; - element[19] = 5; - element[20] = 3; - element[21] = 3; - element[22] = 5; - element[23] = 7; - // +Y - element[24] = 3; - element[25] = 7; - element[26] = 2; - element[27] = 2; - element[28] = 7; - element[29] = 6; - // +X - element[30] = 5; - element[31] = 4; - element[32] = 7; - element[33] = 7; - element[34] = 4; - element[35] = 6; + int index = 0; + for (int surface = 0; surface < 3; ++surface) { + for (int y = 0; y < size; ++y) { + for (int x = 0; x < size; ++x, ++index) { + element[6 * index + 0] = 4 * index + 0; + element[6 * index + 1] = 4 * index + 2; + element[6 * index + 2] = 4 * index + 1; + element[6 * index + 3] = 4 * index + 1; + element[6 * index + 4] = 4 * index + 2; + element[6 * index + 5] = 4 * index + 3; + } + } + } + for (int surface = 3; surface < 6; ++surface) { + for (int y = 0; y < size; ++y) { + for (int x = 0; x < size; ++x, ++index) { + element[6 * index + 0] = 4 * index + 0; + element[6 * index + 1] = 4 * index + 1; + element[6 * index + 2] = 4 * index + 2; + element[6 * index + 3] = 4 * index + 2; + element[6 * index + 4] = 4 * index + 1; + element[6 * index + 5] = 4 * index + 3; + } + } + } } vao.Unbind(); } @@ -500,8 +526,9 @@ void SunSurface::SetLight(const glm::vec3 &color, float strength) noexcept { } void SunSurface::Draw() const noexcept { + constexpr int size = 10; vao.Bind(); - vao.DrawTriangles(36); + vao.DrawTriangles(6 * 6 * size * size); }