first code lawl
commit
e9f7c6986d
|
@ -0,0 +1,3 @@
|
|||
Rhubarb is a 'project' 3d engine I've been working on.
|
||||
|
||||
Its aim is to use OpenGL 3.0 as fully as possible, without deprecated features and unnecessary bloat.
|
|
@ -0,0 +1,20 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 11.00
|
||||
# Visual Studio 2010
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Rhubarb", "Rhubarb\Rhubarb.vcxproj", "{781C4B6A-7478-4B13-A7A8-57A06D66FC4A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
Release|Win32 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{781C4B6A-7478-4B13-A7A8-57A06D66FC4A}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{781C4B6A-7478-4B13-A7A8-57A06D66FC4A}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{781C4B6A-7478-4B13-A7A8-57A06D66FC4A}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{781C4B6A-7478-4B13-A7A8-57A06D66FC4A}.Release|Win32.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
Binary file not shown.
|
@ -0,0 +1,43 @@
|
|||
#include "CFlatShader.h"
|
||||
#include <string>
|
||||
|
||||
using namespace rb;
|
||||
|
||||
CFlatShader::CFlatShader(void)
|
||||
{
|
||||
std::string VertexSource = "uniform mat4 mvpMatrix;"
|
||||
"attribute vec4 vVertex;"
|
||||
"void main(void) "
|
||||
"{ gl_Position = mvpMatrix * vVertex; "
|
||||
"}";
|
||||
strncpy_s(m_VertexSource, RB_SHADER_MAX_SIZE, VertexSource.c_str(), VertexSource.length());
|
||||
|
||||
std::string FragmentSource = "uniform vec4 vColor;"
|
||||
"void main(void) "
|
||||
"{ gl_FragColor = vColor; "
|
||||
"}";
|
||||
strncpy_s(m_FragmentSource, RB_SHADER_MAX_SIZE, FragmentSource.c_str(), FragmentSource.length());
|
||||
}
|
||||
|
||||
void CFlatShader::Initialize(void)
|
||||
{
|
||||
CompileAndLink();
|
||||
|
||||
glBindAttribLocation(m_Program, 0, "vVertex");
|
||||
}
|
||||
|
||||
void CFlatShader::Use(float *Color, GLfloat *Matrix)
|
||||
{
|
||||
glUseProgram(m_Program);
|
||||
|
||||
GLint ColorLocation = glGetUniformLocation(m_Program, "vColor");
|
||||
glUniform4fv(ColorLocation, 1, Color);
|
||||
|
||||
GLint MatrixLocation = glGetUniformLocation(m_Program, "mvpMatrix");
|
||||
glUniformMatrix4fv(MatrixLocation, 1, GL_FALSE, Matrix);
|
||||
}
|
||||
|
||||
|
||||
CFlatShader::~CFlatShader(void)
|
||||
{
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
#pragma once
|
||||
#include "CShaderBase.h"
|
||||
|
||||
#include "CMatrix44.h"
|
||||
|
||||
namespace rb
|
||||
{
|
||||
class CFlatShader : public CShaderBase
|
||||
{
|
||||
public:
|
||||
CFlatShader(void);
|
||||
~CFlatShader(void);
|
||||
|
||||
void Initialize(void);
|
||||
|
||||
virtual void Use(float *Color, GLfloat *Matrix);
|
||||
};
|
||||
};
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
#include "CIdentityShader.h"
|
||||
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <assert.h>
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <GL/GL.h>
|
||||
|
||||
using namespace rb;
|
||||
|
||||
CIdentityShader::CIdentityShader(void)
|
||||
{
|
||||
std::string VertexSource = "attribute vec4 vVertex;"
|
||||
"void main(void) "
|
||||
"{ "
|
||||
" gl_Position = vVertex; "
|
||||
"}";
|
||||
strncpy_s(m_VertexSource, RB_SHADER_MAX_SIZE, VertexSource.c_str(), VertexSource.length());
|
||||
|
||||
std::string FragmentSource = "uniform vec4 vColor;"
|
||||
"void main(void) "
|
||||
"{ "
|
||||
" gl_FragColor = vColor;"
|
||||
"}";
|
||||
strncpy_s(m_FragmentSource, RB_SHADER_MAX_SIZE, FragmentSource.c_str(), FragmentSource.length());
|
||||
}
|
||||
|
||||
void CIdentityShader::Initialize(void)
|
||||
{
|
||||
CompileAndLink();
|
||||
|
||||
glBindAttribLocation(m_Program, 0, "vVertex");
|
||||
}
|
||||
|
||||
void CIdentityShader::Use(float *Color)
|
||||
{
|
||||
glUseProgram(m_Program);
|
||||
|
||||
GLint ColorLocation = glGetUniformLocation(m_Program, "vColor");
|
||||
glUniform4fv(ColorLocation, 1, Color);
|
||||
}
|
||||
|
||||
CIdentityShader::~CIdentityShader(void)
|
||||
{
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
#pragma once
|
||||
#include "CShaderBase.h"
|
||||
|
||||
namespace rb
|
||||
{
|
||||
class CIdentityShader : public CShaderBase
|
||||
{
|
||||
public:
|
||||
CIdentityShader(void);
|
||||
~CIdentityShader(void);
|
||||
|
||||
void Initialize(void);
|
||||
|
||||
virtual void Use(float *Color);
|
||||
};
|
||||
};
|
||||
|
|
@ -0,0 +1,162 @@
|
|||
#include "CMatrix44.h"
|
||||
using namespace rb;
|
||||
|
||||
CMatrix44::CMatrix44(void)
|
||||
{
|
||||
Identity();
|
||||
}
|
||||
|
||||
void CMatrix44::Rotation(float Angle, float X, float Y, float Z)
|
||||
{
|
||||
float Length, s, c;
|
||||
float xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c;
|
||||
|
||||
|
||||
s = float(sin(Angle));
|
||||
c = float(cos(Angle));
|
||||
|
||||
Length = float(sqrt(X * X + Y * Y + Z * Z));
|
||||
|
||||
if (Length == 0.0f)
|
||||
return;
|
||||
|
||||
// Rotation matrix is normalized
|
||||
X /= Length;
|
||||
Y /= Length;
|
||||
Z /= Length;
|
||||
|
||||
#define M(col,row) m_Data[col*4+row]
|
||||
|
||||
xx = X * X;
|
||||
yy = Y * Y;
|
||||
zz = X * X;
|
||||
xy = X * Y;
|
||||
yz = Y * Z;
|
||||
zx = Z * X;
|
||||
xs = X * s;
|
||||
ys = Y * s;
|
||||
zs = Z * s;
|
||||
one_c = 1.0f - c;
|
||||
|
||||
M(0,0) = (one_c * xx) + c;
|
||||
M(0,1) = (one_c * xy) - zs;
|
||||
M(0,2) = (one_c * zx) + ys;
|
||||
M(0,3) = 0.0f;
|
||||
|
||||
M(1,0) = (one_c * xy) + zs;
|
||||
M(1,1) = (one_c * yy) + c;
|
||||
M(1,2) = (one_c * yz) - xs;
|
||||
M(1,3) = 0.0f;
|
||||
|
||||
M(2,0) = (one_c * zx) - ys;
|
||||
M(2,1) = (one_c * yz) + xs;
|
||||
M(2,2) = (one_c * zz) + c;
|
||||
M(2,3) = 0.0f;
|
||||
|
||||
M(3,0) = 0.0f;
|
||||
M(3,1) = 0.0f;
|
||||
M(3,2) = 0.0f;
|
||||
M(3,3) = 1.0f;
|
||||
|
||||
#undef M
|
||||
}
|
||||
|
||||
void CMatrix44::Translation(float X, float Y, float Z)
|
||||
{
|
||||
#define M(col, row) m_Data[col*4+row]
|
||||
|
||||
M(3, 0) = X;
|
||||
M(3, 1) = Y;
|
||||
M(3, 2) = Z;
|
||||
M(3, 3) = 1.0f;
|
||||
|
||||
#undef M
|
||||
}
|
||||
|
||||
void CMatrix44::Scale(float X, float Y, float Z)
|
||||
{
|
||||
#define M(col, row) m_Data[col*4+row]
|
||||
|
||||
M(0, 0) = X;
|
||||
M(1, 1) = Y;
|
||||
M(2, 2) = Z;
|
||||
M(3, 3) = 1.0f;
|
||||
|
||||
#undef M
|
||||
}
|
||||
|
||||
void CMatrix44::Identity(void)
|
||||
{
|
||||
for (int i = 0; i < 16; i++)
|
||||
m_Data[i] = 0.0f;
|
||||
|
||||
#define M(col,row) m_Data[col*4+row]
|
||||
|
||||
M(0, 0) = 1.0f;
|
||||
M(1, 1) = 1.0f;
|
||||
M(2, 2) = 1.0f;
|
||||
M(3, 3) = 1.0f;
|
||||
|
||||
#undef M
|
||||
}
|
||||
|
||||
void CMatrix44::Multiply(CMatrix44 &Matrix)
|
||||
{
|
||||
CMatrix44 NewMatrix;
|
||||
|
||||
#define L(col,row) m_Data[col*4+row]
|
||||
#define R(col,row) Matrix.m_Data[col*4+row]
|
||||
#define D(col,row) NewMatrix.m_Data[col*4+row]
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
D(i, j) = L(0, j) * R(i, 0) + L(1, j) * R(i, 1) + L(2, j) * R(i, 2) + L(3, j) * R(i, 3);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
m_Data[i] = NewMatrix.m_Data[i];
|
||||
|
||||
#undef L
|
||||
#undef R
|
||||
#undef D
|
||||
}
|
||||
|
||||
void CMatrix44::Multiply(CMatrix44 &Matrix, CMatrix44 &Target)
|
||||
{
|
||||
for (int i = 0; i < 16; i++)
|
||||
Target.m_Data[i] = m_Data[i];
|
||||
|
||||
Target.Multiply(Matrix);
|
||||
}
|
||||
|
||||
void CMatrix44::Multiply(CVector4 &Vector, CVector4 &Target)
|
||||
{
|
||||
#define M(col,row) m_Data[col*4+row]
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
Target.m_Data[0] = M(i, 0) * Vector.m_Data[0] + M(i, 1) * Vector.m_Data[1] + M(i, 2) * Vector.m_Data[2] + M(i, 3) * Vector.m_Data[3];
|
||||
}
|
||||
#undef M
|
||||
}
|
||||
|
||||
void CMatrix44::Row(int i, CVector4 &Target)
|
||||
{
|
||||
Target.m_Data[0] = m_Data[0 + i];
|
||||
Target.m_Data[1] = m_Data[4 + i];
|
||||
Target.m_Data[2] = m_Data[8 + i];
|
||||
Target.m_Data[3] = m_Data[12 + i];
|
||||
}
|
||||
|
||||
void CMatrix44::Column(int i, CVector4 &Target)
|
||||
{
|
||||
for (int j = 0; j < 4; j++)
|
||||
Target.m_Data[j] = m_Data[j + i * 4];
|
||||
}
|
||||
|
||||
CMatrix44::~CMatrix44(void)
|
||||
{
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "CVector4.h"
|
||||
|
||||
namespace rb
|
||||
{
|
||||
class CMatrix44
|
||||
{
|
||||
public:
|
||||
CMatrix44(void);
|
||||
~CMatrix44(void);
|
||||
|
||||
//This overwrites the existing data
|
||||
void Rotation(float Angle, float X, float Y, float Z);
|
||||
void Translation(float X, float Y, float Z);
|
||||
void Scale(float X, float Y, float Z);
|
||||
void Identity(void);
|
||||
|
||||
void Multiply(CMatrix44 &Matrix);
|
||||
void Multiply(CMatrix44 &Matrix, CMatrix44 &Target);
|
||||
void Multiply(CVector4 &Vector, CVector4 &Target);
|
||||
|
||||
void Row(int i, CVector4 &Target);
|
||||
void Column(int i, CVector4 &Target);
|
||||
|
||||
float m_Data[16];
|
||||
};
|
||||
}
|
|
@ -0,0 +1,123 @@
|
|||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "CMatrixManager.h"
|
||||
using namespace rb;
|
||||
|
||||
CMatrixManager::CMatrixManager(unsigned int StackSize)
|
||||
{
|
||||
m_StackBottom = new float[StackSize * 4];
|
||||
m_StackPointer = m_StackBottom;
|
||||
m_MaxSize = StackSize;
|
||||
m_ProjectionMatrix = new CMatrix44();
|
||||
m_MVPMatrix = new CMatrix44();
|
||||
|
||||
PushIdentity();
|
||||
}
|
||||
|
||||
void CMatrixManager::PushIdentity(void)
|
||||
{
|
||||
assert(CanAllocate());
|
||||
CMatrix44 *Matrix = (CMatrix44*)m_StackPointer;
|
||||
Matrix->Identity();
|
||||
m_StackPointer += 16;
|
||||
}
|
||||
|
||||
void CMatrixManager::Push(CMatrix44 &Matrix)
|
||||
{
|
||||
assert(CanAllocate());
|
||||
memcpy_s(m_StackPointer, 64, Matrix.m_Data, 64);
|
||||
m_StackPointer += 16;
|
||||
}
|
||||
|
||||
void CMatrixManager::Push(void)
|
||||
{
|
||||
CMatrix44 *OldMatrix = (CMatrix44*)(m_StackPointer - 16);
|
||||
CMatrix44 *NewMatrix = (CMatrix44*)(m_StackPointer);
|
||||
memcpy_s(NewMatrix, 64, OldMatrix, 64);
|
||||
m_StackPointer += 16;
|
||||
}
|
||||
|
||||
void CMatrixManager::Pop(void)
|
||||
{
|
||||
assert(m_StackPointer - 16 >= m_StackBottom);
|
||||
m_StackPointer -= 16;
|
||||
}
|
||||
|
||||
bool CMatrixManager::CanAllocate(void)
|
||||
{
|
||||
return (((unsigned int)m_StackPointer - (unsigned int)m_StackBottom) <= m_MaxSize * 64 - 64);
|
||||
}
|
||||
|
||||
void CMatrixManager::Multiply(CMatrix44 &Matrix)
|
||||
{
|
||||
CMatrix44 *OldMatrix = (CMatrix44 *)((float*)m_StackPointer - 16);
|
||||
OldMatrix->Multiply(Matrix);
|
||||
}
|
||||
|
||||
void CMatrixManager::Translate(GLfloat X, GLfloat Y, GLfloat Z)
|
||||
{
|
||||
CMatrix44 Translation;
|
||||
Translation.Translation(X, Y, Z);
|
||||
|
||||
Multiply(Translation);
|
||||
}
|
||||
|
||||
void CMatrixManager::Rotate(GLfloat Angle, GLfloat X, GLfloat Y, GLfloat Z)
|
||||
{
|
||||
CMatrix44 Rotation;
|
||||
Rotation.Rotation(Angle, X, Y, Z);
|
||||
|
||||
Multiply(Rotation);
|
||||
}
|
||||
|
||||
void CMatrixManager::Scale(GLfloat X, GLfloat Y, GLfloat Z)
|
||||
{
|
||||
CMatrix44 Scale;
|
||||
Scale.Scale(X, Y, Z);
|
||||
|
||||
Multiply(Scale);
|
||||
}
|
||||
|
||||
void CMatrixManager::SetProjection(CMatrix44 &Matrix)
|
||||
{
|
||||
memcpy_s(m_ProjectionMatrix->m_Data, 64, Matrix.m_Data, 64);
|
||||
}
|
||||
|
||||
void CMatrixManager::SetPerspective(GLfloat FOV, GLfloat AspectRatio, GLfloat Near, GLfloat Far)
|
||||
{
|
||||
CMatrix44 Matrix;
|
||||
|
||||
float yMax = Near * tanf(FOV * 0.5f);
|
||||
float yMin = -yMax;
|
||||
float xMin = yMin * AspectRatio;
|
||||
float xMax = -xMin;
|
||||
|
||||
Matrix.m_Data[0] = (2.0f * Near) / (xMax - xMin);
|
||||
Matrix.m_Data[5] = (2.0f * Near) / (yMax - yMin);
|
||||
Matrix.m_Data[8] = (xMax + xMin) / (xMax - xMin);
|
||||
Matrix.m_Data[9] = (yMax + yMin) / (yMax - yMin);
|
||||
Matrix.m_Data[10] = -((Far + Near) / (Far - Near));
|
||||
Matrix.m_Data[11] = -1.0f;
|
||||
Matrix.m_Data[14] = -((2.0f * (Far * Near))/(Far - Near));
|
||||
Matrix.m_Data[15] = 0.0f;
|
||||
|
||||
memcpy_s(m_ProjectionMatrix->m_Data, 64, Matrix.m_Data, 64);
|
||||
}
|
||||
|
||||
GLfloat *CMatrixManager::GetMVP(void)
|
||||
{
|
||||
m_MVPMatrix->Identity();
|
||||
|
||||
CMatrix44 *MatrixMV = (CMatrix44*)(m_StackPointer - 16);
|
||||
m_ProjectionMatrix->Multiply(*MatrixMV, *m_MVPMatrix);
|
||||
|
||||
return (GLfloat *)m_MVPMatrix;
|
||||
}
|
||||
|
||||
CMatrixManager::~CMatrixManager(void)
|
||||
{
|
||||
delete [] m_StackBottom;
|
||||
delete m_ProjectionMatrix;
|
||||
delete m_MVPMatrix;
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
#pragma once
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <GL/GL.h>
|
||||
|
||||
#include "CMatrix44.h"
|
||||
|
||||
namespace rb
|
||||
{
|
||||
class CMatrixManager
|
||||
{
|
||||
public:
|
||||
CMatrixManager(unsigned int StackSize = 64);
|
||||
~CMatrixManager(void);
|
||||
|
||||
////// Model & View Matrix methods (in form of a stack)
|
||||
//Push the top matrix (copy)
|
||||
void Push(void);
|
||||
void Pop(void);
|
||||
|
||||
//Push a new matrix
|
||||
void PushIdentity();
|
||||
//Push an existing matrix
|
||||
void Push(CMatrix44 &Matrix);
|
||||
|
||||
//Transform top matrix
|
||||
void Translate(GLfloat X, GLfloat Y, GLfloat Z);
|
||||
void Rotate(GLfloat Angle, GLfloat X, GLfloat Y, GLfloat Z);
|
||||
void Scale(GLfloat X, GLfloat Y, GLfloat Z);
|
||||
void Multiply(CMatrix44 &Matrix);
|
||||
|
||||
////// Projection Matrix methods
|
||||
//Set projection matrix
|
||||
void SetProjection(CMatrix44 &ProjectionMatrix);
|
||||
//Quickly set perspective
|
||||
void SetPerspective(GLfloat FOV, GLfloat AspectRatio, GLfloat Near, GLfloat Far);
|
||||
|
||||
|
||||
|
||||
//Get combined matrix (to feed to a shader)
|
||||
GLfloat *GetMVP(void);
|
||||
private:
|
||||
float *m_StackBottom;
|
||||
float *m_StackPointer;
|
||||
unsigned int m_MaxSize;
|
||||
|
||||
CMatrix44 *m_ProjectionMatrix;
|
||||
CMatrix44 *m_MVPMatrix;
|
||||
|
||||
bool CanAllocate(void);
|
||||
};
|
||||
};
|
|
@ -0,0 +1,59 @@
|
|||
#include "CMesh.h"
|
||||
using namespace rb;
|
||||
|
||||
CMesh::CMesh(void)
|
||||
{
|
||||
m_NumVertices = 0;
|
||||
m_VertexArray = 0;
|
||||
|
||||
m_BeingFed = false;
|
||||
}
|
||||
|
||||
|
||||
CMesh::~CMesh(void)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void CMesh::BeginRaw(GLenum Primitive, GLuint NumberVertices)
|
||||
{
|
||||
m_Primitive = Primitive;
|
||||
m_NumVertices = NumberVertices;
|
||||
|
||||
glGenVertexArrays(1, &m_VertexArrayObject);
|
||||
glBindVertexArray(m_VertexArrayObject);
|
||||
|
||||
m_BeingFed = true;
|
||||
}
|
||||
|
||||
void CMesh::CopyRawVertexData(float *Data)
|
||||
{
|
||||
glGenBuffers(1, &m_VertexArray);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_VertexArray);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * m_NumVertices, Data, GL_DYNAMIC_DRAW);
|
||||
}
|
||||
|
||||
void CMesh::EndRaw(void)
|
||||
{
|
||||
glBindVertexArray(m_VertexArrayObject);
|
||||
|
||||
if (m_VertexArray != 0)
|
||||
{
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_VertexArray);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
}
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
m_BeingFed = false;
|
||||
}
|
||||
|
||||
void CMesh::Draw(void)
|
||||
{
|
||||
if (m_BeingFed) return;
|
||||
|
||||
glBindVertexArray(m_VertexArrayObject);
|
||||
glDrawArrays(m_Primitive, 0, m_NumVertices);
|
||||
glBindVertexArray(0);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
namespace rb
|
||||
{
|
||||
class CMesh
|
||||
{
|
||||
public:
|
||||
CMesh(void);
|
||||
~CMesh(void);
|
||||
|
||||
//Raw access mode (a.k.a. FEED ME FLOATS)
|
||||
void BeginRaw(GLenum Primitive, GLuint NumberVertices);
|
||||
void EndRaw();
|
||||
void CopyRawVertexData(float *Data);
|
||||
|
||||
void Draw(void);
|
||||
private:
|
||||
GLenum m_Primitive;
|
||||
|
||||
GLuint m_VertexArray;
|
||||
GLuint m_NumVertices;
|
||||
|
||||
GLuint m_VertexArrayObject;
|
||||
|
||||
bool m_BeingFed;
|
||||
};
|
||||
};
|
|
@ -0,0 +1,80 @@
|
|||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "CShaderBase.h"
|
||||
using namespace rb;
|
||||
|
||||
CShaderBase::CShaderBase(void)
|
||||
{
|
||||
}
|
||||
|
||||
void CShaderBase::ReadSource(const char *VertexFile, const char *FragmentFile)
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
fopen_s(&fp, VertexFile, "r");
|
||||
fseek(fp, 0, SEEK_END);
|
||||
size_t Size = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
assert(Size < RB_SHADER_MAX_SIZE);
|
||||
fread_s(m_VertexSource, RB_SHADER_MAX_SIZE, Size, 1, fp);
|
||||
fclose(fp);
|
||||
|
||||
fopen_s(&fp, FragmentFile, "r");
|
||||
fseek(fp, 0, SEEK_END);
|
||||
Size = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
assert(Size < RB_SHADER_MAX_SIZE);
|
||||
fread_s(m_FragmentSource, RB_SHADER_MAX_SIZE, Size, 1, fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
void CShaderBase::LoadShader(const char *Source, GLuint Shader)
|
||||
{
|
||||
GLchar *StringPtr[1];
|
||||
|
||||
StringPtr[0] = (GLchar *)Source;
|
||||
|
||||
glShaderSource(Shader, 1, (const GLchar **)StringPtr, NULL);
|
||||
}
|
||||
|
||||
void CShaderBase::CompileShader(GLuint Shader)
|
||||
{
|
||||
glCompileShader(Shader);
|
||||
|
||||
GLint Test;
|
||||
|
||||
glGetShaderiv(Shader, GL_COMPILE_STATUS, &Test);
|
||||
assert(Test != GL_FALSE);
|
||||
}
|
||||
|
||||
void CShaderBase::CompileAndLink(void)
|
||||
{
|
||||
GLuint VertexShader, FragmentShader;
|
||||
|
||||
VertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
||||
LoadShader(m_VertexSource, VertexShader);
|
||||
LoadShader(m_FragmentSource, FragmentShader);
|
||||
|
||||
CompileShader(VertexShader);
|
||||
CompileShader(FragmentShader);
|
||||
|
||||
m_Program = glCreateProgram();
|
||||
glAttachShader(m_Program, VertexShader);
|
||||
glAttachShader(m_Program, FragmentShader);
|
||||
|
||||
glLinkProgram(m_Program);
|
||||
|
||||
GLint Test;
|
||||
glGetProgramiv(m_Program, GL_COMPILE_STATUS, &Test);
|
||||
assert(Test != GL_FALSE);
|
||||
|
||||
glDeleteShader(VertexShader);
|
||||
glDeleteShader(FragmentShader);
|
||||
}
|
||||
|
||||
CShaderBase::~CShaderBase(void)
|
||||
{
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
#pragma once
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
#define RB_SHADER_MAX_SIZE 1024
|
||||
|
||||
namespace rb
|
||||
{
|
||||
class CShaderBase
|
||||
{
|
||||
public:
|
||||
CShaderBase(void);
|
||||
~CShaderBase(void);
|
||||
|
||||
protected:
|
||||
void CompileAndLink();
|
||||
void ReadSource(const char *VertexFile, const char *FragmentFile);
|
||||
|
||||
char m_FragmentSource[RB_SHADER_MAX_SIZE];
|
||||
char m_VertexSource[RB_SHADER_MAX_SIZE];
|
||||
|
||||
GLuint m_Program;
|
||||
|
||||
private:
|
||||
void LoadShader(const char *Source, GLuint Shader);
|
||||
void CompileShader(GLuint Shader);
|
||||
};
|
||||
};
|
|
@ -0,0 +1,65 @@
|
|||
#include "CVector4.h"
|
||||
using namespace rb;
|
||||
|
||||
CVector4::CVector4(void)
|
||||
{
|
||||
m_Data[0] = 0.0f;
|
||||
m_Data[1] = 0.0f;
|
||||
m_Data[2] = 0.0f;
|
||||
m_Data[3] = 1.0f;
|
||||
}
|
||||
|
||||
|
||||
CVector4::~CVector4(void)
|
||||
{
|
||||
}
|
||||
|
||||
float CVector4::Dot(CVector4 &Vector)
|
||||
{
|
||||
float Result = 0.0f;
|
||||
for (int i = 0; i < 4; i++)
|
||||
Result += m_Data[i] * Vector.m_Data[i];
|
||||
|
||||
return Result;
|
||||
}
|
||||
|
||||
void CVector4::Cross(CVector4 &Vector, CVector4 &Target)
|
||||
{
|
||||
Target.m_Data[0] = m_Data[1] * Target.m_Data[2] - m_Data[2] * Target.m_Data[1];
|
||||
Target.m_Data[1] = m_Data[2] * Target.m_Data[0] - m_Data[0] * Target.m_Data[2];
|
||||
Target.m_Data[2] = m_Data[0] * Target.m_Data[1] - m_Data[1] * Target.m_Data[0];
|
||||
Target.m_Data[3] = 1.0f;
|
||||
}
|
||||
|
||||
void CVector4::Add(CVector4 &Vector, CVector4 &Target)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
Target.m_Data[i] = m_Data[i] + Vector.m_Data[i];
|
||||
Target.m_Data[3] = 1.0f;
|
||||
}
|
||||
|
||||
void CVector4::Subtract(CVector4 &Vector, CVector4 &Target)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
Target.m_Data[i] = m_Data[i] - Vector.m_Data[i];
|
||||
Target.m_Data[3] = 1.0f;
|
||||
}
|
||||
|
||||
void CVector4::Add(CVector4 &Vector)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
m_Data[i] += Vector.m_Data[i];
|
||||
m_Data[3] = 1.0f;
|
||||
}
|
||||
|
||||
void CVector4::Subtract(CVector4 &Vector)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
m_Data[i] -= Vector.m_Data[i];
|
||||
m_Data[3] = 1.0f;
|
||||
}
|
||||
|
||||
void CVector4::Normalize(void)
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
namespace rb
|
||||
{
|
||||
class CVector4
|
||||
{
|
||||
public:
|
||||
CVector4(void);
|
||||
~CVector4(void);
|
||||
|
||||
float Dot(CVector4 &Vector);
|
||||
void Cross(CVector4 &Vector, CVector4 &Target);
|
||||
|
||||
void Add(CVector4 &Vector, CVector4 &Target);
|
||||
void Subtract(CVector4 &Vector, CVector4 &Target);
|
||||
|
||||
void Add(CVector4 &Vector);
|
||||
void Subtract(CVector4 &Vector);
|
||||
|
||||
void Normalize(void);
|
||||
void Normalize(CVector4 &Target);
|
||||
|
||||
float m_Data[4];
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{781C4B6A-7478-4B13-A7A8-57A06D66FC4A}</ProjectGuid>
|
||||
<RootNamespace>Rhubarb</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<IncludePath>C:\Development\SDKs\GLEW\include;C:\Development\SDKs\freeglut\include;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LibraryPath>C:\Development\SDKs\GLEW\lib;C:\Development\SDKs\freeglut\lib;$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<IncludePath>C:\Development\SDKs\GLEW\include;C:\Development\SDKs\freeglut\include;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LibraryPath>C:\Development\SDKs\GLEW\lib;C:\Development\SDKs\freeglut\lib;$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>freeglut.lib;opengl32.lib;glew32s.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<AdditionalDependencies>freeglut.lib;opengl32.lib;glew32s.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CFlatShader.h" />
|
||||
<ClInclude Include="CIdentityShader.h" />
|
||||
<ClInclude Include="CMatrix44.h" />
|
||||
<ClInclude Include="CMatrixManager.h" />
|
||||
<ClInclude Include="CMesh.h" />
|
||||
<ClInclude Include="CShaderBase.h" />
|
||||
<ClInclude Include="CVector4.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="CFlatShader.cpp" />
|
||||
<ClCompile Include="CIdentityShader.cpp" />
|
||||
<ClCompile Include="CMatrix44.cpp" />
|
||||
<ClCompile Include="CMatrixManager.cpp" />
|
||||
<ClCompile Include="CMesh.cpp" />
|
||||
<ClCompile Include="CShaderBase.cpp" />
|
||||
<ClCompile Include="CVector4.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
|
@ -0,0 +1,78 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\Math">
|
||||
<UniqueIdentifier>{02a9ecfc-0392-40a2-b87b-a2457a7b2b97}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\Math">
|
||||
<UniqueIdentifier>{52f0d899-fb8f-4bed-adc1-6a36bd13d0d4}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Header Files\Shaders">
|
||||
<UniqueIdentifier>{e1fbfd4f-a4e0-4587-9cbc-a27dd0cf31df}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\Shaders">
|
||||
<UniqueIdentifier>{83836c22-97e1-45cd-942b-25c8996dce2e}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="CVector4.cpp">
|
||||
<Filter>Source Files\Math</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CMatrix44.cpp">
|
||||
<Filter>Source Files\Math</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CShaderBase.cpp">
|
||||
<Filter>Source Files\Shaders</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CIdentityShader.cpp">
|
||||
<Filter>Source Files\Shaders</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CMesh.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CFlatShader.cpp">
|
||||
<Filter>Source Files\Shaders</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CMatrixManager.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CMatrix44.h">
|
||||
<Filter>Header Files\Math</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CVector4.h">
|
||||
<Filter>Header Files\Math</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CShaderBase.h">
|
||||
<Filter>Header Files\Shaders</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CIdentityShader.h">
|
||||
<Filter>Header Files\Shaders</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CMesh.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CFlatShader.h">
|
||||
<Filter>Header Files\Shaders</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CMatrixManager.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -0,0 +1,3 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
|
@ -0,0 +1,85 @@
|
|||
#include <iostream>
|
||||
#define GLEW_STATIC
|
||||
#include <GL/glew.h>
|
||||
#include <GL/glut.h>
|
||||
#include "CFlatShader.h"
|
||||
#include "CMesh.h"
|
||||
#include "CMatrix44.h"
|
||||
#include "CMatrixManager.h"
|
||||
|
||||
#define DegToRad(x) ((x)*0.017453292519943296f)
|
||||
|
||||
rb::CFlatShader gCubeShader;
|
||||
rb::CMesh gCube;
|
||||
rb::CMatrixManager gManager;
|
||||
|
||||
float gCubeSize = 0.25f;
|
||||
float gCubeVertices[] = {
|
||||
-gCubeSize, -gCubeSize, 0.0f,
|
||||
gCubeSize, -gCubeSize, 0.0f,
|
||||
gCubeSize, gCubeSize, 0.0f,
|
||||
-gCubeSize, gCubeSize, 0.0f
|
||||
};
|
||||
|
||||
void fnChangeSize(int Width, int Height)
|
||||
{
|
||||
glViewport(0, 0, Width, Height);
|
||||
gManager.SetPerspective(DegToRad(35.0f), float(Width)/float(Height), 1.0f, 100.0f);
|
||||
}
|
||||
|
||||
void fnRenderScene(void)
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||
|
||||
static float Rotation = 0.0f;
|
||||
Rotation += 0.1f;
|
||||
|
||||
float Red [] = {1.0f, 0.0f, 0.0f, 1.0f};
|
||||
|
||||
gManager.Push();
|
||||
gManager.Translate(sinf(DegToRad(Rotation)), 0.0f, -2.0f);
|
||||
gManager.Rotate(DegToRad(Rotation), 0.0f, 0.0f, 1.0f);
|
||||
gCubeShader.Use(Red, gManager.GetMVP());
|
||||
gCube.Draw();
|
||||
gManager.Pop();
|
||||
|
||||
|
||||
gManager.Push();
|
||||
gManager.Translate(cosf(DegToRad(Rotation)), 0.0f, -4.0f);
|
||||
gManager.Rotate(-DegToRad(Rotation), 0.0f, 0.0f, 1.0f);
|
||||
gCubeShader.Use(Red, gManager.GetMVP());
|
||||
gCube.Draw();
|
||||
gManager.Pop();
|
||||
|
||||
glutSwapBuffers();
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
rb::CMatrix44 *Matrix = new rb::CMatrix44();
|
||||
|
||||
glutInit(&argc, argv);
|
||||
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
|
||||
glutInitWindowSize(800, 600);
|
||||
glutCreateWindow("Rhubarb");
|
||||
glutReshapeFunc(fnChangeSize);
|
||||
glutDisplayFunc(fnRenderScene);
|
||||
|
||||
GLenum Error = glewInit();
|
||||
if (Error != GLEW_OK)
|
||||
{
|
||||
std::cerr << "GLEW Error: " << glewGetErrorString(Error) << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
|
||||
|
||||
gCubeShader.Initialize();
|
||||
|
||||
gCube.BeginRaw(GL_TRIANGLE_FAN, 4);
|
||||
gCube.CopyRawVertexData(gCubeVertices);
|
||||
gCube.EndRaw();
|
||||
|
||||
glutMainLoop();
|
||||
}
|
Loading…
Reference in New Issue