PDA

View Full Version : Model will not load with anything else



Rockinredross867
01-03-2016, 01:34 PM
Whenever I load my model with another object for example my skybox. It draws for a second and then it disappears. Why does this do this?

core.cpp


#include <iostream>

#include "data\graphics\window.h"
#include "data\shaders\skyboxShader.h"
#include "data\shaders\cubeShader.h"
#include "data\graphics\buffers\buffer.h"
#include "data\graphics\buffers\indexBuffer.h"
#include "data\graphics\buffers\vertexarray.h"
#include "data\graphics\renderer.h"
#include "data\graphics\texture\texture.h"
#include "data\graphics\buffers\bufferdata.h"
#include "data\gameobjects\light.h"
#include "data\gameobjects\model\model.h"
#include "data\graphics\gui\rawframe.h"
#include "data\gameobjects\cubemap.h"

int main() {
std::vector<glm::vec3> lightPositions;
lightPositions.push_back(glm::vec3(0.0f, 4.0f, 0.0f));
lightPositions.push_back(glm::vec3(0.0f, 2.0f, 0.0f));

std::vector<const char*> faces;
faces.push_back("Resources/textures/right.jpg");
faces.push_back("Resources/textures/left.jpg");
faces.push_back("Resources/textures/top.jpg");
faces.push_back("Resources/textures/bottom.jpg");
faces.push_back("Resources/textures/back.jpg");
faces.push_back("Resources/textures/front.jpg");

JokesterEngine::graphics::Window m_Window(1080, 720, "SPACE BATTLES");
JokesterEngine::shaders::SkyboxShader m_SkyboxShader;
JokesterEngine::shaders::CubeShader m_CubeShader;
JokesterEngine::shaders::ShaderProgram m_ModelShader("Resources/shaders/modelVertexShader.txt", "Resources/shaders/modelFragmentShader.txt");
JokesterEngine::gameobject::Model m_Model("Resources/models/nanosuit_reflection/nanosuit.obj");
JokesterEngine::graphics::Renderer m_Renderer;
JokesterEngine::graphics::TextureLoader m_TextureLoader;
JokesterEngine::buffer::VertexData m_VertData;

GLuint skyboxTex = m_TextureLoader.loadCubemap(faces);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

GLuint cVAO, cVBO;
glGenVertexArrays(1, &cVAO);
glGenBuffers(1, &cVBO);
glBindVertexArray(cVAO);
glBindBuffer(GL_ARRAY_BUFFER, cVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(m_VertData.cubeVertices), &m_VertData.cubeVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glBindVertexArray(0);

GLuint sVAO, sVBO;
glGenVertexArrays(1, &sVAO);
glGenBuffers(1, &sVBO);
glBindVertexArray(sVAO);
glBindBuffer(GL_ARRAY_BUFFER, sVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(m_VertData.skyboxVertices), &m_VertData.skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindVertexArray(0);

while (!m_Window.closed())
{
m_Window.varUpdate();
m_Window.Do_Movement();

m_Renderer.screenColor();
m_Window.clear();

m_ModelShader.enable();
glm::mat4 model;
glm::mat4 view = m_Window.m_Camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(m_Window.m_Camera.Zoom, (float)m_Window.getWindowX() / (float)m_Window.getWindowY(), 0.1f, 100.0f);
glUniformMatrix4fv(glGetUniformLocation(m_ModelSha der.m_ProgramID, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(glGetUniformLocation(m_ModelSha der.m_ProgramID, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(m_ModelSha der.m_ProgramID, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniform3f(glGetUniformLocation(m_ModelShader.m_P rogramID, "cameraPos"), m_Window.m_Camera.Position.x, m_Window.m_Camera.Position.y, m_Window.m_Camera.Position.z);

glActiveTexture(GL_TEXTURE3); // We already have 3 texture units active (in this shader) so set the skybox as the 4th texture unit (texture units are 0 based so index number 3)
glUniform1i(glGetUniformLocation(m_ModelShader.m_P rogramID, "skybox"), 3);
// Now draw the nanosuit
glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTex);
m_Model.Draw(m_ModelShader);

/*m_CubeShader.enable();
m_CubeShader.getAllUniformLocations();
m_CubeShader.loadProjection(m_Window.getWindowX(), m_Window.getWindowY());
m_CubeShader.loadView(m_Window.m_Camera);
m_CubeShader.loadModel(glm::vec3(0.0f), 0.0f, glm::vec3(0.0f), 2.0f);
m_CubeShader.loadViewPos(m_Window.m_Camera);

GLuint projLoc = glGetUniformLocation(m_CubeShader.m_ProgramID, "projection");
GLuint viewLoc = glGetUniformLocation(m_CubeShader.m_ProgramID, "view");
GLuint modelLoc = glGetUniformLocation(m_CubeShader.m_ProgramID, "model");
GLuint viewPosLoc = glGetUniformLocation(m_CubeShader.m_ProgramID, "viewPos");

glm::mat4 projection;
projection = glm::perspective(45.0f, (float)m_Window.getWindowX() / (float)m_Window.getWindowY(), 0.1f, 100.0f);

glm::mat4 view;
view = m_Window.m_Camera.GetViewMatrix();

glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0f));

glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniform3f(viewPosLoc, m_Window.m_Camera.Position.x, m_Window.m_Camera.Position.y, m_Window.m_Camera.Position.z);

glBindVertexArray(cVAO);
glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTex);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);*/

glDepthFunc(GL_LEQUAL);
m_SkyboxShader.enable();
m_SkyboxShader.getAllUniformLocations();
m_SkyboxShader.loadProjection(m_Window.m_Camera, m_Window.getWindowX(), m_Window.getWindowY());
m_SkyboxShader.loadView(m_Window.m_Camera);
glBindVertexArray(sVAO);
glActiveTexture(GL_TEXTURE0);
glUniform1i(glGetUniformLocation(m_SkyboxShader.m_ ProgramID, "skybox"), 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTex);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthFunc(GL_LESS);

m_Window.update();
}

glDeleteVertexArrays(1, &cVAO);
glDeleteBuffers(1, &cVBO);
glDeleteVertexArrays(1, &sVAO);
glDeleteBuffers(1, &sVBO);
m_SkyboxShader.disable();

return 0;
}

mesh.h


#pragma once
// Std. Includes
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;

#include <GL/glew.h>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include "..\..\shaders\shaderProgram.h"

namespace JokesterEngine {
namespace gameobject {
struct Vertex {
glm::vec3 Position;
glm::vec3 Normal;
glm::vec2 TexCoords;
};

struct Texture {
GLuint id;
string type;
aiString path;
};

class Mesh {
public:
vector<Vertex> vertices;
vector<GLuint> indices;
vector<Texture> textures;

Mesh(vector<Vertex> vertices, vector<GLuint> indices, vector<Texture> textures)
{
this->vertices = vertices;
this->indices = indices;
this->textures = textures;

this->setupMesh();
}

void Draw(shaders::ShaderProgram shader)
{
GLuint diffuseNr = 1;
GLuint specularNr = 1;
GLuint reflectionNr = 1;

for (GLuint i = 0; i < this->textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);

stringstream ss;
string number;
string name = this->textures[i].type;
if (name == "texture_diffuse")
ss << diffuseNr++;
else if (name == "texture_specular")
ss << specularNr++;
else if (name == "texture_reflection")
ss << reflectionNr++;
number = ss.str();

glUniform1i(glGetUniformLocation(shader.m_ProgramI D, (name + number).c_str()), i);

glBindTexture(GL_TEXTURE_2D, this->textures[i].id);
}
glActiveTexture(GL_TEXTURE0);

glBindVertexArray(this->VAO);
glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}

private:
GLuint VAO, VBO, EBO;

void setupMesh()
{
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glGenBuffers(1, &this->EBO);

glBindVertexArray(this->VAO);

glBindBuffer(GL_ARRAY_BUFFER, this->VBO);

glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex), &this->vertices[0], GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &this->indices[0], GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0);

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, Normal));

glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, TexCoords));

glBindVertexArray(0);
}
};
}
}



