include laba8 static const char pVS version 330 layout location in vec

  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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "laba8.h"
static const char* pVS = " \n\
#version 330 \n\
\n\
layout (location = 0) in vec3 Position; \n\
layout (location = 1) in vec2 TexCoord; \n\
\n\
uniform mat4 gWVP; \n\
\n\
out vec2 TexCoord0; \n\
\n\
void main() \n\
{ \n\
gl_Position = gWVP * vec4(Position, 1.0); \n\
TexCoord0 = TexCoord; \n\
}";
static const char* pFS = " \n\
#version 330 \n\
\n\
in vec2 TexCoord0; \n\
\n\
out vec4 FragColor; \n\
\n\
struct DirectionalLight \n\
{ \n\
vec3 Color; \n\
float AmbientIntensity; \n\
}; \n\
\n\
uniform DirectionalLight gDirectionalLight; \n\
uniform sampler2D gSampler; \n\
\n\
void main() \n\
{ \n\
FragColor = texture2D(gSampler, TexCoord0.xy) * \n\
vec4(gDirectionalLight.Color, 1.0f) * \n\
gDirectionalLight.AmbientIntensity; \n\
}";
void init_shaders();
Laba8::Laba8(QObject *parent) :
LabaAbstract(parent)
{
glEnable(GL_DEPTH_TEST);
/*glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_SMOOTH);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
init_shaders();*/
/*m_light->Init();
m_light->Enable();
m_light->SetTextureUnit(0);*/
init_shaders();
}
// Shader sources
const GLchar* vertexSource =
"#version 150 core\n"
"in vec2 position;"
"in vec3 color;"
"out vec3 Color;"
"void main() {"
" Color = color;"
" gl_Position = vec4(position, 0.0, 1.0);"
"}";
const GLchar* fragmentSource =
"#version 150 core\n"
"in vec3 Color;"
"out vec4 outColor;"
"void main() {"
" outColor = vec4(Color, 1.0);"
"}";
GLuint vao;
GLuint vbo;
GLuint ebo;
GLuint vertexShader;
GLuint fragmentShader;
GLuint shaderProgram;
void init_shaders(void)
{
// Create Vertex Array Object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Create a Vertex Buffer Object and copy the vertex data to it
glGenBuffers(1, &vbo);
GLfloat vertices[] = {
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // Top-left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Bottom-right
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f // Bottom-left
};
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Create an element array
glGenBuffers(1, &ebo);
GLuint elements[] = {
0, 1, 2,
3, 2, 0
};
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
// Create and compile the vertex shader
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSource, NULL);
glCompileShader(vertexShader);
// Create and compile the fragment shader
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
glCompileShader(fragmentShader);
// Link the vertex and fragment shader into a shader program
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
// Specify the layout of the vertex data
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0);
GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
}
//2гумантираный
void draw_shaders(void)
{
// Clear the screen to black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw a rectangle from the 2 triangles using 6 indices
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
void clear_shaders(void)
{
glDeleteProgram(shaderProgram);
glDeleteShader(fragmentShader);
glDeleteShader(vertexShader);
glDeleteBuffers(1, &ebo);
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
}
void Laba8::render()
{
super::render();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float position[] = {m_lightPosition.x(), m_lightPosition.y(), m_lightPosition.z(), m_lightSpoted}; // устанавливаем направление света
qDebug()<<m_lightPosition;
glOrtho( -50.0, (GLdouble)50, -50.0, (GLdouble)50, 0, 200);
gluLookAt( 0,0,0,
-10, -10, -100,
1,0,0);
draw_shaders();
}
void Laba8::mouseAction(GLFWwindow *window, int button, int action, int mods)
{
super::mouseAction(window, button, action, mods);
}
void Laba8::keyboardAction(GLFWwindow *window, int key, int action)
{
super::keyboardAction(window, key, action);
if (isKeyPressed(KEY(L))) {
if (m_lightSpoted > 0.5) {
m_lightSpoted = 0.0;
} else {
m_lightSpoted = 1.0;
}
}
}
void Laba8::cursorAction(GLFWwindow *window, double x, double y)
{
super::cursorAction(window, x, y);
}
void Laba8::update()
{
super::update();
float delta = 1;
if (isKeyPressed(KEY(LEFT))) {
m_lightPosition.setY( m_lightPosition.y() + delta );
} else if (isKeyPressed(KEY(RIGHT))) {
m_lightPosition.setY( m_lightPosition.y() - delta );
} else if (isKeyPressed(KEY(DOWN))) {
m_lightPosition.setX( m_lightPosition.x() - delta );
} else if (isKeyPressed(KEY(UP))) {
m_lightPosition.setX( m_lightPosition.x() + delta );
} else if (isKeyPressed(KEY(1))) {
m_lightPosition.setZ( m_lightPosition.z() + delta );
} else if (isKeyPressed(KEY(2))) {
m_lightPosition.setZ( m_lightPosition.z() - delta );
}
}