trippy-cube/src/main.cpp
Nicolás Ortega Froysa 6e80cc759f
Added sources.
2018-10-25 16:19:07 +02:00

195 lines
5.1 KiB
C++

/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net> All rights reserved.
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <glm/gtc/matrix_transform.hpp>
#include "input.hpp"
#include "globals.hpp"
#include "shaders.hpp"
#include "camera.hpp"
input *in_sys;
int main() {
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "ERROR: " << SDL_GetError() << std::endl;
exit(1);
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Window *window = SDL_CreateWindow("OpenGL Tutorial",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if(not window)
{
std::cerr << "ERROR: " << SDL_GetError() << std::endl;
SDL_Quit();
exit(1);
}
//SDL_ShowCursor(SDL_DISABLE);
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(0);
glewExperimental = true;
glewInit();
// create the VAO
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// vertex buffer
GLuint vbuffer;
glGenBuffers(1, &vbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data),
g_vertex_buffer_data, GL_STATIC_DRAW);
srand(static_cast<unsigned int>(time(0)));
GLfloat color_buffer_data[108];
// generate random colors every time
for(int i = 0; i < 108; ++i)
{
color_buffer_data[i] = static_cast<float>(rand()) /
static_cast<float>(RAND_MAX);
}
bool color_ascend[108];
for(int i = 0; i < 108; ++i)
color_ascend[i] = rand() % 2 ? true : false;
// color buffer
GLuint cbuffer;
glGenBuffers(1, &cbuffer);
glBindBuffer(GL_ARRAY_BUFFER, cbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(color_buffer_data),
color_buffer_data, GL_STATIC_DRAW);
// load the shaders into a GLSL program that can be run
GLuint program_id = load_shaders("../shaders/vert_shader.glsl",
"../shaders/frag_shader.glsl");
GLuint matrix_id = glGetUniformLocation(program_id, "MVP");
// set the clear color to black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
in_sys = new input();
camera cam;
while(true)
{
cam.update(in_sys);
// clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program_id);
glm::mat4 mvp;
{
glm::mat4 proj = glm::perspective(glm::radians(45.0f),
static_cast<float>(SCREEN_WIDTH) / static_cast<float>(SCREEN_HEIGHT),
0.1f, 100.0f);
glm::mat4 view = glm::lookAt(
cam.get_pos(), // camera position
glm::vec3(0,0,0), // where the camera is looking
glm::vec3(0,1,0) // which way is vertically up
);
glm::mat4 mod = glm::mat4(1.0f); // identity matrix, object is at origin
mvp = proj * view * mod;
}
glUniformMatrix4fv(matrix_id, 1, GL_FALSE, &mvp[0][0]);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
// change colors
for(int i = 0; i < 108; ++i)
{
const float color_delta = 0.0001f;
if(color_ascend[i])
color_buffer_data[i] += color_delta;
else
color_buffer_data[i] -= color_delta;
if(color_buffer_data[i] >= 1.0f)
{
color_buffer_data[i] = 1.0f;
color_ascend[i] = false;
}
else if(color_buffer_data[i] <= 0.0f)
{
color_buffer_data[i] = 0.0f;
color_ascend[i] = true;
}
}
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, cbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(color_buffer_data),
color_buffer_data, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glDrawArrays(GL_TRIANGLES, 0, 12*3);
glDisableVertexAttribArray(0);
SDL_GL_SwapWindow(window);
in_sys->sync_events();
if(in_sys->get_action("quit"))
break;
}
#ifdef DEBUG
std::cout << "Shutting down..." << std::endl;
#endif
glDeleteProgram(program_id);
glDeleteBuffers(1, &cbuffer);
glDeleteBuffers(1, &vbuffer);
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}