funny-animated-gif-from-pro.../native/main.cpp

200 lines
6.5 KiB
C++
Raw Normal View History

2021-01-29 04:09:10 +02:00
/*
Copyright (C) 2021 hiimgoodpack
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <mesh.h>
#include <GLFW/glfw3.h>
#include <shader.h>
#include <cstring>
#include <cassert>
#include <iostream>
#include <type_traits>
2021-01-29 04:09:10 +02:00
typedef struct {
linalg::aliases::float2 position;
2021-01-29 04:09:10 +02:00
} Vertex;
typedef unsigned int Element;
const Mesh::Attribute VERTEX_ATTRIBUTES[] = {
{
.index = 0,
.size = 2,
2021-01-29 04:09:10 +02:00
.type = GL_FLOAT,
.stride = sizeof(Vertex),
.pointer = (void*)offsetof(Vertex, position)
}
};
#include <fstream>
std::string readExternalFile(const char* name) {
std::string contents;
std::ifstream file;
file.open(name, std::fstream::in);
if (!file.is_open())
std::cerr << "Failed to load file " << name << ": " << strerror(errno) << "\n";
std::string line;
while (getline(file, line)) {
contents += line;
contents += "\n";
}
file.close();
return std::string(contents);
}
void onDebugOutput(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char* message, const void* /* userParam */) {
std::cerr << "OpenGL ";
switch (severity) {
case GL_DEBUG_SEVERITY_HIGH: std::cerr << "high severity"; break;
case GL_DEBUG_SEVERITY_MEDIUM: std::cerr << "moderately severity"; break;
case GL_DEBUG_SEVERITY_LOW: std::cerr << "low severity"; break;
case GL_DEBUG_SEVERITY_NOTIFICATION: std::cerr << "notification"; break;
default: std::cerr << "[unknown severity " << severity << "]"; break;
}
std::cerr << " ";
switch (type) {
case GL_DEBUG_TYPE_ERROR: std::cerr << "error"; break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: std::cerr << "deprecation warning"; break;
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: std::cerr << "undefined behavior warning"; break;
case GL_DEBUG_TYPE_PORTABILITY: std::cerr << "portability warning"; break;
case GL_DEBUG_TYPE_PERFORMANCE: std::cerr << "performance warning"; break;
case GL_DEBUG_TYPE_MARKER: std::cerr << "debug marker"; break;
case GL_DEBUG_TYPE_PUSH_GROUP: std::cerr << "pushed group"; break;
case GL_DEBUG_TYPE_POP_GROUP: std::cerr << "popped group"; break;
case GL_DEBUG_TYPE_OTHER: std::cerr << "debug message"; break;
default: std::cerr << "[unknown message type " << type << "]"; break;
}
std::cerr << " from ";
switch (source) {
case GL_DEBUG_SOURCE_API: std::cerr << "API"; break;
case GL_DEBUG_SOURCE_WINDOW_SYSTEM: std::cerr << "window system"; break;
case GL_DEBUG_SOURCE_SHADER_COMPILER: std::cerr << "shader compiler"; break;
case GL_DEBUG_SOURCE_THIRD_PARTY: std::cerr << "third party"; break;
case GL_DEBUG_SOURCE_APPLICATION: std::cerr << "application"; break;
case GL_DEBUG_SOURCE_OTHER: std::cerr << "unknown source"; break;
default: std::cerr << "[unknown source type " << source << "]"; break;
}
std::cerr << ": ";
if (length < 0)
std::cerr << message;
else
std::cerr << std::string(message, length);
std::cerr << " (message id: " << id << ")\n";
}
void onGLFWError(int errorCode, const char* error) {
std::cerr << "GLFW error: " << error << " (error code: " << errorCode << ")\n";
}
2021-01-29 04:49:11 +02:00
Shader::Program shader;
const unsigned int resultLength = 350;
2021-01-29 04:09:10 +02:00
int main() {
// Get a context
2021-01-29 04:09:10 +02:00
glfwSetErrorCallback(onGLFWError);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
glfwWindowHint(GLFW_SAMPLES, 4);
//glfwWindowHint(GLFW_VISIBLE, false);
GLFWwindow* window = glfwCreateWindow(resultLength, resultLength, "You aren't supposed to see this", NULL, NULL);
2021-01-29 04:09:10 +02:00
glfwMakeContextCurrent(window);
// Load a square
/*
0--1
| |
3--2
*/
const Vertex squareVertices[] = {
{
.position = {-1, 1}
},
{
.position = {1, 1}
},
{
.position = {1, -1}
},
{
.position = {-1, -1}
}
};
const Element squareElements[] = {0, 1, 3, 1, 2, 3};
Buffer::Specialized<Vertex, GL_ARRAY_BUFFER> vertexBuffer(squareVertices, sizeof(squareVertices), GL_STATIC_DRAW);
Buffer::Specialized<Element, GL_ELEMENT_ARRAY_BUFFER> elementBuffer(squareElements, sizeof(squareElements), GL_STATIC_DRAW);
Mesh::Buffer buffers[2] = {
{
.buffer = vertexBuffer,
.attributes = VERTEX_ATTRIBUTES,
.attributeCount = sizeof(VERTEX_ATTRIBUTES)/sizeof(Mesh::Attribute)
},
{
.buffer = elementBuffer
}
};
Mesh::InstancedElements square(buffers, 2, sizeof(squareElements)/sizeof(Element), 1, GL_UNSIGNED_INT);
2021-01-29 04:09:10 +02:00
// Load shaders
2021-01-29 04:49:11 +02:00
{
std::string vertexCode = readExternalFile("../shaders/vertex.glsl");
assert(shader.add(vertexCode.c_str(), -1, GL_VERTEX_SHADER) == Shader::Error::NONE);
2021-01-29 04:49:11 +02:00
std::string fragmentCode = readExternalFile("../shaders/fragment.glsl");
assert(shader.add(fragmentCode.c_str(), -1, GL_FRAGMENT_SHADER) == Shader::Error::NONE);
2021-01-29 04:49:11 +02:00
}
2021-01-29 04:09:10 +02:00
shader.link();
shader.use();
// Set up a framebuffer
/*unsigned int frameBuffer;
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
unsigned int colorBuffer;
glGenTextures(1, &colorBuffer);
glBindTexture(GL_TEXTURE_2D, colorBuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, resultLength, resultLength, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorBuffer, 0);
unsigned int renderBuffer;
glGenRenderbuffers(1, &renderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, resultLength, resultLength);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);
assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE && "Generated framebuffer is incomplete");
*/
glClearColor(0.5, 0, 0, 1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_SRC_ALPHA);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
square.draw();
glfwSwapBuffers(window);
glfwPollEvents();
}
2021-01-29 04:09:10 +02:00
}