PDA

View Full Version : Model with Cubemap destroys everything



Rockinredross867
01-02-2016, 12:28 PM
Whenever I load my skybox and my model at the same time they make a very weird result (I would show you, but opengl.com is not letting me). Here you go.

core.cpp


#include <iostream>

#include "data\graphics\window.h"
#include "data\shaders\shaderProgram.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"

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::ShaderProgram m_ModelShader("Resources/shaders/modelVertexShader.txt", "Resources/shaders/modelFragmentShader.txt");
JokesterEngine::shaders::ShaderProgram m_CubeShader("Resources/shaders/cubeVertexShader.txt", "Resources/shaders/cubeFragmentShader.txt");
JokesterEngine::shaders::ShaderProgram m_SkyboxShader("Resources/shaders/skyboxVertexShader.txt", "Resources/shaders/skyboxFragmentShader.txt");
JokesterEngine::gameobject::Model m_Model("Resources/models/nanosuit/nanosuit.obj");
JokesterEngine::graphics::Renderer m_Renderer;
JokesterEngine::gameobject::Light light(lightPositions);
JokesterEngine::graphics::TextureLoader m_TextureLoader;
JokesterEngine::buffer::VertexData m_VertData;

GLuint cubeTex = m_TextureLoader.loadTexture("Resources/textures/diffusemap_container.png", GL_TRUE);
GLuint cubemapTexture = m_TextureLoader.loadCubemap(faces);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

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, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * 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();

glEnable(GL_DEPTH_TEST);

glDepthMask(GL_FALSE);
m_SkyboxShader.enable();
GLuint projLoc = glGetUniformLocation(m_SkyboxShader.m_ProgramID, "projection");
GLuint viewLoc = glGetUniformLocation(m_SkyboxShader.m_ProgramID, "view");

glm::mat4 view;
view = glm::mat4(glm::mat3(m_Window.m_Camera.GetViewMatri x()));

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

glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));

glBindVertexArray(sVAO);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthMask(GL_TRUE);


m_ModelShader.enable();

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

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

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

glUniformMatrix4fv(projectionLoc, 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);

m_ModelShader.loadPointLight(light, 2);
m_Model.Draw(m_ModelShader);

m_Window.update();
}

glDeleteVertexArrays(1, &cVAO);
glDeleteBuffers(1, &cVBO);
m_ModelShader.disable();

return 0;
}

texture.cpp


#include "texture.h"

namespace JokesterEngine {
namespace graphics {

GLuint TextureLoader::loadTexture(const char* path, GLboolean boolean)
{
GLuint textureID;

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, boolean == GL_TRUE ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, boolean == GL_TRUE ? GL_REPEAT : GL_CLAMP_TO_EDGE);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int width, height;
unsigned char* image = SOIL_load_image(path, &width, &height, 0, SOIL_LOAD_RGBA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);

return textureID;
}

GLuint TextureLoader::generateAttachment(GLboolean depth, GLboolean stencil)
{
GLenum attachment_type;
if (!depth && !stencil)
attachment_type = GL_RGB;
else if (depth && !stencil)
attachment_type = GL_DEPTH_COMPONENT;
else if (!depth && stencil)
attachment_type = GL_STENCIL_INDEX;

GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
if (!depth && !stencil)
glTexImage2D(GL_TEXTURE_2D, 0, attachment_type, 1080, 720, 0, attachment_type, GL_UNSIGNED_BYTE, NULL);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, 1080, 720, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);

return textureID;
}

GLuint TextureLoader::loadCubemap(std::vector<const char*> paths)
{
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
glActiveTexture(GL_TEXTURE0);

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);

int width, height;
unsigned char* image;
for (GLuint i = 0; i < paths.size(); i++)
{
image = SOIL_load_image(paths[i], &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
}

glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

return textureID;
}

}
}