|
|
|
@ -15,11 +15,77 @@
@@ -15,11 +15,77 @@
|
|
|
|
|
#include <stdio.h> |
|
|
|
|
#include <cmath> |
|
|
|
|
#include <vector> |
|
|
|
|
#include <map> |
|
|
|
|
|
|
|
|
|
#include "shaders.h" |
|
|
|
|
#include "mesh.h" |
|
|
|
|
#include "stb_image.h" |
|
|
|
|
|
|
|
|
|
#define glCheckError() glCheckError_(__FILE__, __LINE__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GLenum glCheckError_(const char *file, int line) |
|
|
|
|
{ |
|
|
|
|
GLenum errorCode; |
|
|
|
|
while ((errorCode = glGetError()) != GL_NO_ERROR) |
|
|
|
|
{ |
|
|
|
|
std::string error; |
|
|
|
|
switch (errorCode) |
|
|
|
|
{ |
|
|
|
|
case GL_INVALID_ENUM: error = "INVALID_ENUM"; break; |
|
|
|
|
case GL_INVALID_VALUE: error = "INVALID_VALUE"; break; |
|
|
|
|
case GL_INVALID_OPERATION: error = "INVALID_OPERATION"; break; |
|
|
|
|
case GL_STACK_OVERFLOW: error = "STACK_OVERFLOW"; break; |
|
|
|
|
case GL_STACK_UNDERFLOW: error = "STACK_UNDERFLOW"; break; |
|
|
|
|
case GL_OUT_OF_MEMORY: error = "OUT_OF_MEMORY"; break; |
|
|
|
|
case GL_INVALID_FRAMEBUFFER_OPERATION: error = "INVALID_FRAMEBUFFER_OPERATION"; break; |
|
|
|
|
} |
|
|
|
|
std::cout << error << " | " << file << " (" << line << ")" << std::endl; |
|
|
|
|
} |
|
|
|
|
return errorCode; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Cubemap { |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
unsigned int id; |
|
|
|
|
|
|
|
|
|
Cubemap(std::vector<std::string> texture_faces) { |
|
|
|
|
|
|
|
|
|
/* faces must be provided in the order:
|
|
|
|
|
* |
|
|
|
|
* right, left, top, bottom, front, back |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
int width, height, nrChannels; |
|
|
|
|
|
|
|
|
|
glGenTextures(1, &id); |
|
|
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, id); |
|
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < texture_faces.size(); i++) { |
|
|
|
|
unsigned char *data = stbi_load(texture_faces[i].c_str(),
|
|
|
|
|
&width, &height, &nrChannels, 0); |
|
|
|
|
|
|
|
|
|
if (data) { |
|
|
|
|
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB,
|
|
|
|
|
width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); |
|
|
|
|
glCheckError(); |
|
|
|
|
stbi_image_free(data); |
|
|
|
|
} else { |
|
|
|
|
throw std::exception(); |
|
|
|
|
stbi_image_free(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, GL_CLAMP_TO_EDGE); |
|
|
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|
|
|
|
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
void printProgramLog( GLuint program ) |
|
|
|
|
{ |
|
|
|
@ -58,9 +124,16 @@ class SDLGLGLWindow {
@@ -58,9 +124,16 @@ class SDLGLGLWindow {
|
|
|
|
|
|
|
|
|
|
/* array buffers */ |
|
|
|
|
GLuint gVBO = 0; |
|
|
|
|
GLuint quadVBO = 0; |
|
|
|
|
GLuint gIBO = 0; |
|
|
|
|
GLuint gVAO = 0; |
|
|
|
|
unsigned int lightVAO; |
|
|
|
|
unsigned int quadVAO; |
|
|
|
|
unsigned int framebuffer; |
|
|
|
|
unsigned int texColourBuffer; |
|
|
|
|
unsigned int rbo; |
|
|
|
|
unsigned int texture[2]; |
|
|
|
|
unsigned int cubemap; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glm::vec3 cubePositions[10] = { |
|
|
|
@ -83,6 +156,8 @@ class SDLGLGLWindow {
@@ -83,6 +156,8 @@ class SDLGLGLWindow {
|
|
|
|
|
glm::mat4 view = glm::mat4(1.0f); |
|
|
|
|
private: |
|
|
|
|
Shader *shader; |
|
|
|
|
Shader *texShader; |
|
|
|
|
Shader *skyboxShader; |
|
|
|
|
Model *bp_model; |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
@ -115,7 +190,8 @@ class SDLGLGLWindow {
@@ -115,7 +190,8 @@ class SDLGLGLWindow {
|
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
lightPositions.push_back(glm::vec3(1.2f, 2.0f, 2.0f)); |
|
|
|
|
lightPositions.push_back(glm::vec3(5.2f, 0.0f, 3.7f)); |
|
|
|
|
lightPositions.push_back(glm::vec3(-3.5f, 0.9f, 7.3f)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void update_camera() { |
|
|
|
@ -171,55 +247,85 @@ class SDLGLGLWindow {
@@ -171,55 +247,85 @@ class SDLGLGLWindow {
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int initGL() { |
|
|
|
|
//Success flag
|
|
|
|
|
bool success = true; |
|
|
|
|
//Generate program
|
|
|
|
|
shader = new Shader("src/shaders/test.vert", "src/shaders/test.frag"); |
|
|
|
|
texShader = new Shader("src/shaders/screentexture.vert", "src/shaders/screentexture.frag"); |
|
|
|
|
skyboxShader = new Shader("src/shaders/sky.vert", "src/shaders/sky.frag"); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
//Link program
|
|
|
|
|
stbi_set_flip_vertically_on_load(true); |
|
|
|
|
|
|
|
|
|
bp_model = new Model("src/assets/objects/backpack/backpack.obj"); |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
// model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
|
|
|
|
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); |
|
|
|
|
projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); |
|
|
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST); |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
/* generate a framebuffer to draw to */ |
|
|
|
|
glGenFramebuffers(1, &framebuffer); |
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
|
|
|
|
|
|
|
|
|
glGenTextures(1, &texColourBuffer); |
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texColourBuffer); |
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800,600, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); |
|
|
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); |
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0); |
|
|
|
|
|
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColourBuffer, 0); |
|
|
|
|
|
|
|
|
|
glGenRenderbuffers(1, &rbo); |
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, rbo); |
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800,600); |
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); |
|
|
|
|
|
|
|
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { |
|
|
|
|
std::cout << "ERROR::FRAMEBUFFER:: not complete\n"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind
|
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* textures */ |
|
|
|
|
|
|
|
|
|
int width, height, nrChannels; |
|
|
|
|
stbi_set_flip_vertically_on_load(true); |
|
|
|
|
unsigned int texture[2]; |
|
|
|
|
glGenTextures(2, texture); |
|
|
|
|
unsigned char *data = stbi_load("src/assets/container.jpg", &width, &height, &nrChannels, 0); |
|
|
|
|
unsigned char *data = stbi_load("src/assets/blending_transparent_window.png", &width, &height, &nrChannels, 0); |
|
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture[0]); |
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); |
|
|
|
|
glCheckError(); |
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); |
|
|
|
|
|
|
|
|
|
glGenerateMipmap(GL_TEXTURE_2D); |
|
|
|
|
glCheckError(); |
|
|
|
|
stbi_image_free(data); |
|
|
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
throw std::exception () ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
data = stbi_load("src/assets/specular.jpg", &width, &height, &nrChannels, 0); |
|
|
|
|
/*
|
|
|
|
|
data = stbi_load("src/assets/grass.png", &width, &height, &nrChannels, 0); |
|
|
|
|
if (data) |
|
|
|
|
{ |
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture[1]); |
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); |
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); |
|
|
|
|
|
|
|
|
|
glGenerateMipmap(GL_TEXTURE_2D); |
|
|
|
|
stbi_image_free(data); |
|
|
|
@ -229,17 +335,21 @@ class SDLGLGLWindow {
@@ -229,17 +335,21 @@ class SDLGLGLWindow {
|
|
|
|
|
} else { |
|
|
|
|
throw std::exception () ; |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0); |
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture[0]); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
glActiveTexture(GL_TEXTURE1); |
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture[1]); |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
shader->use(); |
|
|
|
|
shader->setInt("material.diffusemap", 0); |
|
|
|
|
shader->setInt("material.texture_diffuse1", 0); |
|
|
|
|
|
|
|
|
|
shader->setInt("material.specularmap", 1); |
|
|
|
|
// shader->setInt("material.specularmap", 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shader->setBool("lighting_emit", false); |
|
|
|
@ -290,78 +400,132 @@ class SDLGLGLWindow {
@@ -290,78 +400,132 @@ class SDLGLGLWindow {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
//VBO data
|
|
|
|
|
/*
|
|
|
|
|
GLfloat vertexData[] = |
|
|
|
|
GLfloat quadVertices[] = |
|
|
|
|
{ |
|
|
|
|
// Vertices // Texture Coords
|
|
|
|
|
-0.5f, -0.5f, 0.0f, 0.0f, |
|
|
|
|
0.5f, -0.5f, 1.0f, 0.0f, |
|
|
|
|
0.5f, 0.5f, 1.0f, 1.0f, |
|
|
|
|
-0.5f, 0.5f, 0.0f, 1.0f |
|
|
|
|
-1.0f, -1.0f, 0.0f, 0.0f, |
|
|
|
|
-1.0f, 1.0f, 0.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, 0.0f, |
|
|
|
|
-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); |
|
|
|
|
|
|
|
|
|
//IBO data
|
|
|
|
|
// GLuint indexData[] = { 0, 1, 2, 3, 0, 2};
|
|
|
|
|
|
|
|
|
|
/*********** CUBES **************/ |
|
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &gVAO); |
|
|
|
|
glGenBuffers( 1, &gVBO ); |
|
|
|
|
|
|
|
|
|
glBindVertexArray(gVAO); |
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, gVBO); |
|
|
|
|
glBufferData( GL_ARRAY_BUFFER, sizeof cubeVertices, cubeVertices, GL_STATIC_DRAW ); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Create IBO
|
|
|
|
|
// glGenBuffers( 1, &gIBO );
|
|
|
|
|
// glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, gIBO );
|
|
|
|
|
// glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof indexData, indexData, GL_STATIC_DRAW );
|
|
|
|
|
|
|
|
|
|
GLint gVertexPos2DLocation = -1; |
|
|
|
|
GLint gtexlocation = -1; |
|
|
|
|
GLint normloc = -1; |
|
|
|
|
gVertexPos2DLocation = shader->get_attrib("LVertexPos2D"); |
|
|
|
|
gtexlocation = shader->get_attrib("texCoord"); |
|
|
|
|
normloc = shader->get_attrib( "aNormal" ); |
|
|
|
|
unsigned int gVertexPos2DLocation = shader->get_attrib("LVertexPos2D"); |
|
|
|
|
unsigned int gtexlocation = shader->get_attrib("aTexCoords"); |
|
|
|
|
unsigned int normloc = shader->get_attrib( "aNormal" ); |
|
|
|
|
|
|
|
|
|
glVertexAttribPointer( gVertexPos2DLocation, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), NULL); |
|
|
|
|
glVertexAttribPointer( normloc, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(3 * sizeof (float))); |
|
|
|
|
glVertexAttribPointer(gtexlocation, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(6 * sizeof(float))); |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
glEnableVertexAttribArray(gtexlocation); |
|
|
|
|
glEnableVertexAttribArray(gVertexPos2DLocation); |
|
|
|
|
glEnableVertexAttribArray(normloc); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
/*********** ENDCUBES **************/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********** CUBEMAPS ***************/ |
|
|
|
|
|
|
|
|
|
std::vector<std::string> faces { |
|
|
|
|
"src/assets/skybox/right.jpg", |
|
|
|
|
"src/assets/skybox/left.jpg", |
|
|
|
|
"src/assets/skybox/top.jpg", |
|
|
|
|
"src/assets/skybox/bottom.jpg", |
|
|
|
|
"src/assets/skybox/front.jpg", |
|
|
|
|
"src/assets/skybox/back.jpg", |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
stbi_set_flip_vertically_on_load(false); |
|
|
|
|
Cubemap c (faces); |
|
|
|
|
cubemap = c.id; |
|
|
|
|
|
|
|
|
|
/************ ENDCUBEMAP ***********/ |
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0); |
|
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &quadVAO); |
|
|
|
|
glGenBuffers( 1, &quadVBO ); |
|
|
|
|
|
|
|
|
|
glBindVertexArray(quadVAO); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, quadVBO); |
|
|
|
|
glBufferData( GL_ARRAY_BUFFER, sizeof quadVertices, quadVertices, GL_STATIC_DRAW ); |
|
|
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void *)0); |
|
|
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void *)(2 * sizeof (float))); |
|
|
|
|
glEnableVertexAttribArray(0); |
|
|
|
|
glEnableVertexAttribArray(1); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Create IBO
|
|
|
|
|
// glGenBuffers( 1, &gIBO );
|
|
|
|
|
// glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, gIBO );
|
|
|
|
|
// glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof indexData, indexData, GL_STATIC_DRAW );
|
|
|
|
|
|
|
|
|
|
//glBindVertexArray(gVAO);
|
|
|
|
|
|
|
|
|
|
/*********** LIGHTS ****************/ |
|
|
|
|
glGenVertexArrays(1, &lightVAO); |
|
|
|
|
glBindVertexArray(lightVAO); |
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, gVBO); |
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0); |
|
|
|
|
glBufferData( GL_ARRAY_BUFFER, sizeof cubeVertices, cubeVertices, GL_STATIC_DRAW ); |
|
|
|
|
glEnableVertexAttribArray(0); |
|
|
|
|
|
|
|
|
|
glBindVertexArray(quadVAO); |
|
|
|
|
glCheckError(); |
|
|
|
|
/*********** LIGHTS ****************/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return success; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void render() |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); |
|
|
|
|
glEnable(GL_DEPTH_TEST); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
view = glm::lookAt(camera_pos, camera_pos + camera_front, camera_up); |
|
|
|
|
|
|
|
|
|
//Clear color buffer
|
|
|
|
|
|
|
|
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); |
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
float timeVal = SDL_GetTicks() / 1000.0; |
|
|
|
|
float green = (sin(timeVal) / 2.0f) + 0.5; |
|
|
|
|
|
|
|
|
|
//Bind program
|
|
|
|
|
shader->use(); |
|
|
|
|
|
|
|
|
|
glUniform3f(glGetUniformLocation(shader->program, "objectColour"), 1.0f, 0.5f, 0.31f); |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
shader->setInt("lights[0].type", 0); |
|
|
|
|
shader->setFloat("lights[0].angle_cutoff", glm::cos(glm::radians(12.5f))); |
|
|
|
|
shader->setFloat("lights[0].outer_angle_cutoff", glm::cos(glm::radians(17.5f))); |
|
|
|
@ -374,21 +538,31 @@ class SDLGLGLWindow {
@@ -374,21 +538,31 @@ class SDLGLGLWindow {
|
|
|
|
|
shader->setVec3("lights[0].specular", glm::vec3(1.0)); |
|
|
|
|
shader->setBool("lights[0].attenuate", true); |
|
|
|
|
shader->setVec3("lights[0].attenuation", glm::vec3(1.0, 0.09, 0.032)); |
|
|
|
|
shader->setInt("num_lights", 2); |
|
|
|
|
shader->setInt("num_lights", 3); |
|
|
|
|
shader->setInt("lights[1].type", 1); |
|
|
|
|
shader->setVec3("lights[1].position", lightPositions[0]); |
|
|
|
|
|
|
|
|
|
shader->setVec3("lights[1].ambient", glm::vec3(0.3)); |
|
|
|
|
shader->setVec3("lights[1].diffuse", glm::vec3(0.8)); |
|
|
|
|
shader->setVec3("lights[1].diffuse", glm::vec3(0.8, 0.9, 0.4)); |
|
|
|
|
shader->setVec3("lights[1].specular", glm::vec3(1.0)); |
|
|
|
|
shader->setBool("lights[1].attenuate", false); |
|
|
|
|
shader->setVec3("lights[1].attenuation", glm::vec3(1.0, 0.09, 0.032)); |
|
|
|
|
|
|
|
|
|
shader->setInt("lights[2].type", 1); |
|
|
|
|
shader->setVec3("lights[2].position", lightPositions[1]); |
|
|
|
|
|
|
|
|
|
shader->setVec3("lights[2].ambient", glm::vec3(0.3)); |
|
|
|
|
shader->setVec3("lights[2].diffuse", glm::vec3(0.4, 0.92, 0.4)); |
|
|
|
|
shader->setVec3("lights[2].specular", glm::vec3(0.3, 1.0, 0.5)); |
|
|
|
|
shader->setBool("lights[2].attenuate", false); |
|
|
|
|
shader->setVec3("lights[2].attenuation", glm::vec3(1.0, 0.09, 0.032)); |
|
|
|
|
|
|
|
|
|
glUniform3f(glGetUniformLocation(shader->program, "cameraPosition"), camera_pos.x, camera_pos.y, camera_pos.z); |
|
|
|
|
shader->setVec3("material.specular", glm::vec3(0.7)); |
|
|
|
|
shader->setVec3("material.diffuse", glm::vec3(1.0, 0.7, 0.8)); |
|
|
|
|
shader->setVec3("material.ambient", glm::vec3(0.4)); |
|
|
|
|
shader->setFloat("material.shininess", 32); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
/***** Light Attenuation
|
|
|
|
|
* |
|
|
|
@ -411,49 +585,109 @@ class SDLGLGLWindow {
@@ -411,49 +585,109 @@ class SDLGLGLWindow {
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
//Set vertex data
|
|
|
|
|
glBindVertexArray(gVAO); |
|
|
|
|
shader->use(); |
|
|
|
|
|
|
|
|
|
/* set phong values */ |
|
|
|
|
shader->setBool("lighting_emit", false); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
glm::mat4 model = glm::mat4(1.0f); |
|
|
|
|
|
|
|
|
|
unsigned int loc = shader->get_uniform("model");
|
|
|
|
|
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(model)); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
loc = shader->get_uniform("view");
|
|
|
|
|
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(view)); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
loc = shader->get_uniform("projection"); |
|
|
|
|
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(projection)); |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
/** draw skybox cubemap */ |
|
|
|
|
// glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
|
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
glDepthMask(GL_FALSE); |
|
|
|
|
glCheckError(); |
|
|
|
|
skyboxShader->use(); |
|
|
|
|
glCheckError(); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
loc = skyboxShader->get_uniform("view"); |
|
|
|
|
glm::mat4 skybox_view = glm::mat4(glm::mat3(view)); |
|
|
|
|
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(skybox_view)); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
loc = skyboxShader->get_uniform("projection");
|
|
|
|
|
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(projection)); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
glBindVertexArray(gVAO); |
|
|
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap); |
|
|
|
|
glCheckError(); |
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
glDepthMask(GL_TRUE); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
printProgramLog(skyboxShader->program); |
|
|
|
|
/** Draw guitar backpack **/ |
|
|
|
|
shader->use(); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
model = glm::mat4(1.0); |
|
|
|
|
loc = shader->get_uniform("model"); |
|
|
|
|
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(model)); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
loc = shader->get_uniform("view"); |
|
|
|
|
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(view)); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
bp_model->draw(*shader); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
//*** Draw random cubes ****/
|
|
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture[0]); |
|
|
|
|
glBindVertexArray(gVAO); |
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
std::map<float, glm::vec3> sorted; |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
for (auto pos: cubePositions) { |
|
|
|
|
float dist = glm::length(camera_pos - pos); |
|
|
|
|
sorted[dist] = pos; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
for (int i = 0; i < 10; i++) { |
|
|
|
|
for(auto it = sorted.rbegin(); it != sorted.rend(); ++it) { |
|
|
|
|
|
|
|
|
|
glm::mat4 model = glm::mat4(1.0f); |
|
|
|
|
model = glm::mat4(1.0f); |
|
|
|
|
|
|
|
|
|
model = glm::translate(model, cubePositions[i]); |
|
|
|
|
model = glm::rotate(model, glm::radians( i * 20.0f + 160.0f *(float)SDL_GetTicks() / 1000.0f), glm::vec3(0.6, 0.3, 1.0)); |
|
|
|
|
model = glm::translate(model, it->second); |
|
|
|
|
// model = glm::rotate(model, glm::radians( i * 20.0f + 160.0f *(float)SDL_GetTicks() / 1000.0f), glm::vec3(0.6, 0.3, 1.0));
|
|
|
|
|
|
|
|
|
|
unsigned int loc = glGetUniformLocation(gProgramID, "model"); |
|
|
|
|
unsigned int loc = shader->get_uniform("model"); |
|
|
|
|
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(model)); |
|
|
|
|
|
|
|
|
|
loc = glGetUniformLocation(gProgramID, "view"); |
|
|
|
|
loc = shader->get_uniform("view"); |
|
|
|
|
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(view)); |
|
|
|
|
|
|
|
|
|
loc = glGetUniformLocation(gProgramID, "projection"); |
|
|
|
|
loc = shader->get_uniform("projection");
|
|
|
|
|
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(projection)); |
|
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
glBindVertexArray(lightVAO); |
|
|
|
|
shader->setBool("lighting_emit", true); |
|
|
|
|
|
|
|
|
@ -472,8 +706,24 @@ class SDLGLGLWindow {
@@ -472,8 +706,24 @@ class SDLGLGLWindow {
|
|
|
|
|
|
|
|
|
|
//Unbind program
|
|
|
|
|
//
|
|
|
|
|
glUseProgram( NULL ); |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT); |
|
|
|
|
|
|
|
|
|
texShader->use(); |
|
|
|
|
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL); |
|
|
|
|
glBindVertexArray(quadVAO); |
|
|
|
|
|
|
|
|
|
glDisable(GL_DEPTH_TEST); |
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texColourBuffer); |
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6); |
|
|
|
|
|
|
|
|
|
glCheckError(); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -491,6 +741,9 @@ class SDLGLGLWindow {
@@ -491,6 +741,9 @@ class SDLGLGLWindow {
|
|
|
|
|
gWindow = NULL; |
|
|
|
|
|
|
|
|
|
delete shader; |
|
|
|
|
delete texShader; |
|
|
|
|
delete bp_model; |
|
|
|
|
delete skyboxShader; |
|
|
|
|
|
|
|
|
|
//Quit SDL subsystems
|
|
|
|
|
SDL_Quit(); |
|
|
|
@ -498,7 +751,7 @@ class SDLGLGLWindow {
@@ -498,7 +751,7 @@ class SDLGLGLWindow {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) { |
|
|
|
|
SDLGLGLWindow cont {640, 480}; |
|
|
|
|
SDLGLGLWindow cont {800, 600}; |
|
|
|
|
|
|
|
|
|
bool quit = false; |
|
|
|
|
|
|
|
|
@ -529,6 +782,7 @@ int main(int argc, char **argv) {
@@ -529,6 +782,7 @@ int main(int argc, char **argv) {
|
|
|
|
|
mouse_mode = !mouse_mode; |
|
|
|
|
SDL_SetRelativeMouseMode(mouse_mode ? SDL_TRUE : SDL_FALSE); |
|
|
|
|
break; |
|
|
|
|
case SDL_SCANCODE_Q: |
|
|
|
|
case SDL_SCANCODE_ESCAPE: |
|
|
|
|
quit = true; |
|
|
|
|
break; |
|
|
|
@ -564,6 +818,7 @@ int main(int argc, char **argv) {
@@ -564,6 +818,7 @@ int main(int argc, char **argv) {
|
|
|
|
|
|
|
|
|
|
//Render quad
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cont.render(); |
|
|
|
|
//Update screen
|
|
|
|
|
cont.swap_window(); |
|
|
|
|