model.h


#pragma once

#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#include <GL/glew.h>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <SOIL/SOIL.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "..\..\shaders\shaderProgram.h"

#include "mesh.h"

GLint TextureFromFile(const char* path, string directory);

namespace JokesterEngine {
namespace gameobject {
class Model
{
public:

Model(GLchar* path)
{
this->loadModel(path);
}

void Draw(shaders::ShaderProgram shader)
{
for (GLuint i = 0; i < this->meshes.size(); i++)
this->meshes[i].Draw(shader);
}

private:
vector<Mesh> meshes;
string directory;
vector<Texture> textures_loaded;

void loadModel(string path)
{

Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);

if (!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) // if is Not Zero
{
cout << "ERROR::ASSIMP:: " << importer.GetErrorString() << endl;
return;
}
this->directory = path.substr(0, path.find_last_of('/'));

this->processNode(scene->mRootNode, scene);
}

void processNode(aiNode* node, const aiScene* scene)
{
for (GLuint i = 0; i < node->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
this->meshes.push_back(this->processMesh(mesh, scene));
}
for (GLuint i = 0; i < node->mNumChildren; i++)
{
this->processNode(node->mChildren[i], scene);
}

}

Mesh processMesh(aiMesh* mesh, const aiScene* scene)
{
vector<Vertex> vertices;
vector<GLuint> indices;
vector<Texture> textures;

for (GLuint i = 0; i < mesh->mNumVertices; i++)
{
Vertex vertex;
glm::vec3 vector;

vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertex.Position = vector;

vector.x = mesh->mNormals[i].x;
vector.y = mesh->mNormals[i].y;
vector.z = mesh->mNormals[i].z;
vertex.Normal = vector;

if (mesh->mTextureCoords[0])
{
glm::vec2 vec;

vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertex.TexCoords = vec;
}
else
vertex.TexCoords = glm::vec2(0.0f, 0.0f);
vertices.push_back(vertex);
}
for (GLuint i = 0; i < mesh->mNumFaces; i++)
{
aiFace face = mesh->mFaces[i];

for (GLuint j = 0; j < face.mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}

if (mesh->mMaterialIndex >= 0)
{
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];

vector<Texture> diffuseMaps = this->loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());

vector<Texture> specularMaps = this->loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());

vector<Texture> reflectionMaps = this->loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_reflection");
textures.insert(textures.end(), reflectionMaps.begin(), reflectionMaps.end());
}

return Mesh(vertices, indices, textures);
}

