Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Texture {
- unsigned int textureId;
- Texture(int height = 0, int width = 0, vec3* image = NULL) {
- glGenTextures(1, &textureId);
- glBindTexture(GL_TEXTURE_2D, textureId); // binding
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, image); //Texture -> GPU
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- }
- };
- ...
- class Test : public Drawable {
- public:
- std::vector<vec2> tex;
- Texture texture;
- public:
- Test() { Animate(0); }
- void Create() {
- Drawable::Create();
- pts.clear();
- tex.clear();
- pts.push_back(vec2(0, 2));
- pts.push_back(vec2(2, 0));
- pts.push_back(vec2(0, 0));
- tex.push_back(vec2(0, 1));
- tex.push_back(vec2(1, 0));
- tex.push_back(vec2(0, 0));
- int w = 128;
- float s = 1.0f / w;
- vec3* image = new vec3[w * w];
- for (int y = 0; y < w; y++) {
- for (int x = 0; x < w; x++) {
- if (x > 64) {
- image[x * w + y] = vec3(0, 1, 0);
- }
- else {
- image[x * w + y] = vec3(1, 0, 0);
- }
- }
- }
- texture = Texture(w, w, image);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vec2) * pts.size(), &pts[0], GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vec2) * tex.size(), &tex[0], GL_STATIC_DRAW);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
- }
- void Draw() {
- glUseProgram(shaderTProgram);
- glBindVertexArray(vao);
- mat4 MVPTransform = camera.V() * camera.P();
- int location = glGetUniformLocation(shaderTProgram, "MVP");
- if (location >= 0) glUniformMatrix4fv(location, 1, GL_TRUE, MVPTransform);
- else printf("uniform MVP cannot be set\n");
- int sampler = 0;
- location = glGetUniformLocation(shaderTProgram, "samplerUnit");
- glUniform1i(location, sampler);
- glActiveTexture(GL_TEXTURE0 + sampler);
- glBindTexture(GL_TEXTURE_2D, texture.textureId);
- glDrawArrays(GL_TRIANGLES, 0, pts.size());
- glUseProgram(shaderProgram);
- }
- void Animate(float t) {
- sx = 0.8;
- sy = 0.8;
- UpdateM();
- }
- void UpdateM() {
- Mscale = mat4(sx, 0, 0, 0,
- 0, sy, 0, 0,
- 0, 0, 1, 0,
- 0, 0, 0, 1);
- Mtranslate = mat4(1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- wTx, wTy, 0, 1);
- MrotateY = mat4(cosf(fiY), 0, -sinf(fiY), 0,
- 0, 1, 0, 0,
- sinf(fiY), 0, cosf(fiY), 0,
- 0, 0, 0, 1);
- MrotateZ = mat4(cosf(fiZ), sinf(fiZ), 0, 0,
- -sinf(fiZ), cosf(fiZ), 0, 0,
- 0, 0, 1, 0,
- 0, 0, 0, 1);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement