textureGen.cpp
上传用户:liujun12jf
上传日期:2022-07-12
资源大小:638k
文件大小:3k
源码类别:

OpenGL

开发平台:

Visual C++

  1. #include "textureGen.h"
  2. void FillArray(float *data, int size, float c)
  3. {
  4. for (int i=0 ; i < size ; i++)
  5. {
  6. data[i] = c;
  7. }
  8. }
  9. void GenTexture(GLuint *id, float *data, int size_x, int size_y, int type)
  10. {
  11. if (*id == 0) glGenTextures(1, id); // texture still not created
  12. glBindTexture(GL_TEXTURE_2D, *id);
  13. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  14. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  15.   glTexImage2D(GL_TEXTURE_2D, 0, type, size_x, size_y, 0, type, GL_FLOAT, data);
  16. }
  17. void CreateCircle(float *data, int size_x, int x_offs, int y_offs, float radius, float contrast)
  18. {
  19. float aspect = 3.14159f / radius;
  20. x_offs -= radius/2-2;
  21. y_offs -= radius/2;
  22. for (int y=0 ; y < radius ; y++){
  23. for (int x=0 ; x < radius ; x++)
  24. {
  25. float c = sin(x*aspect) + sin(y*aspect);
  26. c /= 2;
  27. c = pow(c, contrast);
  28. float cc = data[size_x * (y+y_offs) + x+x_offs];
  29. data[size_x * (y+y_offs) + x+x_offs] = c+cc;
  30. }}
  31. }
  32. GLuint CreateLineObjectTexture(unsigned int size) // returns opengl texture id
  33. {
  34. float contrast = 3;
  35. float *data = new float[size*size];
  36. int x_offs = size/2-3;
  37. int y_offs = 15;
  38. int line_x = 5;
  39. int line_y = size-y_offs*2;
  40. int circles_radius = y_offs;
  41. float qx = 3.14159f;
  42. float qy = 3.14159f;
  43. qx /= line_x;
  44. qy /= line_y;
  45. FillArray(data, size*size*1, 0);
  46. {
  47. for (int y=0 ; y < line_y ; y++){
  48. for (int x=0 ; x < line_x ; x++)
  49. {
  50. float c = sin(x*qx);
  51. c = pow(c, contrast);
  52. data[size * (y+y_offs) + x+x_offs] = c;
  53. }}
  54. }
  55. CreateCircle(data, size, x_offs, y_offs, circles_radius, contrast);
  56. CreateCircle(data, size, x_offs, y_offs+line_y, circles_radius, contrast);
  57. static GLuint id = 0;
  58. GenTexture(&id, data, size, size, GL_LUMINANCE);
  59. delete[] data;
  60. return id;
  61. }
  62. GLuint CreateStarTexture(unsigned int size) // returns opengl texture id
  63. {
  64. float contrast = 5;
  65. float q = 3.14159f;
  66. q /= size;
  67. float *data = new float[size*size];
  68. for (int y=0 ; y < size ; y++)
  69. {
  70. for (int x=0 ; x < size ; x++)
  71. {
  72. float c = ( sin(x*q) + sin(y*q) ) / 2;
  73. c = pow(c, contrast);
  74. data[size * y + x] = c;
  75. }
  76. }
  77. static GLuint id = 0;
  78. GenTexture(&id, data, size, size, GL_LUMINANCE);
  79. delete[] data;
  80. return id;
  81. }
  82. GLuint CreateOneColourTexture(unsigned int size) // returns opengl texture id
  83. {
  84. float *data = new float[size*size];
  85. for (int y=0 ; y < size ; y++)
  86. {
  87. for (int x=0 ; x < size ; x++)
  88. {
  89. data[size * y + x] = rnd(0.7f, 1);
  90. }
  91. }
  92. static GLuint id = 0;
  93. GenTexture(&id, data, size, size, GL_LUMINANCE);
  94. delete[] data;
  95. return id;
  96. }
  97. GLuint CreateChaosTexture(unsigned int size) // returns opengl texture id
  98. {
  99. float contrast = 3;
  100. float q = 3.14159f;
  101. q /= size;
  102. float *data = new float[size*size];
  103. for (int y=0 ; y < size ; y++)
  104. {
  105. for (int x=0 ; x < size ; x++)
  106. {
  107. float c = rnd(0, 1);
  108. c = pow(c, contrast);
  109. data[size * y + x] = c;
  110. }
  111. }
  112. static GLuint id = 0;
  113. GenTexture(&id, data, size, size, GL_LUMINANCE);
  114. delete[] data;
  115. return id;
  116. }