Finished shader chapter and fix #1
This commit is contained in:
parent
2d230155a7
commit
82649e7648
|
@ -142,8 +142,12 @@
|
||||||
<ClCompile Include="Shaders\Shader.cpp" />
|
<ClCompile Include="Shaders\Shader.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="Shaders\default\default.frag" />
|
<CopyFileToFolders Include="Shaders\default\default.frag">
|
||||||
<None Include="Shaders\default\default.vert" />
|
<FileType>Document</FileType>
|
||||||
|
</CopyFileToFolders>
|
||||||
|
<CopyFileToFolders Include="Shaders\default\default.vert">
|
||||||
|
<FileType>Document</FileType>
|
||||||
|
</CopyFileToFolders>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Exceptions\IOException.h" />
|
<ClInclude Include="Exceptions\IOException.h" />
|
||||||
|
|
|
@ -43,9 +43,9 @@
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="Shaders\default\default.vert">
|
<CopyFileToFolders Include="Shaders\default\default.frag" />
|
||||||
|
<CopyFileToFolders Include="Shaders\default\default.vert">
|
||||||
<Filter>Resource Files</Filter>
|
<Filter>Resource Files</Filter>
|
||||||
</None>
|
</CopyFileToFolders>
|
||||||
<None Include="Shaders\default\default.frag" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -64,10 +64,10 @@ int main()
|
||||||
glfwSetFramebufferSizeCallback(window, OnWindowResize);
|
glfwSetFramebufferSizeCallback(window, OnWindowResize);
|
||||||
|
|
||||||
GLfloat vertices[] = {
|
GLfloat vertices[] = {
|
||||||
0.5f, 0.5f, 0.0f, // top right
|
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right
|
||||||
0.5f, -0.5f, 0.0f, // bottom right
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
|
||||||
-0.5f, -0.5f, 0.0f, // bottom left
|
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left
|
||||||
-0.5f, 0.5f, 0.0f // top left
|
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f // top left
|
||||||
};
|
};
|
||||||
GLuint faces[]{
|
GLuint faces[]{
|
||||||
0, 1, 3,
|
0, 1, 3,
|
||||||
|
@ -87,8 +87,12 @@ int main()
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(faces), faces, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(faces), faces, GL_STATIC_DRAW);
|
||||||
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
// position attribute
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
// colour (or more general, the second vertex Attribute)
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
// I could unbind the VAO, but this is usually not needed because when creating a new VAO we need to bind that to change it anyway, so there shouldn't be a problem
|
// I could unbind the VAO, but this is usually not needed because when creating a new VAO we need to bind that to change it anyway, so there shouldn't be a problem
|
||||||
|
|
||||||
std::string vertexPath = "shaders/default/default.vert";
|
std::string vertexPath = "shaders/default/default.vert";
|
||||||
|
@ -104,7 +108,7 @@ int main()
|
||||||
std::cerr << e.what();
|
std::cerr << e.what();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
|
|
||||||
// main loop
|
// main loop
|
||||||
while(!glfwWindowShouldClose(window))
|
while(!glfwWindowShouldClose(window))
|
||||||
|
@ -115,6 +119,10 @@ int main()
|
||||||
ProcessInput(window);
|
ProcessInput(window);
|
||||||
|
|
||||||
shader->Use();
|
shader->Use();
|
||||||
|
//float time = glfwGetTime();
|
||||||
|
//float greenValue = (sin(time) / 2.0f) + 0.5f;
|
||||||
|
//shader->setFloat("ourColour", 0.0f, greenValue, 0.0f, 0.0f);
|
||||||
|
|
||||||
glBindVertexArray(vertexArrayObject);
|
glBindVertexArray(vertexArrayObject);
|
||||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||||
|
|
||||||
|
|
|
@ -8,18 +8,22 @@
|
||||||
|
|
||||||
namespace Nebulix
|
namespace Nebulix
|
||||||
{
|
{
|
||||||
|
// debugging
|
||||||
const char* vertexShaderSourceCode = "#version 330 core\n"
|
const char* vertexShaderSource = "#version 330 core\n"
|
||||||
"layout (location = 0) in vec3 aPos;\n"
|
"layout (location = 0) in vec3 aPos;\n"
|
||||||
|
"layout (location = 1) in vec3 aColor;\n"
|
||||||
|
"out vec3 ourColor;\n"
|
||||||
"void main()\n"
|
"void main()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
|
" gl_Position = vec4(aPos, 1.0);\n"
|
||||||
|
" ourColor = aColor;\n"
|
||||||
"}\0";
|
"}\0";
|
||||||
const char* fragmentShaderSourceCode = "#version 330 core\n"
|
const char* fragmentShaderSource = "#version 330 core\n"
|
||||||
"out vec4 FragColor;\n"
|
"out vec4 FragColor;\n"
|
||||||
|
"in vec3 ourColor;\n"
|
||||||
"void main()\n"
|
"void main()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
|
" FragColor = vec4(ourColor, 1.0f);\n"
|
||||||
"}\n\0";
|
"}\n\0";
|
||||||
|
|
||||||
Shader::Shader(std::string& vertexShaderfile, std::string& fragmentShaderfile): _vertexFile{vertexShaderfile}, _fragmentFile{fragmentShaderfile}
|
Shader::Shader(std::string& vertexShaderfile, std::string& fragmentShaderfile): _vertexFile{vertexShaderfile}, _fragmentFile{fragmentShaderfile}
|
||||||
|
@ -33,29 +37,29 @@ namespace Nebulix
|
||||||
CreateProgram(vertexShader, fragmentShader);
|
CreateProgram(vertexShader, fragmentShader);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::CompileShadercode(std::ifstream& shaderSource, GLuint& shaderObject, ShaderType type)
|
void Shader::CompileShadercode(std::ifstream& shaderFile, GLuint& shaderObject, ShaderType type)
|
||||||
{
|
{
|
||||||
if (!shaderSource.is_open())
|
if (!shaderFile.is_open())
|
||||||
{
|
{
|
||||||
throw IOException("ERROR::SHADER::CANNOT_OPEN_FILE");
|
throw IOException("ERROR::SHADER::CANNOT_OPEN_FILE");
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* shader;
|
std::string code;
|
||||||
|
const char* shaderCode;
|
||||||
|
|
||||||
shaderSource.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::stringstream sourceCode;
|
std::stringstream shaderStream;
|
||||||
sourceCode << shaderSource.rdbuf();
|
shaderStream << shaderFile.rdbuf();
|
||||||
shaderSource.close();
|
shaderFile.close();
|
||||||
std::string code = sourceCode.str();
|
code = shaderStream.str();
|
||||||
shader = code.c_str();
|
|
||||||
}
|
}
|
||||||
catch (const std::exception&)
|
catch (const std::exception&)
|
||||||
{
|
{
|
||||||
throw Nebulix::IOException("ERROR::SHADER::READ_FILE");
|
throw Nebulix::IOException("ERROR::SHADER::READ_FILE");
|
||||||
}
|
}
|
||||||
|
shaderCode = code.c_str();
|
||||||
|
|
||||||
if (type == ShaderType::Vertex)
|
if (type == ShaderType::Vertex)
|
||||||
shaderObject = glCreateShader(GL_VERTEX_SHADER);
|
shaderObject = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
@ -64,7 +68,7 @@ namespace Nebulix
|
||||||
else
|
else
|
||||||
throw std::runtime_error("ERROR::SHADER::TYPE \nCannot create shader program with the given shader type '" + std::to_string((int)type) + "'");
|
throw std::runtime_error("ERROR::SHADER::TYPE \nCannot create shader program with the given shader type '" + std::to_string((int)type) + "'");
|
||||||
|
|
||||||
glShaderSource(shaderObject, 1, &shader, NULL);
|
glShaderSource(shaderObject, 1, &shaderCode, NULL);
|
||||||
glCompileShader(shaderObject);
|
glCompileShader(shaderObject);
|
||||||
|
|
||||||
int success;
|
int success;
|
||||||
|
|
|
@ -31,9 +31,21 @@ namespace Nebulix
|
||||||
{
|
{
|
||||||
glUniform1i(glGetUniformLocation(shaderId, name.c_str()), value);
|
glUniform1i(glGetUniformLocation(shaderId, name.c_str()), value);
|
||||||
}
|
}
|
||||||
void setFloat(const std::string& name, float value) const
|
void setFloat(const std::string& name, float x) const
|
||||||
{
|
{
|
||||||
glUniform1f(glGetUniformLocation(shaderId, name.c_str()), value);
|
glUniform1f(glGetUniformLocation(shaderId, name.c_str()), x);
|
||||||
|
}
|
||||||
|
void setFloat(const std::string& name, float x, float y) const
|
||||||
|
{
|
||||||
|
glUniform2f(glGetUniformLocation(shaderId, name.c_str()), x, y);
|
||||||
|
}
|
||||||
|
void setFloat(const std::string& name, float x, float y, float z) const
|
||||||
|
{
|
||||||
|
glUniform3f(glGetUniformLocation(shaderId, name.c_str()), x, y, z);
|
||||||
|
}
|
||||||
|
void setFloat(const std::string& name, float x, float y, float z, float w) const
|
||||||
|
{
|
||||||
|
glUniform4f(glGetUniformLocation(shaderId, name.c_str()), x, y, z, w);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 ourColour;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
FragColor = vec4(ourColour, 1.0);
|
||||||
}
|
}
|
|
@ -1,7 +1,11 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColour;
|
||||||
|
|
||||||
|
out vec3 ourColour;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
ourColour = aColour;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user