Skip to content

Commit

Permalink
Added support for rendering using an index buffer object.
Browse files Browse the repository at this point in the history
  • Loading branch information
kromenak committed Sep 22, 2017
1 parent ad42eb0 commit 5f1d856
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 86 deletions.
17 changes: 12 additions & 5 deletions Source/GLVertexArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ GLVertexArray::GLVertexArray(const GLfloat* vertPositions, int vertPositionsCoun
glBindBuffer(GL_ARRAY_BUFFER, mVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

//glGenBuffers(1, &ibo);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, 24 * sizeof(GLushort), cube_elements, GL_STATIC_DRAW);

mVertexCount = vertPositionsCount / 3;
}

GLVertexArray::GLVertexArray(const GLfloat* vertPositions, int vertPositionsCount,
const GLushort* indexes, int indexesCount)
: GLVertexArray(vertPositions, vertPositionsCount)
{
glGenBuffers(1, &mIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexesCount * sizeof(GLushort), indexes, GL_STATIC_DRAW);
mIndexesCount = indexesCount;
}

GLVertexArray::~GLVertexArray()
{
if(mVBO != GL_NONE)
Expand All @@ -44,14 +50,15 @@ GLVertexArray::~GLVertexArray()
void GLVertexArray::Activate()
{
glBindVertexArray(mVAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIBO);
}

void GLVertexArray::Draw()
{
Activate();
if(mIBO != GL_NONE)
{
//TODO: Draw using indexes.
glDrawElements(GL_TRIANGLES, mIndexesCount, GL_UNSIGNED_SHORT, (void*)0);
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions Source/GLVertexArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class GLVertexArray
{
public:
GLVertexArray(const GLfloat* vertPositions, int vertPositionsCount);
GLVertexArray(const GLfloat* vertPositions, int vertPositionsCount,
const GLushort* indexes, int indexesCount);
~GLVertexArray();

void Activate();
Expand All @@ -24,4 +26,5 @@ class GLVertexArray
GLuint mIBO = GL_NONE;

int mVertexCount = 0;
int mIndexesCount = 0;
};
96 changes: 15 additions & 81 deletions Source/SDLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ GLfloat triangle_vertices[] = {
-0.5f, -0.5f, 0.0f
};

GLfloat triange_colors[] {
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f
};

GLfloat cube_vertices[] = {
// front
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
-0.5, -0.5, 0.5,
0.5, -0.5, 0.5,
0.5, 0.5, 0.5,
-0.5, 0.5, 0.5,
// back
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0
-0.5, -0.5, -0.5,
0.5, -0.5, -0.5,
0.5, 0.5, -0.5,
-0.5, 0.5, -0.5
};

GLfloat cube_colors[] = {
Expand Down Expand Up @@ -119,6 +125,7 @@ bool SDLRenderer::Initialize()
return false;
}
mVertArray = new GLVertexArray(triangle_vertices, 9);
//mVertArray = new GLVertexArray(cube_vertices, 24, cube_elements, 36);

// Init succeeded!
return true;
Expand Down Expand Up @@ -164,76 +171,3 @@ void SDLRenderer::Present()
{
SDL_GL_SwapWindow(mWindow);
}

GLuint SDLRenderer::LoadAndCompileShaderFromFile(const char* filePath, GLuint shaderType)
{
// Open the file, but freak out if not valid.
std::ifstream file(filePath);
if(!file.good())
{
std::cout << "Couldn't open shader file for loading: " << filePath << std::endl;
return GL_NONE;
}

// Read the file contents into a char buffer.
// Important to read to intermediate std::string first!
// Doesn't read correctly without that bit.
std::stringstream buffer;
buffer << file.rdbuf();
std::string fileContentsStr = buffer.str();
const char* fileContents = fileContentsStr.c_str();

// Create shader, load file contents into it, and compile it.
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &fileContents, nullptr);
glCompileShader(shader);
return shader;
}

bool SDLRenderer::IsShaderCompiled(GLuint shader)
{
// Ask GL whether compile succeeded for this shader.
GLint compileSucceeded = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileSucceeded);

// If not, we'll output the error log and fail.
if(compileSucceeded == GL_FALSE)
{
GLint errorLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &errorLength);

GLchar* errorLog = new GLchar[errorLength];
glGetShaderInfoLog(shader, errorLength, &errorLength, errorLog);

std::cout << "Error compiling shader: " << errorLog << std::endl;
delete[] errorLog;
return false;
}

// GL reports the compilation was successful!
return true;
}

bool SDLRenderer::IsProgramLinked(GLuint program)
{
// Ask GL whether link succeeded for this program.
GLint linkSucceeded = 0;
glGetProgramiv(program, GL_LINK_STATUS, &linkSucceeded);

// If not, we'll output the error log and fail.
if(linkSucceeded == GL_FALSE)
{
GLint errorLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &errorLength);

GLchar* errorLog = new GLchar[errorLength];
glGetProgramInfoLog(program, errorLength, &errorLength, errorLog);

std::cout << "Error linking shader program: " << errorLog << std::endl;
delete[] errorLog;
return false;
}

// GL reports the linking was successful!
return true;
}

0 comments on commit 5f1d856

Please sign in to comment.