PDA

View Full Version : Why is OpenGL not loading my triangle?



Rockinredross867
12-05-2015, 11:45 AM
I have been following some tutorials and I had everything ready and when I launched the program the triangle didn't draw. I have been trying to figure this out for hours but just couldn't do it. So can one of you smart people help. You'd make me so happy if one of you guys figure this out.

loader.h


#pragma once

#include <iostream>
#include <vector>
#include "rawmodel.h"
#include <GL\glew.h>

namespace JokesterEngine {
namespace graphics {
class Loader {
private:
std::vector<GLuint> vaos;
std::vector<GLuint> vbos;
private:
GLuint createVAO();
void storeDataInAttributeList(GLuint position, GLfloat data[]);
void bindIndicesBuffer(GLuint indices[]);
public:
RawModel loadToVAO(GLfloat positions[], GLuint indices[]);
void unbindVAO();
void cleanUp();
};
}
}


loader.cpp


#include "loader.h"

#define FLOAT_ARRAY_LENGTH(tuple) sizeof(tuple) / sizeof(GLfloat)
#define UNSIGNED_INT_ARRAY_LENGTH(tuple) sizeof(tuple) / sizeof(GLuint)

namespace JokesterEngine {
namespace graphics {

RawModel Loader::loadToVAO(GLfloat positions[], GLuint indices[])
{
GLuint vao = createVAO();
//bindIndicesBuffer(indices);
storeDataInAttributeList(0, positions);
unbindVAO();
return RawModel(vao, 3);
}

void Loader::cleanUp()
{
for (GLuint vao : vaos)
{
glDeleteVertexArrays(1, &vao);
}
for (GLuint vbo : vbos)
{
glDeleteBuffers(1, &vbo);
}
}

GLuint Loader::createVAO()
{
GLuint vaoID;
glGenVertexArrays(1, &vaoID);
vaos.push_back(vaoID);
glBindVertexArray(vaoID);
return vaoID;
}

void Loader::storeDataInAttributeList(GLuint position, GLfloat data[])
{
GLuint vboID;
glGenBuffers(1, &vboID);
vbos.push_back(vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), &data, GL_STATIC_DRAW);
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(position);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void Loader::bindIndicesBuffer(GLuint indices[])
{
GLuint eboID;
glGenBuffers(1, &eboID);
vbos.push_back(eboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices, GL_STATIC_DRAW);
}

void Loader::unbindVAO()
{
glBindVertexArray(0);
}

}
}

rawmodel.h


#pragma once

#include <GL\glew.h>

namespace JokesterEngine {
namespace graphics {
class RawModel {
private:
GLuint m_VAO;
GLuint m_VertexCount;
public:
RawModel(GLuint vao, GLuint vertexCount);
GLuint getVAO() const { return m_VAO; }
GLuint getVertexCount() const { return m_VertexCount; }
};
}
}

rawmodel.cpp


#include "rawmodel.h"

namespace JokesterEngine {
namespace graphics {

RawModel::RawModel(GLuint vao, GLuint vertexCount) :
m_VAO(vao), m_VertexCount(vertexCount)
{

}

}
}

renderer.h


#pragma once

#include <GL\glew.h>
#include "rawmodel.h"

namespace JokesterEngine {
namespace graphics {
class Renderer {
public:
void prepare();
void render(RawModel model);
};
}
}


renderer.cpp


#include "renderer.h"

namespace JokesterEngine {
namespace graphics {

void Renderer::prepare()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
}

void Renderer::render(RawModel model)
{
glBindVertexArray(model.getVAO());
glDrawArrays(GL_TRIANGLES, 0, model.getVertexCount());
glBindVertexArray(0);
}

}
}

Finally core.cpp


#include <iostream>

#include "data\graphics\window.h"
#include "data\graphics\loader.h"
#include "data\graphics\renderer.h"
#include "data\shaders\shaderProgram.h"

#define ARRAY_LENGTH(x) sizeof(x) / sizeof(GLuint)

int main() {
JokesterEngine::graphics::Window m_Window(1080, 720, "JOKESTER GAME ENGINE");
JokesterEngine::graphics::Loader m_Loader;
JokesterEngine::graphics::Renderer m_Renderer;
//JokesterEngine::shaders::ShaderProgram shader("data\shaders\res\worldVertexShader.txt", "data\shaders\res\wolrdFragmentShader.txt");

GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};

GLuint indices[] = {
0, 1, 3,
3, 1, 2
};

JokesterEngine::graphics::RawModel model = m_Loader.loadToVAO(vertices, indices);

std::cout << model.getVertexCount() << std::endl;

while (!m_Window.closed())
{
m_Renderer.prepare();
m_Window.clear();

m_Renderer.render(model);

m_Window.update();
}

m_Loader.cleanUp();

return 0;
}

uwi2k2
12-08-2015, 07:32 AM
hi,

i cant see the bug in your code,
but maybe you wanne start more `simple` ... some basic opengl ...
So mybe try this one .. its slow and step by step: https://www.youtube.com/watch?v=U5BV4a57Tv0

cu
uwi2k2