Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <iostream>
- using namespace std;
- static unsigned int CompileShader(unsigned int type, const string& source){
- unsigned int id = glCreateShader(type);
- const char* src = source.c_str();
- glShaderSource(id, 1, &src, nullptr);
- glCompileShader(id);
- int result;
- glGetShaderiv(id, GL_COMPILE_STATUS, &result);
- if (result == GL_FALSE) {
- int length;
- glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
- char* message = (char*) alloca(length * sizeof(char));
- glGetShaderInfoLog(id, length, &length, message);
- cout << "Failed to compile" << (type == GL_VERTEX_SHADER ? " vertex" : " fragment") << " shader!" << endl;
- cout << message << endl;
- glDeleteShader(id);
- return 0;
- }
- return id;
- }
- static unsigned int CreateShader(const string& vertexShader, const string& fragmentShader) {
- unsigned int program = glCreateProgram();
- unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
- unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
- glAttachShader(program, vs);
- glAttachShader(program, fs);
- glLinkProgram(program);
- glValidateProgram(program);
- glDeleteShader(vs);
- glDeleteShader(fs);
- return program;
- }
- int main() {
- GLFWwindow* window;
- if (!glfwInit()) return -1;
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- window = glfwCreateWindow(640, 480, "Srinjoy", NULL, NULL);
- if (!window) {
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- if (glewInit() != GLEW_OK) {
- cout << "Error initialising glew" << endl;
- return -1;
- }
- cout << glGetString(GL_VERSION) << endl;
- float position[6] = {
- -0.5F, -0.5F,
- 0.0F, 0.5F,
- 0.5F, -0.5F
- };
- unsigned int buffer;
- glGenBuffers(1, &buffer);
- glBindBuffer(GL_ARRAY_BUFFER, buffer);
- glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), position, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
- string vertexShader =
- "#version 330 core\n"
- "layout(location = 0) in vec4 position;\n"
- "void main() {\n"
- " gl_Position = position;\n"
- "}";
- string fragmentShader =
- "#version 330 core\n"
- "layout(location = 0) out vec4 color;\n"
- "void main() {\n"
- " color = vec4(1.0, 1.0, 0.0, 1.0);\n"
- "}";
- unsigned int shader = CreateShader(vertexShader, fragmentShader);
- glUseProgram(shader);
- while (!glfwWindowShouldClose(window)){
- glClear(GL_COLOR_BUFFER_BIT);
- glDrawArrays(GL_TRIANGLES, 0, 3);
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement