Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task-1 done #88

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ add_subdirectory(sayfudinova)
add_subdirectory(Antonova)
add_subdirectory(Biryuchkov)
add_subdirectory(Bondarchuk)
add_subdirectory(Galaktionov)
1 change: 1 addition & 0 deletions src/Galaktionov/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(task1)
19 changes: 19 additions & 0 deletions src/Galaktionov/task1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set(SRCS
main.cpp
CubeWindow.cpp
CubeWindow.h
shaders.qrc
textures.qrc
f_shader.fsh
v_shader.vsh
)

find_package(Qt5 COMPONENTS Widgets REQUIRED)

add_executable(Galaktionov-task1 ${SRCS})

target_link_libraries(Galaktionov-task1
PRIVATE
Qt5::Widgets
FGL::Base
)
139 changes: 139 additions & 0 deletions src/Galaktionov/task1/CubeWindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include "CubeWindow.h"

CubeWindow::CubeWindow(QWindow *parent)
: fgl::GLWindow(parent), m_shader_texture(nullptr), m_index_buffer(QOpenGLBuffer::IndexBuffer)
{
}

void CubeWindow::init()//called once
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//rgb and 0-transparent 1-opaque

glEnable(GL_DEPTH_TEST);//depth buffer
glEnable(GL_CULL_FACE);//clipping back faces

//shaders
m_shader_program = std::make_unique<QOpenGLShaderProgram>(this);
m_shader_program->addCacheableShaderFromSourceFile(QOpenGLShader::Vertex, ":/v_shader.vsh");
m_shader_program->addCacheableShaderFromSourceFile(QOpenGLShader::Fragment, ":/f_shader.fsh");
m_shader_program->link();

//TEXTURE
m_shader_texture = std::shared_ptr<QOpenGLTexture>(new QOpenGLTexture(QImage(":/cube.jpg").mirrored()));
//Set nearest filtering mode for texture minification
m_shader_texture->setMinificationFilter(QOpenGLTexture::Nearest);
//Set bilinear filtering mode for texture magnification
m_shader_texture->setMagnificationFilter(QOpenGLTexture::Linear);
//Wrap texture coordinates by repeating
m_shader_texture->setWrapMode(QOpenGLTexture::Repeat);

Init_Cube(0.0f, 0.0f, 0.0f, 1.0f);

matrixUniform_ = m_shader_program->uniformLocation("qt_ModelViewProjectionMatrix");
vertex_location = m_shader_program->attributeLocation("qt_Vertex");
texture_location = m_shader_program->attributeLocation("qt_MultiTexCoord0");
}

void CubeWindow::render()//paint
{
const auto retinaScale = devicePixelRatio();
glViewport(0, 0, width() * retinaScale, height() * retinaScale);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clear buffers

QMatrix4x4 model_view_matrix;
model_view_matrix.perspective(60.0f, (float)width() / height(), 0.1f, 100.0f);
model_view_matrix.translate(0.0, 0.0, -3.0);
model_view_matrix.rotate(100.0 * frame_ / screen()->refreshRate(), rotationAxis);

m_shader_texture->bind(0);

m_shader_program->bind();
m_shader_program->setUniformValue("qt_ModelViewProjectionMatrix", m_projection_matrix * model_view_matrix);
m_shader_program->setUniformValue("qt_Texture0", 0);

int offset = 0;

m_shader_program->enableAttributeArray(vertex_location);
m_shader_program->setAttributeBuffer(vertex_location, GL_FLOAT, offset, 3, sizeof(Vertex_Data));

offset += sizeof(QVector3D);

m_shader_program->enableAttributeArray(texture_location);
m_shader_program->setAttributeBuffer(texture_location, GL_FLOAT, offset, 2, sizeof(Vertex_Data));

m_vertex_buffer.bind();
m_index_buffer.bind();

glDrawElements(GL_TRIANGLES, m_index_buffer.size(), GL_UNSIGNED_INT, nullptr);//draw triangles

++frame_;
}

