Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- #define GLEW_STATIC
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <iostream>
- #include <vector>
- #include <string.h>
- #include <complex>
- #include <fstream>
- const char *vertex = R"(
- #version 330 core
- layout (location = 0) in vec3 position;
- layout (location = 1) in vec3 color;
- layout (location = 2) in vec2 texCoord;
- out vec3 ourColor;
- out vec2 TexCoord;
- void main() {
- gl_Position = vec4(position, 1.0f);
- ourColor = color;
- TexCoord = texCoord;
- }
- )";
- const char *fragment = R"(
- #version 330 core
- uniform vec2 screenSize;
- uniform sampler2D image;
- in vec3 ourColor;
- in vec2 TexCoord;
- out vec4 color;
- uniform sampler2D ourTexture;
- void main() {
- color = texture(ourTexture, TexCoord);
- }
- )";
- GLuint compileShader(GLenum shaderType, const char* source) {
- GLuint shader = glCreateShader(shaderType);
- if (shader) {
- glShaderSource(shader, 1, &source, NULL);
- glCompileShader(shader);
- GLint status = 0;
- glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
- if (!status) {
- GLint log_len;
- glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
- if (log_len > 0) {
- vector<char> message(log_len + 1);
- glGetShaderInfoLog(shader, log_len, NULL, &message[0]);
- printf("%s\n", &message[0]);
- }
- glDeleteShader(shader);
- shader = 0;
- }
- }
- return shader;
- }
- GLuint createShader(const char* vertexSource, const char* fragmentSource) {
- GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexSource);
- if (!vertexShader) return 0;
- GLuint pixelShader = compileShader(GL_FRAGMENT_SHADER, fragmentSource);
- if (!pixelShader) return 0;
- GLuint program = glCreateProgram();
- if (program) {
- glAttachShader(program, vertexShader);
- glAttachShader(program, pixelShader);
- glLinkProgram(program);
- GLint status = GL_FALSE;
- glGetProgramiv(program, GL_LINK_STATUS, &status);
- if (status != GL_TRUE) {
- GLint log_len;
- glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len);
- if (log_len > 0) {
- vector<char> message(log_len + 1);
- glGetProgramInfoLog(program, log_len, NULL, &message[0]);
- printf("%s\n", &message[0]);
- }
- glDeleteProgram(program);
- program = 0;
- }
- }
- return program;
- }
- struct Color {
- unsigned char r, g, b;
- };
- struct TextureData {
- GLuint bufferId;
- int width, height;
- Color *pixels;
- TextureData(int width, int height) : width(width), height(height) {
- pixels = new Color[width * height];
- glGenTextures(1, &bufferId);
- glBindTexture(GL_TEXTURE_2D, bufferId);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glBindTexture(GL_TEXTURE_2D, 0);
- apply();
- }
- void apply() {
- glBindTexture(GL_TEXTURE_2D, bufferId);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- ~TextureData() {
- delete[] pixels;
- }
- Color getPixel(int x, int y) {
- return pixels[y * width + x];
- }
- void setPixel(int x, int y, Color color) {
- pixels[y * width + x] = color;
- }
- };
- int main() {
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- GLFWwindow* window = glfwCreateWindow(800, 600, "Computer graphics", nullptr, nullptr);
- if (window == nullptr) {
- cout << "Failed to create GLFW window" << endl;
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- glewExperimental = GL_TRUE;
- if (glewInit() != GLEW_OK) {
- cout << "Failed to initialize GLEW" << endl;
- return -1;
- }
- int width, height;
- glfwGetFramebufferSize(window, &width, &height);
- glViewport(0, 0, width, height);
- auto shader = createShader(vertex, fragment);
- GLfloat vertices[] = {
- // Positions // Colors // Texture Coords
- 1, 1, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top Right
- 1, -1, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Bottom Right
- -1, -1, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Left
- -1, 1, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // Top Left
- };
- GLuint indices[] = { // Note that we start from 0!
- 0, 1, 3, // First Triangle
- 1, 2, 3 // Second Triangle
- };
- GLuint VBO, VAO, EBO;
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
- glBindVertexArray(VAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
- // Position attribute
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
- glEnableVertexAttribArray(0);
- // Color attribute
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
- glEnableVertexAttribArray(1);
- // TexCoord attribute
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
- glEnableVertexAttribArray(2);
- glBindVertexArray(0); // Unbind VAO
- enum { pallete_size = 64 };
- unsigned char pallete[pallete_size][3];
- for (int i = 0; i < pallete_size; i++) {
- pallete[i][0] = i < (2 * pallete_size / 3) ? (i * 255 * 3 / (2 * pallete_size)) : 255;
- pallete[i][1] = i < pallete_size / 3 ? 0 : (i - pallete_size / 3) * 255 * 3 / (2 * pallete_size);
- pallete[i][2] = 0;
- }
- TextureData texture(width, height);
- for (int y = 0; y < height; ++y) {
- for (int x = 0; x < width; ++x) {
- complex<double> c(1.0 * (x - (width - height) / 2.0) / height * 4.0 - 2.0, 1.0 * y / height * 4.0 - 2.0);
- complex<double> z(0.0, 0.0);
- int n = 0;
- while (n < pallete_size) {
- z = z * z + c;
- if (abs(z) > 2.0) break;
- ++n;
- }
- unsigned char r = pallete[n][0];
- unsigned char g = 0;//pallete[n][1];
- unsigned char b = 0;//pallete[n][2];
- texture.setPixel(x, y, Color{r, g, b});
- }
- }
- texture.apply();
- glUseProgram(shader);
- glUniform2f(glGetUniformLocation(shader, "screenSize"), width, height);
- while(!glfwWindowShouldClose(window)) {
- glfwPollEvents();
- glClearColor(0, 0, 1, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, texture.bufferId);
- glUniform1i(glGetUniformLocation(shader, "ourTexture"), 0);
- // Draw container
- glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- glfwSwapBuffers(window);
- }
- glUseProgram(0);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement