1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <glm\glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using namespace glm;
GLfloat vao_data [] =
{
0.0f,1.0f,0.0f,
-1.0f,-1.0f,0.0f,
1.0f,-1.0f,0.0f
};
const char* loadShader(string path)
{
string out = "";
ifstream in(path, ios::in);
string line = "";
if (in.is_open())
{
while (getline(in, line))
{
out += line + "\n";
}
in.close();
}
return out.c_str();
}
int main()
{
if (!glfwInit())
{
cerr << "Cannot initialize GLFW." << endl;
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 8);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL tutorial", nullptr, nullptr);
if (window == nullptr)
{
cerr << "Failed to open window/create context. Please check if your GPU supports OpenGL 3.3." << endl;
glfwTerminate();
return -2;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
cerr << "Cannot initialize GLEW." << endl;
return -3;
}
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vao_data), vao_data, GL_STATIC_DRAW);
const char* vsSource = loadShader("vertex_shader.glsl");
const char* fsSource = loadShader("frag_shader.glsl");
GLuint vs, fs, program;
vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vsSource, nullptr);
glCompileShader(vs);
char vsLog[512];
glGetShaderInfoLog(vs, sizeof(vsLog), nullptr, vsLog);
cout << "Vertex shader compile log:" << endl;
cout << vsLog << endl;
fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fsSource, nullptr);
glCompileShader(fs);
char fsLog[512];
glGetShaderInfoLog(fs, sizeof(fsLog), nullptr, fsLog);
cout << "Fragment shader compile log:" << endl;
cout << fsLog << endl;
program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
char programLog[512];
glGetProgramInfoLog(program, sizeof(programLog), nullptr, programLog);
cout << "Program link log:" << endl;
cout << programLog << endl;
// game loop
do
{
// draw
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
// swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} while (!glfwWindowShouldClose(window));
return 0;
}
|