Initial commit

This commit is contained in:
Ilshat Safiullin 2018-06-15 00:41:30 +05:00
commit a9454741d4
13 changed files with 55764 additions and 0 deletions

23
CMakeLists.txt Normal file
View File

@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.10)
project(tes-tiger)
include_directories(includes)
link_directories(libraries)
set(CMAKE_CXX_STANDARD 17)
add_executable(tes-tiger
includes/boost/property_tree/ptree.hpp
includes/boost/property_tree/json_parser.hpp
includes/glm/gtc/type_ptr.hpp
includes/glm/gtc/matrix_transform.hpp
includes/glm/glm.hpp
includes/SOIL/SOIL.h
includes/GL/glew.h
includes/GLFW/glfw3.h
glew.c
main.cpp)
target_link_libraries(tes-tiger
libSOIL.a
libglfw3.a
opengl32.lib)

28581
glew.c Normal file

File diff suppressed because it is too large Load Diff

494
main.cpp Normal file
View File

@ -0,0 +1,494 @@
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <initializer_list>
#include <vector>
#define GLEW_STATIC
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "SOIL/SOIL.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
const int windowWidth = 800;
const int windowHeight = 600;
const float pi = 3.14159f;
std::string readFile(const std::string& filePath) {
std::ifstream file(filePath);
auto result = std::string(
std::istreambuf_iterator<char>(file),
std::istreambuf_iterator<char>()
);
file.close();
return result;
}
const GLuint loadShader(const GLenum& shaderType, const std::string& shaderFilePath) {
const GLuint shader = glCreateShader(shaderType);
auto shaderSource = readFile(shaderFilePath);
auto shaderSourceC = shaderSource.c_str();
glShaderSource(shader, 1, &shaderSourceC, nullptr);
glCompileShader(shader);
GLint success;
GLchar infoLog[512];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
std::cout << "Failed to compile shader: " << infoLog << std::endl;
}
return shader;
}
const GLuint linkShaderProgram(const std::initializer_list<std::pair<const GLenum, const std::string>>& shadersInfo) {
const GLuint shaderProgram = glCreateProgram();
for(const auto& shaderInfo: shadersInfo) {
auto shader = loadShader(shaderInfo.first, shaderInfo.second);
glAttachShader(shaderProgram, shader);
glDeleteShader(shader);
}
glLinkProgram(shaderProgram);
{
GLint success;
GLchar infoLog[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
std::cout << "Failed to link shader program: " << infoLog << std::endl;
}
}
return shaderProgram;
}
GLuint loadTexture(const std::string& textureFilePath) {
int imageWidth, imageHeight;
unsigned char *imageData = SOIL_load_image(
textureFilePath.c_str(),
&imageWidth, &imageHeight,
nullptr, SOIL_LOAD_RGB
);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
SOIL_free_image_data(imageData);
return texture;
}
auto cameraPosition = glm::vec3(0.0f, 0.0f, 512.0f);
auto cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
auto cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
bool keyPressed[1024];
void keyCallback(GLFWwindow* /* window */, int key, int /* scanCode */, int action, int /* mode */) {
if(key == GLFW_KEY_W) {
if(action == GLFW_PRESS) {
keyPressed[GLFW_KEY_W] = true;
} else if(action == GLFW_RELEASE) {
keyPressed[GLFW_KEY_W] = false;
}
} else if(key == GLFW_KEY_S) {
if(action == GLFW_PRESS) {
keyPressed[GLFW_KEY_S] = true;
} else if(action == GLFW_RELEASE) {
keyPressed[GLFW_KEY_S] = false;
}
} else if(key == GLFW_KEY_A) {
if(action == GLFW_PRESS) {
keyPressed[GLFW_KEY_A] = true;
} else if(action == GLFW_RELEASE) {
keyPressed[GLFW_KEY_A] = false;
}
} else if(key == GLFW_KEY_D) {
if(action == GLFW_PRESS) {
keyPressed[GLFW_KEY_D] = true;
} else if(action == GLFW_RELEASE) {
keyPressed[GLFW_KEY_D] = false;
}
}
}
void moveCamera(double deltaTime) {
float cameraSpeed = 1024.0f * static_cast<float>(deltaTime);
if(keyPressed[GLFW_KEY_W]) {
cameraPosition = cameraPosition + cameraSpeed * cameraFront;
} else if(keyPressed[GLFW_KEY_S]) {
cameraPosition = cameraPosition - cameraSpeed * cameraFront;
} else if(keyPressed[GLFW_KEY_A]) {
cameraPosition = cameraPosition - cameraSpeed * glm::normalize(glm::cross(cameraFront, cameraUp));
} else if(keyPressed[GLFW_KEY_D]) {
cameraPosition = cameraPosition + cameraSpeed * glm::normalize(glm::cross(cameraFront, cameraUp));
}
}
float lastX = windowWidth / 2.0f, lastY = windowHeight / 2.0f;
float yaw = -90.0f, pitch = 0.0f;
bool firstMouse = false;
void mouseCallback(GLFWwindow* /* window */, double x, double y) {
auto xf = static_cast<float>(x);
auto yf = static_cast<float>(y);
if(!firstMouse) {
lastX = xf; lastY = yf;
firstMouse = true;
}
auto offsetX = xf - lastX, offsetY = lastY - yf;
lastX = xf; lastY = yf;
float sensivity = 0.05f;
offsetX *= sensivity;
offsetY *= sensivity;
yaw += offsetX;
pitch += offsetY;
if(pitch > 89.0f) {
pitch = 89.0f;
} else if(pitch < -89.0f) {
pitch = -89.0f;
}
glm::vec3 newCameraFront;
newCameraFront.x = cosf(glm::radians(pitch)) * cosf(glm::radians(yaw));
newCameraFront.y = sinf(glm::radians(pitch));
newCameraFront.z = cosf(glm::radians(pitch)) * sinf(glm::radians(yaw));
cameraFront = glm::normalize(newCameraFront);
}
const std::pair<const glm::vec3, const glm::vec3> findPlaneBasis(const glm::vec3& normal) {
std::pair<glm::vec3, glm::vec3> result;
glm::vec3 e0, e1;
if(normal.z != 0.0f) {
e0 = glm::normalize(glm::vec3(1.0f, 0.0f, -normal.x / normal.z));
e1 = glm::vec3(0.0f, 1.0f, -normal.y / normal.z);
} else if(normal.y != 0.0f) {
e0 = glm::normalize(glm::vec3(1.0f, -normal.x / normal.y, 0.0f));
e1 = glm::vec3(0.0f, -normal.z / normal.y, 1.0f);
} else {
e0 = glm::normalize(glm::vec3(-normal.y / normal.x, 1.0f, 0.0f));
e1 = glm::vec3(-normal.z / normal.x, 0.0f, 1.0f);
}
e1 = glm::normalize(e1 - (glm::dot(e1, e0) / glm::dot(e0, e0)) * e0);
result.first = e0;
result.second = e1;
return result;
}
const float radius = 8.0f;
const float step = 100.0f;
const float angle = pi / 6.0f;
const size_t threadsCount = 3;
const size_t verticesCount = 6;
void bufferVertices(
std::vector<GLfloat>& verticesBuffer,
const glm::vec3& start,
const glm::vec3& end,
const glm::vec3& color
) {
auto direction = glm::normalize(end - start);
size_t iterationsCount = glm::length(end - start) / step;
if(iterationsCount == 0) {
iterationsCount = 1;
}
auto adjustedStep = glm::length(end - start) / iterationsCount;
auto e = findPlaneBasis(direction);
std::vector<glm::vec3> threadCenters;
std::vector<std::vector<glm::vec4>> threads;
for(size_t i = 0; i < threadsCount; i++) {
const auto iPhase = i * 2 * pi / threadsCount;
const glm::vec3 threadCenter = radius * (e.first * cosf(iPhase) + e.second * sinf(iPhase));
std::vector<glm::vec4> vertices;
for(size_t j = 0; j < verticesCount; j++) {
const auto jPhase = j * 2 * pi / verticesCount;
const glm::vec3 vertex = threadCenter + radius * (e.first * cosf(jPhase) + e.second * sinf(jPhase));
vertices.emplace_back(glm::vec4(vertex.x, vertex.y, vertex.z, 1.0f));
}
threadCenters.emplace_back(threadCenter);
threads.emplace_back(vertices);
}
auto transform = glm::mat4(1.0f);
transform = glm::rotate(transform, angle, direction);
transform = glm::translate(transform, adjustedStep * direction);
auto p = [&verticesBuffer] (const GLfloat& value) mutable {verticesBuffer.push_back(value);};
auto v = [&p] (const glm::vec3& vector) mutable {p(vector.x); p(vector.y); p(vector.z);};
auto st = [&p] (const GLfloat& s, const GLfloat& t) mutable {p(s); p(t);};
auto n = v;
auto c = v;
for(size_t i = 0; i < iterationsCount; i++) {
std::vector<std::vector<glm::vec4>> newThreads;
for(size_t j = 0; j < threadsCount; j++) {
auto vertices = threads[j];
std::vector<glm::vec4> newVertices;
for(size_t k = 0; k < verticesCount; k++) {
newVertices.push_back(transform * vertices[k]);
}
newThreads.push_back(newVertices);
auto threadCenter = threadCenters[j];
auto threadCenter4 = glm::vec4(threadCenter.x, threadCenter.y, threadCenter.z, 1.0f);
auto newThreadCenter4 = transform * threadCenter4;
auto newThreadCenter = glm::vec3(newThreadCenter4.x, newThreadCenter4.y, newThreadCenter4.z);
threadCenters[j] = newThreadCenter;
for(size_t k = 0; k < verticesCount; k++) {
size_t k1 = (k + 1) % verticesCount;
auto vk = glm::vec3(vertices[k].x, vertices[k].y, vertices[k].z);
auto vk1 = glm::vec3(vertices[k1].x, vertices[k1].y, vertices[k1].z);
auto nvk = glm::vec3(newVertices[k].x, newVertices[k].y, newVertices[k].z);
auto nvk1 = glm::vec3(newVertices[k1].x, newVertices[k1].y, newVertices[k1].z);
v(start + vk); n(vk - threadCenter); st(0.1f, 0.1f); c(color);
v(start + vk1); n(vk1 - threadCenter); st(0.2f, 0.1f); c(color);
v(start + nvk); n(nvk - newThreadCenter); st(0.1f, 0.2f); c(color);
v(start + vk1); n(vk1 - threadCenter); st(0.2f, 0.1f); c(color);
v(start + nvk1); n(nvk1 - newThreadCenter); st(0.2f, 0.2f); c(color);
v(start + nvk); n(nvk - newThreadCenter); st(0.1f, 0.2f); c(color);
}
}
threads = newThreads;
}
};
int main() {
if(glfwInit() != GLFW_TRUE) {
std::cout << "Failed to initialize glfw." << std::endl;
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "gl", nullptr, nullptr);
if(window == nullptr) {
std::cout << "Failed to create window." << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyCallback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouseCallback);
glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK) {
std::cout << "Failed to initialize glew." << std::endl;
glfwTerminate();
return -1;
}
int frameBufferWidth, frameBufferHeight;
glfwGetFramebufferSize(window, &frameBufferWidth, &frameBufferHeight);
glViewport(0, 0, frameBufferWidth, frameBufferHeight);
auto shaderProgram = linkShaderProgram({
{GL_VERTEX_SHADER, "../resources/shader.vertex"},
{GL_FRAGMENT_SHADER, "../resources/shader.fragment"}
});
auto texture = loadTexture("../resources/wooden-container.jpg");
GLfloat vertices[] = {
// position, texture coordinates
-2048.0f, -2048.0f, 0.0f, 0.0f, 0.0f,
2048.0f, -2048.0f, 0.0f, 1.0f, 0.0f,
2048.0f, 2048.0f, 0.0f, 1.0f, 1.0f,
2048.0f, 2048.0f, 0.0f, 1.0f, 1.0f,
-2048.0f, 2048.0f, 0.0f, 0.0f, 1.0f,
-2048.0f, -2048.0f, 0.0f, 0.0f, 0.0f
};
GLuint vertexArrayObject;
glGenVertexArrays(1, &vertexArrayObject);
GLuint vertexBufferObject;
glGenBuffers(1, &vertexBufferObject);
glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(0 * sizeof(GLfloat)));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
// threads
auto threadShaderProgram = linkShaderProgram({
{GL_VERTEX_SHADER, "../resources/thread.vertex"},
{GL_FRAGMENT_SHADER, "../resources/thread.fragment"}
});
auto threadTexture = loadTexture("../resources/fabric.jpg");
boost::property_tree::ptree root;
glfwSetWindowTitle(window, "parsing lines.json...");
boost::property_tree::read_json("../resources/lines100500.json", root);
size_t linesCount = 0;
for(const auto& line: root.get_child("lines")) {
linesCount++;
}
std::vector<GLfloat> verticesBuffer;
size_t lineIndex = 0;
for(const auto& line: root.get_child("lines")) {
std::vector<int> startPosition;
std::vector<int> endPosition;
std::vector<float> color;
for(const auto& value: line.second.get_child("start")) {
startPosition.push_back(value.second.get_value<int>());
}
for(const auto& value: line.second.get_child("end")) {
endPosition.push_back(value.second.get_value<int>());
}
for(const auto& value: line.second.get_child("color")) {
color.push_back(value.second.get_value<float>());
}
if(startPosition[0] != endPosition[0] || startPosition[1] != endPosition[1]) {
bufferVertices(
verticesBuffer,
glm::vec3(
static_cast<float>(startPosition[0]) / 2.0f,
static_cast<float>(startPosition[1]) / 2.0f,
lineIndex * 0.0025f
),
glm::vec3(
static_cast<float>(endPosition[0]) / 2.0f,
static_cast<float>(endPosition[1]) / 2.0f,
lineIndex * 0.0025f
),
glm::vec3(color[0], color[1], color[2])
);
}
lineIndex++;
std::stringstream progressString;
progressString << "loading " << lineIndex << "/" << linesCount;
glfwSetWindowTitle(window, progressString.str().c_str());
}
GLuint threadVAO;
glGenVertexArrays(1, &threadVAO);
GLuint threadVBO;
glGenBuffers(1, &threadVBO);
glBindVertexArray(threadVAO);
glBindBuffer(GL_ARRAY_BUFFER, threadVBO);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer.size() * sizeof(GLfloat), verticesBuffer.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(0 * sizeof(GLfloat)));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(GLfloat), (GLvoid*)(8 * sizeof(GLfloat)));
glEnableVertexAttribArray(3);
glBindVertexArray(0);
auto threadModelTransform = glm::mat4(1.0f);
threadModelTransform = glm::translate(threadModelTransform, glm::vec3(0.0f, 0.0f, 100.0f));
auto projectionTransform = glm::infinitePerspective(
glm::radians(45.0f),
static_cast<float>(frameBufferWidth) / static_cast<float>(frameBufferHeight),
0.1f
);
double lastTime = 0.0f;
glEnable(GL_DEPTH_TEST);
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
double currentTime = glfwGetTime();
double deltaTime = currentTime - lastTime;
lastTime = currentTime;
moveCamera(deltaTime);
auto viewTransform = glm::lookAt(
cameraPosition,
cameraPosition + cameraFront,
cameraUp
);
glfwSetWindowTitle(window, std::to_string(static_cast<size_t>(1.0 / deltaTime)).c_str());
glClearColor(0.5f, 0.5f, 1.0f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0);
glUniformMatrix4fv(
glGetUniformLocation(shaderProgram, "modelTransform"),
1,
GL_FALSE,
glm::value_ptr(glm::mat4(1.0f))
);
glUniformMatrix4fv(
glGetUniformLocation(shaderProgram, "viewTransform"),
1,
GL_FALSE,
glm::value_ptr(viewTransform)
);
glUniformMatrix4fv(
glGetUniformLocation(shaderProgram, "projectionTransform"),
1,
GL_FALSE,
glm::value_ptr(projectionTransform)
);
glBindVertexArray(vertexArrayObject);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
glUseProgram(threadShaderProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, threadTexture);
glUniform1i(glGetUniformLocation(threadShaderProgram, "sampler"), 0);
glUniformMatrix4fv(
glGetUniformLocation(threadShaderProgram, "modelTransform"),
1,
GL_FALSE,
glm::value_ptr(threadModelTransform)
);
glUniformMatrix4fv(
glGetUniformLocation(threadShaderProgram, "viewTransform"),
1,
GL_FALSE,
glm::value_ptr(viewTransform)
);
glUniformMatrix4fv(
glGetUniformLocation(threadShaderProgram, "projectionTransform"),
1,
GL_FALSE,
glm::value_ptr(projectionTransform)
);
glBindVertexArray(threadVAO);
glDrawArrays(GL_TRIANGLES, 0, verticesBuffer.size());
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}

