From: Luke Lau Date: Tue, 6 Nov 2018 22:48:27 +0000 (+0000) Subject: Skybox X-Git-Tag: cs7gv3-a3~23 X-Git-Url: http://git.lukelau.me/?p=opengl.git;a=commitdiff_plain;h=c44e69ec78367fb2957324026894aef970f2481a Skybox --- diff --git a/Makefile b/Makefile index acf1a24..30f1503 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ all: main main: - clang++ -g --std=c++11 *.cpp -L/usr/local/lib -lassimp -framework OpenGL -framework glut -framework CoreGraphics -framework CoreFoundation -lglew -o bin/main + clang++ -g --std=c++17 *.cpp -L/usr/local/lib -lassimp -framework OpenGL -framework glut -framework CoreGraphics -framework CoreFoundation -lglew -o bin/main ctags *.cpp diff --git a/image.cpp b/image.cpp new file mode 100644 index 0000000..71114b0 --- /dev/null +++ b/image.cpp @@ -0,0 +1,31 @@ +#include "image.hpp" + +Image::Image(const std::string &path) { + auto provider = CGDataProviderCreateWithFilename(path.c_str()); + std::ifstream file(path); + long magic; + file.read((char*)&magic, 8); + file.close(); + + CGImageRef ref; + + if (magic == 0x0a1a0a0d474e5089) // png magic number + ref = CGImageCreateWithPNGDataProvider(provider, nullptr, false, kCGRenderingIntentDefault); + else + ref = CGImageCreateWithJPEGDataProvider(provider, nullptr, false, kCGRenderingIntentDefault); + + _width = CGImageGetWidth(ref), _height = CGImageGetHeight(ref); + + dataRef = CGDataProviderCopyData(CGImageGetDataProvider(ref)); + CGImageRelease(ref); + +} + +unsigned char *Image::data() { + return (unsigned char*) CFDataGetBytePtr(dataRef); +} + +GLfloat Image::width() const { return _width; } +GLfloat Image::height() const { return _height; } + +Image::~Image() { CFRelease(dataRef); } diff --git a/image.hpp b/image.hpp new file mode 100644 index 0000000..b6d7920 --- /dev/null +++ b/image.hpp @@ -0,0 +1,20 @@ +#include +#include +#ifdef __APPLE__ +#include +#include +#endif + +class Image { + public: + Image(const std::string &path); + ~Image(); + unsigned char *data(); + GLfloat width() const; + GLfloat height() const; + private: + #ifdef __APPLE__ + CFDataRef dataRef; + GLfloat _width, _height; + #endif +}; diff --git a/main.cpp b/main.cpp index 742c898..613d679 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,6 @@ #include #include #include -#include -#include #include #include #ifdef __APPLE__ @@ -11,36 +9,49 @@ #include #endif #include +#include "shapes.hpp" #include #include #include #include "model.hpp" -#include "teapot.h" +#include "program.hpp" +#include "skybox.hpp" #pragma clang diagnostic ignored "-Wdeprecated-declarations" using namespace std; -GLuint pyramidVao, lightVao, teapotVao; -GLuint gradientProgId, plainProgId, normalProgId, solidProgId, textureProgId; +GLuint lightVao; + +Program *textureProg, *plainProg; +Skybox *skybox; + glm::vec3 camPos = glm::vec3(0.0f, 0.0f, 0.0f); glm::vec3 camFront = glm::vec3(0.0f, 0.0f, 1.0f); glm::vec3 camUp = glm::vec3(0.0f, 1.0f, 0.0f); float yaw = 1.57, pitch = 0; bool doScale, doRotate, doTranslate; -Model *monkeyHead, *chest; +Model *chest; glm::vec3 lightPos(0); const int WIDTH = 800, HEIGHT = 600; const float ASPECT = (float)WIDTH / (float)HEIGHT; +glm::mat4 projMat() { + return glm::perspective(glm::radians(45.f), ASPECT, 0.01f, 10000.f); +} + +glm::mat4 viewMat() { + return glm::lookAt(camPos, camPos + camFront, camUp); +} + void setProjectionAndViewUniforms(GLuint progId) { GLuint projLoc = glGetUniformLocation(progId, "projection"); - glm::mat4 proj = glm::perspective(glm::radians(45.f), ASPECT, 0.01f, 10000.f); + glm::mat4 proj = projMat(); glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj)); GLuint viewLoc = glGetUniformLocation(progId, "view"); - glm::mat4 view = glm::lookAt(camPos, camPos + camFront, camUp); + glm::mat4 view = viewMat(); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); } @@ -56,114 +67,36 @@ void setLightColorAndPos(GLuint progId, glm::vec3 lightPos, glm::vec4 lightColor } void drawLight(float d, glm::vec3 lightPos, glm::vec4 lightColor) { - glUseProgram(plainProgId); + glUseProgram(plainProg->progId); glBindVertexArray(lightVao); - setProjectionAndViewUniforms(plainProgId); + setProjectionAndViewUniforms(plainProg->progId); glm::mat4 model = glm::translate(glm::mat4(1.f), lightPos); model = glm::scale(model, glm::vec3(0.2)); - GLuint modelLoc = glGetUniformLocation(plainProgId, "model"); + GLuint modelLoc = glGetUniformLocation(plainProg->progId, "model"); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - GLuint colorLoc = glGetUniformLocation(plainProgId, "color"); + GLuint colorLoc = glGetUniformLocation(plainProg->progId, "color"); glm::vec4 color(lightColor); glUniform4fv(colorLoc, 1, glm::value_ptr(color)); glDrawArrays(GL_TRIANGLES, 0, 36); -}; - -void drawPyramids(float d, glm::vec3 lightPos, glm::vec4 lightColor) { - glUseProgram(gradientProgId); - glBindVertexArray(pyramidVao); - setProjectionAndViewUniforms(gradientProgId); - - setLightColorAndPos(gradientProgId, lightPos, lightColor); - - GLuint modelId = glGetUniformLocation(gradientProgId, "model"); - - for (int i = 0; i < 10; i++) { - - glm::mat4 model = glm::mat4(1.f); - - model = glm::translate(model, glm::vec3(sin(i * 30) * 10, 0, i * 2 - 10)); - - if (doRotate) { - model = glm::rotate(model, d * glm::radians(30.f), glm::vec3(0.f, 1.f, 0.f)); - model = glm::rotate(model, d * glm::radians(20.f), glm::vec3(1.f, 0.f, 0.f)); } - if (doScale) - model = glm::scale(model, glm::vec3(1.f, 0.7f + 0.7f * (1 + sin(d + (i + 3))), 1.f)); - - if (doTranslate) - model = glm::translate(model, glm::vec3(sin(d + (i + 1)), cos(d + (i + -3)), sin(d + (i + 4)))); - - - glUniformMatrix4fv(modelId, 1, GL_FALSE, glm::value_ptr(model)); - - glDrawArrays(GL_TRIANGLES, 0, 18); - } - -}; - -enum TeapotProjection { teapotOrtho, teapotCamera, teapotPerspStatic, teapotPerspAnimated }; - -void drawTeapot(TeapotProjection proj, bool rotate, float d, glm::vec3 lightPos, glm::vec4 lightColor) { - glUseProgram(normalProgId); - - GLuint projLoc = glGetUniformLocation(normalProgId, "projection"); - GLuint viewLoc = glGetUniformLocation(normalProgId, "view"); - switch (proj) { - case teapotCamera: - setProjectionAndViewUniforms(normalProgId); - break; - case teapotOrtho: - { - glm::mat4 proj = glm::ortho(-5.f * ASPECT, 5.f * ASPECT, -5.f, 5.f, 0.01f, 1000.f); - glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj)); - - glm::vec3 camPos(-5, 5, -5), camFront = glm::vec3(0) - camPos; - glm::mat4 view = glm::lookAt(camPos, camPos + camFront, camUp); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - } - break; - case teapotPerspStatic: - case teapotPerspAnimated: - { - float fov = glm::radians(45.f); - if (proj == teapotPerspAnimated) - fov += glm::radians(sin(d) * 30.f); - glm::mat4 proj = glm::perspective(fov, ASPECT, 0.01f, 10000.f); - glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj)); - - glm::mat4 view = glm::lookAt(glm::vec3(0, 0, -10), glm::vec3(0), camUp); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - } - break; - } - setLightColorAndPos(normalProgId, lightPos, lightColor); - glBindVertexArray(teapotVao); - GLuint modelId = glGetUniformLocation(normalProgId, "model"); - glm::mat4 model(1); - model = glm::scale(model, glm::vec3(0.3)); - if (rotate) model = glm::rotate(model, d, glm::vec3(0, 1, 0)); - glUniformMatrix4fv(modelId, 1, GL_FALSE, glm::value_ptr(model)); - - glDrawArrays(GL_TRIANGLES, 0, teapot_vertex_count); -} void display() { glClearColor(0.5, 0.5, 0.5, 1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f; - /* glm::vec3 lightPos = glm::vec3(sin(d / 10.f) * 10, 0, cos(d / 10.f) * 10); */ + skybox->draw(projMat(), viewMat()); + glm::vec4 lightColor(1, 1, 1, 1); drawLight(d, lightPos, lightColor); - glUseProgram(textureProgId); - setProjectionAndViewUniforms(textureProgId); - setLightColorAndPos(textureProgId, lightPos, lightColor); + glUseProgram(textureProg->progId); + setProjectionAndViewUniforms(textureProg->progId); + setLightColorAndPos(textureProg->progId, lightPos, lightColor); Model::Node *top = chest->find("top"); top->model = glm::translate(glm::mat4(1), glm::vec3(0, 1, -1)); @@ -187,190 +120,8 @@ void display() { glutSwapBuffers(); } -void attachShader(GLuint progId, const char* filePath, GLenum type) { - GLuint shader = glCreateShader(type); - - if (!shader) { - fprintf(stderr, "error creating shader\n"); - exit(1); - } - - ifstream file(filePath); - stringstream buffer; - buffer << file.rdbuf(); - string str = buffer.str(); - const char* contents = str.c_str(); - - glShaderSource(shader, 1, (const GLchar**)&contents, NULL); - glCompileShader(shader); - GLint success; - glGetShaderiv(shader, GL_COMPILE_STATUS, &success); - if (!success) { - GLchar log[1024]; - glGetShaderInfoLog(shader, 1024, NULL, log); - fprintf(stderr, "error: %s\n", log); - exit(1); - } - glAttachShader(progId, shader); -} - -GLuint compileShaders(char* vertexShader, char* fragmentShader) { - GLuint progId = glCreateProgram(); - - attachShader(progId, vertexShader, GL_VERTEX_SHADER); - attachShader(progId, fragmentShader, GL_FRAGMENT_SHADER); - - glLinkProgram(progId); - GLint success = 0; - glGetProgramiv(progId, GL_LINK_STATUS, &success); - if (!success) { - GLchar log[1024]; - glGetProgramInfoLog(progId, sizeof(log), NULL, log); - fprintf(stderr, "error linking: %s\n", log); - exit(1); - } - - return progId; -} - -#define BUFFER_OFFSET(i) ((char *)NULL + (i)) - -GLuint setupBuffers(glm::vec3* vertices, glm::vec3* normals, GLuint progId) { - - GLfloat colors[] = { - 0, 1, 0, 1, - 1, 0, 0, 1, - 0, 0, 1, 1, - - 0, 1, 0, 1, - 1, 0, 0, 1, - 0, 0, 1, 1, - - 0, 1, 0, 1, - 1, 0, 0, 1, - 0, 0, 1, 1, - - 0, 1, 0, 1, - 1, 0, 0, 1, - 0, 0, 1, 1, - - 0, 1, 0, 1, - 1, 0, 0, 1, - 0, 0, 1, 1, - - 0, 1, 0, 1, - 1, 0, 0, 1, - 0, 0, 1, 1 - }; - - GLuint numVerts = 18; - - GLuint vbo; - glGenBuffers(1, &vbo); - - GLuint vao; - glGenVertexArrays(1, &vao); - - GLuint posId = glGetAttribLocation(progId, "vPosition"); - GLuint colorId = glGetAttribLocation(progId, "vColor"); - GLuint normalLoc = glGetAttribLocation(progId, "vNormal"); - - GLuint vertsLen = numVerts * 3 * sizeof(GLfloat); - GLuint colorsLen = numVerts * 4 * sizeof(GLfloat); - GLuint normalLen = numVerts * 3 * sizeof(GLfloat); - - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, vertsLen + colorsLen + normalLen, NULL, GL_STATIC_DRAW); - - glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, glm::value_ptr(vertices[0])); - glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors); - glBufferSubData(GL_ARRAY_BUFFER, vertsLen + colorsLen, normalLen, glm::value_ptr(normals[0])); - - glBindVertexArray(vao); - - glEnableVertexAttribArray(posId); - glVertexAttribPointer(posId, 3, GL_FLOAT, GL_FALSE, 0, 0); - - glEnableVertexAttribArray(colorId); - glVertexAttribPointer(colorId, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(vertsLen)); - - glEnableVertexAttribArray(normalLoc); - glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(vertsLen + colorsLen)); - - return vao; -} - -vector quadToTriangles(glm::vec3 *quads) { - vector triangles(6); - triangles[0] = quads[0]; - triangles[1] = quads[1]; - triangles[2] = quads[2]; - triangles[3] = quads[2]; - triangles[4] = quads[3]; - triangles[5] = quads[0]; - return triangles; -} - -template -void append(vector &a, vector &b) { - a.insert(a.end(), b.begin(), b.end()); -}; - void setupLightBuffers(GLuint progId) { - vector vertices; - glm::vec3 front[] = { - glm::vec3(1, -1, -1), - glm::vec3(-1, -1, -1), - glm::vec3(-1, 1, -1), - glm::vec3(1, 1, -1) - }; - vector frontTriangles = quadToTriangles(front); - append(vertices, frontTriangles); - - glm::vec3 back[] = { - glm::vec3(1, 1, 1), - glm::vec3(-1, 1, 1), - glm::vec3(-1, -1, 1), - glm::vec3(1, -1, 1) - }; - vector backQuads = quadToTriangles(back); - append(vertices, backQuads); - - glm::vec3 top[] = { - glm::vec3(1, 1, -1), - glm::vec3(-1, 1, -1), - glm::vec3(-1, 1, 1), - glm::vec3(1, 1, 1) - }; - vector topTriangles = quadToTriangles(top); - append(vertices, topTriangles); - - glm::vec3 bottom[] = { - glm::vec3(1, -1, 1), - glm::vec3(-1, -1, 1), - glm::vec3(-1, -1, -1), - glm::vec3(1, -1, -1) - }; - vector bottomTriangles = quadToTriangles(bottom); - append(vertices, bottomTriangles); - - glm::vec3 left[] = { - glm::vec3(-1, 1, 1), - glm::vec3(-1, 1, -1), - glm::vec3(-1, -1, -1), - glm::vec3(-1, -1, 1) - }; - vector leftTriangles = quadToTriangles(left); - append(vertices, leftTriangles); - - glm::vec3 right[] = { - glm::vec3(1, 1, -1), - glm::vec3(1, 1, 1), - glm::vec3(1, -1, 1), - glm::vec3(1, -1, -1) - }; - vector rightTriangles = quadToTriangles(right); - append(vertices, rightTriangles); + auto vertices = cube(); GLuint verticesSize = 36 * 3 * sizeof(GLfloat); glGenVertexArrays(1, &lightVao); @@ -385,29 +136,6 @@ void setupLightBuffers(GLuint progId) { glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); } -void setupTeapotBuffers(GLuint progId) { - GLuint vbos[2]; - glGenBuffers(2, vbos); - glBindBuffer(GL_ARRAY_BUFFER, vbos[0]); - glBufferData(GL_ARRAY_BUFFER, 3 * teapot_vertex_count * sizeof(float), teapot_vertex_points, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, vbos[1]); - glBufferData(GL_ARRAY_BUFFER, 3 * teapot_vertex_count * sizeof(float), teapot_normals, GL_STATIC_DRAW); - - glGenVertexArrays(1, &teapotVao); - glBindVertexArray(teapotVao); - - GLuint posLoc = glGetAttribLocation(progId, "vPosition"); - GLuint normalLoc = glGetAttribLocation(progId, "vNormal"); - - glEnableVertexAttribArray(posLoc); - glBindBuffer(GL_ARRAY_BUFFER, vbos[0]); - glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL); - - glEnableVertexAttribArray(normalLoc); - glBindBuffer(GL_ARRAY_BUFFER, vbos[1]); - glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL); -} - void validateProgram(GLuint progId) { glValidateProgram(progId); @@ -422,70 +150,25 @@ void validateProgram(GLuint progId) { } void init() { - glm::vec3 vertices[18] = { - glm::vec3(0.0f, 1.0f, 0.0f), - glm::vec3(1.0f, -1.0f, -1.0f), - glm::vec3(-1.0f, -1.0f, -1.0f), - - glm::vec3(0.0f, 1.0f, 0.0f), - glm::vec3(-1.0f, -1.0f, 1.0f), - glm::vec3(1.0f, -1.0f, 1.0f), - - glm::vec3(0.0f, 1.0f, 0.0f), - glm::vec3(-1.0f, -1.0f, -1.0f), - glm::vec3(-1.0f, -1.0f, 1.0f), - - glm::vec3(0.0f, 1.0f, 0.0f), - glm::vec3(1.0f, -1.0f, 1.0f), - glm::vec3(1.0f, -1.0f, -1.0f), - - glm::vec3(1, -1, 1), - glm::vec3(-1, -1, 1), - glm::vec3(-1, -1, -1), - glm::vec3(-1, -1, -1), - glm::vec3(1, -1, -1), - glm::vec3(1, -1, 1) + plainProg = new Program("plainvertex.glsl", "plainfrag.glsl"); + glUseProgram(plainProg->progId); + setupLightBuffers(plainProg->progId); + validateProgram(plainProg->progId); + + textureProg = new Program("texturevertex.glsl", "texturefrag.glsl"); + glUseProgram(textureProg->progId); + + chest = new Model("models/chest.dae", *textureProg); + + std::vector faces = { + "models/skybox/right.jpg", + "models/skybox/left.jpg", + "models/skybox/top.jpg", + "models/skybox/bottom.jpg", + "models/skybox/front.jpg", + "models/skybox/back.jpg" }; - - // work out the normals - glm::vec3 normals[18]; - for (int i = 0; i < 6; i++) { - glm::vec3 a = vertices[i * 3]; - glm::vec3 b = vertices[i * 3 + 1]; - glm::vec3 c = vertices[i * 3 + 2]; - glm::vec3 u = glm::normalize(a - c); - glm::vec3 v = glm::normalize(b - c); - glm::vec3 norm = glm::normalize(glm::cross(v, u)); - for(int j = 0; j < 3; j++) { - normals[i * 3 + j] = -glm::vec3(norm); - } - } - - gradientProgId = compileShaders((char*)"vertex.glsl", (char*)"gradientfrag.glsl"); - glUseProgram(gradientProgId); - pyramidVao = setupBuffers(vertices, normals, gradientProgId); - validateProgram(gradientProgId); - - plainProgId = compileShaders((char*)"plainvertex.glsl", (char*)"plainfrag.glsl"); - glUseProgram(plainProgId); - setupLightBuffers(plainProgId); - validateProgram(plainProgId); - - normalProgId = compileShaders((char*)"vertex.glsl", (char*)"normalfrag.glsl"); - glUseProgram(normalProgId); - setupTeapotBuffers(normalProgId); - validateProgram(normalProgId); - - solidProgId = compileShaders((char*)"vertex.glsl", (char*)"solidfrag.glsl"); - glUseProgram(solidProgId); - validateProgram(solidProgId); - - textureProgId = compileShaders((char*)"texturevertex.glsl", (char*)"texturefrag.glsl"); - glUseProgram(textureProgId); - validateProgram(textureProgId); - - monkeyHead = new Model("monkeyhead_smooth.dae", solidProgId); - chest = new Model("models/chest.dae", textureProgId); + skybox = new Skybox(faces); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); diff --git a/material.cpp b/material.cpp index 7cd8d80..0e655a1 100644 --- a/material.cpp +++ b/material.cpp @@ -1,7 +1,5 @@ #include "material.hpp" -#include -#include -#include +#include "image.hpp" Material::Material(const aiMaterial &ai, GLuint progId): progId(progId) { if (ai.GetTextureCount(aiTextureType_DIFFUSE) > 0) { @@ -26,31 +24,11 @@ Material::Material(const aiMaterial &ai, GLuint progId): progId(progId) { } Material::Texture::Texture(const std::string &fileName) { - auto path = "models/" + fileName; - auto provider = CGDataProviderCreateWithFilename(path.c_str()); - std::ifstream file(path); - long magic; - file.read((char*)&magic, 8); - file.close(); - - CGImageRef ref; - - if (magic == 0x0a1a0a0d474e5089) // png magic number - ref = CGImageCreateWithPNGDataProvider(provider, nullptr, false, kCGRenderingIntentDefault); - else - ref = CGImageCreateWithJPEGDataProvider(provider, nullptr, false, kCGRenderingIntentDefault); - - auto dataRef = CGDataProviderCopyData(CGImageGetDataProvider(ref)); - auto img = (unsigned char*) CFDataGetBytePtr(dataRef); - glGenTextures(1, &texId); glBindTexture(GL_TEXTURE_2D, texId); - auto width = CGImageGetWidth(ref), height = CGImageGetHeight(ref); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img); + Image img("models/" + fileName); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width(), img.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.data()); glGenerateMipmap(GL_TEXTURE_2D); - - CGImageRelease(ref); - CFRelease(dataRef); } void Material::bind() const { diff --git a/model.cpp b/model.cpp index 8046e29..524b913 100644 --- a/model.cpp +++ b/model.cpp @@ -132,7 +132,7 @@ void Model::Node::draw( const std::vector &meshes, for (Node *child: children) child->draw(meshes, materials, m); } -Model::Model(const std::string &path, GLuint progId): progId(progId) { +Model::Model(const std::string &path, Program p): program(p) { const aiScene *scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_GenNormals); if (!scene) { @@ -142,18 +142,19 @@ Model::Model(const std::string &path, GLuint progId): progId(progId) { for (int i = 0; i < scene->mNumMeshes; i++) { const aiMesh *mesh = scene->mMeshes[i]; - meshes.push_back(Mesh(mesh, progId)); + meshes.push_back(Mesh(mesh, p.progId)); } for (unsigned int i = 0; i < scene->mNumMaterials; i++) { const aiMaterial &material = *scene->mMaterials[i]; - materials.push_back(Material(material, progId)); + materials.push_back(Material(material, p.progId)); } - root = new Node(*(scene->mRootNode), progId); + root = new Node(*(scene->mRootNode), p.progId); } void Model::draw() const { + glUseProgram(program.progId); root->draw(meshes, materials); } diff --git a/model.hpp b/model.hpp index a39c70a..de8e27f 100644 --- a/model.hpp +++ b/model.hpp @@ -8,6 +8,7 @@ #include #include #include "material.hpp" +#include "program.hpp" class Model { @@ -18,7 +19,7 @@ class Model { }; public: - Model(const std::string &path, GLuint progId); + Model(const std::string &path, Program p); void draw() const; class Node { @@ -40,7 +41,7 @@ class Model { Node* find(const std::string &name); private: - const GLuint progId; + const Program program; std::vector meshes; Node *root; diff --git a/models/skybox/back.jpg b/models/skybox/back.jpg new file mode 100755 index 0000000..470a679 Binary files /dev/null and b/models/skybox/back.jpg differ diff --git a/models/skybox/bottom.jpg b/models/skybox/bottom.jpg new file mode 100755 index 0000000..893f394 Binary files /dev/null and b/models/skybox/bottom.jpg differ diff --git a/models/skybox/front.jpg b/models/skybox/front.jpg new file mode 100755 index 0000000..4e17b77 Binary files /dev/null and b/models/skybox/front.jpg differ diff --git a/models/skybox/left.jpg b/models/skybox/left.jpg new file mode 100755 index 0000000..5750b91 Binary files /dev/null and b/models/skybox/left.jpg differ diff --git a/models/skybox/right.jpg b/models/skybox/right.jpg new file mode 100755 index 0000000..8963037 Binary files /dev/null and b/models/skybox/right.jpg differ diff --git a/models/skybox/top.jpg b/models/skybox/top.jpg new file mode 100755 index 0000000..4db3c2a Binary files /dev/null and b/models/skybox/top.jpg differ diff --git a/program.cpp b/program.cpp new file mode 100644 index 0000000..b1f2ede --- /dev/null +++ b/program.cpp @@ -0,0 +1,50 @@ +#include "program.hpp" +#include +#include +#include + +using namespace std; + +void attachShader(GLuint progId, string filePath, GLenum type) { + GLuint shader = glCreateShader(type); + + if (!shader) { + cerr << "error creating shader" << endl; + exit(1); + } + + ifstream file(filePath); + stringstream buffer; + buffer << file.rdbuf(); + string str = buffer.str(); + const char* contents = str.c_str(); + + glShaderSource(shader, 1, (const GLchar**)&contents, NULL); + glCompileShader(shader); + GLint success; + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) { + GLchar log[1024]; + glGetShaderInfoLog(shader, 1024, NULL, log); + fprintf(stderr, "error: %s\n", log); + exit(1); + } + glAttachShader(progId, shader); +} + +Program::Program(const string vertexShader, const string fragmentShader) { + progId = glCreateProgram(); + + attachShader(progId, vertexShader, GL_VERTEX_SHADER); + attachShader(progId, fragmentShader, GL_FRAGMENT_SHADER); + + glLinkProgram(progId); + GLint success = 0; + glGetProgramiv(progId, GL_LINK_STATUS, &success); + if (!success) { + GLchar log[1024]; + glGetProgramInfoLog(progId, sizeof(log), NULL, log); + fprintf(stderr, "error linking: %s\n", log); + exit(1); + } +} diff --git a/program.hpp b/program.hpp new file mode 100644 index 0000000..0436338 --- /dev/null +++ b/program.hpp @@ -0,0 +1,10 @@ +#ifndef PROGRAM_HPP +#define PROGRAM_HPP +#include +#include +class Program { + public: + Program(const std::string vert, const std::string frag); + GLuint progId; +}; +#endif diff --git a/shapes.hpp b/shapes.hpp new file mode 100644 index 0000000..69520aa --- /dev/null +++ b/shapes.hpp @@ -0,0 +1,84 @@ +#include +#define GLM_FORCE_PURE +#include + +using namespace std; + +constexpr array quadToTriangles(const array quads) { + return { + quads[0], + quads[1], + quads[2], + quads[2], + quads[3], + quads[0] + }; +} + +constexpr const glm::vec3 foo() { + constexpr glm::vec3 foo(1,2,3); + return foo; +} + +constexpr array cube() { + int i = 0; + array vertices; + + const array front = { + glm::vec3(1, -1, -1), + glm::vec3(-1, -1, -1), + glm::vec3(-1, 1, -1), + glm::vec3(1, 1, -1) + }; + + for (auto v: quadToTriangles(front)) + vertices[i++] = v; + + const array back = { + glm::vec3(1, 1, 1), + glm::vec3(-1, 1, 1), + glm::vec3(-1, -1, 1), + glm::vec3(1, -1, 1) + }; + for (auto v: quadToTriangles(back)) + vertices[i++] = v; + + + const array top = { + glm::vec3(1, 1, -1), + glm::vec3(-1, 1, -1), + glm::vec3(-1, 1, 1), + glm::vec3(1, 1, 1) + }; + for (auto v: quadToTriangles(top)) + vertices[i++] = v; + + const array bottom = { + glm::vec3(1, -1, 1), + glm::vec3(-1, -1, 1), + glm::vec3(-1, -1, -1), + glm::vec3(1, -1, -1) + }; + for (auto v: quadToTriangles(bottom)) + vertices[i++] = v; + + const array left = { + glm::vec3(-1, 1, 1), + glm::vec3(-1, 1, -1), + glm::vec3(-1, -1, -1), + glm::vec3(-1, -1, 1) + }; + for (auto v: quadToTriangles(left)) + vertices[i++] = v; + + const array right = { + glm::vec3(1, 1, -1), + glm::vec3(1, 1, 1), + glm::vec3(1, -1, 1), + glm::vec3(1, -1, -1) + }; + for (auto v: quadToTriangles(right)) + vertices[i++] = v; + + return vertices; +} diff --git a/skybox.cpp b/skybox.cpp new file mode 100644 index 0000000..bd097cb --- /dev/null +++ b/skybox.cpp @@ -0,0 +1,72 @@ +#include "shapes.hpp" +#include "skybox.hpp" +#include "image.hpp" +#include +#include + +Skybox::Skybox(const std::vector faces): program("skyboxvert.glsl", "skyboxfrag.glsl") { + glUseProgram(program.progId); + glGenTextures(1, &texId); + glBindTexture(GL_TEXTURE_CUBE_MAP, texId); + + int width, height, numChans; + for (int i = 0; i < faces.size(); i++) { + Image img(faces[i]); + + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, img.width(), img.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.data()); + } + + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_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); + + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + GLuint vbo; + glGenBuffers(1, &vbo); + + auto vertices = cube(); + + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW); + + GLuint posLoc = glGetAttribLocation(program.progId, "pos"); + glEnableVertexAttribArray(posLoc); + glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); +} +void validatePrograms(GLuint progId) { + glValidateProgram(progId); + + GLint success; + glGetProgramiv(progId, GL_VALIDATE_STATUS, &success); + if (!success) { + GLchar log[1024]; + glGetProgramInfoLog(progId, sizeof(log), NULL, log); + fprintf(stderr, "error: %s\n", log); + exit(1); + } +} + +void Skybox::draw(glm::mat4 proj, glm::mat4 view) const { + glUseProgram(program.progId); + + glDepthMask(GL_FALSE); + glDisable(GL_CULL_FACE); + glBindVertexArray(vao); + validatePrograms(program.progId); + + GLuint projLoc = glGetUniformLocation(program.progId, "projection"); + glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj)); + + GLuint viewLoc = glGetUniformLocation(program.progId, "view"); + view = glm::mat4(glm::mat3(view)); + glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); + + glBindTexture(GL_TEXTURE_CUBE_MAP, texId); + glDrawArrays(GL_TRIANGLES, 0, 36); + glEnable(GL_CULL_FACE); + glDepthMask(GL_TRUE); +} diff --git a/skybox.hpp b/skybox.hpp new file mode 100644 index 0000000..f1ac4ee --- /dev/null +++ b/skybox.hpp @@ -0,0 +1,13 @@ +#include +#include +#include +#include +#include "program.hpp" +class Skybox { + public: + Skybox(const std::vector faces); + void draw(glm::mat4 proj, glm::mat4 view) const; + private: + GLuint texId, vao; + const Program program; +}; diff --git a/skyboxfrag.glsl b/skyboxfrag.glsl new file mode 100644 index 0000000..90c7ebe --- /dev/null +++ b/skyboxfrag.glsl @@ -0,0 +1,10 @@ +#version 330 + +in vec3 texCoords; +out vec4 fragColor; + +uniform samplerCube skybox; + +void main() { + fragColor = texture(skybox, texCoords); +} diff --git a/skyboxvert.glsl b/skyboxvert.glsl new file mode 100644 index 0000000..f1ba729 --- /dev/null +++ b/skyboxvert.glsl @@ -0,0 +1,12 @@ +#version 330 + +in vec3 pos; +out vec3 texCoords; + +uniform mat4 projection; +uniform mat4 view; + +void main() { + texCoords = pos; + gl_Position = projection * view * vec4(pos, 1); +}