Skip to content

Commit

Permalink
Helper methods from class
Browse files Browse the repository at this point in the history
  • Loading branch information
kaandemiroz authored and kaandemiroz committed Nov 10, 2016
1 parent b2ad523 commit 630172a
Show file tree
Hide file tree
Showing 7 changed files with 2,039 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Angel.h
@@ -0,0 +1,76 @@
//////////////////////////////////////////////////////////////////////////////
//
// --- Angel.h ---
//
// The main header file for all examples from Angel 6th Edition
//
//////////////////////////////////////////////////////////////////////////////

#ifndef __ANGEL_H__
#define __ANGEL_H__

//----------------------------------------------------------------------------
//
// --- Include system headers ---
//

#include <cmath>
#include <iostream>

// Define M_PI in the case it's not defined in the math header file
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif

//----------------------------------------------------------------------------
//
// --- Include OpenGL header files and helpers ---
//
// The location of these files vary by operating system. We've included
// copies of open-soruce project headers in the "GL" directory local
// this this "include" directory.
//

#ifdef __APPLE__ // include Mac OS X verions of headers
# include <OpenGL/OpenGL.h>
# include <GLUT/glut.h>
#else // non-Mac OS X operating systems
# include <GL/glew.h>
# include <GL/freeglut.h>
# include <GL/freeglut_ext.h>
#endif // __APPLE__

// Define a helpful macro for handling offsets into buffer objects
#define BUFFER_OFFSET( offset ) ((GLvoid*) (offset))

//----------------------------------------------------------------------------
//
// --- Include our class libraries and constants ---
//

namespace Angel {

// Helper function to load vertex and fragment shader files
GLuint InitShader( const char* vertexShaderFile,
const char* fragmentShaderFile );

// Defined constant for when numbers are too small to be used in the
// denominator of a division operation. This is only used if the
// DEBUG macro is defined.
const GLfloat DivideByZeroTolerance = GLfloat(1.0e-07);

// Degrees-to-radians constant
const GLfloat DegreesToRadians = M_PI / 180.0;

} // namespace Angel

#include "vec.h"
#include "mat.h"
//#include "CheckError.h"

// #define Print(x) do { std::cerr << #x " = " << (x) << std::endl; } while(0)

// Globally use our namespace in our example programs.
using namespace Angel;

#endif // __ANGEL_H__
54 changes: 54 additions & 0 deletions CheckError.h
@@ -0,0 +1,54 @@
//////////////////////////////////////////////////////////////////////////////
//
// --- CheckError.h ---
//
//////////////////////////////////////////////////////////////////////////////

#ifndef __CHECKERROR_H__
#define __CHECKERROR_H__

#include <stdio.h>
//#include <GL/gl.h>
#include <GLUT/glut.h>

//----------------------------------------------------------------------------

static const char*
ErrorString( GLenum error )
{
const char* msg;
switch( error ) {
#define Case( Token ) case Token: msg = #Token; break;
Case( GL_NO_ERROR );
Case( GL_INVALID_VALUE );
Case( GL_INVALID_ENUM );
Case( GL_INVALID_OPERATION );
Case( GL_STACK_OVERFLOW );
Case( GL_STACK_UNDERFLOW );
Case( GL_OUT_OF_MEMORY );
#undef Case
}

return msg;
}

//----------------------------------------------------------------------------

static void
_CheckError( const char* file, int line )
{
GLenum error = glGetError();

do {
fprintf( stderr, "[%s:%d] %s\n", file, line, ErrorString(error) );
} while ((error = glGetError()) != GL_NO_ERROR );

}

//----------------------------------------------------------------------------

#define CheckError() _CheckError( __FILE__, __LINE__ )

//----------------------------------------------------------------------------

#endif // !__CHECKERROR_H__
97 changes: 97 additions & 0 deletions InitShader.cpp
@@ -0,0 +1,97 @@

#include "Angel.h"

namespace Angel {

// Create a NULL-terminated string by reading the provided file
static char*
readShaderSource(const char* shaderFile)
{
FILE* fp = fopen(shaderFile, "r");

if ( fp == NULL ) { return NULL; }

fseek(fp, 0L, SEEK_END);
long size = ftell(fp);

fseek(fp, 0L, SEEK_SET);
char* buf = new char[size + 1];
fread(buf, 1, size, fp);

buf[size] = '\0';
fclose(fp);

return buf;
}


// Create a GLSL program object from vertex and fragment shader files
GLuint
InitShader(const char* vShaderFile, const char* fShaderFile)
{
struct Shader {
const char* filename;
GLenum type;
GLchar* source;
} shaders[2] = {
{ vShaderFile, GL_VERTEX_SHADER, NULL },
{ fShaderFile, GL_FRAGMENT_SHADER, NULL }
};

GLuint program = glCreateProgram();

for ( int i = 0; i < 2; ++i ) {
Shader& s = shaders[i];
s.source = readShaderSource( s.filename );
if ( shaders[i].source == NULL ) {
// std::cerr << "Failed to read " << s.filename << std::endl;
exit( EXIT_FAILURE );
}

GLuint shader = glCreateShader( s.type );
glShaderSource( shader, 1, (const GLchar**) &s.source, NULL );
glCompileShader( shader );

GLint compiled;
glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
if ( !compiled ) {
// std::cerr << s.filename << " failed to compile:" << std::endl;
GLint logSize;
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize );
char* logMsg = new char[logSize];
glGetShaderInfoLog( shader, logSize, NULL, logMsg );
// std::cerr << logMsg << std::endl;
delete [] logMsg;

exit( EXIT_FAILURE );
}

delete [] s.source;

glAttachShader( program, shader );
}

/* link and error check */
glLinkProgram(program);

GLint linked;
glGetProgramiv( program, GL_LINK_STATUS, &linked );
if ( !linked ) {
// std::cerr << "Shader program failed to link" << std::endl;
GLint logSize;
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
char* logMsg = new char[logSize];
glGetProgramInfoLog( program, logSize, NULL, logMsg );
// std::cerr << logMsg << std::endl;
delete [] logMsg;

exit( EXIT_FAILURE );
}

/* use program object */
glUseProgram(program);

return program;
}

} // Close namespace Angel block

0 comments on commit 630172a

Please sign in to comment.