void CubeWindow::Init_Cube(float x, float y, float z, float width)
{
float wd2 = width / 2.0f;//wd2 = width divided by 2
QVector<Vertex_Data> vertexes;

//z
vertexes.append(Vertex_Data{QVector3D(x - wd2, y + wd2, z + wd2), QVector2D(0.0, 1.0), QVector3D(0.0, 0.0, 1.0)});
vertexes.append(Vertex_Data{QVector3D(x - wd2, y - wd2, z + wd2), QVector2D(0.0, 0.0), QVector3D(0.0, 0.0, 1.0)});
vertexes.append(Vertex_Data{QVector3D(x + wd2, y + wd2, z + wd2), QVector2D(1.0, 1.0), QVector3D(0.0, 0.0, 1.0)});
vertexes.append(Vertex_Data{QVector3D(x + wd2, y - wd2, z + wd2), QVector2D(1.0, 0.0), QVector3D(0.0, 0.0, 1.0)});
//x
vertexes.append(Vertex_Data{QVector3D(x + wd2, y - wd2, z + wd2), QVector2D(0.0, 1.0), QVector3D(1.0, 0.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x + wd2, y - wd2, z - wd2), QVector2D(0.0, 0.0), QVector3D(1.0, 0.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x + wd2, y + wd2, z + wd2), QVector2D(1.0, 1.0), QVector3D(1.0, 0.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x + wd2, y + wd2, z - wd2), QVector2D(1.0, 0.0), QVector3D(1.0, 0.0, 0.0)});
//y
vertexes.append(Vertex_Data{QVector3D(x + wd2, y + wd2, z + wd2), QVector2D(0.0, 1.0), QVector3D(0.0, 1.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x + wd2, y + wd2, z - wd2), QVector2D(0.0, 0.0), QVector3D(0.0, 1.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x - wd2, y + wd2, z + wd2), QVector2D(1.0, 1.0), QVector3D(0.0, 1.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x - wd2, y + wd2, z - wd2), QVector2D(1.0, 0.0), QVector3D(0.0, 1.0, 0.0)});
//-z
vertexes.append(Vertex_Data{QVector3D(x + wd2, y + wd2, z - wd2), QVector2D(0.0, 1.0), QVector3D(0.0, 0.0, -1.0)});
vertexes.append(Vertex_Data{QVector3D(x + wd2, y - wd2, z - wd2), QVector2D(0.0, 0.0), QVector3D(0.0, 0.0, -1.0)});
vertexes.append(Vertex_Data{QVector3D(x - wd2, y + wd2, z - wd2), QVector2D(1.0, 1.0), QVector3D(0.0, 0.0, -1.0)});
vertexes.append(Vertex_Data{QVector3D(x - wd2, y - wd2, z - wd2), QVector2D(1.0, 0.0), QVector3D(0.0, 0.0, -1.0)});
//-x
vertexes.append(Vertex_Data{QVector3D(x - wd2, y + wd2, z + wd2), QVector2D(0.0, 1.0), QVector3D(-1.0, 0.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x - wd2, y + wd2, z - wd2), QVector2D(0.0, 0.0), QVector3D(-1.0, 0.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x - wd2, y - wd2, z + wd2), QVector2D(1.0, 1.0), QVector3D(-1.0, 0.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x - wd2, y - wd2, z - wd2), QVector2D(1.0, 0.0), QVector3D(-1.0, 0.0, 0.0)});
//-y
vertexes.append(Vertex_Data{QVector3D(x - wd2, y - wd2, z + wd2), QVector2D(0.0, 1.0), QVector3D(0.0, -1.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x - wd2, y - wd2, z - wd2), QVector2D(0.0, 0.0), QVector3D(0.0, -1.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x + wd2, y - wd2, z + wd2), QVector2D(1.0, 1.0), QVector3D(0.0, -1.0, 0.0)});
vertexes.append(Vertex_Data{QVector3D(x + wd2, y - wd2, z - wd2), QVector2D(1.0, 0.0), QVector3D(0.0, -1.0, 0.0)});

QVector<GLuint> indexes;
for (int i = 0; i < 24; i += 4) {
indexes.append(i + 0);
indexes.append(i + 1);
indexes.append(i + 2);
indexes.append(i + 2);
indexes.append(i + 1);
indexes.append(i + 3);
}

m_vertex_buffer.create();
m_vertex_buffer.bind();
m_vertex_buffer.allocate(vertexes.constData(), vertexes.size() * sizeof(Vertex_Data));

m_index_buffer.create();
m_index_buffer.bind();
m_index_buffer.allocate(indexes.constData(), indexes.size() * sizeof(GLuint));
}

