-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
186 lines (149 loc) · 4.91 KB
/
main.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// -*- c-file-style: "stroustrup" -*-
#include <GL/glew.h>
#include <GL/glfw.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <unistd.h>
template <GLuint>
void PrintError(GLuint) { }
template <>
void PrintError<GL_COMPILE_STATUS>(GLuint shaderId)
{
GLint result = GL_FALSE;
int infoLength;
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &result);
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &infoLength);
std::string errorMessage(std::max(1, infoLength), char());
glGetShaderInfoLog(shaderId, infoLength, NULL, &errorMessage[0]);
std::cerr << "Compile: " << errorMessage << std::endl;
}
template <>
void PrintError<GL_LINK_STATUS>(GLuint programId)
{
GLint result = GL_FALSE;
int infoLength;
glGetProgramiv(programId, GL_LINK_STATUS, &result);
glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLength);
std::string errorMessage(std::max(1, infoLength), char());
glGetProgramInfoLog(programId, infoLength, NULL, &errorMessage[0]);
std::cerr << "Link: " << errorMessage << std::endl;
}
GLuint CompileShader(GLuint shaderType, const char* shaderCode)
{
GLuint shaderId = glCreateShader(shaderType);
glShaderSource(shaderId, 1, &shaderCode, NULL);
glCompileShader(shaderId);
PrintError<GL_COMPILE_STATUS>(shaderId);
return shaderId;
}
GLuint LinkProgram(const char* vertexShader, const char* fragmentShader)
{
GLuint vertexShaderId = CompileShader(GL_VERTEX_SHADER, vertexShader);
GLuint fragmentShaderId = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
GLuint programId = glCreateProgram();
glAttachShader(programId, vertexShaderId);
glAttachShader(programId, fragmentShaderId);
glLinkProgram(programId);
PrintError<GL_LINK_STATUS>(programId);
glDeleteShader(vertexShaderId);
glDeleteShader(fragmentShaderId);
return programId;
}
int main()
{
bool running = true;
if (!glfwInit())
{
std::cerr << "Failed to init GLFW " << std::endl;
exit(-1);
}
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if (!glfwOpenWindow(320, 240, 0, 0, 0, 0, 32, 0, GLFW_WINDOW))
{
glfwTerminate();
exit(-1);
}
glewExperimental = GL_TRUE;
GLenum glewErr = glewInit();
if (glewErr != GLEW_OK)
{
std::cerr << "Failed to init GLEW " << glewGetErrorString(glewErr) << std::endl;
glfwTerminate();
exit(-1);
}
glfwSetWindowTitle("Hello World!");
GLuint vertexArrayId;
glGenVertexArrays(1, &vertexArrayId);
glBindVertexArray(vertexArrayId);
static const GLfloat vertexBufferData[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBufferData), vertexBufferData, GL_STATIC_DRAW);
static const GLfloat colorData[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
};
GLuint colorBuffer;
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(colorData), colorData, GL_STATIC_DRAW);
const char shaderSource[] = ""
"#version 330 core\n"
"layout(location = 0) in vec3 in_Position;\n"
"layout(location = 1) in vec3 in_Color;\n"
"uniform mat4 MVP;\n"
"out vec4 ex_Color;\n"
"void main() {\n"
" gl_Position = MVP * vec4(in_Position, 1.0);\n"
" ex_Color = vec4(in_Color, 1.0);\n"
"}";
const char fragmentSource[] = ""
"#version 330 core\n"
"in vec4 ex_Color;\n"
"out vec4 out_Color;\n"
"void main() {\n"
" out_Color = ex_Color;\n"
"}";
GLuint programId = LinkProgram(shaderSource, fragmentSource);
GLuint matrixId = glGetUniformLocation(programId, "MVP");
glm::mat4 projection = glm::perspective(45.0f, 4.0f/3.0f, 0.1f, 100.0f);
glm::mat4 view = glm::lookAt(glm::vec3(4.0f, 3.0f, 3.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f));
float angle = 0.0;
while (running)
{
glm::mat4 model = glm::rotate(glm::mat4(1.0f),
angle,
glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 MVP = projection * view * model;
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(programId);
glUniformMatrix4fv(matrixId, 1, GL_FALSE, &MVP[0][0]);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glfwSwapBuffers();
angle += 2.0;
running = !glfwGetKey(GLFW_KEY_ESC)
&& glfwGetWindowParam(GLFW_OPENED);
}
glfwTerminate();
return 0;
}