PDA

View Full Version : Skybox not working



Rockinredross867
01-02-2016, 01:02 PM
I have gotten my skybox to work, but when I make a class everything wants to not work and I just get a black screen. Please tell me why this is really annoying.

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"
#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::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;
JokesterEngine::gameobject::Skybox skybox(faces, m_TextureLoader);

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

glEnable(GL_DEPTH_TEST);
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);

skybox.load(m_VertData.skyboxVertices);

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

glDepthMask(GL_FALSE);
m_SkyboxShader.enable();
m_SkyboxShader.loadCubemap(m_Window.getWindowX(), m_Window.getWindowY(), m_Window.m_Camera);
skybox.render();
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;
}

cubemap.cpp


#include "cubemap.h"

namespace JokesterEngine {
namespace gameobject {
Skybox::Skybox(std::vector<const char*> faces, graphics::TextureLoader loader) :
m_Faces(faces)
{
m_Texture = loader.loadCubemap(faces);
}

void Skybox::load(GLfloat* data)
{
glGenVertexArrays(1, &sVAO);
glGenBuffers(1, &sVBO);
glBindVertexArray(sVAO);
glBindBuffer(GL_ARRAY_BUFFER, sVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindVertexArray(0);
}

void Skybox::render()
{
glBindVertexArray(sVAO);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_Texture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
}
}
}

loadCubemap function inside the shaderProgram.cpp


void ShaderProgram::loadCubemap(int width, int height, gameobject::Camera& camera)
{
GLuint projLoc = glGetUniformLocation(m_ProgramID, "projection");
GLuint viewLoc = glGetUniformLocation(m_ProgramID, "view");

glm::mat4 projection;
projection = glm::perspective(45.0f, (float)width / (float)height, 0.1f, 100.0f);

glm::mat4 view;
view = glm::mat4(glm::mat3(camera.GetViewMatrix()));

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