Finished shader chapter and fix #1

This commit is contained in:
Daniel 2023-07-18 10:24:18 +02:00
parent 2d230155a7
commit 82649e7648
7 changed files with 66 additions and 32 deletions

View File

@ -142,8 +142,12 @@
<ClCompile Include="Shaders\Shader.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="Shaders\default\default.frag" />
<None Include="Shaders\default\default.vert" />
<CopyFileToFolders Include="Shaders\default\default.frag">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="Shaders\default\default.vert">
<FileType>Document</FileType>
</CopyFileToFolders>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Exceptions\IOException.h" />

View File

@ -43,9 +43,9 @@
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="Shaders\default\default.vert">
<CopyFileToFolders Include="Shaders\default\default.frag" />
<CopyFileToFolders Include="Shaders\default\default.vert">
<Filter>Resource Files</Filter>
</None>
<None Include="Shaders\default\default.frag" />
</CopyFileToFolders>
</ItemGroup>
</Project>

View File

@ -64,10 +64,10 @@ int main()
glfwSetFramebufferSizeCallback(window, OnWindowResize);
GLfloat vertices[] = {
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f // top left
};
GLuint faces[]{
0, 1, 3,
@ -87,8 +87,12 @@ int main()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
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);
// 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
std::string vertexPath = "shaders/default/default.vert";
@ -104,7 +108,7 @@ int main()
std::cerr << e.what();
return -1;
}
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// main loop
while(!glfwWindowShouldClose(window))
@ -115,6 +119,10 @@ int main()
ProcessInput(window);
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);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

View File

@ -8,18 +8,22 @@
namespace Nebulix
{
const char* vertexShaderSourceCode = "#version 330 core\n"
// debugging
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"out vec3 ourColor;\n"
"void main()\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";
const char* fragmentShaderSourceCode = "#version 330 core\n"
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec3 ourColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
" FragColor = vec4(ourColor, 1.0f);\n"
"}\n\0";
Shader::Shader(std::string& vertexShaderfile, std::string& fragmentShaderfile): _vertexFile{vertexShaderfile}, _fragmentFile{fragmentShaderfile}
@ -33,29 +37,29 @@ namespace Nebulix
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");
}
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
{
std::stringstream sourceCode;
sourceCode << shaderSource.rdbuf();
shaderSource.close();
std::string code = sourceCode.str();
shader = code.c_str();
std::stringstream shaderStream;
shaderStream << shaderFile.rdbuf();
shaderFile.close();
code = shaderStream.str();
}
catch (const std::exception&)
{
throw Nebulix::IOException("ERROR::SHADER::READ_FILE");
}
shaderCode = code.c_str();
if (type == ShaderType::Vertex)
shaderObject = glCreateShader(GL_VERTEX_SHADER);
@ -64,7 +68,7 @@ namespace Nebulix
else
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);
int success;

View File

@ -31,9 +31,21 @@ namespace Nebulix
{
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:

View File

@ -1,7 +1,9 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColour;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
FragColor = vec4(ourColour, 1.0);
}

View File

@ -1,7 +1,11 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColour;
out vec3 ourColour;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
gl_Position = vec4(aPos, 1.0);
ourColour = aColour;
}