From 3cce34310ff4061c5f9cbedc0afb1364e23ebf5a Mon Sep 17 00:00:00 2001 From: devZoGok Date: Tue, 9 Nov 2021 21:22:17 +0200 Subject: [PATCH] updated samples to use the new shader system some bugfixes --- driverSample.cpp | 4 ++-- light.cpp | 9 +++++++-- lightSample.cpp | 12 +++++------- material.cpp | 5 ++++- material.h | 1 - mesh.cpp | 2 +- morphAnimationSample.cpp | 2 +- shader.cpp | 2 +- skeletalAnimationSample.cpp | 2 +- text.cpp | 15 ++++++++++++++- text.frag | 2 ++ textSample.cpp | 4 ++-- 12 files changed, 40 insertions(+), 20 deletions(-) diff --git a/driverSample.cpp b/driverSample.cpp index 3e9ed2e..83cc97b 100644 --- a/driverSample.cpp +++ b/driverSample.cpp @@ -75,8 +75,8 @@ int main(){ * and frame number. */ KeyframeChannel kcL; - kcL.animatable = leftMat; - kcL.type = KeyframeChannel::DIFFUSE_COLOR_X; + kcL.animatable = leftMat->getUniform("diffuseColor"); + kcL.type = KeyframeChannel::UNIFORM_1; kcL.keyframes = vector({ KeyframeChannel::createKeyframe(Keyframe::LINEAR, 1, 0), KeyframeChannel::createKeyframe(Keyframe::LINEAR, 0, 1) diff --git a/light.cpp b/light.cpp index 5a70c8b..185e036 100755 --- a/light.cpp +++ b/light.cpp @@ -51,10 +51,9 @@ namespace vb01{ } void Light::initDepthMap(){ - string basePath = Root::getSingleton()->getLibPath() + "depthMap."; - glGenFramebuffers(1, &depthmapFBO); glBindFramebuffer(GL_FRAMEBUFFER, depthmapFBO); + string basePath = Root::getSingleton()->getLibPath() + "depthMap"; if(type == POINT){ depthMap = new Texture(depthMapSize); @@ -131,8 +130,10 @@ namespace vb01{ for(Node *n : descendants){ vector meshes = n->getMeshes(); + for(Mesh *mesh : meshes){ Material *mat = mesh->getMaterial(); + if(mesh->isCastShadow() && n->isVisible() && mat && ((Material::BoolUniform*)mat->getUniform("lightingEnabled"))->value){ Vector3 pos = n->localToGlobalPosition(Vector3::VEC_ZERO); Quaternion rot = n->localToGlobalOrientation(Quaternion::QUAT_W); @@ -140,6 +141,7 @@ namespace vb01{ if(rotAxis == Vector3::VEC_ZERO) rotAxis = Vector3::VEC_I; + mat4 model = translate(mat4(1.f), vec3(pos.x, pos.y, pos.z)); model = rotate(model, rot.getAngle(), vec3(rotAxis.x, rotAxis.y, rotAxis.z)); @@ -147,12 +149,15 @@ namespace vb01{ if(type == POINT){ vec3 dirs[] = {vec3(1, 0, 0), vec3(-1, 0, 0), vec3(0, 1, 0), vec3(0, -1, 0), vec3(0, 0, 1), vec3(0, 0, -1)}; + for(int i = 0; i < 6; i++){ vec3 upVec; + if(1 < i && i < 4) upVec = vec3(0, 0, -1); else upVec = vec3(0, -1, 0); + depthMapShader->setMat4(proj * lookAt(lightPos, lightPos + dirs[i], upVec), "shadowMat[" + to_string(i) + "]"); } diff --git a/lightSample.cpp b/lightSample.cpp index ee1105b..5b185be 100644 --- a/lightSample.cpp +++ b/lightSample.cpp @@ -7,8 +7,6 @@ #include "animation.h" #include "animationController.h" #include "animationChannel.h" -#include -#include FT_FREETYPE_H using namespace std; using namespace vb01; @@ -49,19 +47,19 @@ int main(){ mat->addVariable("texturingEnabled", true); mat->addVariable("lightingEnabled", true); mat->addVariable("normalMapEnabled", true); - mat->addVariable("specularMapEnabled", true); + mat->addVariable("specularMapEnabled", false); string im0[] = {TEX_PATH + "bricks.jpg"}; Texture *diffuseTex = new Texture(im0, 1); - mat->addVariable("textures[0]", diffuseTex); + mat->addVariable("textures[0]", diffuseTex, false); string im1[] = {TEX_PATH + "bricksNormal.jpg"}; Texture *normalTex = new Texture(im1, 1, Texture::NORMAL); - mat->addVariable("textures[1]", normalTex); + mat->addVariable("textures[1]", normalTex, false); string im2[] = {TEX_PATH + "bricksSpecular.jpg"}; Texture *specularTex = new Texture(im2, 1, Texture::SPECULAR); - mat->addVariable("textures[2]", specularTex); + mat->addVariable("textures[2]", specularTex, false); //Setting specular parameteres mat->addVariable("specularStrength", 1); @@ -79,7 +77,7 @@ int main(){ Node *lightNode = new Node(Vector3::VEC_ZERO, Quaternion::QUAT_W, Vector3::VEC_IJK, "", new AnimationController()); lightNode->addLight(light); rootNode->attachChild(lightNode); - lightNode->setOrientation(Quaternion(.7, Vector3(1, 0, 0))); + lightNode->setOrientation(Quaternion(.0, Vector3(1, 0, 0))); lightNode->setPosition(Vector3(0, 5, -5)); /* diff --git a/material.cpp b/material.cpp index 8eef143..f073baf 100755 --- a/material.cpp +++ b/material.cpp @@ -66,17 +66,20 @@ namespace vb01{ case Uniform::TEXTURE:{ TextureUniform *u = (TextureUniform*)uni; shader->setInt(2 * i, u->name + (u->animatable ? ".pastTexture" : "")); + shader->setBool(u->animatable, u->name + ".animated"); if(u->value->getNumFrames() > 1){ shader->setInt(2 * i + 1, u->name + ".nextTexture"); shader->setFloat(u->value->getMixRatio(), u->name + ".mixRatio"); } + u->value->update(2 * i); + + ++i; break; } } - ++i; } } } diff --git a/material.h b/material.h index a45e94a..4c0b010 100755 --- a/material.h +++ b/material.h @@ -44,7 +44,6 @@ namespace vb01{ std::string name; Type type; - Material *material = nullptr; protected: virtual void animateComponent1(float val){} diff --git a/mesh.cpp b/mesh.cpp index 0a72fc4..4415794 100755 --- a/mesh.cpp +++ b/mesh.cpp @@ -162,7 +162,7 @@ namespace vb01{ shader->setBool(castShadow, "castShadow"); shader->setBool(skeleton, "animated"); - shader->setBool(false, "environment"); + shader->setBool(false, "environmentMapEnabled"); shader->setMat4(view, "view"); shader->setMat4(proj, "proj"); shader->setVec3(camPos, "camPos"); diff --git a/morphAnimationSample.cpp b/morphAnimationSample.cpp index 8588b9e..755bc10 100644 --- a/morphAnimationSample.cpp +++ b/morphAnimationSample.cpp @@ -45,7 +45,7 @@ int main(){ mat->addVariable("texturingEnabled", true); string images[] = {TEX_PATH + "defaultTexture.jpg"}; Texture *texture = new Texture(images, 1); - mat->addVariable("textures[0]", texture); + mat->addVariable("textures[0]", texture, true); model->setMaterial(mat); rootNode->attachChild(model); diff --git a/shader.cpp b/shader.cpp index 7a40292..99906cd 100755 --- a/shader.cpp +++ b/shader.cpp @@ -24,7 +24,7 @@ namespace vb01{ string Shader::getName(){ int dirId = path.find_last_of('/'); - return (dirId != -1 ? path.substr(dirId) : path); + return (dirId != -1 ? path.substr(dirId + 1) : path); } void Shader::initShaders(string vertShaderPath, string fragShaderPath, string geoShaderPath){ diff --git a/skeletalAnimationSample.cpp b/skeletalAnimationSample.cpp index 335d479..c043c8f 100644 --- a/skeletalAnimationSample.cpp +++ b/skeletalAnimationSample.cpp @@ -45,7 +45,7 @@ int main(){ mat->addVariable("texturingEnabled", true); string images[] = {TEX_PATH + "defaultTexture.jpg"}; Texture *texture = new Texture(images, 1); - mat->addVariable("textures[0]", texture); + mat->addVariable("textures[0]", texture, true); model->setMaterial(mat); rootNode->attachChild(model); diff --git a/text.cpp b/text.cpp index 3f83363..a8e142c 100755 --- a/text.cpp +++ b/text.cpp @@ -28,8 +28,10 @@ namespace vb01{ void Text::initFont(string fontPath, u16 firstChar, u16 lastChar){ FT_Library ft; FT_Face face; + if(FT_Init_FreeType(&ft)) cout << "Couldn't init Freetype\n"; + if(FT_New_Face(ft, fontPath.c_str(), 0, &face)) cout << "Could not load font\n"; @@ -42,6 +44,7 @@ namespace vb01{ cout << "Could not load glyph\n"; continue; } + Glyph c; c.ch = i; c.texture = new Texture(face, i); @@ -65,14 +68,17 @@ namespace vb01{ shader->setVec3(node->getPosition(), "pos"); Vector2 advanceOffset = Vector2::VEC_ZERO; + for(int i = (leftToRight ? 0 : entry.length() - 1); (leftToRight ? (i < entry.length()) : (i >= 0)); (leftToRight ? i++ : i--)){ Glyph *glyphPtr = getGlyph(entry[i]); + if(!glyphPtr) continue; Glyph glyph = *glyphPtr; prepareGlyphs(glyph, advanceOffset); Vector2 size = glyph.size, bearing = glyph.bearing; + if(horizontal) advanceOffset.x += (size.x + bearing.x); else @@ -85,6 +91,7 @@ namespace vb01{ Vector2 origin = Vector2(nodePos.x, nodePos.y) + (advanceOffset * scale); Vector2 size = glyph.size * scale, bearing = glyph.bearing * scale; + float data[] = { origin.x + bearing.x, origin.y - bearing.y, 0, 0, origin.x + bearing.x + size.x, origin.y - bearing.y, 1, 0, @@ -102,7 +109,11 @@ namespace vb01{ glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferSubData(GL_ARRAY_BUFFER, 0, dataSize, data); - glyph.texture->select(2); + + int id = 2; + material->getShader()->setInt(id, "textures[1].pastTexture"); + glyph.texture->select(id); + glDrawArrays(GL_TRIANGLES, 0, 6); } @@ -122,9 +133,11 @@ namespace vb01{ Text::Glyph* Text::getGlyph(u16 ch){ Glyph *glyph = nullptr; + for(Glyph &g : characters) if(g.ch == ch) glyph = &g; + return glyph; } } diff --git a/text.frag b/text.frag index 51ae864..d8ead9f 100755 --- a/text.frag +++ b/text.frag @@ -16,8 +16,10 @@ uniform vec4 diffuseColor; void main(){ vec4 diffCol; + if(texturingEnabled){ diffCol = texture(textures[0].pastTexture, texCoords); + if(textures[0].animated) diffCol = mix(diffCol, texture(textures[0].nextTexture, texCoords), textures[0].mixRatio); } diff --git a/textSample.cpp b/textSample.cpp index d5d414b..288648f 100644 --- a/textSample.cpp +++ b/textSample.cpp @@ -56,10 +56,10 @@ int main(){ string frames[]{TEX_PATH + "bricks.jpg", TEX_PATH + "defaultTexture.jpg"}; Texture *texture = new Texture(frames, 2); - textMat->addVariable("textures[0]", texture); + textMat->addVariable("textures[0]", texture, true); for(int i = 0; i < numTexts; i++){ - Text *text = new Text(FONT_PATH + fonts[i], texts[i]); + Text *text = new Text(FONT_PATH + fonts[i], texts[i], 0, 60000); text->setLeftToRight(leftToRight[i]); text->setScale(.5); text->setHorizontal(horizontal[i]);