alistair 2 years ago
parent
commit
f496d1180c
  1. 48
      data/levels/init.json
  2. 17
      data/shaders/instanced.vert
  3. 62
      data/shaders/test.frag
  4. 1
      src/defaultobjects/cubemap.cpp
  5. 2
      src/drawing.cpp
  6. 5
      src/level.cpp
  7. 43
      src/main.cpp
  8. 14
      src/mesh.cpp

48
data/levels/init.json

@ -1,12 +1,19 @@ @@ -1,12 +1,19 @@
{
"name": "init",
"staticmodels": {
"room": {
"object": "room.obj"
},
"backpack": {
"object": "backpack.obj",
"fliptextures": true,
"textures": ["ao.jpg", "diffuse.jpg", "roughness.jpg", "normal.jpg", "specular.jpg"]
},
"unnamed_6": {
"object": "unnamed_6.obj"
},
"orb": {
"object": "orb.obj"
}
},
"shaders": [
@ -18,45 +25,28 @@ @@ -18,45 +25,28 @@
"lights": [
{
"type": "point",
"position": [0,5,0],
"direction": [1,0,1],
"ambient": [0.9,0.0,0.0],
"diffuse": [0.9,0.3,0.5],
"specular": [1,1,1],
"attenuate": true,
"attenuation": [0.9,0.09,0.032]
},
{
"type": "point",
"position": [0,-4,0],
"direction": [1,0,1],
"ambient": [0.0,0.0,0.0],
"diffuse": [0.9,0.9,0.9],
"position": [8,5,0],
"direction": [0,0,0],
"ambient": [0.1,0.0,0.2],
"diffuse": [0.5,0.1,7.0],
"specular": [1,1,1],
"attenuate": true,
"attenuation": [0.9,0.09,0.032]
"attenuate": false,
"attenuation": [0.1,0.1,0.1]
}
],
"entities": [
{
"draw": {"model": "unnamed_6"},
"physics": {
"position": [1,4,1],
"orientation": [0,2,0]
}
},
{
"draw": {"model": "backpack"},
"draw": {"model": "room"},
"physics": {
"position": [1,1,1],
"orientation": [1,2,0]
"position": [0,0,0],
"orientation": [0,0,0]
}
},
{
"draw": {"model": "backpack"},
"draw": {"model": "orb"},
"physics": {
"position": [9,2,2],
"orientation": [1,0,0]
"position": [8,0,0],
"orientation": [0,0,0]
}
}
]

17
data/shaders/instanced.vert

@ -1,11 +1,13 @@ @@ -1,11 +1,13 @@
#version 330 core
layout (location = 0) in vec3 LVertexPos2D;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in vec3 offset;
layout (location = 4) in vec3 aTangent;
layout (location = 5) in mat4 modelInst;
layout (location = 3) in vec3 aTangent;
// layout (location = 4) in vec3 offset;
// layout (location = 5) in mat4 modelInst;
uniform mat4 model;
uniform mat4 view;
@ -18,7 +20,7 @@ out mat3 TBN; @@ -18,7 +20,7 @@ out mat3 TBN;
void main()
{
vec4 pos = (vec4( LVertexPos2D, 1.0) + vec4(offset, 0));
vec4 pos = (vec4( LVertexPos2D, 1.0));
vec4 ppos = projection * view * model * pos;
@ -29,8 +31,7 @@ void main() @@ -29,8 +31,7 @@ void main()
snapped.xy = floor(resolution * snapped.xy) / resolution;
snapped.xyz *= ppos.w;
// use gl_Position = ppos; to disable jiggle
gl_Position = snapped;
gl_Position = ppos;
theTexCoord = vec3(aTexCoords, 1.0);
Normal = mat3(transpose(inverse(model))) * aNormal;
@ -40,10 +41,10 @@ void main() @@ -40,10 +41,10 @@ void main()
vec3 T = normalize(vec3(model * vec4(aTangent, 0.0)));
vec3 N = normalize(vec3(model * vec4(aNormal, 0.0)));
T = normalize(T - dot(T, N) * N);
// T = normalize(T - dot(T, N) * N);
vec3 B = cross(N,T);
TBN = mat3(T, B, N);
TBN = mat3(T, B, Normal);
}

62
data/shaders/test.frag

@ -6,10 +6,15 @@ in vec3 Normal; @@ -6,10 +6,15 @@ in vec3 Normal;
in vec3 FragPos;
in mat3 TBN;
in vec3 theTexCoord;
in vec3 Position;
uniform vec3 cameraPosition;
uniform samplerCube skybox;
uniform bool flatshading = true;
uniform vec3 fogcolour = vec3(0.9, 0.9, 1.0);
uniform float fogdistance = 20;
uniform float fogheight = 0.1;
float gamma = 2.2;
@ -17,6 +22,13 @@ struct Material { @@ -17,6 +22,13 @@ struct Material {
sampler2D texture_diffuse1;
sampler2D texture_diffuse2;
sampler2D texture_diffuse3;
sampler2D texture_specular1;
sampler2D texture_specular2;
sampler2D texture_specular3;
sampler2D texture_normal1;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};
@ -84,8 +96,11 @@ specular(int i, vec3 lightDir, vec3 norm) @@ -84,8 +96,11 @@ specular(int i, vec3 lightDir, vec3 norm)
{
float spec = max(dot(normalize(cameraPosition - FragPos), reflect(-lightDir, norm)), 0.0);
spec = pow(spec, material.shininess);
vec3 specular = lights[i].specular * (spec *
vec3(texture(material.texture_specular1, vec2(theTexCoord))));
vec3 specmap = vec3(0);
if (!flatshading) {
specmap = vec3(texture(material.texture_specular1, vec2(theTexCoord)));
}
vec3 specular = lights[i].specular * (spec * specmap);
return specular;
}
@ -93,7 +108,7 @@ vec3 @@ -93,7 +108,7 @@ vec3
diffuse(int i, vec3 lightDir, vec3 norm)
{
float diff_val = max(dot(norm, lightDir), 0.0);
vec3 diffuse = lights[i].diffuse * (diff_val * material.diffuse);
vec3 diffuse = lights[i].diffuse * diff_val;
return diffuse;
}
@ -103,11 +118,14 @@ main() @@ -103,11 +118,14 @@ main()
{
vec3 endResult = vec3(0);
// normal
vec3 norm;
norm = texture(material.texture_normal1, vec2(theTexCoord)).rgb;
vec3 norm = vec3(1,1,1);
if (!flatshading) {
norm = texture(material.texture_normal1, vec2(theTexCoord)).rgb;
}
norm = norm * 2.0 - 1.0;
norm = normalize(TBN * norm);
for (int i = 0; i < num_lights; i++) {
vec3 ambient = lights[i].ambient * material.ambient;
@ -141,8 +159,42 @@ main() @@ -141,8 +159,42 @@ main()
endResult += result;
}
vec3 dist = cameraPosition - FragPos;
vec3 entrydist = (dist / dist.y) * (cameraPosition.y - fogheight);
if (cameraPosition.y > fogheight) {
dist = dist - entrydist;
} else {
// not intersect
if (FragPos.y < fogheight) {
dist = (cameraPosition - FragPos);
} else {
// intersect
dist = cameraPosition - entrydist;
}
}
float fogfactor = length(dist) * length(dist) / (fogdistance * fogdistance);
// fogfactor *= min(0.0, entrydist.y) / (min(entrydist.y, 1.0));
if (FragPos.y > fogheight && cameraPosition.y > fogheight) {
fogfactor = 0;
}
vec4 parta = (vec4(endResult, 1.0)) * texture(material.texture_diffuse1, vec2(theTexCoord));
parta.rgb += fogfactor * fogcolour;
LFragment = parta;
// LFragment.rgb = parta.rgb;
// LFragment.rbg += ((1.0 - parta.a) * skyval.rgb);
// gamma correction
// LFragment.rgb = pow(parta.rgb, vec3(1.0/gamma));
// LFragment = vec4(norm, 1);

1
src/defaultobjects/cubemap.cpp

@ -12,6 +12,7 @@ Cubemap::Cubemap(std::vector<std::string> texture_faces) { @@ -12,6 +12,7 @@ Cubemap::Cubemap(std::vector<std::string> texture_faces) {
*
* right, left, top, bottom, front, back
*/
glActiveTexture(GL_TEXTURE11);
int width, height, nrChannels;

2
src/drawing.cpp

@ -46,8 +46,8 @@ void simple_object_draw_system::draw(int i, const glm::mat4 view, const glm::ma @@ -46,8 +46,8 @@ void simple_object_draw_system::draw(int i, const glm::mat4 view, const glm::ma
void simple_object_draw_system::draw(const glm::mat4 view, const glm::mat4 projection) {
glCheckError();
shader->use();
glCheckError();
glCheckError();
int loc = shader->get_uniform("projection");

5
src/level.cpp

@ -115,8 +115,11 @@ level::level(const std::string &json_source) @@ -115,8 +115,11 @@ level::level(const std::string &json_source)
shader->use();
draw_system = simple_object_draw_system(&shaders[0]);
stbi_set_flip_vertically_on_load(true);
for (auto & [key, v] : l["staticmodels"].items()) {
stbi_set_flip_vertically_on_load(false);
if (v.count("fliptextures")) {
stbi_set_flip_vertically_on_load(v.at("fliptextures"));
}
models.emplace_back(new Model(fmt::format("{}/{}/{}", OBJ_DIR, key, v["object"])));
int mid = models.size() - 1;
model_ids.insert({key, mid});

43
src/main.cpp

@ -141,7 +141,7 @@ class SDLGLGLWindow { @@ -141,7 +141,7 @@ class SDLGLGLWindow {
SDL_GL_CONTEXT_PROFILE_CORE );
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED,
gWindow = SDL_CreateWindow( "𝔭𝔬𝔫𝔡𝔢𝔯 𝔱𝔥𝔢 𝔬𝔯𝔟", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
@ -518,9 +518,9 @@ class SDLGLGLWindow { @@ -518,9 +518,9 @@ class SDLGLGLWindow {
-1.0f, -1.0f, 0.0f, 0.0f,
};
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
//IBO data
// GLuint indexData[] = { 0, 1, 2, 3, 0, 2};
@ -560,17 +560,17 @@ class SDLGLGLWindow { @@ -560,17 +560,17 @@ class SDLGLGLWindow {
/********** CUBEMAPS ***************/
std::vector<std::string> faces {
;
stbi_set_flip_vertically_on_load(false);
Cubemap c (std::vector<std::string> {
fmt::format("{}/sky/right.png", SKYBOX_DIR),
fmt::format("{}/sky/left.png",SKYBOX_DIR),
fmt::format("{}/sky/up.png",SKYBOX_DIR),
fmt::format("{}/sky/down.png",SKYBOX_DIR),
fmt::format("{}/sky/front.png",SKYBOX_DIR),
fmt::format("{}/sky/back.png",SKYBOX_DIR),
};
stbi_set_flip_vertically_on_load(false);
Cubemap c (faces);
});
cubemap = c.id;
/************ ENDCUBEMAP ***********/
@ -627,7 +627,7 @@ class SDLGLGLWindow { @@ -627,7 +627,7 @@ class SDLGLGLWindow {
//Clear color buffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glCheckError();
@ -750,8 +750,6 @@ class SDLGLGLWindow { @@ -750,8 +750,6 @@ class SDLGLGLWindow {
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(projection));
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
running_level->draw_system->draw(view, projection);
//bp_model->draw(*shader);
// objects->draw(view, projection);
@ -778,6 +776,9 @@ class SDLGLGLWindow { @@ -778,6 +776,9 @@ class SDLGLGLWindow {
/** draw skybox cubemap */
glActiveTexture(GL_TEXTURE11);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
glCheckError();
skyboxShader->use();
@ -785,6 +786,7 @@ class SDLGLGLWindow { @@ -785,6 +786,7 @@ class SDLGLGLWindow {
glCheckError();
glCheckError();
skyboxShader->setInt("skybox", 11);
loc = skyboxShader->get_uniform("view");
glm::mat4 skybox_view = glm::mat4(glm::mat3(view));
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(skybox_view));
@ -795,7 +797,6 @@ class SDLGLGLWindow { @@ -795,7 +797,6 @@ class SDLGLGLWindow {
glCheckError();
glBindVertexArray(gVAO);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
glDrawArrays(GL_TRIANGLES, 0, 36);
glCheckError();
@ -803,6 +804,13 @@ class SDLGLGLWindow { @@ -803,6 +804,13 @@ class SDLGLGLWindow {
printProgramLog(skyboxShader->program);
// glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
running_level->draw_system->draw(view, projection);
running_level->shader->setInt("skybox", 11);
// 2. now blit multisampled buffer(s) to normal colorbuffer of intermediate FBO. Image is stored in screenTexture
glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, intermediateFBO);
@ -816,7 +824,7 @@ class SDLGLGLWindow { @@ -816,7 +824,7 @@ class SDLGLGLWindow {
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
//glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
texShader->use();
glBindVertexArray(quadVAO);
@ -885,8 +893,17 @@ int main(int argc, char **argv) { @@ -885,8 +893,17 @@ int main(int argc, char **argv) {
quit = true;
}
if (e.type == SDL_KEYDOWN) {
bool shademode = true;
const float camera_speed = 0.4f;
switch (e.key.keysym.scancode) {
case SDL_SCANCODE_X:
if ((shademode = !shademode)) {
glShadeModel(GL_FLAT);
} else {
glShadeModel(GL_SMOOTH);
}
glCheckError_("mpoo", 0);
break;
case SDL_SCANCODE_SPACE:
mouse_mode = !mouse_mode;
SDL_SetRelativeMouseMode(mouse_mode ? SDL_TRUE : SDL_FALSE);

14
src/mesh.cpp

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
#include "mesh.h"
#include "glutil.h"
#include "stb_image.h"
#include "glerror.h"
#include <assimp/postprocess.h>
@ -78,6 +79,7 @@ void Mesh::draw(Shader *shader) { @@ -78,6 +79,7 @@ void Mesh::draw(Shader *shader) {
glCheckError();
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
printProgramLog(shader->program);
glCheckError();
glBindVertexArray(0);
glCheckError();
@ -98,7 +100,7 @@ void Model::draw(Shader *shader) @@ -98,7 +100,7 @@ void Model::draw(Shader *shader)
}
void Model::load_model(std::string path) {
const aiScene *scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_OptimizeMeshes | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
const aiScene *scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
@ -196,24 +198,24 @@ unsigned int Model::texture_from_file(const char *fname, std::string directory) @@ -196,24 +198,24 @@ unsigned int Model::texture_from_file(const char *fname, std::string directory)
if (data)
{
GLenum format;
GLenum formatout;
GLenum internalformat;
if (nrComponents == 1) {
format = GL_RED;
formatout = GL_RED;
internalformat = GL_RED;
}
else if (nrComponents == 3) {
format = GL_RGB;
formatout = GL_SRGB;
internalformat = GL_RGBA;
}
else if (nrComponents == 4) {
format = GL_RGBA;
formatout = GL_SRGB_ALPHA;
internalformat = GL_RGBA;
} else {
throw std::exception();
}
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, formatout, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

Loading…
Cancel
Save