Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. struct Texture {
  2. unsigned int textureId;
  3. Texture(int height = 0, int width = 0, vec3* image = NULL) {
  4. glGenTextures(1, &textureId);
  5. glBindTexture(GL_TEXTURE_2D, textureId); // binding
  6. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, image); //Texture -> GPU
  7. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  8. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  9. }
  10. };
  11.  
  12.  
  13. ...
  14.  
  15. class Test : public Drawable {
  16. public:
  17. std::vector<vec2> tex;
  18. Texture texture;
  19. public:
  20. Test() { Animate(0); }
  21.  
  22. void Create() {
  23. Drawable::Create();
  24.  
  25. pts.clear();
  26. tex.clear();
  27.  
  28. pts.push_back(vec2(0, 2));
  29. pts.push_back(vec2(2, 0));
  30. pts.push_back(vec2(0, 0));
  31.  
  32. tex.push_back(vec2(0, 1));
  33. tex.push_back(vec2(1, 0));
  34. tex.push_back(vec2(0, 0));
  35.  
  36. int w = 128;
  37. float s = 1.0f / w;
  38.  
  39. vec3* image = new vec3[w * w];
  40. for (int y = 0; y < w; y++) {
  41. for (int x = 0; x < w; x++) {
  42. if (x > 64) {
  43. image[x * w + y] = vec3(0, 1, 0);
  44. }
  45. else {
  46. image[x * w + y] = vec3(1, 0, 0);
  47. }
  48. }
  49. }
  50.  
  51. texture = Texture(w, w, image);
  52.  
  53.  
  54. glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  55. glBufferData(GL_ARRAY_BUFFER, sizeof(vec2) * pts.size(), &pts[0], GL_STATIC_DRAW);
  56. glEnableVertexAttribArray(0);
  57. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  58.  
  59. glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
  60. glBufferData(GL_ARRAY_BUFFER, sizeof(vec2) * tex.size(), &tex[0], GL_STATIC_DRAW);
  61. glEnableVertexAttribArray(1);
  62. glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  63. }
  64.  
  65. void Draw() {
  66. glUseProgram(shaderTProgram);
  67. glBindVertexArray(vao);
  68.  
  69. mat4 MVPTransform = camera.V() * camera.P();
  70.  
  71. int location = glGetUniformLocation(shaderTProgram, "MVP");
  72. if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform);
  73. else printf("uniform MVP cannot be set\n");
  74.  
  75. int sampler = 0;
  76. location = glGetUniformLocation(shaderTProgram, "samplerUnit");
  77. glUniform1i(location, sampler);
  78. glActiveTexture(GL_TEXTURE0 + sampler);
  79. glBindTexture(GL_TEXTURE_2D, texture.textureId);
  80.  
  81. glDrawArrays(GL_TRIANGLES, 0, pts.size());
  82.  
  83. glUseProgram(shaderProgram);
  84. }
  85.  
  86. void Animate(float t) {
  87. sx = 0.8;
  88. sy = 0.8;
  89. UpdateM();
  90. }
  91.  
  92. void UpdateM() {
  93. Mscale = mat4(sx, 0, 0, 0,
  94. 0, sy, 0, 0,
  95. 0, 0, 1, 0,
  96. 0, 0, 0, 1);
  97.  
  98. Mtranslate = mat4(1, 0, 0, 0,
  99. 0, 1, 0, 0,
  100. 0, 0, 1, 0,
  101. wTx, wTy, 0, 1);
  102.  
  103. MrotateY = mat4(cosf(fiY), 0, -sinf(fiY), 0,
  104. 0, 1, 0, 0,
  105. sinf(fiY), 0, cosf(fiY), 0,
  106. 0, 0, 0, 1);
  107.  
  108. MrotateZ = mat4(cosf(fiZ), sinf(fiZ), 0, 0,
  109. -sinf(fiZ), cosf(fiZ), 0, 0,
  110. 0, 0, 1, 0,
  111. 0, 0, 0, 1);
  112. }
  113. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement