Add example code to test shaders and camera

This commit is contained in:
Evert Prants 2018-04-14 22:56:06 +03:00
parent 7d5cf856e6
commit 8f489ea7f1
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
7 changed files with 137 additions and 27 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.5)
project(universium-engine)
# Modules

8
data/shaders/skybox.frag Normal file
View File

@ -0,0 +1,8 @@
#version 130
out vec4 out_Color;
in vec3 cardial_color;
void main(void) {
out_Color = vec4(cardial_color.xyz, 1.0f);
}

15
data/shaders/skybox.vert Normal file
View File

@ -0,0 +1,15 @@
#version 130
in vec3 position;
in vec3 color;
out vec3 cardial_color;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
void main(void) {
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
cardial_color = color;
}

View File

@ -16,8 +16,9 @@
#include "Application.h"
#include "util/Log.h"
#include "Shader.h"
Application::Application()
Application::Application() : m_width(1080), m_height(720)
{
}
@ -40,8 +41,8 @@ void Application::initialize()
"Universium",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1080,
720,
m_width,
m_height,
SDL_WINDOW_OPENGL
);
@ -65,6 +66,8 @@ void Application::initialize()
return;
}
SDL_GL_MakeCurrent(m_window, m_glContext);
// Initialize GLEW
glewInit();
@ -112,6 +115,7 @@ void Application::handleEvents()
glm::vec2 mousepos = Input::getInstance().getMouseCoords();
// Handle Camera Movement Keys
if(Input::getInstance().isKeyDown(SDLK_w))
m_camera->processKeyboard(Camera_Movement::FORWARD, 0.01f);
@ -124,10 +128,22 @@ void Application::handleEvents()
if(Input::getInstance().isKeyDown(SDLK_a))
m_camera->processKeyboard(Camera_Movement::LEFT, 0.01f);
// Handle Camera Zoom
m_camera->processMouseScroll((float) Input::getInstance().getMouseWheelVertical() / 10.0f);
// Force mouse to the center of the screen
SDL_WarpMouseInWindow(m_window, m_width/2, m_height/2);
// Handle Camera Movement
m_camera->processMouseMovement(-(m_width/2 - mousepos.x), m_height/2 - mousepos.y, GL_TRUE);
// Print mouse position on click
if(Input::getInstance().isKeyPressed(SDL_BUTTON_LEFT))
std::cout << "mX: " << mousepos.x << " mY: " << mousepos.y << std::endl;
// Exit game on Esc
if(Input::getInstance().isKeyPressed(SDLK_ESCAPE))
exit();
}
@ -137,6 +153,75 @@ void Application::run()
m_now = SDL_GetPerformanceCounter();
m_last = 0;
// TEST CODE
static const GLfloat vertices[] = {
-1.0f,-1.0f,-1.0f, 0.0f, 1.0f, 1.0f, // triangle 1 : begin
-1.0f,-1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // triangle 1 : end
1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 0.0f, // triangle 2 : begin
-1.0f,-1.0f,-1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 0.0f, // triangle 2 : end
1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 1.0f,
1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 0.0f,
1.0f,-1.0f,-1.0f, 1.0f, 1.0f, 0.0f,
-1.0f,-1.0f,-1.0f, 1.0f, 1.0f, 0.0f,
-1.0f,-1.0f,-1.0f, 0.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f,-1.0f, 1.0f, 0.0f, 0.0f,
1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 0.0f,
-1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
GLuint vertexArray, vertexBuffer;
// Create VAO
glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);
// Generate 1 buffer, put the resulting identifier in vertexBuffer
glGenBuffers(1, &vertexBuffer);
// The following commands will talk about our 'vertexBuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Create the shader and link them together
Shader& skyboxShader = Shader::createShader("data/shaders/skybox.vert", "data/shaders/skybox.frag");
skyboxShader.linkShaders();
// Give the buffers to the shader
skyboxShader.setBuffers(vertexArray, vertexBuffer, 0);
// Set attribute arrays
skyboxShader.setAttribute("position", 3, GL_FALSE, 6, 0, GL_FLOAT);
skyboxShader.setAttribute("color", 3, GL_FALSE, 6, 3, GL_FLOAT);
glEnable(GL_DEPTH_TEST);
SDL_ShowCursor(SDL_DISABLE);
// END TEST CODE
while(m_run)
{
m_last = m_now;
@ -144,17 +229,30 @@ void Application::run()
handleEvents();
// Clear color buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.39f, 0.58f, 0.93f, 1.f);
deltaTime = ((m_now - m_last) / (double)SDL_GetPerformanceFrequency());
// TEST CODE
glm::vec3 cameraPos = m_camera->getPosition();
glm::mat4 projection = glm::perspective(m_camera->getFOV(), (GLfloat)m_width/(GLfloat)m_height, 0.1f, 100.0f);
glm::mat4 model = glm::scale(glm::translate(glm::mat4(1.0f), cameraPos), glm::vec3(50.0f, 50.0f, 50.0f));
skyboxShader.use();
skyboxShader.setUniform("projectionMatrix", projection);
skyboxShader.setUniform("viewMatrix", m_camera->getViewMatrix());
skyboxShader.setUniform("modelMatrix", model);
glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles -> 6 squares
// END TEST CODE
update(deltaTime);
render();
// Set background color as cornflower blue
glClearColor(0.39f, 0.58f, 0.93f, 1.f);
// Clear color buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Update window with OpenGL rendering
SDL_GL_SwapWindow(m_window);
}

View File

@ -32,6 +32,8 @@ class Application : public Singleton<Application>
friend class Singleton<Application>;
private:
int m_width, m_height;
Camera* m_camera;
SDL_Window* m_window;
SDL_Event m_event;

View File

@ -16,6 +16,8 @@
#include "Camera.h"
#include "util/Log.h"
// TODO: use Roll
Camera::Camera(glm::vec3 position, glm::vec3 up, GLfloat yaw, GLfloat pitch, GLfloat roll) :

View File

@ -87,7 +87,7 @@ Shader& Shader::createShader(const string& vertexShaderFilePath, const string& f
{
fatalError("Fragment shader failed to be created!");
}
Shader::compileShader(vertexShaderFilePath, shader->m_vertexShaderID);
Shader::compileShader(fragmentShaderFilePath, shader->m_fragmentShaderID);
@ -171,6 +171,7 @@ void Shader::setAttribute(const std::string& name,
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, size, type, normalized, stride * sizeof(GLfloat),
(void*)(offset * sizeof(GLfloat)));
m_attributes[name].size = size;
m_attributes[name].normalized = normalized;
m_attributes[name].stride = stride;
@ -285,25 +286,9 @@ void Shader::use()
if (!m_valid)
{
m_valid = true;
glLinkProgram(m_programID);
start();
GLint result;
glGetProgramiv(m_programID, GL_LINK_STATUS, &result);
if (result != GL_TRUE)
{
logError("Shader relink failed!");
GLsizei maxLength = 0;
glGetProgramiv(m_programID, GL_INFO_LOG_LENGTH, &maxLength);
char* log = new char[maxLength];
glGetProgramInfoLog(m_programID, maxLength, &maxLength, log);
std::printf("%s\n", log);
}
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);