I am trying to abstract OpenGL shader but shader compilation fails even though it is a tutorial shader. The main problem is that when I run it shader compilation fails and it shoots out arbitrary text in the console instead of some error message that could help me out fixing the error.
this is my Shader.h
#include "glm/vec2.hpp"
#include "glm/vec3.hpp"
#include "glm/vec4.hpp"
#include "glm/gtc/type_ptr.hpp"
#include <iostream>
class Shader
{
public:
Shader(std::string vert, std::string frag);
void setInt(const char* uniformName, int data);
void set2Int(const char* uniformName, glm::ivec2 data);
void set3Int(const char* uniformName, glm::ivec3 data);
void set4Int(const char* uniformName, glm::ivec4 data);
void setFloat(const char* uniformName, float data);
void set2Float(const char* uniformName, glm::vec2 data);
void set3Float(const char* uniformName, glm::vec3 data);
void set4Float(const char* uniformName, glm::vec4 data);
void setMat3x3(const char* uniformName, glm::mat3 data);
void setMat4x4(const char* uniformName, glm::mat4 data);
void Bind();
void UnBind();
~Shader();
private:
unsigned int makeShader(unsigned int type, std::string data);
unsigned int id;
};
this is my Shader.cpp
#include "Shader.h"
#include "glew/glew.h"
Shader::Shader(std::string vert, std::string frag)
{
unsigned int vertexShader = makeShader(GL_VERTEX_SHADER, vert);
unsigned int fragmentShader = makeShader(GL_FRAGMENT_SHADER, frag);
id = glCreateProgram();
glAttachShader(id, vertexShader);
glAttachShader(id, fragmentShader);
glLinkProgram(id);
glValidateProgram(id);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
void Shader::setInt(const char* uniformName, int data)
{
int loc = glGetUniformLocation(id, uniformName);
glUniform1iv(loc, 1, &data);
}
void Shader::set2Int(const char* uniformName, glm::ivec2 data)
{
int loc = glGetUniformLocation(id, uniformName);
glUniform1iv(loc, 2, glm::value_ptr(data));
}
void Shader::set3Int(const char* uniformName, glm::ivec3 data)
{
int loc = glGetUniformLocation(id, uniformName);
glUniform1iv(loc, 3, glm::value_ptr(data));
}
void Shader::set4Int(const char* uniformName, glm::ivec4 data)
{
int loc = glGetUniformLocation(id, uniformName);
glUniform1iv(loc, 4, glm::value_ptr(data));
}
void Shader::setFloat(const char* uniformName, float data)
{
int loc = glGetUniformLocation(id, uniformName);
glUniform1fv(loc, 1, &data);
}
void Shader::set2Float(const char* uniformName, glm::vec2 data)
{
int loc = glGetUniformLocation(id, uniformName);
glUniform1fv(loc, 2, glm::value_ptr(data));
}
void Shader::set3Float(const char* uniformName, glm::vec3 data)
{
int loc = glGetUniformLocation(id, uniformName);
glUniform1fv(loc, 3, glm::value_ptr(data));
}
void Shader::set4Float(const char* uniformName, glm::vec4 data)
{
int loc = glGetUniformLocation(id, uniformName);
glUniform1fv(loc, 4, glm::value_ptr(data));
}
void Shader::setMat3x3(const char* uniformName, glm::mat3 data)
{
int loc = glGetUniformLocation(id, uniformName);
glUniformMatrix3fv(loc, 1, GL_FALSE, glm::value_ptr(data));
}
void Shader::setMat4x4(const char* uniformName, glm::mat4 data)
{
int loc = glGetUniformLocation(id, uniformName);
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(data));
}
void Shader::Bind()
{
glUseProgram(id);
}
void Shader::UnBind()
{
glUseProgram(0);
}
Shader::~Shader()
{
glDeleteProgram(id);
}
unsigned int Shader::makeShader(unsigned int type, std::string data)
{
unsigned int shaderId = glCreateShader(type);
const char* src = data.c_str();
glShaderSource(shaderId, 1, &src, nullptr);
glCompileShader(shaderId);
int didCompiled = 0;
glGetProgramiv(shaderId, GL_COMPILE_STATUS, &didCompiled);
if (didCompiled == GL_FALSE) {
int length = 0;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*)alloca(sizeof(char) * length);
glGetShaderInfoLog(shaderId, length, &length, message);
std::cout << (type == GL_VERTEX_SHADER ? "Vertex Shader" : "Fragment Shader") << " Compilation Failed: " << message << std::endl;
glDeleteShader(shaderId);
shaderId = 0;
}
return shaderId;
}
here is my code for Vertex Shader
#version 330 core
layout (location=0) in vec2 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, 1.0f, 1.0f);
}
here is my code for Fragment Shader
#version 330 core
out vec4 FragColor;
uniform vec4 uColor;
void main()
{
FragColor = uColor;
}
is there something wrong with the way I am abstracting things or it is the shader code itself
CodePudding user response:
In the makeShader function:glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
Change id
to shaderId
.
Fix it and try to get correct fail message.