BIN
resources/awesome-face.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

BIN
resources/fabric.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

5
resources/line.json Normal file
View File

@ -0,0 +1,5 @@
{
"lines": [
{"start": [-500, -500], "end": [500, 500], "color": [0.25, 0, 0.4]}
]
}

26587
resources/lines100500.json Normal file

File diff suppressed because it is too large Load Diff

10
resources/lines6.json Normal file
View File

@ -0,0 +1,10 @@
{
"lines": [
{"start": [-500, -500], "end": [500, 500], "color": [0.25, 0, 0.4]},
{"start": [-500, 500], "end": [500, -500], "color": [0.1, 0.7, 0.4]},
{"start": [-400, -400], "end": [400, -400], "color": [0.3, 0.3, 0.0]},
{"start": [400, -400], "end": [400, 400], "color": [0.0, 0.3, 0.15]},
{"start": [400, 400], "end": [-400, 400], "color": [0.2, 0.6, 0.9]},
{"start": [-400, 400], "end": [-400, -400], "color": [0.5, 0.3, 0.1]}
]
}

11
resources/shader.fragment Normal file
View File

@ -0,0 +1,11 @@
#version 460 core
in vec2 fragmentTextureCoordinates;
uniform sampler2D sampler;
out vec4 color;
void main() {
color = texture(sampler, fragmentTextureCoordinates);
}

