#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 );
}
}