vector<Texture> loadMaterialTextures(aiMaterial* mat, aiTextureType type, string typeName)
{
vector<Texture> textures;
for (GLuint i = 0; i < mat->GetTextureCount(type); i++)
{
aiString str;
mat->GetTexture(type, i, &str);

GLboolean skip = false;
for (GLuint j = 0; j < textures_loaded.size(); j++)
{
if (textures_loaded[j].path == str)
{
textures.push_back(textures_loaded[j]);
skip = true;
break;
}
}
if (!skip)
{
Texture texture;
texture.id = TextureFromFile(str.C_Str(), this->directory);
texture.type = typeName;
texture.path = str;
textures.push_back(texture);
this->textures_loaded.push_back(texture);
}
}
return textures;
}
};
}
}

GLint TextureFromFile(const char* path, string directory)
{
string filename = string(path);
filename = directory + '/' + filename;
GLuint textureID;
glGenTextures(1, &textureID);
int width, height;
unsigned char* image = SOIL_load_image(filename.c_str(), &width, &height, 0, SOIL_LOAD_RGB);

glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);

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_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
SOIL_free_image_data(image);

return textureID;
}

carsten neumann
01-04-2016, 04:37 PM
Hmm, you've made a couple of posts that all follow the same pattern: you state that something is not working and then post a large amount of code. While you may get lucky and somebody feels motivated to look through all that you have a much better chance of getting help if you try to minimize the problematic code and post something that is self contained (i.e. someone motivated to debug the program for you can actually build and run it). It also helps if you state what you have done so far to isolate and track down the problem. For example you may want to run your program under an OpenGL debugger (e.g. apitrace, or the tools from the GPU vendors) to make sure there are no OpenGL errors.
Please also take a look at Forum Posting Guidelines for suggestions how to ask questions such that you are likely to get an answer.