updated samples to use the new shader system

some bugfixes
This commit is contained in:
devZoGok
2021-11-10 20:23:15 +02:00
parent ec8dd7ac1e
commit 3cce34310f
12 changed files with 40 additions and 20 deletions
+2 -2
View File
@@ -75,8 +75,8 @@ int main(){
* and frame number. * and frame number.
*/ */
KeyframeChannel kcL; KeyframeChannel kcL;
kcL.animatable = leftMat; kcL.animatable = leftMat->getUniform("diffuseColor");
kcL.type = KeyframeChannel::DIFFUSE_COLOR_X; kcL.type = KeyframeChannel::UNIFORM_1;
kcL.keyframes = vector<Keyframe>({ kcL.keyframes = vector<Keyframe>({
KeyframeChannel::createKeyframe(Keyframe::LINEAR, 1, 0), KeyframeChannel::createKeyframe(Keyframe::LINEAR, 1, 0),
KeyframeChannel::createKeyframe(Keyframe::LINEAR, 0, 1) KeyframeChannel::createKeyframe(Keyframe::LINEAR, 0, 1)
+7 -2
View File
@@ -51,10 +51,9 @@ namespace vb01{
} }
void Light::initDepthMap(){ void Light::initDepthMap(){
string basePath = Root::getSingleton()->getLibPath() + "depthMap.";
glGenFramebuffers(1, &depthmapFBO); glGenFramebuffers(1, &depthmapFBO);
glBindFramebuffer(GL_FRAMEBUFFER, depthmapFBO); glBindFramebuffer(GL_FRAMEBUFFER, depthmapFBO);
string basePath = Root::getSingleton()->getLibPath() + "depthMap";
if(type == POINT){ if(type == POINT){
depthMap = new Texture(depthMapSize); depthMap = new Texture(depthMapSize);
@@ -131,8 +130,10 @@ namespace vb01{
for(Node *n : descendants){ for(Node *n : descendants){
vector<Mesh*> meshes = n->getMeshes(); vector<Mesh*> meshes = n->getMeshes();
for(Mesh *mesh : meshes){ for(Mesh *mesh : meshes){
Material *mat = mesh->getMaterial(); Material *mat = mesh->getMaterial();
if(mesh->isCastShadow() && n->isVisible() && mat && ((Material::BoolUniform*)mat->getUniform("lightingEnabled"))->value){ if(mesh->isCastShadow() && n->isVisible() && mat && ((Material::BoolUniform*)mat->getUniform("lightingEnabled"))->value){
Vector3 pos = n->localToGlobalPosition(Vector3::VEC_ZERO); Vector3 pos = n->localToGlobalPosition(Vector3::VEC_ZERO);
Quaternion rot = n->localToGlobalOrientation(Quaternion::QUAT_W); Quaternion rot = n->localToGlobalOrientation(Quaternion::QUAT_W);
@@ -140,6 +141,7 @@ namespace vb01{
if(rotAxis == Vector3::VEC_ZERO) if(rotAxis == Vector3::VEC_ZERO)
rotAxis = Vector3::VEC_I; rotAxis = Vector3::VEC_I;
mat4 model = translate(mat4(1.f), vec3(pos.x, pos.y, pos.z)); 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)); model = rotate(model, rot.getAngle(), vec3(rotAxis.x, rotAxis.y, rotAxis.z));
@@ -147,12 +149,15 @@ namespace vb01{
if(type == POINT){ 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)}; 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++){ for(int i = 0; i < 6; i++){
vec3 upVec; vec3 upVec;
if(1 < i && i < 4) if(1 < i && i < 4)
upVec = vec3(0, 0, -1); upVec = vec3(0, 0, -1);
else else
upVec = vec3(0, -1, 0); upVec = vec3(0, -1, 0);
depthMapShader->setMat4(proj * lookAt(lightPos, lightPos + dirs[i], upVec), "shadowMat[" + to_string(i) + "]"); depthMapShader->setMat4(proj * lookAt(lightPos, lightPos + dirs[i], upVec), "shadowMat[" + to_string(i) + "]");
} }
+5 -7
View File
@@ -7,8 +7,6 @@
#include "animation.h" #include "animation.h"
#include "animationController.h" #include "animationController.h"
#include "animationChannel.h" #include "animationChannel.h"
#include <ft2build.h>
#include FT_FREETYPE_H
using namespace std; using namespace std;
using namespace vb01; using namespace vb01;
@@ -49,19 +47,19 @@ int main(){
mat->addVariable("texturingEnabled", true); mat->addVariable("texturingEnabled", true);
mat->addVariable("lightingEnabled", true); mat->addVariable("lightingEnabled", true);
mat->addVariable("normalMapEnabled", true); mat->addVariable("normalMapEnabled", true);
mat->addVariable("specularMapEnabled", true); mat->addVariable("specularMapEnabled", false);
string im0[] = {TEX_PATH + "bricks.jpg"}; string im0[] = {TEX_PATH + "bricks.jpg"};
Texture *diffuseTex = new Texture(im0, 1); Texture *diffuseTex = new Texture(im0, 1);
mat->addVariable("textures[0]", diffuseTex); mat->addVariable("textures[0]", diffuseTex, false);
string im1[] = {TEX_PATH + "bricksNormal.jpg"}; string im1[] = {TEX_PATH + "bricksNormal.jpg"};
Texture *normalTex = new Texture(im1, 1, Texture::NORMAL); 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"}; string im2[] = {TEX_PATH + "bricksSpecular.jpg"};
Texture *specularTex = new Texture(im2, 1, Texture::SPECULAR); Texture *specularTex = new Texture(im2, 1, Texture::SPECULAR);
mat->addVariable("textures[2]", specularTex); mat->addVariable("textures[2]", specularTex, false);
//Setting specular parameteres //Setting specular parameteres
mat->addVariable("specularStrength", 1); 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()); Node *lightNode = new Node(Vector3::VEC_ZERO, Quaternion::QUAT_W, Vector3::VEC_IJK, "", new AnimationController());
lightNode->addLight(light); lightNode->addLight(light);
rootNode->attachChild(lightNode); 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)); lightNode->setPosition(Vector3(0, 5, -5));
/* /*
+4 -1
View File
@@ -66,17 +66,20 @@ namespace vb01{
case Uniform::TEXTURE:{ case Uniform::TEXTURE:{
TextureUniform *u = (TextureUniform*)uni; TextureUniform *u = (TextureUniform*)uni;
shader->setInt(2 * i, u->name + (u->animatable ? ".pastTexture" : "")); shader->setInt(2 * i, u->name + (u->animatable ? ".pastTexture" : ""));
shader->setBool(u->animatable, u->name + ".animated");
if(u->value->getNumFrames() > 1){ if(u->value->getNumFrames() > 1){
shader->setInt(2 * i + 1, u->name + ".nextTexture"); shader->setInt(2 * i + 1, u->name + ".nextTexture");
shader->setFloat(u->value->getMixRatio(), u->name + ".mixRatio"); shader->setFloat(u->value->getMixRatio(), u->name + ".mixRatio");
} }
u->value->update(2 * i);
++i;
break; break;
} }
} }
++i;
} }
} }
} }
-1
View File
@@ -44,7 +44,6 @@ namespace vb01{
std::string name; std::string name;
Type type; Type type;
Material *material = nullptr;
protected: protected:
virtual void animateComponent1(float val){} virtual void animateComponent1(float val){}
+1 -1
View File
@@ -162,7 +162,7 @@ namespace vb01{
shader->setBool(castShadow, "castShadow"); shader->setBool(castShadow, "castShadow");
shader->setBool(skeleton, "animated"); shader->setBool(skeleton, "animated");
shader->setBool(false, "environment"); shader->setBool(false, "environmentMapEnabled");
shader->setMat4(view, "view"); shader->setMat4(view, "view");
shader->setMat4(proj, "proj"); shader->setMat4(proj, "proj");
shader->setVec3(camPos, "camPos"); shader->setVec3(camPos, "camPos");
+1 -1
View File
@@ -45,7 +45,7 @@ int main(){
mat->addVariable("texturingEnabled", true); mat->addVariable("texturingEnabled", true);
string images[] = {TEX_PATH + "defaultTexture.jpg"}; string images[] = {TEX_PATH + "defaultTexture.jpg"};
Texture *texture = new Texture(images, 1); Texture *texture = new Texture(images, 1);
mat->addVariable("textures[0]", texture); mat->addVariable("textures[0]", texture, true);
model->setMaterial(mat); model->setMaterial(mat);
rootNode->attachChild(model); rootNode->attachChild(model);
+1 -1
View File
@@ -24,7 +24,7 @@ namespace vb01{
string Shader::getName(){ string Shader::getName(){
int dirId = path.find_last_of('/'); 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){ void Shader::initShaders(string vertShaderPath, string fragShaderPath, string geoShaderPath){
+1 -1
View File
@@ -45,7 +45,7 @@ int main(){
mat->addVariable("texturingEnabled", true); mat->addVariable("texturingEnabled", true);
string images[] = {TEX_PATH + "defaultTexture.jpg"}; string images[] = {TEX_PATH + "defaultTexture.jpg"};
Texture *texture = new Texture(images, 1); Texture *texture = new Texture(images, 1);
mat->addVariable("textures[0]", texture); mat->addVariable("textures[0]", texture, true);
model->setMaterial(mat); model->setMaterial(mat);
rootNode->attachChild(model); rootNode->attachChild(model);
+14 -1
View File
@@ -28,8 +28,10 @@ namespace vb01{
void Text::initFont(string fontPath, u16 firstChar, u16 lastChar){ void Text::initFont(string fontPath, u16 firstChar, u16 lastChar){
FT_Library ft; FT_Library ft;
FT_Face face; FT_Face face;
if(FT_Init_FreeType(&ft)) if(FT_Init_FreeType(&ft))
cout << "Couldn't init Freetype\n"; cout << "Couldn't init Freetype\n";
if(FT_New_Face(ft, fontPath.c_str(), 0, &face)) if(FT_New_Face(ft, fontPath.c_str(), 0, &face))
cout << "Could not load font\n"; cout << "Could not load font\n";
@@ -42,6 +44,7 @@ namespace vb01{
cout << "Could not load glyph\n"; cout << "Could not load glyph\n";
continue; continue;
} }
Glyph c; Glyph c;
c.ch = i; c.ch = i;
c.texture = new Texture(face, i); c.texture = new Texture(face, i);
@@ -65,14 +68,17 @@ namespace vb01{
shader->setVec3(node->getPosition(), "pos"); shader->setVec3(node->getPosition(), "pos");
Vector2 advanceOffset = Vector2::VEC_ZERO; Vector2 advanceOffset = Vector2::VEC_ZERO;
for(int i = (leftToRight ? 0 : entry.length() - 1); (leftToRight ? (i < entry.length()) : (i >= 0)); (leftToRight ? i++ : i--)){ for(int i = (leftToRight ? 0 : entry.length() - 1); (leftToRight ? (i < entry.length()) : (i >= 0)); (leftToRight ? i++ : i--)){
Glyph *glyphPtr = getGlyph(entry[i]); Glyph *glyphPtr = getGlyph(entry[i]);
if(!glyphPtr) if(!glyphPtr)
continue; continue;
Glyph glyph = *glyphPtr; Glyph glyph = *glyphPtr;
prepareGlyphs(glyph, advanceOffset); prepareGlyphs(glyph, advanceOffset);
Vector2 size = glyph.size, bearing = glyph.bearing; Vector2 size = glyph.size, bearing = glyph.bearing;
if(horizontal) if(horizontal)
advanceOffset.x += (size.x + bearing.x); advanceOffset.x += (size.x + bearing.x);
else else
@@ -85,6 +91,7 @@ namespace vb01{
Vector2 origin = Vector2(nodePos.x, nodePos.y) + (advanceOffset * scale); Vector2 origin = Vector2(nodePos.x, nodePos.y) + (advanceOffset * scale);
Vector2 size = glyph.size * scale, bearing = glyph.bearing * scale; Vector2 size = glyph.size * scale, bearing = glyph.bearing * scale;
float data[] = { float data[] = {
origin.x + bearing.x, origin.y - bearing.y, 0, 0, origin.x + bearing.x, origin.y - bearing.y, 0, 0,
origin.x + bearing.x + size.x, origin.y - bearing.y, 1, 0, origin.x + bearing.x + size.x, origin.y - bearing.y, 1, 0,
@@ -102,7 +109,11 @@ namespace vb01{
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, dataSize, data); 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); glDrawArrays(GL_TRIANGLES, 0, 6);
} }
@@ -122,9 +133,11 @@ namespace vb01{
Text::Glyph* Text::getGlyph(u16 ch){ Text::Glyph* Text::getGlyph(u16 ch){
Glyph *glyph = nullptr; Glyph *glyph = nullptr;
for(Glyph &g : characters) for(Glyph &g : characters)
if(g.ch == ch) if(g.ch == ch)
glyph = &g; glyph = &g;
return glyph; return glyph;
} }
} }
+2
View File
@@ -16,8 +16,10 @@ uniform vec4 diffuseColor;
void main(){ void main(){
vec4 diffCol; vec4 diffCol;
if(texturingEnabled){ if(texturingEnabled){
diffCol = texture(textures[0].pastTexture, texCoords); diffCol = texture(textures[0].pastTexture, texCoords);
if(textures[0].animated) if(textures[0].animated)
diffCol = mix(diffCol, texture(textures[0].nextTexture, texCoords), textures[0].mixRatio); diffCol = mix(diffCol, texture(textures[0].nextTexture, texCoords), textures[0].mixRatio);
} }
+2 -2
View File
@@ -56,10 +56,10 @@ int main(){
string frames[]{TEX_PATH + "bricks.jpg", TEX_PATH + "defaultTexture.jpg"}; string frames[]{TEX_PATH + "bricks.jpg", TEX_PATH + "defaultTexture.jpg"};
Texture *texture = new Texture(frames, 2); Texture *texture = new Texture(frames, 2);
textMat->addVariable("textures[0]", texture); textMat->addVariable("textures[0]", texture, true);
for(int i = 0; i < numTexts; i++){ 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->setLeftToRight(leftToRight[i]);
text->setScale(.5); text->setScale(.5);
text->setHorizontal(horizontal[i]); text->setHorizontal(horizontal[i]);