Rewrite time
This commit is contained in:
1841
legacy/glad.cpp
Normal file
1841
legacy/glad.cpp
Normal file
File diff suppressed because it is too large
Load Diff
237
legacy/main.cpp
Normal file
237
legacy/main.cpp
Normal file
@@ -0,0 +1,237 @@
|
||||
// General includes
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
// GL includes
|
||||
#include <glad/glad.hpp>
|
||||
|
||||
// SDL includes different on windows
|
||||
// the way i have it set up so i gotta
|
||||
// do it like this unfortunately
|
||||
#if _WIN32
|
||||
#include <SDL.h>
|
||||
#else
|
||||
#include <SDL2/SDL.h>
|
||||
#endif
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
// Custom includes
|
||||
#define LOGGER_DEFINITION
|
||||
#include <logger.h>
|
||||
#include "timers.h"
|
||||
#include "object.h"
|
||||
#include "shader.h"
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::cout << "-----------------------------" << std::endl;
|
||||
std::cout << "----- OpenGL Playground -----" << std::endl;
|
||||
std::cout << "-------- Version 1.0 --------" << std::endl;
|
||||
std::cout << "----- ©Benjamin Kyd 2019 ----" << std::endl;
|
||||
std::cout << "-----------------------------" << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
// Get global variables ready
|
||||
Logger logger;
|
||||
SDL_Window* window = nullptr;
|
||||
SDL_GLContext glContext;
|
||||
bool isWindowOpen = false;
|
||||
|
||||
// Initialize SDL and OpenGL
|
||||
// isWindowOpen = init(logger, window, glContext);
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
// MXAA
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
|
||||
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "4");
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
|
||||
|
||||
window = SDL_CreateWindow("OpenGL Playground V1.0",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
// 640, 480,
|
||||
1280, 720,
|
||||
SDL_WINDOW_OPENGL);
|
||||
glContext = SDL_GL_CreateContext(window);
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
|
||||
gladLoadGLLoader(SDL_GL_GetProcAddress);
|
||||
// SDL_SetRelativeMouseMode(SDL_TRUE);
|
||||
// SDL_WarpMouseInWindow(window, 0, 0);
|
||||
|
||||
isWindowOpen = true;
|
||||
|
||||
logger << LOGGER_INFO << "OpenGL and SDL initialized" << LOGGER_ENDL;
|
||||
|
||||
// Load an object into system memory
|
||||
std::vector<glm::vec3> vertices;
|
||||
std::vector<glm::vec3> normals;
|
||||
std::vector<GLushort> elements;
|
||||
|
||||
// LoadOBJ(logger, "./resources/dragon.obj", vertices, normals, elements);
|
||||
LoadOBJ(logger, "./resources/lucy.obj", vertices, normals, elements);
|
||||
// LoadOBJ(logger, "./resources/donut.obj", vertices, normals, elements);
|
||||
|
||||
std::vector<glm::vec3> toGPU;
|
||||
toGPU.insert(toGPU.end(), vertices.begin(), vertices.end());
|
||||
toGPU.insert(toGPU.end(), normals.begin(), normals.end());
|
||||
|
||||
// Generate a vertex array object
|
||||
GLuint vao;
|
||||
glGenVertexArrays(1, &vao);
|
||||
// Bind array to GPU
|
||||
glBindVertexArray(vao);
|
||||
|
||||
// Generate a vertex buffer object
|
||||
GLuint vbo;
|
||||
glGenBuffers(1, &vbo);
|
||||
// Bind buffer to the GPU
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
// Copy vertex data to the vertex buffer already on the GPU
|
||||
glBufferData(GL_ARRAY_BUFFER, toGPU.size() * sizeof(glm::vec3), &toGPU[0], GL_STATIC_DRAW);
|
||||
|
||||
// Generate another vertex buffer for the element array buffer
|
||||
GLuint ebo;
|
||||
glGenBuffers(1, &ebo);
|
||||
// Bind buffer to the GPU
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
// Copy buffer data to the buffer already on the GPU
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(GLushort), &elements[0], GL_STATIC_DRAW);
|
||||
|
||||
// Load, compile, apply and link shader programs
|
||||
Shader simpleShader{ logger };
|
||||
simpleShader.load("./resources/shaders/phong").attatch().link().use();
|
||||
|
||||
GLint posAttrib = glGetAttribLocation(simpleShader.getProgram(), "position");
|
||||
glEnableVertexAttribArray(posAttrib);
|
||||
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
GLint normalAttrib = glGetAttribLocation(simpleShader.getProgram(), "normal");
|
||||
glEnableVertexAttribArray(normalAttrib);
|
||||
glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, 0, (const void*)(vertices.size() * sizeof(glm::vec3)));
|
||||
|
||||
// Set up camera
|
||||
// Camera camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, -90.0f, 0.0f), 45.0f, 640.0f / 480.0f, 0.1f, 1000.0f);
|
||||
|
||||
|
||||
// Model matrice
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, { -170.0f, -170.0f, -170.0f });
|
||||
model = glm::rotate(model, glm::radians(-160.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
// Gets uniform for model matrice, to be used later
|
||||
GLint uniTrans = glGetUniformLocation(simpleShader.getProgram(), "model");
|
||||
glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
|
||||
|
||||
|
||||
// View matrice
|
||||
glm::mat4 view = glm::lookAt(
|
||||
glm::vec3(1.0f, 1.0f, 1.0f),
|
||||
glm::vec3(0.0f, 0.4f, 0.0f),
|
||||
glm::vec3(0.0f, 1.0f, 0.0f)
|
||||
);
|
||||
// Get uniform and send it to the GPU
|
||||
GLint uniView = glGetUniformLocation(simpleShader.getProgram(), "view");
|
||||
glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
|
||||
|
||||
// Projection matrice
|
||||
glm::mat4 proj = glm::perspective(glm::radians(45.0f), 1280.0f / 720.0f, 1.0f, 1000.0f);//camera.perspective;
|
||||
// Get uniform and send it to the GPU
|
||||
GLint uniProj = glGetUniformLocation(simpleShader.getProgram(), "proj");
|
||||
glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
|
||||
|
||||
|
||||
glm::vec3 lightPos = { -2.0f, 4.0f, -1.0f };
|
||||
|
||||
GLint uniLight = glGetUniformLocation(simpleShader.getProgram(), "lightpos");
|
||||
glUniformMatrix3fv(uniLight, 1, GL_FALSE, glm::value_ptr(lightPos));
|
||||
|
||||
simpleShader.use();
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
|
||||
SDL_Event event;
|
||||
logger << LOGGER_ENDL;
|
||||
while (isWindowOpen) {
|
||||
FPSCount(logger);
|
||||
|
||||
// Update tick (60ups)
|
||||
if (UPSTimer()) {
|
||||
const Uint8 *state = SDL_GetKeyboardState(NULL);
|
||||
|
||||
if (state[SDL_SCANCODE_Q]) {
|
||||
model = glm::rotate(model, glm::radians(-1.5f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
|
||||
}
|
||||
|
||||
if (state[SDL_SCANCODE_E]) {
|
||||
model = glm::rotate(model, glm::radians(1.5f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
|
||||
}
|
||||
|
||||
UpdateClock = SDL_GetTicks();
|
||||
}
|
||||
|
||||
// Handle events
|
||||
while (SDL_PollEvent(&event) != 0) {
|
||||
if (event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE)
|
||||
isWindowOpen = false;
|
||||
if (event.key.keysym.sym == SDLK_r) {
|
||||
simpleShader.reload();
|
||||
|
||||
posAttrib = glGetAttribLocation(simpleShader.getProgram(), "position");
|
||||
glEnableVertexAttribArray(posAttrib);
|
||||
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
normalAttrib = glGetAttribLocation(simpleShader.getProgram(), "normal");
|
||||
glEnableVertexAttribArray(normalAttrib);
|
||||
glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, 0, (const void*)(vertices.size() * sizeof(glm::vec3)));
|
||||
|
||||
uniTrans = glGetUniformLocation(simpleShader.getProgram(), "model");
|
||||
glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
|
||||
|
||||
uniView = glGetUniformLocation(simpleShader.getProgram(), "view");
|
||||
glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
|
||||
|
||||
uniProj = glGetUniformLocation(simpleShader.getProgram(), "proj");
|
||||
glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
|
||||
|
||||
uniLight = glGetUniformLocation(simpleShader.getProgram(), "lightpos");
|
||||
glUniformMatrix3fv(uniLight, 1, GL_FALSE, glm::value_ptr(lightPos));
|
||||
}
|
||||
if (event.type == SDL_MOUSEMOTION) {
|
||||
int mouseX = event.motion.xrel;
|
||||
int mouseY = event.motion.yrel;
|
||||
// camera.rot.y += mouseX * 0.5f;
|
||||
// camera.rot.x += mouseY * -0.5f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
glViewport(0, 0, 1280, 720);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
const float clear[] = {0.1f, 0.45f, 0.9f, 1.0f};
|
||||
glClearBufferfv(GL_COLOR, 0, clear);
|
||||
simpleShader.use();
|
||||
|
||||
glDrawElements(GL_TRIANGLES, elements.size(), GL_UNSIGNED_SHORT, 0);
|
||||
|
||||
// Swap
|
||||
SDL_GL_SwapWindow(window);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
1156
legacy/resources/donut.obj
Normal file
1156
legacy/resources/donut.obj
Normal file
File diff suppressed because it is too large
Load Diff
150016
legacy/resources/dragon.obj
Normal file
150016
legacy/resources/dragon.obj
Normal file
File diff suppressed because it is too large
Load Diff
150078
legacy/resources/lucy.obj
Normal file
150078
legacy/resources/lucy.obj
Normal file
File diff suppressed because it is too large
Load Diff
39
legacy/resources/shaders/phong.frag
Normal file
39
legacy/resources/shaders/phong.frag
Normal file
@@ -0,0 +1,39 @@
|
||||
#version 330
|
||||
|
||||
in vec3 Normal;
|
||||
in vec3 FragPos;
|
||||
// in vec4 FragPosLightSpace;
|
||||
|
||||
uniform vec3 lightPos;
|
||||
|
||||
out vec4 outColour;
|
||||
|
||||
vec3 viewPos = vec3(0.0, 0.0, 0.0);
|
||||
vec3 objectColour = vec3(1.0, 1.0, 1.0);
|
||||
vec3 lightColour = vec3(0.7, 0.0, 0.65);
|
||||
// vec3 lightColour = vec3(0.3, 0.85, 1.0);
|
||||
|
||||
void main() {
|
||||
vec3 normal = normalize(Normal);
|
||||
vec3 lightDir = normalize(lightPos - FragPos);
|
||||
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
vec3 diffuse = diff * lightColour;
|
||||
|
||||
|
||||
float specularStrength = 0;
|
||||
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
|
||||
vec3 specular = specularStrength * spec * lightColour;
|
||||
|
||||
|
||||
float ambientStrength = 0.4;
|
||||
vec3 ambient = ambientStrength * lightColour;
|
||||
|
||||
vec3 result = (ambient + diffuse + specular) * objectColour;
|
||||
|
||||
outColour = vec4(result, 1.0);
|
||||
}
|
||||
21
legacy/resources/shaders/phong.vert
Normal file
21
legacy/resources/shaders/phong.vert
Normal file
@@ -0,0 +1,21 @@
|
||||
#version 330
|
||||
|
||||
in vec3 position;
|
||||
in vec3 normal;
|
||||
|
||||
out vec3 Normal;
|
||||
out vec3 FragPos;
|
||||
out vec4 FragPosLightSpace;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 proj;
|
||||
|
||||
uniform mat4 lightSpaceMatrix;
|
||||
|
||||
void main() {
|
||||
gl_Position = proj * view * model * vec4(position, 1.0);
|
||||
FragPos = vec3(model * vec4(position, 1.0));
|
||||
FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.0);
|
||||
Normal = mat3(model) * normal;
|
||||
}
|
||||
7
legacy/resources/shaders/shadowmap.frag
Normal file
7
legacy/resources/shaders/shadowmap.frag
Normal file
@@ -0,0 +1,7 @@
|
||||
#version 330
|
||||
|
||||
out float fragdepth;
|
||||
|
||||
void main(){
|
||||
gl_FragDepth = gl_FragCoord.z;
|
||||
}
|
||||
10
legacy/resources/shaders/shadowmap.vert
Normal file
10
legacy/resources/shaders/shadowmap.vert
Normal file
@@ -0,0 +1,10 @@
|
||||
#version 330
|
||||
|
||||
in vec3 vertPos;
|
||||
|
||||
uniform mat4 lightSpaceMatrix;
|
||||
uniform mat4 model;
|
||||
|
||||
void main() {
|
||||
gl_Position = lightSpaceMatrix * model * vec4(vertPos, 1.0);
|
||||
}
|
||||
7
legacy/resources/shaders/simple.frag
Normal file
7
legacy/resources/shaders/simple.frag
Normal file
@@ -0,0 +1,7 @@
|
||||
#version 330
|
||||
|
||||
out vec4 outColour;
|
||||
|
||||
void main() {
|
||||
outColour = vec4(0.58, 0.61, 0.627, 1.0);
|
||||
}
|
||||
11
legacy/resources/shaders/simple.vert
Normal file
11
legacy/resources/shaders/simple.vert
Normal file
@@ -0,0 +1,11 @@
|
||||
#version 330
|
||||
|
||||
in vec3 position;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 proj;
|
||||
|
||||
void main() {
|
||||
gl_Position = proj * view * model * vec4(position, 1.0);
|
||||
}
|
||||
BIN
legacy/resources/stanford_dragon.zip
Normal file
BIN
legacy/resources/stanford_dragon.zip
Normal file
Binary file not shown.
1156
resources/donut.obj
Normal file
1156
resources/donut.obj
Normal file
File diff suppressed because it is too large
Load Diff
@@ -2,30 +2,17 @@
|
||||
|
||||
in vec3 Normal;
|
||||
in vec3 FragPos;
|
||||
in vec4 FragPosLightSpace;
|
||||
// in vec4 FragPosLightSpace;
|
||||
|
||||
uniform vec3 lightPos;
|
||||
|
||||
out vec4 outColour;
|
||||
|
||||
uniform sampler2D diffuseTexture;
|
||||
uniform sampler2D shadowMap;
|
||||
|
||||
vec3 viewPos = vec3(0.0, 0.0, 0.0);
|
||||
vec3 objectColour = vec3(1.0, 1.0, 1.0);
|
||||
vec3 lightColour = vec3(0.65, 0.65, 0.65);
|
||||
vec3 lightColour = vec3(0.7, 0.0, 0.65);
|
||||
// vec3 lightColour = vec3(0.3, 0.85, 1.0);
|
||||
|
||||
float calculateShadow(vec4 fragPosLightSpace) {
|
||||
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
|
||||
projCoords = projCoords * 0.5 + 0.5;
|
||||
float closestDepth = texture(shadowMap, projCoords.xy).r;
|
||||
float currentDepth = projCoords.z;
|
||||
float shadow = currentDepth > closestDepth ? 1.0 : 0.0;
|
||||
|
||||
return shadow;
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 normal = normalize(Normal);
|
||||
vec3 lightDir = normalize(lightPos - FragPos);
|
||||
@@ -34,7 +21,7 @@ void main() {
|
||||
vec3 diffuse = diff * lightColour;
|
||||
|
||||
|
||||
float specularStrength = 0.1;
|
||||
float specularStrength = 0;
|
||||
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
@@ -43,11 +30,10 @@ void main() {
|
||||
vec3 specular = specularStrength * spec * lightColour;
|
||||
|
||||
|
||||
float ambientStrength = 0.1;
|
||||
float ambientStrength = 0.4;
|
||||
vec3 ambient = ambientStrength * lightColour;
|
||||
|
||||
float shadow = calculateShadow(FragPosLightSpace);
|
||||
vec3 result = (ambient + (1.0 - shadow) * (diffuse + specular)) * objectColour;
|
||||
vec3 result = (ambient + diffuse + specular) * objectColour;
|
||||
|
||||
outColour = vec4(result, 1.0);
|
||||
}
|
||||
|
||||
143
src/display.cpp
Normal file
143
src/display.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
#include "display.h"
|
||||
|
||||
Display::Display(std::string name, int w, int h, Logger& logger,
|
||||
SMH_VSYNC_DISPLAY_MODE vsyncMode,
|
||||
SMH_MXAA_MODE mxaaMode) {
|
||||
|
||||
MXAAMode = mxaaMode;
|
||||
VSyncMode = vsyncMode;
|
||||
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
// MXAA
|
||||
if (mxaaMode != MXAA_DEFAULT || mxaaMode != MXAA_DISABLED) {
|
||||
int mxaaLevel = 1;
|
||||
std::string smxaaLevel = "1";
|
||||
|
||||
switch (mxaaMode) {
|
||||
case MXAA_ENABLED:
|
||||
case MXAA_2X:
|
||||
mxaaLevel = 2;
|
||||
smxaaLevel = "2";
|
||||
break;
|
||||
case MXAA_4X:
|
||||
mxaaLevel = 4;
|
||||
smxaaLevel = "4";
|
||||
break;
|
||||
case MXAA_8X:
|
||||
mxaaLevel = 8;
|
||||
smxaaLevel = "8";
|
||||
break;
|
||||
default:
|
||||
mxaaLevel = 2;
|
||||
smxaaLevel = "2";
|
||||
break;
|
||||
}
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, mxaaLevel);
|
||||
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, smxaaLevel.c_str());
|
||||
}
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
|
||||
|
||||
// Create GL window
|
||||
window = SDL_CreateWindow(name.c_str(),
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED, w, h,
|
||||
SDL_WINDOW_OPENGL);
|
||||
// Create GL context
|
||||
glContext = SDL_GL_CreateContext(window);
|
||||
|
||||
// Set VSYNC swap interval
|
||||
if (vsyncMode == VSYNC_DEFAULT || vsyncMode == VSYNC_ENABLED)
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
if (vsyncMode == VSYNC_DISABLED)
|
||||
SDL_GL_SetSwapInterval(0);
|
||||
|
||||
// Load OpenGL
|
||||
gladLoadGLLoader(SDL_GL_GetProcAddress);
|
||||
}
|
||||
|
||||
Display::Display(std::string name, int w, int h, Logger& logger,
|
||||
SMH_MXAA_MODE mxaaMode,
|
||||
SMH_VSYNC_DISPLAY_MODE vsyncMode) {
|
||||
|
||||
MXAAMode = mxaaMode;
|
||||
VSyncMode = vsyncMode;
|
||||
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
// MXAA
|
||||
if (mxaaMode != MXAA_DEFAULT || mxaaMode != MXAA_DISABLED) {
|
||||
int mxaaLevel = 1;
|
||||
std::string smxaaLevel = "1";
|
||||
|
||||
switch (mxaaMode) {
|
||||
case MXAA_ENABLED:
|
||||
case MXAA_2X:
|
||||
mxaaLevel = 2;
|
||||
smxaaLevel = "2";
|
||||
break;
|
||||
case MXAA_4X:
|
||||
mxaaLevel = 4;
|
||||
smxaaLevel = "4";
|
||||
break;
|
||||
case MXAA_8X:
|
||||
mxaaLevel = 8;
|
||||
smxaaLevel = "8";
|
||||
break;
|
||||
default:
|
||||
mxaaLevel = 2;
|
||||
smxaaLevel = "2";
|
||||
break;
|
||||
}
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, mxaaLevel);
|
||||
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, smxaaLevel.c_str());
|
||||
}
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
|
||||
|
||||
// Create GL window
|
||||
window = SDL_CreateWindow(name.c_str(),
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED, w, h,
|
||||
SDL_WINDOW_OPENGL);
|
||||
// Create GL context
|
||||
glContext = SDL_GL_CreateContext(window);
|
||||
|
||||
// Set VSYNC swap interval
|
||||
if (vsyncMode == VSYNC_DEFAULT || vsyncMode == VSYNC_ENABLED)
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
if (vsyncMode == VSYNC_DISABLED)
|
||||
SDL_GL_SetSwapInterval(0);
|
||||
|
||||
// Load OpenGL
|
||||
gladLoadGLLoader(SDL_GL_GetProcAddress);
|
||||
}
|
||||
|
||||
void Display::setName(std::string name) {
|
||||
SDL_SetWindowTitle(window, name.c_str());
|
||||
}
|
||||
|
||||
Display::~Display() {
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
55
src/display.h
Normal file
55
src/display.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef SMHENGINE_SRC_DISPLAY_H_
|
||||
#define SMHENGINE_SRC_DISPLAY_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <glad/glad.hpp>
|
||||
|
||||
// SDL includes different on windows
|
||||
// the way i have it set up so i gotta
|
||||
// do it like this unfortunately
|
||||
#if _WIN32
|
||||
#include <SDL.h>
|
||||
#else
|
||||
#include <SDL2/SDL.h>
|
||||
#endif
|
||||
|
||||
#include <logger.h>
|
||||
|
||||
typedef enum {
|
||||
VSYNC_DEFAULT,
|
||||
VSYNC_DISABLED,
|
||||
VSYNC_ENABLED,
|
||||
} SMH_VSYNC_DISPLAY_MODE;
|
||||
|
||||
typedef enum {
|
||||
MXAA_DEFAULT,
|
||||
MXAA_DISABLED,
|
||||
MXAA_ENABLED,
|
||||
MXAA_2X,
|
||||
MXAA_4X,
|
||||
MXAA_8X
|
||||
} SMH_MXAA_MODE;
|
||||
|
||||
class Display {
|
||||
public:
|
||||
Display(std::string name, int w, int h, Logger& logger,
|
||||
SMH_VSYNC_DISPLAY_MODE vsyncMode = VSYNC_DEFAULT,
|
||||
SMH_MXAA_MODE mxaaMode = MXAA_DEFAULT);
|
||||
|
||||
Display(std::string name, int w, int h, Logger& logger,
|
||||
SMH_MXAA_MODE mxaaMode = MXAA_DEFAULT,
|
||||
SMH_VSYNC_DISPLAY_MODE vsyncMode = VSYNC_DEFAULT);
|
||||
|
||||
void setName(std::string name);
|
||||
|
||||
SDL_Window* window;
|
||||
SDL_GLContext glContext;
|
||||
|
||||
SMH_VSYNC_DISPLAY_MODE VSyncMode = VSYNC_DEFAULT;
|
||||
SMH_MXAA_MODE MXAAMode = MXAA_DEFAULT;
|
||||
|
||||
virtual ~Display();
|
||||
};
|
||||
|
||||
#endif
|
||||
226
src/main.cpp
226
src/main.cpp
@@ -2,18 +2,6 @@
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
// GL includes
|
||||
#include <glad/glad.hpp>
|
||||
|
||||
// SDL includes different on windows
|
||||
// the way i have it set up so i gotta
|
||||
// do it like this unfortunately
|
||||
#if _WIN32
|
||||
#include <SDL.h>
|
||||
#else
|
||||
#include <SDL2/SDL.h>
|
||||
#endif
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
@@ -21,216 +9,14 @@
|
||||
// Custom includes
|
||||
#define LOGGER_DEFINITION
|
||||
#include <logger.h>
|
||||
#include "timers.h"
|
||||
#include "object.h"
|
||||
#include "shader.h"
|
||||
|
||||
#include "display.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::cout << "-----------------------------" << std::endl;
|
||||
std::cout << "----- OpenGL Playground -----" << std::endl;
|
||||
std::cout << "-------- Version 1.0 --------" << std::endl;
|
||||
std::cout << "----- ©Benjamin Kyd 2019 ----" << std::endl;
|
||||
std::cout << "-----------------------------" << std::endl;
|
||||
std::cout << std::endl;
|
||||
int main (int argc, char** argv) {
|
||||
Logger logger;
|
||||
Display display {"SMH Engine", 1280, 720, logger, MXAA_8X, VSYNC_ENABLED};
|
||||
|
||||
// Get global variables ready
|
||||
Logger logger;
|
||||
SDL_Window* window = nullptr;
|
||||
SDL_GLContext glContext;
|
||||
bool isWindowOpen = false;
|
||||
|
||||
|
||||
// Initialize SDL and OpenGL
|
||||
// isWindowOpen = init(logger, window, glContext);
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
// MXAA
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
|
||||
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "4");
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
|
||||
|
||||
window = SDL_CreateWindow("OpenGL Playground V1.0",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
// 640, 480,
|
||||
1280, 720,
|
||||
SDL_WINDOW_OPENGL);
|
||||
glContext = SDL_GL_CreateContext(window);
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
|
||||
gladLoadGLLoader(SDL_GL_GetProcAddress);
|
||||
// SDL_SetRelativeMouseMode(SDL_TRUE);
|
||||
// SDL_WarpMouseInWindow(window, 0, 0);
|
||||
|
||||
isWindowOpen = true;
|
||||
|
||||
logger << LOGGER_INFO << "OpenGL and SDL initialized" << LOGGER_ENDL;
|
||||
|
||||
// Load an object into system memory
|
||||
std::vector<glm::vec3> vertices;
|
||||
std::vector<glm::vec3> normals;
|
||||
std::vector<GLushort> elements;
|
||||
|
||||
// LoadOBJ(logger, "./resources/dragon.obj", vertices, normals, elements);
|
||||
LoadOBJ(logger, "./resources/lucy.obj", vertices, normals, elements);
|
||||
|
||||
std::vector<glm::vec3> toGPU;
|
||||
toGPU.insert(toGPU.end(), vertices.begin(), vertices.end());
|
||||
toGPU.insert(toGPU.end(), normals.begin(), normals.end());
|
||||
|
||||
// Generate a vertex array object
|
||||
GLuint vao;
|
||||
glGenVertexArrays(1, &vao);
|
||||
// Bind array to GPU
|
||||
glBindVertexArray(vao);
|
||||
|
||||
// Generate a vertex buffer object
|
||||
GLuint vbo;
|
||||
glGenBuffers(1, &vbo);
|
||||
// Bind buffer to the GPU
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
// Copy vertex data to the vertex buffer already on the GPU
|
||||
glBufferData(GL_ARRAY_BUFFER, toGPU.size() * sizeof(glm::vec3), &toGPU[0], GL_STATIC_DRAW);
|
||||
|
||||
// Generate another vertex buffer for the element array buffer
|
||||
GLuint ebo;
|
||||
glGenBuffers(1, &ebo);
|
||||
// Bind buffer to the GPU
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
// Copy buffer data to the buffer already on the GPU
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(GLushort), &elements[0], GL_STATIC_DRAW);
|
||||
|
||||
// Load, compile, apply and link shader programs
|
||||
Shader simpleShader{ logger };
|
||||
simpleShader.load("./resources/shaders/phong").attatch().link().use();
|
||||
|
||||
GLint posAttrib = glGetAttribLocation(simpleShader.getProgram(), "position");
|
||||
glEnableVertexAttribArray(posAttrib);
|
||||
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
GLint normalAttrib = glGetAttribLocation(simpleShader.getProgram(), "normal");
|
||||
glEnableVertexAttribArray(normalAttrib);
|
||||
glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, 0, (const void*)(vertices.size() * sizeof(glm::vec3)));
|
||||
|
||||
// Set up camera
|
||||
// Camera camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, -90.0f, 0.0f), 45.0f, 640.0f / 480.0f, 0.1f, 1000.0f);
|
||||
|
||||
|
||||
// Model matrice
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, { -170.0f, -170.0f, -170.0f });
|
||||
model = glm::rotate(model, glm::radians(-160.0f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
// Gets uniform for model matrice, to be used later
|
||||
GLint uniTrans = glGetUniformLocation(simpleShader.getProgram(), "model");
|
||||
glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
|
||||
|
||||
|
||||
// View matrice
|
||||
glm::mat4 view = glm::lookAt(
|
||||
glm::vec3(1.0f, 1.0f, 1.0f),
|
||||
glm::vec3(0.0f, 0.4f, 0.0f),
|
||||
glm::vec3(0.0f, 1.0f, 0.0f)
|
||||
);
|
||||
// Get uniform and send it to the GPU
|
||||
GLint uniView = glGetUniformLocation(simpleShader.getProgram(), "view");
|
||||
glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
|
||||
|
||||
// Projection matrice
|
||||
glm::mat4 proj = glm::perspective(glm::radians(45.0f), 1280.0f / 720.0f, 1.0f, 1000.0f);//camera.perspective;
|
||||
// Get uniform and send it to the GPU
|
||||
GLint uniProj = glGetUniformLocation(simpleShader.getProgram(), "proj");
|
||||
glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
|
||||
|
||||
|
||||
glm::vec3 lightPos = { -2.0f, 4.0f, -1.0f };
|
||||
|
||||
GLint uniLight = glGetUniformLocation(simpleShader.getProgram(), "lightpos");
|
||||
glUniformMatrix3fv(uniLight, 1, GL_FALSE, glm::value_ptr(lightPos));
|
||||
|
||||
simpleShader.use();
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
|
||||
SDL_Event event;
|
||||
logger << LOGGER_ENDL;
|
||||
while (isWindowOpen) {
|
||||
FPSCount(logger);
|
||||
|
||||
// Update tick (60ups)
|
||||
if (UPSTimer()) {
|
||||
const Uint8 *state = SDL_GetKeyboardState(NULL);
|
||||
|
||||
if (state[SDL_SCANCODE_Q]) {
|
||||
model = glm::rotate(model, glm::radians(-1.5f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
|
||||
}
|
||||
|
||||
if (state[SDL_SCANCODE_E]) {
|
||||
model = glm::rotate(model, glm::radians(1.5f), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
|
||||
}
|
||||
|
||||
UpdateClock = SDL_GetTicks();
|
||||
}
|
||||
|
||||
// Handle events
|
||||
while (SDL_PollEvent(&event) != 0) {
|
||||
if (event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE)
|
||||
isWindowOpen = false;
|
||||
if (event.key.keysym.sym == SDLK_r) {
|
||||
simpleShader.reload();
|
||||
|
||||
posAttrib = glGetAttribLocation(simpleShader.getProgram(), "position");
|
||||
glEnableVertexAttribArray(posAttrib);
|
||||
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
normalAttrib = glGetAttribLocation(simpleShader.getProgram(), "normal");
|
||||
glEnableVertexAttribArray(normalAttrib);
|
||||
glVertexAttribPointer(normalAttrib, 3, GL_FLOAT, GL_FALSE, 0, (const void*)(vertices.size() * sizeof(glm::vec3)));
|
||||
|
||||
uniTrans = glGetUniformLocation(simpleShader.getProgram(), "model");
|
||||
glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
|
||||
|
||||
uniView = glGetUniformLocation(simpleShader.getProgram(), "view");
|
||||
glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
|
||||
|
||||
uniProj = glGetUniformLocation(simpleShader.getProgram(), "proj");
|
||||
glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
|
||||
|
||||
uniLight = glGetUniformLocation(simpleShader.getProgram(), "lightpos");
|
||||
glUniformMatrix3fv(uniLight, 1, GL_FALSE, glm::value_ptr(lightPos));
|
||||
}
|
||||
if (event.type == SDL_MOUSEMOTION) {
|
||||
int mouseX = event.motion.xrel;
|
||||
int mouseY = event.motion.yrel;
|
||||
// camera.rot.y += mouseX * 0.5f;
|
||||
// camera.rot.x += mouseY * -0.5f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
glViewport(0, 0, 1280, 720);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
const float clear[] = {0.1f, 0.45f, 0.9f, 1.0f};
|
||||
glClearBufferfv(GL_COLOR, 0, clear);
|
||||
simpleShader.use();
|
||||
|
||||
glDrawElements(GL_TRIANGLES, elements.size(), GL_UNSIGNED_SHORT, 0);
|
||||
|
||||
// Swap
|
||||
SDL_GL_SwapWindow(window);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user