void CubeWindow::mousePressEvent(QMouseEvent *event) {
// Save mouse press position
mousePressPosition = QVector2D(event->localPos());
}

void CubeWindow::mouseReleaseEvent(QMouseEvent *event) {
// Mouse release position - mouse press position
const auto diff = QVector2D(event->localPos()) - mousePressPosition;

rotationAxis = QVector3D(diff.y(), diff.x(), 0.0).normalized();
}

57 changes: 57 additions & 0 deletions src/Galaktionov/task1/CubeWindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include <Base/GLWindow.hpp>
#include <QColor>
#include <QColorDialog>
#include <QKeyEvent>
#include <QMatrix4x4>
#include <QOpenGLBuffer>
#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
#include <QQuaternion>
#include <QVector2D>
#include <QVector3D>
#include <memory>
#include <QColorDialog>
#include <QMouseEvent>
#include <QOpenGLFunctions>
#include <QScreen>
#include <array>
#include <QWindow>

struct Vertex_Data {
QVector3D position;
QVector2D texture_coordinates;
QVector3D normal;
};

class CubeWindow final : public fgl::GLWindow {

public:
CubeWindow(QWindow *parent = nullptr);

void init() override;
void render() override;

protected:
void Init_Cube(float x, float y, float z, float width);

void mousePressEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;

private:
QMatrix4x4 m_projection_matrix;
std::unique_ptr<QOpenGLShaderProgram> m_shader_program;
std::shared_ptr<QOpenGLTexture> m_shader_texture;

QOpenGLBuffer m_vertex_buffer;
QOpenGLBuffer m_index_buffer;
int frame_ = 0;

GLint vertex_location;
GLint texture_location;
GLint matrixUniform_;

QVector2D mousePressPosition{0.0, 0.0};
QVector3D rotationAxis{0.0, 1.0, 0.0};
};
Binary file added src/Galaktionov/task1/cube.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/Galaktionov/task1/f_shader.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
uniform sampler2D qt_Texture0;

varying highp vec4 qt_TexCoord0;

void main(void)
{
gl_FragColor = texture2D(qt_Texture0, qt_TexCoord0.st);
}
19 changes: 19 additions & 0 deletions src/Galaktionov/task1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <QApplication>
#include <QSurfaceFormat>
#include "CubeWindow.h"

int main(int argc, char **argv) {
QApplication app(argc, argv);

QSurfaceFormat format;
format.setSamples(16);
format.setVersion(2, 1);

CubeWindow window;
window.setFormat(format);
window.resize(1280, 720);
window.show();
window.setAnimated(true);

return app.exec();
}
6 changes: 6 additions & 0 deletions src/Galaktionov/task1/shaders.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/">
<file>f_shader.fsh</file>
<file>v_shader.vsh</file>
</qresource>
</RCC>
5 changes: 5 additions & 0 deletions src/Galaktionov/task1/textures.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>cube.jpg</file>
</qresource>
</RCC>
12 changes: 12 additions & 0 deletions src/Galaktionov/task1/v_shader.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
attribute highp vec4 qt_Vertex;
attribute highp vec4 qt_MultiTexCoord0;

uniform highp mat4 qt_ModelViewProjectionMatrix;

varying highp vec4 qt_TexCoord0;

void main(void)
{
gl_Position = qt_ModelViewProjectionMatrix * qt_Vertex;
qt_TexCoord0 = qt_MultiTexCoord0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

префикс qt_ в названиях мне не понятен если честно :)

}