From 95ef47ac4432899867c3c4f425290832eacb3495 Mon Sep 17 00:00:00 2001 From: ExMix Date: Tue, 24 Sep 2013 19:04:24 +0300 Subject: [PATCH] [drape_head] geometry generator. need for debug --- drape/utils/list_generator.cpp | 161 +++++++++++++++++++++++++++++++++ drape/utils/list_generator.hpp | 22 +++++ 2 files changed, 183 insertions(+) create mode 100644 drape/utils/list_generator.cpp create mode 100644 drape/utils/list_generator.hpp diff --git a/drape/utils/list_generator.cpp b/drape/utils/list_generator.cpp new file mode 100644 index 0000000000..146b4fde36 --- /dev/null +++ b/drape/utils/list_generator.cpp @@ -0,0 +1,161 @@ +#include "list_generator.hpp" + +#include "../../base/math.hpp" + +#include "../../std/algorithm.hpp" +#include "../../std/vector.hpp" + +struct Vertex +{ + Vertex() {} + Vertex(float x , float y) + : m_x(x) + , m_y(y) + { + } + + float m_x; + float m_y; +}; + +struct Color +{ + Color() {} + Color(float r, float g, float b, float a) + : m_r(r) + , m_g(g) + , m_b(b) + , m_a(a) + { + } + + float m_r; + float m_g; + float m_b; + float m_a; +}; + +ListGenerator::ListGenerator() + : m_depth(0.0f) + , m_x(0.0f) + , m_y(0.0f) + , m_width(0.0f) + , m_height(0.0f) +{ +} + +void ListGenerator::SetDepth(float depth) +{ + m_depth = depth; +} + +void ListGenerator::SetViewport(float x, float y, float width, float height) +{ + m_x = x; + m_y = y; + m_width = width; + m_height = height; +} + +void ListGenerator::SetProgram(uint32_t program) +{ + m_programIndex = program; +} + +void ListGenerator::SetUniforms(const vector & uniforms) +{ + m_uniforms = uniforms; +} + +namespace +{ + float clamp01(float value) + { + return min(max(value, 0.0f), 1.0f); + } + + float colorChannel() + { + return clamp01((rand() % 100) / 99.0f); + } +} + +void ListGenerator::Generate(int count, Batcher & batcher) +{ + float quadCount = count /2.0; + int dimensionCount = ceil(sqrt(quadCount)); + float dx = m_width / (float)dimensionCount; + float dy = m_height / (float)dimensionCount; + int vertexCount = count * 3; + + vector vertexes; + vertexes.reserve(vertexCount); + + int counter = 0; + for (int x = 0; x < dimensionCount; ++x) + { + for (int y = 0; y < dimensionCount; ++y) + { + if (counter >= count) + break; + + vertexes.push_back(Vertex(m_x + dx * (float)x, m_y + dy * (float)y)); + vertexes.push_back(Vertex(m_x + dx * (float)x, m_y + dy * (float)y + dy)); + vertexes.push_back(Vertex(m_x + dx * (float)x + dx, m_y + dy * (float)y)); + + vertexes.push_back(Vertex(m_x + dx * (float)x + dx, m_y + dy * (float)y)); + vertexes.push_back(Vertex(m_x + dx * (float)x, m_y + dy * (float)y + dy)); + vertexes.push_back(Vertex(m_x + dx * (float)x + dx, m_y + dy * (float)y + dy)); + + counter += 2; + } + } + + vector colors; + colors.reserve(vertexCount); + for (int i = 0; i < vertexCount; ++i) + colors.push_back(Color( colorChannel() + , colorChannel() + , colorChannel() + , colorChannel())); + + AttributeProvider provider(3, vertexCount); + { + BindingInfo info(1); + BindingDecl & decl = info.GetBindingDecl(0); + decl.m_attributeName = "position"; + decl.m_componentCount = 2; + decl.m_componentType = GLConst::GLFloatType; + decl.m_offset = 0; + decl.m_stride = 0; + provider.InitStream(0, info, &vertexes[0]); + } + + { + BindingInfo info(1); + BindingDecl & decl = info.GetBindingDecl(0); + decl.m_attributeName = "depth"; + decl.m_componentCount = 1; + decl.m_componentType = GLConst::GLFloatType; + decl.m_offset = 0; + decl.m_stride = 0; + vector depthMemory(vertexCount, m_depth); + provider.InitStream(1, info, &depthMemory[0]); + } + + { + BindingInfo info(1); + BindingDecl & decl = info.GetBindingDecl(0); + decl.m_attributeName = "color"; + decl.m_componentCount = 4; + decl.m_componentType = GLConst::GLFloatType; + decl.m_offset = 0; + decl.m_stride = 0; + provider.InitStream(2, info, &colors[0]); + } + + TextureBinding textureBinding("", false, 0, ReferencePoiner(NULL)); + GLState state(m_programIndex, (int16_t)m_depth, textureBinding); + state.GetUniformValues() = m_uniforms; + batcher.InsertTriangleList(state, &provider); +} diff --git a/drape/utils/list_generator.hpp b/drape/utils/list_generator.hpp new file mode 100644 index 0000000000..a92c925e8f --- /dev/null +++ b/drape/utils/list_generator.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "../batcher.hpp" + +class ListGenerator +{ +public: + ListGenerator(); + + void SetDepth(float depth); + void SetViewport(float x, float y, float width, float height); + void SetProgram(uint32_t program); + void SetUniforms(const vector & uniforms); + + void Generate(int count, Batcher & batcher); + +private: + float m_depth; + float m_x, m_y, m_width, m_height; + uint32_t m_programIndex; + vector m_uniforms; +};