Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <SOIL/SOIL.h>
- #define GLEW STATIC
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include "functions.h"
- // Constansts and variables
- const GLuint WIDTH = 800, HEIGHT = 600;
- const GLchar *windowName = "OpenGLAAAAA";
- GLFWwindow *window;
- GLuint shaderProgram;
- int main() {
- // Loading shaders
- // Vertex shader
- char *content001;
- textFileRead("shaders/vertexShader.vert", &content001);
- const GLchar *vertexShaderSourceExt = content001;
- // Fragment shader
- char *content002;
- textFileRead("shaders/fragmentShader.frag", &content002);
- const GLchar *fragmentShaderSourceExt = content002;
- // Textures
- GLuint texture;
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- // Set texture parameters
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- // Set texture filtering
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- // Loading texture
- int width, height;
- unsigned char *image = 0;
- image = SOIL_load_image("res/textures/container.jpg", &width, &height, 0, SOIL_LOAD_RGB);
- if (image == 0) {
- printf("Can not open file res/textures/container.jpg\n");
- return -1;
- }
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
- //glGenerateMipmap(GL_TEXTURE_2D);
- SOIL_free_image_data(image);
- glBindTexture(GL_TEXTURE_2D, texture);
- // Initialization
- if (aaaInit(&window, WIDTH, HEIGHT, &windowName) != 0){
- return -1;
- }
- aaaShaderCompile(&vertexShaderSourceExt, &fragmentShaderSourceExt, &shaderProgram);
- // Set function for key callback
- glfwSetKeyCallback(window, key_callback);
- // Set up vertex data (and buffer(s)) and attribute pointers
- GLfloat vertices[] = {
- // Positions Colors Texture Coords
- 0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top
- -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Bottom Left
- 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f // Bottom Right
- };
- GLuint indices[] = { // Note that we start from 0!
- 0, 1, 2, // First Triangle
- };
- GLuint VBO, VAO, EBO;
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
- // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
- 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);
- // Pisistion 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);
- // Texture cut coords attribute
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
- glEnableVertexAttribArray(2);
- glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
- glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO
- // Uncommenting this call will result in wireframe polygons.
- //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- // window main cycle
- while(!glfwWindowShouldClose(window))
- {
- glfwPollEvents();
- // Render commands here
- glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- GLfloat timeValue = glfwGetTime();
- GLfloat redValue = (sin(timeValue*8) / 2) + 0.5;
- GLfloat greenValue = (cos(timeValue*8) / 2) + 0.5;
- GLint vertexPosLocation = glGetUniformLocation(shaderProgram, "ourPos2");
- glUseProgram(shaderProgram);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, texture);
- glUniform1i(glGetUniformLocation(shaderProgram, "ourTexture"), 0);
- glUniform4f(vertexPosLocation, redValue, greenValue, 1.0f, 1.0f);
- glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- glfwSwapBuffers(window); // эта хуйня нужна чтобы всё не зависло к ебеням!!
- }
- glDeleteVertexArrays(1, &VAO);
- glDeleteBuffers(1, &VBO);
- glDeleteBuffers(1, &EBO);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement