PDA

View Full Version : Why doesn't my code work in a function but in the main function



Rockinredross867
01-01-2016, 07:47 PM
I know this isn't much about OpenGL, but I was just trying to implement some GUI for my game engine. When I put my code in my rawframe.cpp load and render function and call it from my main.cpp file it doesn't work. But when I put it directly in the main.cpp file it works fine. What am I doing wrong?

CALLING THE FUNCTION FROM THE main.cpp

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

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_BasicShader("Resources/shaders/basicVertexShader.txt", "Resources/shaders/basicFragmentShader.txt");
JokesterEngine::gameobject::Model m_Model("Resources/models/nanosuit/nanosuit.obj");
JokesterEngine::graphics::Renderer m_Renderer;
JokesterEngine::gameobject::Light light(lightPositions);
JokesterEngine::gui::RawFrame frame("wow", glm::vec2(0.0f, 0.0f), glm::vec3(0.2f), 0.3f);
JokesterEngine::buffer::VertexData vertData;

glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

GLfloat guiVertices[8] = {
-1, 1,
-1, -1,
1, 1,
1, -1
};

frame.load(guiVertices);

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

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

/*m_ModelShader.enable();

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

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

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

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

frame.render(m_BasicShader);

m_Window.update();
}

m_ModelShader.disable();

return 0;
}

rawframe.h


#pragma once

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

namespace JokesterEngine {
namespace gui {
class RawFrame {
private:
glm::vec2 m_Position;
glm::vec3 m_Color;
const char* m_Title;
float m_Size;
GLuint m_VAO, m_VBO;
public:
RawFrame(const char* title, glm::vec2 position, glm::vec3 color, float size);

void render(shaders::ShaderProgram& shader);
void load(GLfloat* vertices);
};
}
}

rawframe.cpp


#include "rawframe.h"

namespace JokesterEngine {
namespace gui {

RawFrame::RawFrame(const char* title, glm::vec2 position, glm::vec3 color, float size) :
m_Title(title), m_Position(position), m_Color(color), m_Size(size)
{
}

void RawFrame::load(GLfloat* vertices)
{
glGenVertexArrays(1, &m_VAO);
glGenBuffers(1, &m_VBO);

glBindVertexArray(m_VAO);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);

glBindVertexArray(0);
}

void RawFrame::render(shaders::ShaderProgram& shader)
{
shader.enable();
glBindVertexArray(m_VAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 8);
glBindVertexArray(0);
}
}
}

PUTTING CODE DIRECTLY INTO THE main.cpp

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

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_BasicShader("Resources/shaders/basicVertexShader.txt", "Resources/shaders/basicFragmentShader.txt");
JokesterEngine::gameobject::Model m_Model("Resources/models/nanosuit/nanosuit.obj");
JokesterEngine::graphics::Renderer m_Renderer;
JokesterEngine::gameobject::Light light(lightPositions);
JokesterEngine::gui::RawFrame frame("wow", glm::vec2(0.0f, 0.0f), glm::vec3(0.2f), 0.3f);
JokesterEngine::buffer::VertexData vertData;

glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

GLfloat guiVertices[8] = {
-1, 1,
-1, -1,
1, 1,
1, -1
};

GLuint m_VAO, m_VBO;
glGenVertexArrays(1, &m_VAO);
glGenBuffers(1, &m_VBO);

glBindVertexArray(m_VAO);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(guiVertices), guiVertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * 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();

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

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

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

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_BasicShader.enable();
glBindVertexArray(m_VAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 8);
glBindVertexArray(0);

m_Window.update();
}

m_ModelShader.disable();

return 0;
}

rawmodel.h


#pragma once

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

namespace JokesterEngine {
namespace gui {
class RawFrame {
private:
glm::vec2 m_Position;
glm::vec3 m_Color;
const char* m_Title;
float m_Size;
GLuint m_VAO, m_VBO;
public:
RawFrame(const char* title, glm::vec2 position, glm::vec3 color, float size);

void render(shaders::ShaderProgram& shader);
void load(GLfloat* vertices);
};
}
}

rawmodel.cpp


#include "rawframe.h"

namespace JokesterEngine {
namespace gui {

RawFrame::RawFrame(const char* title, glm::vec2 position, glm::vec3 color, float size) :
m_Title(title), m_Position(position), m_Color(color), m_Size(size)
{
}

void RawFrame::load(GLfloat* vertices)
{
glGenVertexArrays(1, &m_VAO);
glGenBuffers(1, &m_VBO);

glBindVertexArray(m_VAO);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);

glBindVertexArray(0);
}

void RawFrame::render(shaders::ShaderProgram& shader)
{
shader.enable();
glBindVertexArray(m_VAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 8);
glBindVertexArray(0);
}
}
}