Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************/
- /********** main.cpp **********/
- /******************************/
- #define GLEW_STATIC
- #include <glew.h>
- #include <glfw3.h>
- #include <glm\gtc\matrix_transform.hpp>
- #include <glm\gtc\type_ptr.hpp>
- #include <SOIL.h>
- #include "utils.h" // functions for loading shaders and the error() function
- #include <iostream>
- using namespace std;
- float vertices [] =
- {
- -0.5f, -0.5f,
- 0.5f, -0.5f,
- 0.5f, 0.5f,
- 0.5f, 0.5f,
- -0.5f, 0.5f,
- -0.5f, -0.5f
- };
- int main()
- {
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- GLFWwindow* window = glfwCreateWindow(1280, 1024, "error_test", nullptr, nullptr);
- if (window == nullptr)
- {
- cout << "Failed to create window" << endl;
- glfwTerminate();
- system("pause");
- return -1;
- }
- glfwMakeContextCurrent(window);
- glewExperimental = GL_TRUE;
- glewInit();
- // OpenGL initialization goes here
- GLuint vao;
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- GLuint vbo;
- glGenBuffers(1, &vbo);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- GLuint program = setupProgram("vertex.glsl", "fragment.glsl");
- glUseProgram(program);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), nullptr);
- error(glGetError()); // this thing
- while (!glfwWindowShouldClose(window))
- {
- if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
- {
- glfwSetWindowShouldClose(window, GL_TRUE);
- }
- glfwPollEvents();
- glDrawArrays(GL_TRIANGLES, 0, 6);
- glfwSwapBuffers(window);
- }
- glfwTerminate();
- return 0;
- }
- /***************************/
- /**********utils.h**********/
- /***************************/
- #pragma once
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <glew.h>
- #include <SOIL.h>
- using namespace std;
- string loadShader(string path)
- {
- ifstream in(path);
- string text = "";
- if (in.is_open())
- {
- string line;
- while (getline(in, line))
- {
- line += "\n";
- text += line;
- }
- return text;
- }
- else
- {
- cout << "I can't open it m8" << endl;
- return "err";
- }
- }
- GLuint loadTexture(const char* path)
- {
- int w, h;
- unsigned char* texData = SOIL_load_image(path, &w, &h, nullptr, SOIL_LOAD_RGB);
- if (texData == nullptr)
- {
- cout << "Error: Texture is nullptr" << endl;
- cout << SOIL_last_result() << endl;
- return 0;
- }
- GLuint tex;
- glGenTextures(1, &tex);
- glBindTexture(GL_TEXTURE_2D, tex);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, texData);
- SOIL_free_image_data(texData);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glGenerateMipmap(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, 0);
- return tex;
- }
- GLuint setupProgram(string vsFileName, string fsFileName)
- {
- GLuint vs = glCreateShader(GL_VERTEX_SHADER);
- string vsStr = loadShader(vsFileName);
- const char* vsSource = vsStr.c_str();
- glShaderSource(vs, 1, &vsSource, nullptr);
- glCompileShader(vs);
- char vsLog[512];
- glGetShaderInfoLog(vs, 512, nullptr, vsLog);
- cout << "Vertex shader output: " << endl;
- cout << vsLog << endl;
- GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
- string fsStr = loadShader(fsFileName);
- const char* fsSource = fsStr.c_str();
- glShaderSource(fs, 1, &fsSource, nullptr);
- glCompileShader(fs);
- char fsLog[512];
- glGetShaderInfoLog(fs, 512, nullptr, fsLog);
- cout << "Fragment shader output: " << endl;
- cout << fsLog << endl;
- GLuint prog = glCreateProgram();
- glAttachShader(prog, vs);
- glAttachShader(prog, fs);
- glLinkProgram(prog);
- return prog;
- }
- void error(GLenum e)
- {
- switch (e)
- {
- case GL_INVALID_ENUM:
- cout << "Error: GL_INVALID_ENUM" << endl;
- system("pause");
- break;
- case GL_INVALID_VALUE:
- cout << "Error: GL_INVALID_VALUE" << endl;
- system("pause");
- break;
- case GL_INVALID_OPERATION:
- cout << "Error: GL_INVALID_OPERATION" << endl;
- system("pause");
- break;
- case GL_INVALID_FRAMEBUFFER_OPERATION:
- cout << "Error: GL_INVALID_FRAMEBUFFER_OPERATION" << endl;
- system("pause");
- break;
- case GL_OUT_OF_MEMORY:
- cout << "Error: GL_OUT_OF_MEMORY" << endl;
- system("pause");
- break;
- case GL_NO_ERROR:
- cout << "No error reported" << endl;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement