Skip to content

Commit

Permalink
texture bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfrev0 committed Sep 9, 2023
1 parent 1fc90df commit c091961
Show file tree
Hide file tree
Showing 12 changed files with 5,728 additions and 1,254 deletions.
Binary file added models/fox/fox.bin
Binary file not shown.
1,220 changes: 0 additions & 1,220 deletions models/fox/fox.dae

This file was deleted.

2,315 changes: 2,315 additions & 0 deletions models/fox/fox.gltf

Large diffs are not rendered by default.

File renamed without changes
Binary file added models/toon-cat-free/Tex_Cat_Carrot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added models/toon-cat-free/model.bin
Binary file not shown.
Binary file removed models/toon-cat-free/model.glb
Binary file not shown.
3,385 changes: 3,385 additions & 0 deletions models/toon-cat-free/model.gltf

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/game/component/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@
using namespace std;

Material::Material(const std::string& texture_path){
std::cout<<texture_path<<std::endl;
int width, height, channels;
auto path = string(texture_path);
std::replace(path.begin(), path.end(), '\\', '/');
unsigned char* image = stbi_load(path.c_str(), &width, &height, &channels, STBI_rgb);
if(!image)
throw stbi_failure_reason();
if(width!=height)
throw "Non square images are not supported.";
throw "Non square images are not supported. or call glPixelStorei(GL_UNPACK_ALIGNMENT, 1);";
if(popcount<unsigned>(width)!=1)
throw "Non power of 2 sizes are not supported.";
throw "Non power of 2 sizes are not supported. or call glPixelStorei(GL_UNPACK_ALIGNMENT, 1);";
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
stbi_image_free(image);
// glBindTexture(GL_TEXTURE_2D, 0);
// stbi_image_free(image);
}
Material::~Material(){
glDeleteTextures(1, &textureID);
Expand Down
22 changes: 11 additions & 11 deletions src/game/entity/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ static Entity* loadFromFileImpl(
ret->addMesh(meshes[node->mMeshes[idx_mesh]]);
};
assert(!mapping.contains(node->mName.C_Str()));
cout<<ret->name<<endl;
return mapping[node->mName.C_Str()]=ret;
}

Expand Down Expand Up @@ -153,7 +152,6 @@ Entity* Entity::loadFromFile(const std::string& model_path){
mesh->bone_influences[target_vertex][influence_idx].weight=weight;
}
name2bone[bone.name]=&bone;
cout<<"Bone:"<<bone.name<<endl;
}
}

Expand Down Expand Up @@ -221,6 +219,7 @@ void Entity::draw(const Mat44& world2camera, Mat44 parent2world, std::vector<Ren
i->draw(world2camera, parent2world*local2parent(), render_q);
}

// Draw meshes
for(auto mesh:meshes){
if(mesh->bones.size()){
vector<Vec3> vertices_cur(mesh->vertices.size()), normals_cur(mesh->vertices.size());
Expand Down Expand Up @@ -253,15 +252,16 @@ void Entity::draw(const Mat44& world2camera, Mat44 parent2world, std::vector<Ren
mesh->material->getTextureID());
}
}
//bone tree
render_q.emplace_back(
world2camera*parent2world,
PrimitiveType::lines,
std::vector<Vec3>{{0,0,0},position()},
std::vector<Vec3>{},
std::vector<Vec3>{},
std::vector<std::vector<uint>>{},
unsigned(-1));

// // Draw bone tree
// render_q.emplace_back(
// world2camera*parent2world,
// PrimitiveType::lines,
// std::vector<Vec3>{{0,0,0},position()},
// std::vector<Vec3>{},
// std::vector<Vec3>{},
// std::vector<std::vector<uint>>{},
// unsigned(-1));
}

void Entity::update(float delta_time){
Expand Down
21 changes: 7 additions & 14 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <cmath>
#include <chrono>
#include <unistd.h>
#define GLM_FORCE_RADIANS
#include "GLFW/glfw3.h"
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
Expand Down Expand Up @@ -79,10 +78,6 @@ void init(){
}
prev_x=e.x;
prev_y=e.y;

[](glm::quat q){
cout<<q.w<<' '<<q.x<<' '<<q.y<<' '<<q.z<<endl;
}(camera->rotate());
});
evt::Manager<evt::MousePress>::addHandler([&](const evt::MousePress& e){
if(e.btnType==evt::MouseButtonType::left)
Expand All @@ -95,7 +90,7 @@ void init(){

auto dv = Entity::loadFromFile("models/dancing_vampire/dancing_vampire.dae");
dv->scale({0.05, 0.05, 0.05});
dv->position({-4,-2,0});
dv->position({-10,-2,0});
dv->onUpdate=[](Entity*self, float delta_time){
self->rotate_acc(glm::angleAxis(delta_time,glm::vec3{0,1,0}));
};
Expand All @@ -111,19 +106,17 @@ void init(){

// cout<<glm::quat{PI,glm::vec3{0.4,0.5,0.1}}.w<<endl;

auto tcf = Entity::loadFromFile("models/toon-cat-free/model.glb");
tcf->scale({1, 1, 1});
tcf->position({6,-1,0});
auto tcf = Entity::loadFromFile("models/toon-cat-free/model.gltf");
tcf->scale({1.5, 1.5, 1.5});
tcf->rotate(glm::angleAxis(PI/4,glm::vec3{0, 1, 0}));
tcf->onUpdate=[](Entity*self, float delta_time){
int asdf=0;
cout<<self->position().x<<' '<<self->position().y<<' '<<self->position().z<<endl;
};
entity_root->adopt(tcf);

auto fox = Entity::loadFromFile("models/fox/fox.dae");
auto fox = Entity::loadFromFile("models/fox/fox.gltf");
fox->scale({0.1, 0.1, 0.1});
fox->rotate(glm::angleAxis(PI,glm::vec3{0, 1, 0}));
fox->position({3,3,0});
fox->rotate(glm::angleAxis(PI/2,glm::vec3{1, 0, 0}));
fox->position({8,-5,0});
fox->onUpdate=[](Entity*self, float delta_time){
};
entity_root->adopt(fox);
Expand Down
11 changes: 5 additions & 6 deletions src/render/RendererOGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ RendererOGL::RendererOGL(){
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);

glEnable(GL_TEXTURE_2D);
}

GLenum PrimitiveType2GL(PrimitiveType prim_type){
Expand Down Expand Up @@ -48,19 +50,18 @@ void RendererOGL::render(const std::vector<RenderRequest>& render_q){
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
GLfloat ambientLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);

glEnable(GL_TEXTURE_2D);

for(const auto&req:render_q){
glLoadMatrixf((const float*)req.to_world.transposed().a);
if(req.textureID!=-1){
glBindTexture(GL_TEXTURE_2D, req.textureID);
}
glBegin(PrimitiveType2GL(req.primitive_type));
if(req.primitive_type==PrimitiveType::lines){
glColor3bv((GLbyte*)&req.textureID);
for(auto vtx:req.vertices)
glVertex3f(vtx.x,vtx.y,vtx.z);
}else{
if(req.textureID!=-1)
glBindTexture(GL_TEXTURE_2D, req.textureID);
for(auto face:req.faces){
for(auto i:face){
if(req.textureID!=-1)
Expand All @@ -72,8 +73,6 @@ void RendererOGL::render(const std::vector<RenderRequest>& render_q){
}
glEnd();
}

glDisable(GL_TEXTURE_2D);
}

//https://www.khronos.org/opengl/wiki/GluPerspective_code
Expand Down

0 comments on commit c091961

Please sign in to comment.