PDA

View Full Version : Problem when passing a matrix4f to my vertex shader.



Lolilolight
07-15-2014, 05:10 AM
Hi!

I'm trying to pass an array of 16 float to my shader (which represent a 4*4 matrix), but My shader doesn't draw nothing event if I pass an identity matrix.

Here is the code :



#include <GL/glew.h>
#include <GL/gl.h>
#include <SFML/Window.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include "odfaeg/Graphics/shader.h"
#include "odfaeg/Graphics/projMatrix.h"
GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){

// Create the shaders
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

// Read the Vertex Shader code from the file
std::string VertexShaderCode;
std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
if(VertexShaderStream.is_open())
{
std::string Line = "";
while(getline(VertexShaderStream, Line))
VertexShaderCode += "\n" + Line;
VertexShaderStream.close();
}

// Read the Fragment Shader code from the file
std::string FragmentShaderCode;
std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
if(FragmentShaderStream.is_open()){
std::string Line = "";
while(getline(FragmentShaderStream, Line))
FragmentShaderCode += "\n" + Line;
FragmentShaderStream.close();
}

GLint Result = GL_FALSE;
int InfoLogLength;

// Compile Vertex Shader
printf("Compiling shader : %s\n", vertex_file_path);
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);

// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> VertexShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);

// Compile Fragment Shader
printf("Compiling shader : %s\n", fragment_file_path);
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
glCompileShader(FragmentShaderID);

// Check Fragment Shader
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);

// Link the program
fprintf(stdout, "Linking program\n");
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);

// Check the program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> ProgramErrorMessage(std::max(InfoLogLength, int(1)) );
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);

glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);

return ProgramID;
}
int main(int argc, char* argv[]) {
sf::Window window(sf::VideoMode(800, 600, 32), "Modern OpenGL",sf::Style::Default,sf::ContextSettings(24, 8, 4, 3, 3));
glewExperimental = GL_TRUE;
GLenum status = glewInit();
if (status == GLEW_OK)
{
std::cout<< "Glew initialized successfully."<<std::endl;
}
else
{
std::cerr<< "Failed to initialize GLEW, " << glewGetErrorString(status) << std::endl;
}
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
odfaeg::ProjMatrix pm;
pm.reset();
//pm.setGlOrthoMatrix(-400, 400, -300, 300, -100, 900);
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader" );
GLuint location = glGetUniformLocation(programID, "mvp");
if (location == -1)
std::cout<<"param not found in shader!"<<std::endl;
float* matrix = pm.get3DMatrix().toGlMatrix();
glUniformMatrix4fv(location, 1, GL_TRUE,pm.get3DMatrix().toGlMatrix());
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
while (window.isOpen()) {
sf::Event event;
while(window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
glClear(GL_COLOR_BUFFER_BIT);
// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
0.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, -1.0f, 0.0f
};
// This will identify our vertex buffer
GLuint vertexbuffer;

// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);

// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Use our shader
glUseProgram(programID);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

glDisableVertexAttribArray(0);
window.display();
}
}
glDeleteVertexArrays(1, &VertexArrayID);
return 0;
}


Vertex shader :



#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
uniform mat4 mvp;
void main () {
gl_Position = mvp * vec4(vertexPosition_modelspace.xyz, 1);
}


Fragment shader :



#version 330 core
out vec3 color;

void main(){
color = vec3(1,0,0);
}


Why my triangle isn't draw when I pass my matrix. ?

Lolilolight
07-15-2014, 05:22 AM
I've solved this, we must bind the shader before changind parameters, I simply didn't know that.