Skip to content

Commit

Permalink
Our first shader.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCherno committed May 26, 2019
1 parent f3e38fe commit 76ae3b8
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Hazel/Hazel.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<ClInclude Include="src\Hazel\Log.h" />
<ClInclude Include="src\Hazel\MouseButtonCodes.h" />
<ClInclude Include="src\Hazel\Renderer\GraphicsContext.h" />
<ClInclude Include="src\Hazel\Renderer\Shader.h" />
<ClInclude Include="src\Hazel\Window.h" />
<ClInclude Include="src\Platform\OpenGL\OpenGLContext.h" />
<ClInclude Include="src\Platform\Windows\WindowsInput.h" />
Expand Down Expand Up @@ -531,6 +532,7 @@
<ClCompile Include="src\Hazel\Layer.cpp" />
<ClCompile Include="src\Hazel\LayerStack.cpp" />
<ClCompile Include="src\Hazel\Log.cpp" />
<ClCompile Include="src\Hazel\Renderer\Shader.cpp" />
<ClCompile Include="src\Platform\OpenGL\OpenGLContext.cpp" />
<ClCompile Include="src\Platform\Windows\WindowsInput.cpp" />
<ClCompile Include="src\Platform\Windows\WindowsWindow.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions Hazel/Hazel.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,7 @@
</ClInclude>
<ClInclude Include="src\Hazel\Renderer\GraphicsContext.h" />
<ClInclude Include="src\Platform\OpenGL\OpenGLContext.h" />
<ClInclude Include="src\Hazel\Renderer\Shader.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Hazel\Application.cpp">
Expand Down Expand Up @@ -1217,5 +1218,6 @@
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\Platform\OpenGL\OpenGLContext.cpp" />
<ClCompile Include="src\Hazel\Renderer\Shader.cpp" />
</ItemGroup>
</Project>
30 changes: 30 additions & 0 deletions Hazel/src/Hazel/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ namespace Hazel {

unsigned int indices[3] = { 0, 1, 2 };
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

std::string vertexSrc = R"(
#version 330 core
layout(location = 0) in vec3 a_Position;
out vec3 v_Position;
void main()
{
v_Position = a_Position;
gl_Position = vec4(a_Position, 1.0);
}
)";

std::string fragmentSrc = R"(
#version 330 core
layout(location = 0) out vec4 color;
in vec3 v_Position;
void main()
{
color = vec4(v_Position * 0.5 + 0.5, 1.0);
}
)";

m_Shader.reset(new Shader(vertexSrc, fragmentSrc));
}

Application::~Application()
Expand Down Expand Up @@ -84,6 +113,7 @@ namespace Hazel {
glClearColor(0.1f, 0.1f, 0.1f, 1);
glClear(GL_COLOR_BUFFER_BIT);

m_Shader->Bind();
glBindVertexArray(m_VertexArray);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);

Expand Down
3 changes: 3 additions & 0 deletions Hazel/src/Hazel/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "Hazel/ImGui/ImGuiLayer.h"

#include "Hazel/Renderer/Shader.h"

namespace Hazel {

class HAZEL_API Application
Expand Down Expand Up @@ -36,6 +38,7 @@ namespace Hazel {
LayerStack m_LayerStack;

unsigned int m_VertexArray, m_VertexBuffer, m_IndexBuffer;
std::unique_ptr<Shader> m_Shader;
private:
static Application* s_Instance;
};
Expand Down
127 changes: 127 additions & 0 deletions Hazel/src/Hazel/Renderer/Shader.cpp
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);
}

}
19 changes: 19 additions & 0 deletions Hazel/src/Hazel/Renderer/Shader.h
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;
};

}
18 changes: 18 additions & 0 deletions Sandbox/imgui.ini
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]

0 comments on commit 76ae3b8

Please sign in to comment.