Added Shader

This commit is contained in:
grayson committed 2021-08-22 21:40:50 -05:00
1 parent 0724c6a84a
commit a9106ee20e
6 files changed
+79 -4

No files matched your search

-3
View File
@@ -43,9 +43,6 @@ namespace nf {
wglMakeCurrent(m_hdc, m_hglrc);
wglSwapIntervalEXT(0);
Log("OpenGL version: " + std::string((char*)glGetString(GL_VERSION)));
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
+49
View File
@@ -0,0 +1,49 @@
#include "Shader.h"
namespace nf {
Shader::Shader(const char* vertexSource, const char* fragmentSource) {
m_id = glCreateProgram();
unsigned int vs = glCreateShader(GL_VERTEX_SHADER);
unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vs, 1, &vertexSource, nullptr);
glShaderSource(fs, 1, &fragmentSource, nullptr);
glCompileShader(vs);
glCompileShader(fs);
for (int i = 0; i < 2; i++) {
unsigned int curr = (i == 0 ? vs : fs);
int result;
glGetShaderiv(curr, GL_COMPILE_STATUS, &result);
if (result != GL_TRUE) {
int length;
glGetShaderiv(curr, GL_INFO_LOG_LENGTH, &length);
char* message = new char[length];
glGetShaderInfoLog(curr, length, &length, message);
message[length - 2] = 0;
Error(("OpenGL Error: " + (std::string)message).c_str());
}
}
glAttachShader(m_id, vs);
glAttachShader(m_id, fs);
glLinkProgram(m_id);
glValidateProgram(m_id);
int result;
glGetProgramiv(m_id, GL_VALIDATE_STATUS, &result);
if (result != GL_TRUE) {
int length;
glGetProgramiv(m_id, GL_INFO_LOG_LENGTH, &length);
char* message = new char[length];
glGetProgramInfoLog(m_id, length, &length, message);
Error(("OpenGL Error: " + (std::string)message).c_str());
}
glDeleteShader(vs);
glDeleteShader(fs);
}
void Shader::bind() {
glUseProgram(m_id);
}
Shader::~Shader() {
glDeleteProgram(m_id);
}
}
+2 -1
View File
@@ -14,7 +14,8 @@ namespace nf {
VertexArray::VertexArray(const void* bufferData, size_t bufferSize) :
m_id(0),
m_vb(bufferData, bufferSize),
m_hasLayout(false)
m_hasLayout(false),
m_vertexStride(0)
{
glGenVertexArrays(1, &m_id);
}
+20
View File
@@ -0,0 +1,20 @@
#pragma once
#ifdef NFENGINE
#include "GL/glew.h"
#endif
#include "Utility.h"
namespace nf {
class Shader {
public:
Shader(const char* vertexSource, const char* fragmentSource);
void bind();
~Shader();
private:
unsigned int m_id;
//Associated resource?
};
}