From 071cc0ec1150be27e154750fc18ba11826b2289f Mon Sep 17 00:00:00 2001 From: Syed Ahmed Hussain Date: Sun, 2 Jun 2024 22:24:38 +0530 Subject: [PATCH] add colormap texture for multistate colors --- pyaccell/pyaccell/automata.cpp | 36 +++++++++++++++++++++++++++++++++- pyaccell/pyaccell/automata.hpp | 1 + pyaccell/pyaccell/sim.fs | 3 ++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/pyaccell/pyaccell/automata.cpp b/pyaccell/pyaccell/automata.cpp index e46f52d..77ad8ce 100644 --- a/pyaccell/pyaccell/automata.cpp +++ b/pyaccell/pyaccell/automata.cpp @@ -122,11 +122,13 @@ int pyaccell::Automata::run(int iterations) unsigned int textureBinomials = pyaccell::generate_binomials(); unsigned int indices = pyaccell::no_of_indices(states); unsigned int textureRule = pyaccell::generate_rule(&rule[0], indices, states); - enum SAMPLER {BINOMIAL = 1, INPUT = 2, RULE = 3}; + unsigned int textureColorMap = create_color_map(); + enum SAMPLER {BINOMIAL = 1, INPUT = 2, RULE = 3, COLOR_MAP=4}; simShader.use(); simShader.setInt("uBinomial", BINOMIAL); simShader.setInt("inputStates", INPUT); simShader.setInt("rule", RULE); + simShader.setInt("colorMap", COLOR_MAP); simShader.setInt("numStates", states); simShader.setInt("inputWidth", sim_width); simShader.setInt("inputHeight", sim_height); @@ -164,6 +166,8 @@ int pyaccell::Automata::run(int iterations) glBindTexture(GL_TEXTURE_2D, textureBinomials); glActiveTexture(GL_TEXTURE0 + RULE); glBindTexture(GL_TEXTURE_2D, textureRule); + glActiveTexture(GL_TEXTURE0 + COLOR_MAP); + glBindTexture(GL_TEXTURE_2D, textureColorMap); glActiveTexture(GL_TEXTURE0 + INPUT); glBindTexture(GL_TEXTURE_2D, textureInput[input_index]); glDrawArrays(GL_TRIANGLES, 0, 6); @@ -207,6 +211,36 @@ std::vector pyaccell::Automata::get_texture_data(unsigned int text return data; } +unsigned int pyaccell::Automata::create_color_map() +{ + unsigned int texture; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + unsigned char *data = new unsigned char[MAX_STATES * 3]; + for (int i=0; i<3; ++i) data[i] = 20; // state 0 (dark) + for (int i=3; i<6; ++i) data[i] = 255; // state 1 (white) + data[6] = 255; data[7] = 20; data[8] = 20; // state 3 (red) + // remaining states (random colors) + unsigned char color[] = {(char)10, (char)20, (char)40}; + for (int i=9; i get_texture_data(unsigned int texture, const unsigned int width, const unsigned int height); + unsigned int create_color_map(); enum RUN_TYPE {NO_FINAL_STATE, FINAL_STATE}; }; } \ No newline at end of file diff --git a/pyaccell/pyaccell/sim.fs b/pyaccell/pyaccell/sim.fs index bc41786..3593e50 100644 --- a/pyaccell/pyaccell/sim.fs +++ b/pyaccell/pyaccell/sim.fs @@ -7,6 +7,7 @@ in vec2 TexCoords; uniform usampler2D uBinomial; uniform usampler2D inputStates; uniform usampler2D rule; +uniform sampler2D colorMap; uniform int numStates; uniform int inputWidth; // width used to create input texture uniform int inputHeight; @@ -45,5 +46,5 @@ void main() uint newstate = texelFetch(rule, ivec2(index, curstate), 0).r; nextstate = newstate; - col = vec3(float(nextstate)); + col = texture(colorMap, vec2((float(nextstate) + 0.5) / 14.0, 0.5)).rgb; } \ No newline at end of file