Added sources.
This commit is contained in:
parent
f202d0499e
commit
6e80cc759f
74
CMakeLists.txt
Normal file
74
CMakeLists.txt
Normal file
@ -0,0 +1,74 @@
|
||||
# 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.
|
||||
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
project(TrippyCube CXX)
|
||||
|
||||
# Binary filename
|
||||
set(TARGET_NAME "trippy-cube")
|
||||
|
||||
# Use DEBUG by default
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE "release")
|
||||
endif()
|
||||
string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
|
||||
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
|
||||
|
||||
# prefer newer versions of OpenGL
|
||||
set(OpenGL_GL_PREFERENCE "GLVND")
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(GLUT REQUIRED)
|
||||
find_package(GLEW REQUIRED)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_search_module(SDL2 REQUIRED sdl2)
|
||||
|
||||
include_directories(
|
||||
${SDL2_INCLUDE_DIRS})
|
||||
|
||||
set(SRCS
|
||||
"src/camera.cpp"
|
||||
"src/input.cpp"
|
||||
"src/main.cpp"
|
||||
"src/shaders.cpp")
|
||||
|
||||
# Define C++ compiler flags
|
||||
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wextra -Wpedantic -Wfatal-errors -Werror -pedantic-errors -fno-elide-constructors")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
|
||||
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O3")
|
||||
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os")
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "debug" OR CMAKE_BUILD_TYPE STREQUAL "relwithdebinfo")
|
||||
add_definitions(-DDEBUG)
|
||||
else()
|
||||
add_definitions(-DNDEBUG)
|
||||
endif()
|
||||
|
||||
add_executable(${TARGET_NAME}
|
||||
${SRCS})
|
||||
|
||||
target_link_libraries(${TARGET_NAME}
|
||||
OpenGL::OpenGL
|
||||
OpenGL::GLU
|
||||
${GLUT_LIBRARIES}
|
||||
${GLEW_LIBRARIES}
|
||||
${SDL2_LIBRARIES})
|
15
LICENSE
Normal file
15
LICENSE
Normal file
@ -0,0 +1,15 @@
|
||||
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.
|
7
shaders/frag_shader.glsl
Normal file
7
shaders/frag_shader.glsl
Normal file
@ -0,0 +1,7 @@
|
||||
#version 330 core
|
||||
in vec3 frag_col;
|
||||
out vec3 color;
|
||||
|
||||
void main() {
|
||||
color = frag_col;
|
||||
}
|
13
shaders/vert_shader.glsl
Normal file
13
shaders/vert_shader.glsl
Normal file
@ -0,0 +1,13 @@
|
||||
#version 330 core
|
||||
layout(location = 0) in vec3 vert_pos;
|
||||
layout(location = 1) in vec3 vert_col;
|
||||
|
||||
uniform mat4 MVP;
|
||||
|
||||
out vec3 frag_col;
|
||||
|
||||
void main() {
|
||||
gl_Position = MVP * vec4(vert_pos, 1);
|
||||
|
||||
frag_col = vert_col;
|
||||
}
|
44
src/camera.cpp
Normal file
44
src/camera.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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 "camera.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
camera::camera() : pos(0.0f, 0.0f, 0.0f),
|
||||
yaw(45.0f), angle(45.0f), dist(5.0f) { }
|
||||
|
||||
void camera::update(const input *in_sys) {
|
||||
std::pair<int, int> mm = in_sys->get_mouse_motion();
|
||||
angle -= glm::radians(static_cast<float>(mm.first));
|
||||
yaw += glm::radians(static_cast<float>(mm.second));
|
||||
std::get<0>(pos) = dist * cos(yaw) * sin(angle);
|
||||
std::get<1>(pos) = dist * sin(yaw);
|
||||
std::get<2>(pos) = dist * cos(yaw) * cos(angle);
|
||||
|
||||
dist -= in_sys->get_scroll();
|
||||
if(dist < 0)
|
||||
dist = 0;
|
||||
}
|
48
src/camera.hpp
Normal file
48
src/camera.hpp
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <GL/gl.h>
|
||||
#include <tuple>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
#include "input.hpp"
|
||||
|
||||
class camera {
|
||||
public:
|
||||
camera();
|
||||
void update(const input *in_sys);
|
||||
inline glm::vec3 get_pos() {
|
||||
return glm::vec3(std::get<0>(pos),
|
||||
std::get<1>(pos),
|
||||
std::get<2>(pos));
|
||||
}
|
||||
|
||||
private:
|
||||
std::tuple<GLfloat, GLfloat, GLfloat> pos;
|
||||
float yaw;
|
||||
float angle;
|
||||
float dist; // distance from origin
|
||||
};
|
70
src/globals.hpp
Normal file
70
src/globals.hpp
Normal file
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "input.hpp"
|
||||
#define SCREEN_WIDTH 800
|
||||
#define SCREEN_HEIGHT 600
|
||||
|
||||
extern input *in_sys;
|
||||
|
||||
static const GLfloat g_vertex_buffer_data[] = {
|
||||
-1.0f,-1.0f,-1.0f,
|
||||
-1.0f,-1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f,-1.0f,
|
||||
-1.0f,-1.0f,-1.0f,
|
||||
-1.0f, 1.0f,-1.0f,
|
||||
1.0f,-1.0f, 1.0f,
|
||||
-1.0f,-1.0f,-1.0f,
|
||||
1.0f,-1.0f,-1.0f,
|
||||
1.0f, 1.0f,-1.0f,
|
||||
1.0f,-1.0f,-1.0f,
|
||||
-1.0f,-1.0f,-1.0f,
|
||||
-1.0f,-1.0f,-1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f,-1.0f,
|
||||
1.0f,-1.0f, 1.0f,
|
||||
-1.0f,-1.0f, 1.0f,
|
||||
-1.0f,-1.0f,-1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
-1.0f,-1.0f, 1.0f,
|
||||
1.0f,-1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f,-1.0f,-1.0f,
|
||||
1.0f, 1.0f,-1.0f,
|
||||
1.0f,-1.0f,-1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f,-1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f,-1.0f,
|
||||
-1.0f, 1.0f,-1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f,-1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
1.0f,-1.0f, 1.0f
|
||||
};
|
99
src/input.cpp
Normal file
99
src/input.cpp
Normal file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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 "input.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
input::input() : scroll(0), mmotion(0,0) {
|
||||
actions["quit"] = false;
|
||||
actions["move_left"] = false;
|
||||
actions["move_right"] = false;
|
||||
actions["move_foward"] = false;
|
||||
actions["move_backward"] = false;
|
||||
|
||||
int mx, my;
|
||||
SDL_GetRelativeMouseState(&mx, &my);
|
||||
old_mpos = std::make_pair(mx, my);
|
||||
}
|
||||
|
||||
void input::sync_events() {
|
||||
scroll = 0;
|
||||
SDL_Event e;
|
||||
while(SDL_PollEvent(&e))
|
||||
{
|
||||
switch(e.type)
|
||||
{
|
||||
case SDL_QUIT:
|
||||
actions["quit"] = true;
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
set_key(e.key.keysym, true);
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
set_key(e.key.keysym, false);
|
||||
break;
|
||||
case SDL_MOUSEWHEEL:
|
||||
if(e.wheel.y not_eq 0)
|
||||
scroll = e.wheel.y;
|
||||
}
|
||||
}
|
||||
|
||||
// calculate mouse deltas
|
||||
int mx, my;
|
||||
SDL_GetRelativeMouseState(&mx, &my);
|
||||
mmotion.first = mx - old_mpos.first;
|
||||
mmotion.second = my - old_mpos.second;
|
||||
}
|
||||
|
||||
bool input::get_action(const std::string &action) const {
|
||||
try {
|
||||
return actions.at(action);
|
||||
} catch(std::exception &e) {
|
||||
std::cerr << "Failed to access action `" << action <<
|
||||
"': " << e.what();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void input::set_key(SDL_Keysym key, bool value) {
|
||||
switch(key.sym)
|
||||
{
|
||||
case SDLK_w:
|
||||
actions["move_foward"] = value;
|
||||
break;
|
||||
case SDLK_s:
|
||||
actions["move_backward"] = value;
|
||||
break;
|
||||
case SDLK_a:
|
||||
actions["move_left"] = value;
|
||||
break;
|
||||
case SDLK_d:
|
||||
actions["move_right"] = value;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
49
src/input.hpp
Normal file
49
src/input.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
|
||||
class input {
|
||||
public:
|
||||
input();
|
||||
void sync_events();
|
||||
bool get_action(const std::string &action) const;
|
||||
inline std::pair<int, int> get_mouse_motion() const {
|
||||
return mmotion;
|
||||
}
|
||||
inline int get_scroll() const {
|
||||
return scroll;
|
||||
}
|
||||
|
||||
private:
|
||||
void set_key(SDL_Keysym key, bool value);
|
||||
int scroll;
|
||||
std::map<std::string, bool> actions;
|
||||
std::pair<int, int> old_mpos; // old mouse position
|
||||
std::pair<int, int> mmotion; // mouse motion
|
||||
};
|
194
src/main.cpp
Normal file
194
src/main.cpp
Normal file
@ -0,0 +1,194 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
158
src/shaders.cpp
Normal file
158
src/shaders.cpp
Normal file
@ -0,0 +1,158 @@
|
||||
/*
|
||||
* 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 "shaders.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#ifdef DEBUG
|
||||
# include <iostream>
|
||||
#endif
|
||||
|
||||
GLuint load_shaders(const std::string &vertex_shader_path,
|
||||
const std::string &fragment_shader_path) {
|
||||
GLuint vshader_id = glCreateShader(GL_VERTEX_SHADER);
|
||||
GLuint fshader_id = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
||||
// load shaders from files
|
||||
std::string vshader_code;
|
||||
{
|
||||
std::ifstream vshader_stream(vertex_shader_path,
|
||||
std::ios::in);
|
||||
if(not vshader_stream.is_open())
|
||||
{
|
||||
throw std::runtime_error("Failed to open file " +
|
||||
vertex_shader_path);
|
||||
}
|
||||
std::stringstream ss;
|
||||
ss << vshader_stream.rdbuf();
|
||||
vshader_code = ss.str();
|
||||
vshader_stream.close();
|
||||
}
|
||||
|
||||
std::string fshader_code;
|
||||
{
|
||||
std::ifstream fshader_stream(fragment_shader_path,
|
||||
std::ios::in);
|
||||
if(not fshader_stream.is_open())
|
||||
{
|
||||
throw std::runtime_error("Failed to open file " +
|
||||
fragment_shader_path);
|
||||
}
|
||||
std::stringstream ss;
|
||||
ss << fshader_stream.rdbuf();
|
||||
fshader_code = ss.str();
|
||||
fshader_stream.close();
|
||||
}
|
||||
|
||||
GLint res = GL_FALSE;
|
||||
int info_log_length = 0;
|
||||
|
||||
// compile and check shaders
|
||||
#ifdef DEBUG
|
||||
std::cout << "Compiling shader: " << vertex_shader_path <<
|
||||
std::endl;
|
||||
#endif
|
||||
{
|
||||
const char *vsp = vshader_code.c_str();
|
||||
glShaderSource(vshader_id, 1, &vsp, nullptr);
|
||||
glCompileShader(vshader_id);
|
||||
}
|
||||
|
||||
glGetShaderiv(vshader_id, GL_COMPILE_STATUS, &res);
|
||||
glGetShaderiv(vshader_id, GL_INFO_LOG_LENGTH,
|
||||
&info_log_length);
|
||||
if(info_log_length > 0)
|
||||
{
|
||||
char *vshader_error = new char[info_log_length+1];
|
||||
glGetShaderInfoLog(vshader_id, info_log_length,
|
||||
nullptr, vshader_error);
|
||||
std::string error = "Shader compilation failed: ";
|
||||
error.append(vshader_error);
|
||||
delete vshader_error;
|
||||
|
||||
throw std::runtime_error(error);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
std::cout << "Compiling shader: " << fragment_shader_path <<
|
||||
std::endl;
|
||||
#endif
|
||||
{
|
||||
const char *fsp = fshader_code.c_str();
|
||||
glShaderSource(fshader_id, 1, &fsp, nullptr);
|
||||
glCompileShader(fshader_id);
|
||||
}
|
||||
|
||||
glGetShaderiv(fshader_id, GL_COMPILE_STATUS, &res);
|
||||
glGetShaderiv(fshader_id, GL_INFO_LOG_LENGTH,
|
||||
&info_log_length);
|
||||
if(info_log_length > 0)
|
||||
{
|
||||
char *fshader_error = new char[info_log_length+1];
|
||||
glGetShaderInfoLog(fshader_id, info_log_length,
|
||||
nullptr, fshader_error);
|
||||
std::string error = "Shader compilation failed: ";
|
||||
error.append(fshader_error);
|
||||
delete fshader_error;
|
||||
|
||||
throw std::runtime_error(error);
|
||||
}
|
||||
|
||||
// link
|
||||
#ifdef DEBUG
|
||||
std::cout << "Linking program..." << std::endl;
|
||||
#endif
|
||||
GLuint program_id = glCreateProgram();
|
||||
glAttachShader(program_id, vshader_id);
|
||||
glAttachShader(program_id, fshader_id);
|
||||
glLinkProgram(program_id);
|
||||
|
||||
// check linkage
|
||||
glGetProgramiv(program_id, GL_LINK_STATUS, &res);
|
||||
glGetProgramiv(program_id, GL_INFO_LOG_LENGTH,
|
||||
&info_log_length);
|
||||
if(info_log_length > 0)
|
||||
{
|
||||
char *link_error = new char[info_log_length+1];
|
||||
glGetProgramInfoLog(program_id, info_log_length,
|
||||
nullptr, link_error);
|
||||
std::string error = "Shader linking failed: ";
|
||||
error.append(link_error);
|
||||
delete link_error;
|
||||
|
||||
throw std::runtime_error(error);
|
||||
}
|
||||
|
||||
// detach
|
||||
glDetachShader(program_id, vshader_id);
|
||||
glDetachShader(program_id, fshader_id);
|
||||
|
||||
glDeleteShader(vshader_id);
|
||||
glDeleteShader(fshader_id);
|
||||
|
||||
return program_id;
|
||||
}
|
32
src/shaders.hpp
Normal file
32
src/shaders.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <GL/glew.h>
|
||||
#include <GL/gl.h>
|
||||
|
||||
GLuint load_shaders(const std::string &vertex_shader_path,
|
||||
const std::string &fragment_shader_path);
|
Loading…
Reference in New Issue
Block a user