15
resources/shader.vertex Normal file
View File

@ -0,0 +1,15 @@
#version 460 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 textureCoordinates;
uniform mat4 modelTransform;
uniform mat4 viewTransform;
uniform mat4 projectionTransform;
out vec2 fragmentTextureCoordinates;
void main() {
gl_Position = projectionTransform * viewTransform * modelTransform * vec4(position, 1.0f);
fragmentTextureCoordinates = vec2(textureCoordinates.x, 1.0f - textureCoordinates.y);
}

17
resources/thread.fragment Normal file
View File

@ -0,0 +1,17 @@
#version 460 core
in vec3 fNormal;
in vec2 fTextureCoordinates;
in vec3 fColor;
uniform sampler2D sampler;
out vec4 color;
const vec3 lightDirection = normalize(vec3(0.0f, 0.0f, -1.0f));
void main() {
vec3 sampleColor = texture(sampler, fTextureCoordinates).rgb;
float intensity = clamp(dot(lightDirection, -normalize(fNormal)), 0.0f, 1.0f);
color = vec4(intensity * sampleColor * fColor + vec3(0.15f, 0.15f, 0.15f), 1.0f);
}

21
resources/thread.vertex Normal file
View File

@ -0,0 +1,21 @@
#version 460 core
layout(location = 0) in vec3 vPosition;
layout(location = 1) in vec3 vNormal;
layout(location = 2) in vec2 vTextureCoordinates;
layout(location = 3) in vec3 vColor;
uniform mat4 modelTransform;
uniform mat4 viewTransform;
uniform mat4 projectionTransform;
out vec3 fNormal;
out vec2 fTextureCoordinates;
out vec3 fColor;
void main() {
gl_Position = projectionTransform * viewTransform * modelTransform * vec4(vPosition, 1.0f);
fNormal = vNormal;
fTextureCoordinates = vTextureCoordinates;
fColor = vColor;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 KiB