Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. #include <SFML\Window.hpp>
  2. #include <SFML\OpenGL.hpp>
  3.  
  4. GLuint Textures;
  5.  
  6. class Draft
  7. {
  8. public:
  9. float* Data = nullptr;
  10. int Size = 0;
  11.  
  12. Draft() : Size(0), Data(nullptr) {}
  13. Draft(const int NewSize) : Size(NewSize)
  14. {
  15. Data = new float[NewSize * NewSize];
  16. for (int i = 0; i < NewSize * NewSize; ++i)
  17. Data[i] = 0.0f;
  18. };
  19. Draft(Draft& In) :
  20. Draft(In.Size)
  21. {
  22. this->Data = new float[In.Size * In.Size];
  23. for (int i = 0; i < In.Size * In.Size; ++i)
  24. this->Data[i] = In.Data[i];
  25. }
  26. ~Draft() { if (Size != 0) delete[] Data; Size = 0; }
  27. Draft& operator=(const Draft& Other) // copy assignment
  28. {
  29. this->Size = Other.Size;
  30. this->Data = new float[this->Size * this->Size];
  31. for (int i = 0; i < this->Size * this->Size; ++i)
  32. this->Data[i] = Other.Data[i];
  33. return *this;
  34. }
  35. void Random()
  36. {
  37. for (int i = 0; i < Size * Size; ++i)
  38. Data[i] = 0;
  39.  
  40. /*for (int x = Size / 4; x < Size - Size / 4; ++x)
  41. for (int y = Size / 4; y < Size - Size / 4; ++y)
  42. Data[x + y * Size] =
  43. (float)rand() / (float)RAND_MAX;*/
  44.  
  45.  
  46. Data[Size / 2 + Size * Size / 2] = 1;
  47. Data[Size / 2 + Size * (Size / 2) - 1] = 0.2;
  48. Data[Size / 2 + Size * (Size / 2) + 1] = 0.2;
  49.  
  50. }
  51. void Draw()
  52. {
  53. glBindTexture(GL_TEXTURE_2D, Textures);
  54. glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, Size, Size, 0, GL_RED, GL_FLOAT, Data);
  55. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  56. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  57.  
  58. glBegin(GL_QUADS); //Render Quads
  59. glTexCoord2f(0, 0); glVertex2f(0.0, 0.0);
  60. glTexCoord2f(0, 1); glVertex2f(0.0, 1.0);
  61. glTexCoord2f(1, 1); glVertex2f(1.0, 1.0);
  62. glTexCoord2f(1, 0); glVertex2f(1.0, 0.0);
  63. glEnd(); //finish quads
  64. }
  65.  
  66. static Draft Resize(const Draft& Other, const int Size)
  67. {
  68. Draft New(Size);
  69. New.Data = new float[Size * Size];
  70. const float Ratio = ((float)Other.Size / (float)Size);
  71. for (int x = 0; x < Size; ++x)
  72. for (int y = 0; y < Size; ++y)
  73. {
  74. const int SourceX = (int)(float(x) * Ratio);
  75. const int SourceY = (int)(float(y) * Ratio);
  76.  
  77. New.Data[x + y * Size] = Other.Data[SourceX + SourceY * Other.Size];
  78. }
  79. return New;
  80. }
  81. };
  82.  
  83. const float SynapseDefaultPower = 1.5f;
  84.  
  85. class AI
  86. {
  87. public:
  88. float* Synapse;
  89. int Radius;
  90. int Size;
  91.  
  92. AI(const int NewRadius) :
  93. Radius(NewRadius),
  94. Size(1 + NewRadius * 2)
  95. {
  96. Synapse = new float[Size * Size];
  97. for (int i = 0; i < Size * Size; ++i)
  98. Synapse[i] = (((float)rand() / (float)RAND_MAX) - 0.5f) * SynapseDefaultPower;
  99. };
  100. AI(AI& In, const float Change) :
  101. Radius(In.Radius),
  102. Size(In.Size)
  103. {
  104. Synapse = new float[Size * Size];
  105. for (int i = 0; i < Size * Size; ++i)
  106. Synapse[i] = In.Synapse[i] + (((float)rand() / (float)RAND_MAX) - 0.5f) * Change;
  107. };
  108. AI(AI& In)
  109. {
  110. this->Size = In.Size;
  111. this->Radius = In.Radius;
  112. Synapse = new float[this->Size * this->Size];
  113.  
  114. for (int i = 0; i < this->Size * this->Size; ++i)
  115. this->Synapse[i] = In.Synapse[i];
  116. }
  117. ~AI()
  118. {
  119. delete[] Synapse;
  120. }
  121. AI& operator=(const AI& Other) // copy assignment
  122. {
  123. if (this != &Other) { // self-assignment check expected
  124. this->Radius = Other.Radius;
  125. this->Size = Other.Size;
  126. Synapse = new float[this->Size * this->Size];
  127. for (int i = 0; i < this->Size * this->Size; ++i)
  128. this->Synapse[i] = Other.Synapse[i];
  129. }
  130. return *this;
  131. }
  132.  
  133. Draft Apply(Draft& In)
  134. {
  135. Draft New(In);
  136.  
  137. for (int x = 0 + Radius + 1; x < In.Size - Radius - 1; ++x)
  138. for (int y = 0 + Radius + 1; y < In.Size - Radius - 1; ++y)
  139. {
  140. float f = Think(In, x, y);
  141. New.Data[x + y * In.Size] = f;
  142. }
  143. return New;
  144. }
  145.  
  146. private:
  147. inline float Think(const Draft& In, const int CenterX, const int CenterY)
  148. {
  149. float f = 0.f;
  150. int S = 0;
  151. for (int x = CenterX - Radius; x <= CenterX + Radius; ++x)
  152. for (int y = CenterY - Radius; y <= CenterY + Radius; ++y)
  153. {
  154. f = f + In.Data[x + y * In.Size] * Synapse[S];
  155. ++S;
  156. }
  157. return (f / (1.f + abs(f)));
  158. }
  159. };
  160.  
  161. int main()
  162. {
  163. //open SFML window
  164. sf::Window Window(sf::VideoMode(1000, 1000), "Test");
  165. Window.setFramerateLimit(60);
  166.  
  167. glGenTextures(1, &Textures);
  168.  
  169. Draft Input(8);
  170. Input.Random();
  171.  
  172. AI AI1(2);
  173. AI AI2(2);
  174. AI AI3(2);
  175.  
  176. AI AIR(2);
  177. AI AIG(2);
  178. AI AIB(2);
  179.  
  180. //Program loop
  181. while (Window.isOpen())
  182. {
  183. //event handling
  184. sf::Event Event;
  185. while (Window.pollEvent(Event))
  186. {
  187. if (Event.type == sf::Event::Closed)
  188. Window.close();
  189.  
  190. if (Event.type == sf::Event::Resized)
  191. glViewport(0, 0, Event.size.width, Event.size.height);
  192.  
  193. else if (Event.type == sf::Event::KeyPressed)
  194. {
  195. //if (Event.key.code == sf::Keyboard::Num1)
  196. {
  197. AI1 = AI(2);
  198. AI2 = AI(2);
  199. AI3 = AI(2);
  200.  
  201. AIR = AI(2);
  202. AIG = AI(2);
  203. AIB = AI(2);
  204.  
  205. }
  206. //else if (Event.key.code == sf::Keyboard::Num2)
  207. {
  208. // Input.Random();
  209. }
  210. }
  211. }
  212.  
  213.  
  214. AI1 = AI(AI1, 0.02f);
  215. AI2 = AI(AI2, 0.02f);
  216. AI3 = AI(AI3, 0.02f);
  217.  
  218. AIR = AI(AIR, 0.02f);
  219. AIG = AI(AIG, 0.02f);
  220. AIB = AI(AIB, 0.02f);
  221.  
  222. const float Ratio = Window.getSize().x / Window.getSize().y;
  223.  
  224. glLoadIdentity();
  225. glTranslatef(-1.f, 1.f, 0.f);
  226. glScalef(2.f, -2.f / Ratio, 1);
  227.  
  228. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  229. glEnable(GL_TEXTURE_2D);
  230.  
  231. //grey
  232. Draft L1In = Draft::Resize(Input, Input.Size * 3);
  233. Draft L1 = AI1.Apply(L1In);
  234.  
  235. Draft L2In = Draft::Resize(L1, L1.Size * 3);
  236. Draft L2 = AI2.Apply(L2In);
  237.  
  238. Draft L3In = Draft::Resize(L2, L2.Size * 1);
  239. Draft L3 = AI3.Apply(L3In);
  240.  
  241. //colors
  242. Draft RIn = Draft::Resize(L3, L3.Size * 1);
  243. Draft R = AIR.Apply(RIn);
  244.  
  245. Draft GIn = Draft::Resize(L3, L3.Size * 1);
  246. Draft G = AIG.Apply(GIn);
  247.  
  248. Draft BIn = Draft::Resize(L3, L3.Size * 1);
  249. Draft B = AIB.Apply(BIn);
  250.  
  251.  
  252. float* RGB = new float[L3.Size * L3.Size * 3];
  253.  
  254. int m = 0;
  255. for (int i = 0; i < L3.Size * L3.Size; ++i)
  256. {
  257. RGB[m+0] = R.Data[i];
  258. RGB[m+1] = G.Data[i];
  259. RGB[m+2] = B.Data[i];
  260.  
  261. m+=3;
  262. }
  263.  
  264. glBindTexture(GL_TEXTURE_2D, Textures);
  265. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, L3.Size, L3.Size, 0, GL_RGB, GL_FLOAT, RGB);
  266. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  267. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  268.  
  269. glBegin(GL_QUADS); //Render Quads
  270. glTexCoord2f(0, 0); glVertex2f(0.0, 0.0);
  271. glTexCoord2f(0, 1); glVertex2f(0.0, 1.0);
  272. glTexCoord2f(1, 1); glVertex2f(1.0, 1.0);
  273. glTexCoord2f(1, 0); glVertex2f(1.0, 0.0);
  274. glEnd(); //finish quads
  275.  
  276. delete[] RGB;
  277.  
  278. //L3.Draw();
  279.  
  280. glDisable(GL_TEXTURE_2D);
  281.  
  282.  
  283. Window.display(); //show SFML in window
  284. }
  285. return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement