add skybox, move assets

This commit is contained in:
Gero Müller 2017-11-03 12:00:32 +10:00
parent e16915428c
commit 0f6ebbc2d4
20 changed files with 136 additions and 68 deletions

View File

@ -8,8 +8,8 @@ if(CMAKE_BUILD_TYPE MATCHES DEBUG)
set(CMAKE_CXX_FLAGS "-Wall ${CMAKE_CXX_FLAGS}")
endif()
SET(CMAKE_C_FLAGS_RELEASE "-Ofast -msse4 -DNDEBUG")
SET(CMAKE_CXX_FLAGS_RELEASE "-Ofast -msse4 -DNDEBUG")
SET(CMAKE_C_FLAGS_RELEASE "-Ofast -msse4 -msse2 -DNDEBUG")
SET(CMAKE_CXX_FLAGS_RELEASE "-Ofast -msse4 -msse2 -DNDEBUG")
if(CMAKE_BUILD_TYPE MATCHES RELEASE)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")

View File

@ -0,0 +1,12 @@
#version 330
in vec3 TexCoord0;
out vec4 color;
uniform samplerCube tCubemap;
void main()
{
color = texture(tCubemap, TexCoord0);
}

View File

@ -0,0 +1,14 @@
#version 330
in vec3 vPosition;
uniform mat4 MVP;
out vec3 TexCoord0;
void main()
{
vec4 WVP_Pos = MVP * vec4(vPosition, 1.0);
gl_Position = WVP_Pos.xyww;
TexCoord0 = vPosition;
}

View File

@ -20,7 +20,7 @@ void main(){
// Light emission properties
// You probably want to put them as uniforms
vec3 LightColor = vec3(1,1,1);
float LightPower = 400.0f;
float LightPower = 200.0f;
// Material properties
vec3 MaterialDiffuseColor = texture( tAlbedo, UV ).rgb;

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

View File

@ -1,12 +0,0 @@
#version 330
in vec3 TexCoord0;
out vec4 FragColor;
uniform samplerCube gCubemapTexture;
void main()
{
FragColor = texture(gCubemapTexture, TexCoord0);
}

View File

@ -1,14 +0,0 @@
#version 330
in vec3 Position;
uniform mat4 gWVP;
out vec3 TexCoord0;
void main()
{
vec4 WVP_Pos = gWVP * vec4(Position, 1.0);
gl_Position = WVP_Pos.xyww;
TexCoord0 = Position;
}

View File

@ -126,8 +126,10 @@ void Application::init() {
loadShader();
glMesh.create();
glMesh.update(vPositionLoc, vNormalLoc, vTexCoordLoc);
glMesh.update(programId);
generateAsteroid();
skybox.Load("background");
}
void Application::shutdown() {
@ -214,7 +216,7 @@ void Application::generateAsteroid() {
}
if (meshProgress.begin("Generate Patched", 1)) {
mesh = polygonizer.mesh.createPatched(0.9);
mesh = polygonizer.mesh.createPatched(0.8);
}
if (meshProgress.begin("Save Patched", 1)) {
@ -253,10 +255,6 @@ void Application::loadShader() {
GLCK(
LightPosition_worldspaceloc = glGetUniformLocation(programId, "LightPosition_worldspace"))
GLCK(vPositionLoc = glGetAttribLocation(programId, "vPosition"))
GLCK(vTexCoordLoc = glGetAttribLocation(programId, "vTexCoord"))
GLCK(vNormalLoc = glGetAttribLocation(programId, "vNormal"))
GLCK(tAlbedoLoc = glGetAttribLocation(programId, "tAlbedo"))
GLCK(tNormalLoc = glGetAttribLocation(programId, "tNormal"))
GLCK(tRoughnessLoc = glGetAttribLocation(programId, "tRoughness"))
@ -281,9 +279,7 @@ void Application::renderMesh() {
glMesh.render();
}
void Application::prepareShader() {
glm::mat4 view;
void Application::updateMatrices() {
double mouseX, mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY);
@ -333,10 +329,12 @@ void Application::prepareShader() {
lastMouseX = mouseX;
lastMouseY = mouseY;
glm::mat4 perspective = glm::perspective(glm::radians(45.0f),
perspective = glm::perspective(glm::radians(45.0f),
(float) width / (float) height, 0.1f, 100.0f);
glm::mat4 mvp = perspective * view * modelTrafo;
mvp = perspective * view * modelTrafo;
}
void Application::prepareShader() {
GLCK(glUseProgram(programId))
if (MVPloc >= 0) {
GLCK(glUniformMatrix4fv(MVPloc, 1, GL_FALSE, glm::value_ptr(mvp)))
@ -421,7 +419,7 @@ void Application::gui() {
ImGui::Begin("Rendering");
if (ImGui::Button("Reload Shader")) {
loadShader();
glMesh.update(vPositionLoc, vNormalLoc, vTexCoordLoc);
glMesh.update(programId);
}
if (ImGui::Button("Wireframe"))
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
@ -458,9 +456,10 @@ void Application::run() {
uploadTexture();
textureUploadRequest = false;
}
updateMatrices();
prepareShader();
renderMesh();
skybox.Render(mvp);
ImGui::Render();
glfwSwapBuffers(window);

View File

@ -4,6 +4,7 @@
#include "Polygoniser.h"
#include "TextureMapper.h"
#include "ProgressMonitor.h"
#include "SkyBox.h"
#include "gl.h"
#include <glm/mat4x4.hpp>
@ -54,6 +55,11 @@ class Application {
bool meshUploadRequest, textureUploadRequest;
void toogleFpsMode();
glm::mat4 perspective, view, mvp;
SkyBox skybox;
public:
Application();
@ -86,6 +92,7 @@ public:
void renderMesh();
void updateMatrices();
void prepareShader();
void gui();

View File

@ -310,7 +310,6 @@ Mesh Mesh::createPatched(float threshold) const {
}
mesh.indices.push_back(idx);
}
printf("\n");
}
return mesh;
}

View File

@ -15,7 +15,7 @@ GLuint loadProgram(const string &name) {
// Read the Vertex Shader code from the file
string VertexShaderCode;
string vertex_file_path = "shader/" + name + ".vertex.glsl";
string vertex_file_path = "assets/shader/" + name + ".vertex.glsl";
ifstream VertexShaderStream(vertex_file_path, ios::in);
if (VertexShaderStream.is_open()) {
string Line = "";
@ -29,7 +29,7 @@ GLuint loadProgram(const string &name) {
// Read the Fragment Shader code from the file
string FragmentShaderCode;
string fragment_file_path = "shader/" + name + ".fragment.glsl";
string fragment_file_path = "assets/shader/" + name + ".fragment.glsl";
ifstream FragmentShaderStream(fragment_file_path, ios::in);
if (FragmentShaderStream.is_open()) {
string Line = "";

View File

@ -3,12 +3,13 @@
#include <glad/glad.h>
#include <stb_image.h>
#include <glm/gtc/type_ptr.hpp>
using namespace std;
SkyBox::SkyBox() :
textureId(GL_INVALID_INDEX), programId(GL_INVALID_INDEX) {
glGenTextures(1, &textureId);
textureId(GL_INVALID_INDEX), programId(GL_INVALID_INDEX), textureLoc(-1) {
}
SkyBox::~SkyBox() {
@ -16,25 +17,70 @@ SkyBox::~SkyBox() {
}
bool SkyBox::Load(const std::string basename) {
glGenTextures(1, &textureId);
float extent = 10;
mesh.positions.clear();
mesh.positions.push_back(glm::vec3(-extent, -extent, -extent));
mesh.positions.push_back(glm::vec3(-extent, -extent, extent));
mesh.positions.push_back(glm::vec3(extent, -extent, extent));
mesh.positions.push_back(glm::vec3(extent, -extent, -extent));
mesh.positions.push_back(glm::vec3(-extent, extent, -extent));
mesh.positions.push_back(glm::vec3(-extent, extent, extent));
mesh.positions.push_back(glm::vec3(extent, extent, extent));
mesh.positions.push_back(glm::vec3(extent, extent, -extent));
mesh.indices.clear();
// bottom
mesh.indices.push_back(glm::uvec3(1, 0, 2));
mesh.indices.push_back(glm::uvec3(2, 0, 3));
// back
mesh.indices.push_back(glm::uvec3(5, 2, 6));
mesh.indices.push_back(glm::uvec3(1, 2, 5));
// front
mesh.indices.push_back(glm::uvec3(3, 4, 7));
mesh.indices.push_back(glm::uvec3(3, 0, 4));
// left
mesh.indices.push_back(glm::uvec3(6, 3, 7));
mesh.indices.push_back(glm::uvec3(2, 3, 6));
// right
mesh.indices.push_back(glm::uvec3(0, 1, 4));
mesh.indices.push_back(glm::uvec3(4, 1, 5));
// top
mesh.indices.push_back(glm::uvec3(5, 6, 4));
mesh.indices.push_back(glm::uvec3(6, 7, 4));
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_X, basename + "_right1.png");
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, basename + "_left2.png");
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, basename + "_top3.png");
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, basename + "_bottom4.png");
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, basename + "_front5.png");
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, basename + "_back6.png");
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_X, "assets/textures/" + basename + "_right1.jpg");
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, "assets/textures/" + basename + "_left2.jpg");
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, "assets/textures/" + basename + "_top3.jpg");
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, "assets/textures/" + basename + "_bottom4.jpg");
Load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, "assets/textures/" + basename + "_front5.jpg");
Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, "assets/textures/" + basename + "_back6.jpg");
programId = loadProgram("skybox");
GLCK(textureLoc = glGetAttribLocation(programId, "tCubemap"))
GLCK(MVPloc = glGetUniformLocation(programId, "MVP"))
glMesh.create();
glMesh.update(programId);
glMesh.upload(mesh);
return true;
}
bool SkyBox::Load(GLuint target, const std::string filename) {
int x, y, comp;
unsigned char *data = stbi_load(filename.c_str(), &x, &y, &comp, 4);
printf ("start\n");
unsigned char *data = stbi_load(filename.c_str(), &x, &y, &comp, 0);
printf ("end\n");
glTexImage2D(target, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexImage2D(target, 0, GL_RGB, x, y, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S,
@ -48,7 +94,7 @@ bool SkyBox::Load(GLuint target, const std::string filename) {
return true;
}
void SkyBox::Render() {
void SkyBox::Render(glm::mat4 mvp) {
GLint OldCullFaceMode;
glGetIntegerv(GL_CULL_FACE_MODE, &OldCullFaceMode);
GLint OldDepthFuncMode;
@ -67,13 +113,17 @@ void SkyBox::Render() {
p.SetPerspectiveProj(m_persProjInfo);
m_pSkyboxTechnique->SetWVP(p.GetWVPTrans());
m_pCubemapTex->Bind(GL_TEXTURE0);
m_pMesh->Render();
*/
GLCK(glUseProgram(programId));
mvp[3] = glm::vec4(0, 0, 0, 1);
GLCK(glUniformMatrix4fv(MVPloc, 1, GL_FALSE, glm::value_ptr(mvp)))
GLCK(glUniform1i(textureLoc, 0))
GLCK(glActiveTexture(GL_TEXTURE0))
GLCK(glBindTexture(GL_TEXTURE_CUBE_MAP, textureId))
glMesh.render();
glCullFace(OldCullFaceMode);
glDepthFunc(OldDepthFuncMode);
}
void SkyBox::Bind(GLenum unit) {
glActiveTexture(unit);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
}

View File

@ -1,9 +1,13 @@
#pragma once
#include <glad/glad.h>
#include "gl.h"
#include "Mesh.h"
#include <glm/mat4x4.hpp>
#include <string>
class SkyBox {
public:
@ -14,11 +18,13 @@ public:
bool Load(const std::string basename);
bool Load(GLuint target, const std::string filename);
void Bind(GLenum TextureUnit);
void Render(glm::mat4 mvp);
void Render();
private:
std::string fileNames[6];
GLuint textureId, programId;
GLint textureLoc, MVPloc;
Mesh mesh;
GLMesh glMesh;
};

View File

@ -3,6 +3,7 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "Mesh.h"
#include "types.h"
static bool glck(const char* call) {
@ -93,7 +94,7 @@ public:
bool bindVertexAttrib(GLuint id, GLint loc, GLuint size) const {
if (loc < 0) {
GLCK(glDisableVertexAttribArray(loc))
return false;
} else {
GLCK(glBindBuffer(GL_ARRAY_BUFFER, id))
GLCK(glEnableVertexAttribArray(loc))
@ -104,11 +105,17 @@ public:
0,// stride
(void* ) 0// array buffer offset
))
}
return true;
}
void update(int vPositionLoc, int vNormalLoc, int vTexCoordLoc) const {
}
void update(int programId) const {
int vPositionLoc, vNormalLoc, vTexCoordLoc;
GLCK(vPositionLoc = glGetAttribLocation(programId, "vPosition"))
GLCK(vTexCoordLoc = glGetAttribLocation(programId, "vTexCoord"))
GLCK(vNormalLoc = glGetAttribLocation(programId, "vNormal"))
GLCK(glBindVertexArray(vertexArrayId))
bindVertexAttrib(vPositionId, vPositionLoc, 3);