forked from TheCherno/Hazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#include "hzpch.h" | ||
#include "Shader.h" | ||
|
||
#include <glad/glad.h> | ||
|
||
namespace Hazel { | ||
|
||
Shader::Shader(const std::string& vertexSrc, const std::string& fragmentSrc) | ||
{ | ||
// Create an empty vertex shader handle | ||
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); | ||
|
||
// Send the vertex shader source code to GL | ||
// Note that std::string's .c_str is NULL character terminated. | ||
const GLchar* source = vertexSrc.c_str(); | ||
glShaderSource(vertexShader, 1, &source, 0); | ||
|
||
// Compile the vertex shader | ||
glCompileShader(vertexShader); | ||
|
||
GLint isCompiled = 0; | ||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiled); | ||
if (isCompiled == GL_FALSE) | ||
{ | ||
GLint maxLength = 0; | ||
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength); | ||
|
||
// The maxLength includes the NULL character | ||
std::vector<GLchar> infoLog(maxLength); | ||
glGetShaderInfoLog(vertexShader, maxLength, &maxLength, &infoLog[0]); | ||
|
||
// We don't need the shader anymore. | ||
glDeleteShader(vertexShader); | ||
|
||
HZ_CORE_ERROR("{0}", infoLog.data()); | ||
HZ_CORE_ASSERT(false, "Vertex shader compilation failure!"); | ||
return; | ||
} | ||
|
||
// Create an empty fragment shader handle | ||
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); | ||
|
||
// Send the fragment shader source code to GL | ||
// Note that std::string's .c_str is NULL character terminated. | ||
source = fragmentSrc.c_str(); | ||
glShaderSource(fragmentShader, 1, &source, 0); | ||
|
||
// Compile the fragment shader | ||
glCompileShader(fragmentShader); | ||
|
||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &isCompiled); | ||
if (isCompiled == GL_FALSE) | ||
{ | ||
GLint maxLength = 0; | ||
glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &maxLength); | ||
|
||
// The maxLength includes the NULL character | ||
std::vector<GLchar> infoLog(maxLength); | ||
glGetShaderInfoLog(fragmentShader, maxLength, &maxLength, &infoLog[0]); | ||
|
||
// We don't need the shader anymore. | ||
glDeleteShader(fragmentShader); | ||
// Either of them. Don't leak shaders. | ||
glDeleteShader(vertexShader); | ||
|
||
HZ_CORE_ERROR("{0}", infoLog.data()); | ||
HZ_CORE_ASSERT(false, "Fragment shader compilation failure!"); | ||
return; | ||
} | ||
|
||
// Vertex and fragment shaders are successfully compiled. | ||
// Now time to link them together into a program. | ||
// Get a program object. | ||
m_RendererID = glCreateProgram(); | ||
GLuint program = m_RendererID; | ||
|
||
// Attach our shaders to our program | ||
glAttachShader(program, vertexShader); | ||
glAttachShader(program, fragmentShader); | ||
|
||
// Link our program | ||
glLinkProgram(program); | ||
|
||
// Note the different functions here: glGetProgram* instead of glGetShader*. | ||
GLint isLinked = 0; | ||
glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked); | ||
if (isLinked == GL_FALSE) | ||
{ | ||
GLint maxLength = 0; | ||
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength); | ||
|
||
// The maxLength includes the NULL character | ||
std::vector<GLchar> infoLog(maxLength); | ||
glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]); | ||
|
||
// We don't need the program anymore. | ||
glDeleteProgram(program); | ||
// Don't leak shaders either. | ||
glDeleteShader(vertexShader); | ||
glDeleteShader(fragmentShader); | ||
|
||
HZ_CORE_ERROR("{0}", infoLog.data()); | ||
HZ_CORE_ASSERT(false, "Shader link failure!"); | ||
return; | ||
} | ||
|
||
// Always detach shaders after a successful link. | ||
glDetachShader(program, vertexShader); | ||
glDetachShader(program, fragmentShader); | ||
} | ||
|
||
Shader::~Shader() | ||
{ | ||
glDeleteProgram(m_RendererID); | ||
} | ||
|
||
void Shader::Bind() const | ||
{ | ||
glUseProgram(m_RendererID); | ||
} | ||
|
||
void Shader::Unbind() const | ||
{ | ||
glUseProgram(0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace Hazel { | ||
|
||
class Shader | ||
{ | ||
public: | ||
Shader(const std::string& vertexSrc, const std::string& fragmentSrc); | ||
~Shader(); | ||
|
||
void Bind() const; | ||
void Unbind() const; | ||
private: | ||
uint32_t m_RendererID; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[Window][Debug##Default] | ||
Pos=60,60 | ||
Size=400,400 | ||
Collapsed=0 | ||
|
||
[Window][Test] | ||
Pos=60,60 | ||
Size=92,48 | ||
Collapsed=0 | ||
|
||
[Window][ImGui Demo] | ||
ViewportPos=1404,134 | ||
ViewportId=0x080FC883 | ||
Size=550,680 | ||
Collapsed=0 | ||
|
||
[Docking][Data] | ||
|