PDA

View Full Version : Problem with Shader



Zion-Reload
05-13-2017, 06:02 AM
Hi,

I can't run properly the following code with shaders. If a activate the "old" opengl approach it work just fine.
I can move the camera around and everything work as it should be (Is a simply square in the plane XY with lengths 2000).

I'm using the last version available for glfw, glew and glm on a Windows 10, x64 machine



glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1, 1, 1, 1);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_FRONT_AND_BACK);
glDrawBuffer(GL_FRONT_AND_BACK);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

m_perspective = glm::perspective(5.0f, m_sizeEvent.width() / (float)m_sizeEvent.height(), 10.0f, 10000.0f);
m_lookAt = glm::lookAt(m_camera.position(), m_camera.lookingAt(), glm::vec3(0.0f, 0.0f, -1.0f));;

if (true)
{
//new opengl
glm::mat4 transformation = m_perspective * m_lookAt;

for (Meshes::Item &mesh : m_meshes)
mesh->render(transformation);
}
else
{
// old opengl
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMultMatrixf(&m_perspective[0][0]);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrixf(&m_lookAt[0][0]);

for (Meshes::Item &mesh : m_meshes)
mesh->oldRender();
}


I can read the shaders without any exception as follow:


Shader::Shader(const std::string& verteShader, const std::string& fragmentShader)
{
m_program = glCreateProgram();
m_shaders[VERTEX_SHADER] = createShader(verteShader, GL_VERTEX_SHADER);
m_shaders[FRAGMENT_SHADER] = createShader(fragmentShader, GL_FRAGMENT_SHADER);

glAttachShader(m_program, m_shaders[VERTEX_SHADER]);
glAttachShader(m_program, m_shaders[FRAGMENT_SHADER]);

glBindAttribLocation(m_program, 0, "position");

glLinkProgram(m_program);
checkShaderError(m_program, GL_LINK_STATUS, true, "Error: glProgram linking failed ");

glValidateProgram(m_program);
checkShaderError(m_program, GL_VALIDATE_STATUS, true, "Error: glProgram validating failed ");

m_transformation[TRANSFORMATION] = glGetUniformLocation(m_program, "transform");

if (m_transformation[TRANSFORMATION] < 0)
throw Exception("transformation variable can not be set in the shader!");
}
....

// public interface
void Shader::bind(const glm::mat4& transformation)
{
glUseProgram(m_program);


glUniformMatrix4fv(m_transformation[TRANSFORMATION], 1, GL_FALSE, &transformation[0][0]);

}

// private interface
GLuint Shader::createShader(const std::string& text, unsigned int type)
{
GLuint shader = glCreateShader(type);

if (shader == 0)
throw Exception::create("Error compiling shader type: {0}", type);

const GLchar* shaderSourceStrings[1];
GLint shaderSourcesStringLength[1];

shaderSourceStrings[0] = text.c_str();
shaderSourcesStringLength[0] = (GLint)text.length();

glShaderSource(shader, 1, shaderSourceStrings, shaderSourcesStringLength);
glCompileShader(shader);

checkShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!");

return shader;
}

void Shader::checkShaderError(GLuint shader, GLuint flag, bool isProgram, std::string errorMessage)
{
GLint success = GL_FALSE;
GLchar error[3024] = { 0 };

if (isProgram)
glGetProgramiv(shader, flag, &success);
else
glGetShaderiv(shader, flag, &success);

if (success == GL_FALSE)
{
if (isProgram)
glGetProgramInfoLog(shader, sizeof(error), NULL, error);
else
glGetShaderInfoLog(shader, sizeof(error), NULL, error);

throw Exception::create("{0} : {1}", errorMessage, error);

}
}


my vertexShader code:


#version 400

in vec3 position;

uniform mat4 transform;

void main()
{
gl_Position = transform * vec4(position,0);
}


my fragment code:


#version 400


void main()
{
gl_FragColor = vec4(0.0,0.0,1.0,0.5);
}



my mesh code:


void xMesh::render(const glm::mat4 &transformation)
{
if (m_vertexCount == 0 || m_shader == nullptr)
return;

m_shader->bind(transformation);

glBindVertexArray(m_vertexArrayObject);
glDrawArrays(GL_TRIANGLES, 0, m_vertexCount);
glBindVertexArray(0);
}

void xMesh::loadMesh(xVertices &vertices)
{
m_vertexCount = (int)vertices.size();

glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);

glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(xVertex)*m_vertexCount, &vertices[0], GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindVertexArray(0);
}


any ideas, what I'm doing wrong?