-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmultipleVBO.cpp
More file actions
182 lines (145 loc) · 4.61 KB
/
multipleVBO.cpp
File metadata and controls
182 lines (145 loc) · 4.61 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <GLES2/gl2.h>
#include <GLFW/glfw3.h>
#include <math.h>
#define GL_SILENCE_DEPRECATION 1
// vertex shader source
const GLchar* vertex120 = R"END(
#version 120
attribute vec4 inPosition;
attribute vec4 inColor;
uniform mat4 matrix;
varying vec4 outColor;
void main()
{
outColor = inColor;
gl_Position = inPosition; //* matrix;
}
)END";
// fragment shader source
const GLchar* raster120 = R"END(
#version 120
varying vec4 outColor;
void main()
{
gl_FragColor = outColor;
}
)END";
int main()
{
// -------------- init
GLFWwindow * window;
if (!glfwInit()) {
std::cout << "Init error";
return -1;
}
window = glfwCreateWindow(600,600,"Hello",0,0);
if (!window) {
std::cout << "Window creation error";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
std::cout << "Init :: checking OpenGL version:\n";
const unsigned char * msg;
msg = glGetString(GL_VERSION);
std::cout << msg << "\n Shader language version: \n";
msg = glGetString(GL_SHADING_LANGUAGE_VERSION);
std::cout << msg << "\n";
const char* source;
GLint compilationStatus;
// ------------- VERTEX SHADER
source = vertex120;
GLuint shaderVertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shaderVertex,1,&source,0);
glCompileShader(shaderVertex);
glGetShaderiv(shaderVertex, GL_COMPILE_STATUS, &compilationStatus);
if (compilationStatus == GL_FALSE) {
GLchar messages[256];
glGetShaderInfoLog(shaderVertex, sizeof(messages), 0, &messages[0]); std::cout << messages;
exit(1);
}
// ---------- FRAGMENT SHADER
source = raster120;
GLuint shaderFragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shaderFragment,1,&source,0);
glCompileShader(shaderFragment);
glGetShaderiv(shaderFragment, GL_COMPILE_STATUS, &compilationStatus);
if (compilationStatus == GL_FALSE) {
GLchar messages[256];
glGetShaderInfoLog(shaderFragment, sizeof(messages), 0, &messages[0]); std::cout << messages;
exit(1);
}
// ------------- SHADER PROGRAM
GLint linkStatus;
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram,shaderVertex);
glAttachShader(shaderProgram,shaderFragment);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram,GL_LINK_STATUS,&linkStatus);
if (linkStatus == GL_FALSE) {
GLchar messages[256];
glGetProgramInfoLog(shaderProgram,sizeof(messages),0,&messages[0]);
std::cout << messages;
exit(1);
}
glUseProgram(shaderProgram);
// ---------------- VBOs
GLfloat positions[] = {
-1, -1, 0,
-1, 1, 0,
1, -1, 0,
1, -1, 0,
-1, 1, 0,
1, 1, 0
};
GLfloat colors[] = {
1, 0, 0,
0, 1, 0,
0, 0, 1,
1, 1, 0,
1, 0, 1,
0, 1, 1
};
GLfloat vertices[] = {
// First triangle
-0.9f, -0.5f, 0.0f, // Left
-0.0f, -0.5f, 0.0f, // Right
-0.45f, 0.5f, 0.0f, // Top
// Second triangle
0.0f, -0.5f, 0.0f, // Left
0.9f, -0.5f, 0.0f, // Right
0.45f, 0.5f, 0.0f // Top
};
GLuint positionsData;
glGenBuffers(1, &positionsData);
glBindBuffer(GL_ARRAY_BUFFER,positionsData);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
GLuint colorsData;
glGenBuffers(1, &colorsData);
glBindBuffer(GL_ARRAY_BUFFER, colorsData);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW); // memcpy() -> to GPU's vram buffer
// ----------------- attributes
GLuint attribPosition;
GLuint attribColor;
attribPosition = glGetAttribLocation(shaderProgram, "inPosition");
glEnableVertexAttribArray(attribPosition);
glBindBuffer(GL_ARRAY_BUFFER, positionsData);
glVertexAttribPointer(attribPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);
attribColor = glGetAttribLocation(shaderProgram, "inColor");
glEnableVertexAttribArray(attribColor);
glBindBuffer(GL_ARRAY_BUFFER, colorsData);
glVertexAttribPointer(attribColor, 3, GL_FLOAT, GL_FALSE, 0, 0);
// ----------------- render loop
while (!glfwWindowShouldClose(window))
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
}