Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML\Window.hpp>
- #include <SFML\OpenGL.hpp>
- GLuint Textures;
- class Draft
- {
- public:
- float* Data = nullptr;
- int Size = 0;
- Draft() : Size(0), Data(nullptr) {}
- Draft(const int NewSize) : Size(NewSize)
- {
- Data = new float[NewSize * NewSize];
- for (int i = 0; i < NewSize * NewSize; ++i)
- Data[i] = 0.0f;
- };
- Draft(Draft& In) :
- Draft(In.Size)
- {
- this->Data = new float[In.Size * In.Size];
- for (int i = 0; i < In.Size * In.Size; ++i)
- this->Data[i] = In.Data[i];
- }
- ~Draft() { if (Size != 0) delete[] Data; Size = 0; }
- Draft& operator=(const Draft& Other) // copy assignment
- {
- this->Size = Other.Size;
- this->Data = new float[this->Size * this->Size];
- for (int i = 0; i < this->Size * this->Size; ++i)
- this->Data[i] = Other.Data[i];
- return *this;
- }
- void Random()
- {
- for (int i = 0; i < Size * Size; ++i)
- Data[i] = 0;
- /*for (int x = Size / 4; x < Size - Size / 4; ++x)
- for (int y = Size / 4; y < Size - Size / 4; ++y)
- Data[x + y * Size] =
- (float)rand() / (float)RAND_MAX;*/
- Data[Size / 2 + Size * Size / 2] = 1;
- Data[Size / 2 + Size * (Size / 2) - 1] = 0.2;
- Data[Size / 2 + Size * (Size / 2) + 1] = 0.2;
- }
- void Draw()
- {
- glBindTexture(GL_TEXTURE_2D, Textures);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, Size, Size, 0, GL_RED, GL_FLOAT, Data);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glBegin(GL_QUADS); //Render Quads
- glTexCoord2f(0, 0); glVertex2f(0.0, 0.0);
- glTexCoord2f(0, 1); glVertex2f(0.0, 1.0);
- glTexCoord2f(1, 1); glVertex2f(1.0, 1.0);
- glTexCoord2f(1, 0); glVertex2f(1.0, 0.0);
- glEnd(); //finish quads
- }
- static Draft Resize(const Draft& Other, const int Size)
- {
- Draft New(Size);
- New.Data = new float[Size * Size];
- const float Ratio = ((float)Other.Size / (float)Size);
- for (int x = 0; x < Size; ++x)
- for (int y = 0; y < Size; ++y)
- {
- const int SourceX = (int)(float(x) * Ratio);
- const int SourceY = (int)(float(y) * Ratio);
- New.Data[x + y * Size] = Other.Data[SourceX + SourceY * Other.Size];
- }
- return New;
- }
- };
- const float SynapseDefaultPower = 1.5f;
- class AI
- {
- public:
- float* Synapse;
- int Radius;
- int Size;
- AI(const int NewRadius) :
- Radius(NewRadius),
- Size(1 + NewRadius * 2)
- {
- Synapse = new float[Size * Size];
- for (int i = 0; i < Size * Size; ++i)
- Synapse[i] = (((float)rand() / (float)RAND_MAX) - 0.5f) * SynapseDefaultPower;
- };
- AI(AI& In, const float Change) :
- Radius(In.Radius),
- Size(In.Size)
- {
- Synapse = new float[Size * Size];
- for (int i = 0; i < Size * Size; ++i)
- Synapse[i] = In.Synapse[i] + (((float)rand() / (float)RAND_MAX) - 0.5f) * Change;
- };
- AI(AI& In)
- {
- this->Size = In.Size;
- this->Radius = In.Radius;
- Synapse = new float[this->Size * this->Size];
- for (int i = 0; i < this->Size * this->Size; ++i)
- this->Synapse[i] = In.Synapse[i];
- }
- ~AI()
- {
- delete[] Synapse;
- }
- AI& operator=(const AI& Other) // copy assignment
- {
- if (this != &Other) { // self-assignment check expected
- this->Radius = Other.Radius;
- this->Size = Other.Size;
- Synapse = new float[this->Size * this->Size];
- for (int i = 0; i < this->Size * this->Size; ++i)
- this->Synapse[i] = Other.Synapse[i];
- }
- return *this;
- }
- Draft Apply(Draft& In)
- {
- Draft New(In);
- for (int x = 0 + Radius + 1; x < In.Size - Radius - 1; ++x)
- for (int y = 0 + Radius + 1; y < In.Size - Radius - 1; ++y)
- {
- float f = Think(In, x, y);
- New.Data[x + y * In.Size] = f;
- }
- return New;
- }
- private:
- inline float Think(const Draft& In, const int CenterX, const int CenterY)
- {
- float f = 0.f;
- int S = 0;
- for (int x = CenterX - Radius; x <= CenterX + Radius; ++x)
- for (int y = CenterY - Radius; y <= CenterY + Radius; ++y)
- {
- f = f + In.Data[x + y * In.Size] * Synapse[S];
- ++S;
- }
- return (f / (1.f + abs(f)));
- }
- };
- int main()
- {
- //open SFML window
- sf::Window Window(sf::VideoMode(1000, 1000), "Test");
- Window.setFramerateLimit(60);
- glGenTextures(1, &Textures);
- Draft Input(8);
- Input.Random();
- AI AI1(2);
- AI AI2(2);
- AI AI3(2);
- AI AIR(2);
- AI AIG(2);
- AI AIB(2);
- //Program loop
- while (Window.isOpen())
- {
- //event handling
- sf::Event Event;
- while (Window.pollEvent(Event))
- {
- if (Event.type == sf::Event::Closed)
- Window.close();
- if (Event.type == sf::Event::Resized)
- glViewport(0, 0, Event.size.width, Event.size.height);
- else if (Event.type == sf::Event::KeyPressed)
- {
- //if (Event.key.code == sf::Keyboard::Num1)
- {
- AI1 = AI(2);
- AI2 = AI(2);
- AI3 = AI(2);
- AIR = AI(2);
- AIG = AI(2);
- AIB = AI(2);
- }
- //else if (Event.key.code == sf::Keyboard::Num2)
- {
- // Input.Random();
- }
- }
- }
- AI1 = AI(AI1, 0.02f);
- AI2 = AI(AI2, 0.02f);
- AI3 = AI(AI3, 0.02f);
- AIR = AI(AIR, 0.02f);
- AIG = AI(AIG, 0.02f);
- AIB = AI(AIB, 0.02f);
- const float Ratio = Window.getSize().x / Window.getSize().y;
- glLoadIdentity();
- glTranslatef(-1.f, 1.f, 0.f);
- glScalef(2.f, -2.f / Ratio, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glEnable(GL_TEXTURE_2D);
- //grey
- Draft L1In = Draft::Resize(Input, Input.Size * 3);
- Draft L1 = AI1.Apply(L1In);
- Draft L2In = Draft::Resize(L1, L1.Size * 3);
- Draft L2 = AI2.Apply(L2In);
- Draft L3In = Draft::Resize(L2, L2.Size * 1);
- Draft L3 = AI3.Apply(L3In);
- //colors
- Draft RIn = Draft::Resize(L3, L3.Size * 1);
- Draft R = AIR.Apply(RIn);
- Draft GIn = Draft::Resize(L3, L3.Size * 1);
- Draft G = AIG.Apply(GIn);
- Draft BIn = Draft::Resize(L3, L3.Size * 1);
- Draft B = AIB.Apply(BIn);
- float* RGB = new float[L3.Size * L3.Size * 3];
- int m = 0;
- for (int i = 0; i < L3.Size * L3.Size; ++i)
- {
- RGB[m+0] = R.Data[i];
- RGB[m+1] = G.Data[i];
- RGB[m+2] = B.Data[i];
- m+=3;
- }
- glBindTexture(GL_TEXTURE_2D, Textures);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, L3.Size, L3.Size, 0, GL_RGB, GL_FLOAT, RGB);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glBegin(GL_QUADS); //Render Quads
- glTexCoord2f(0, 0); glVertex2f(0.0, 0.0);
- glTexCoord2f(0, 1); glVertex2f(0.0, 1.0);
- glTexCoord2f(1, 1); glVertex2f(1.0, 1.0);
- glTexCoord2f(1, 0); glVertex2f(1.0, 0.0);
- glEnd(); //finish quads
- delete[] RGB;
- //L3.Draw();
- glDisable(GL_TEXTURE_2D);
- Window.display(); //show SFML in window
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement