转换错误

时间:2016-08-02 00:02:41

标签: c++ opengl

我正在使用' thebennybox'和他的#34;现代OpenGl教程简介"在卡在视频#5之前,他开始应用矩阵来操纵他的顶点的位置,但是我已经把它放到视频的末尾并应用了他所有的变化,但是我的着色器似乎没有应用他的教程显示的转换,我的三角形根本不动。我想知道我错过了什么或我哪里出错了所以我可以更好地理解矩阵。

我没有包含任何预处理程序指令,但在构建程序时没有收到任何错误。

这是我的Transform.h

using namespace std;

class Transform
{
public:

Transform(const glm::vec3& pos = glm::vec3(), const glm::vec3& rot = glm::vec3(), const glm::vec3& scale = glm::vec3(1.0f, 1.0f, 1.0f)) :
m_pos(pos),
m_rot(rot),
m_scale(scale) {}

inline glm::mat4 GetModel() const
{
    glm::mat4 posMatrix = glm::translate(m_pos);
    glm::mat4 rotXMatrix = glm::rotate(m_rot.x, glm::vec3(1, 0, 0));
    glm::mat4 rotYMatrix = glm::rotate(m_rot.y, glm::vec3(0, 1, 0));
    glm::mat4 rotZMatrix = glm::rotate(m_rot.z, glm::vec3(0, 0, 1));
    glm::mat4 scaleMatrix = glm::scale(m_scale);

    glm::mat4 rotMatrix = rotZMatrix * rotYMatrix * rotXMatrix;

    return  scaleMatrix * rotMatrix * posMatrix;

}

inline glm::vec3& GetPos() { return m_pos; }
inline glm::vec3& GetRot() { return m_rot; }
inline glm::vec3& GetScale() { return m_scale; }

inline void SetPos(glm::vec3& pos) { m_pos = pos; }
inline void SetRot(glm::vec3& rot) { m_rot = rot; }
inline void SetScale(glm::vec3& scale) { m_scale = scale; }

private:

glm::vec3 m_pos;
glm::vec3 m_rot;
glm::vec3 m_scale;

};

这是我的shader.h

using namespace std;

class Shader
{
public:
Shader(const string& fileName);

void Bind();
void Update(const Transform& transform);

virtual ~Shader();

private:
static const unsigned int NUM_SHADERS = 2;

void checkShaderError(GLuint shader, GLuint flag, bool isProgram, const string& errorMessage);

GLuint createShader(const string& fileName, unsigned int type);
string loaderShader(const string& text);

enum
{
    TRANSFORM_U,

    NUM_UNIFORMS

};

GLuint m_program;
GLuint m_shaders[NUM_SHADERS];
GLuint m_uniforms[NUM_UNIFORMS];
};

这是我的shader.cpp

using namespace std;

static GLuint createShader(const std::string& text, unsigned int type);
void checkShaderError(GLuint shader, GLuint flag, bool isProgram, const     std::string& errorMessage);
string loadShader(const string& fileName);

Shader::Shader(const string& fileName)
{
m_program = glCreateProgram();

m_shaders[0] = createShader(loaderShader(fileName + ".vs"), GL_VERTEX_SHADER);
m_shaders[1] = createShader(loaderShader(fileName + ".fs"), GL_FRAGMENT_SHADER);

for (unsigned int i = 0; i < NUM_SHADERS; i++)
{
    cout << i << endl;
    glAttachShader(m_program, m_shaders[i]);

}

glBindAttribLocation(m_program, 0, "position");
glBindAttribLocation(m_program, 1, "texCoord");


glLinkProgram(m_program);
checkShaderError(m_program, GL_LINK_STATUS, true, "Error: Linking Program!");

glValidateProgram(m_program);
checkShaderError(m_program, GL_VALIDATE_STATUS, true, "Error: Shader Invalid!");

m_uniforms[TRANSFORM_U] = glGetUniformLocation(m_program, "transform");


}

Shader::~Shader()
{
for (unsigned int i = 0; i < NUM_SHADERS; i++)
{
    glDetachShader(m_program, m_shaders[i]);
    glDeleteShader(m_shaders[i]);

}
glDeleteProgram(m_program);

}
void Shader::Bind()
{

glUseProgram(m_program);

}
void Shader::Update(const Transform& transform)
{
glm::mat4 model = transform.GetModel();
glUniformMatrix4fv(m_uniforms[TRANSFORM_U], 1, GL_FALSE, &model[0][0]);

//to see the changes being applied to the matrix 
cout << model[0][0] << ", " << model[0][1] << ", " 
     << model[0][2] << ", " <<          model[0][3] << endl
     << model[1][0] << ", " << model[1][1] << ", "
     << model[1][2] << ", " << model[1][3] << endl
     << model[2][0] << ", " << model[2][1] << ", " 
     << model[2][2] << ", " << model[2][3] << endl
     << model[3][0] << ", " << model[3][1] << ", " 
     << model[3][2] << ", " << model[3][3] << endl;


 }
 string Shader::loaderShader(const string& fileName)
 {
ifstream input_stream;
string output, line;

input_stream.open((fileName).c_str());

if (input_stream.is_open())
{

    while (input_stream.good())
    {
        getline(input_stream, line);
        output.append(line + "\n");
    }

}
else
    cerr << "Unable to load Shader" << endl;

return output;

}
void Shader::checkShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& errorMessage)
{
GLint success = 0;
GLchar error[1024] = { 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);

    std::cerr << errorMessage << ": '" << error << "'" << std::endl;
}
}

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

if (shader == 0)
    std::cerr << "Error compiling shader type " << type << std::endl;

const GLchar* p[1];
p[0] = text.c_str();
GLint lengths[1];
lengths[0] = text.length();


glShaderSource(shader, 1, p, lengths);
glCompileShader(shader);

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

return shader;
}

这是我的Main.cpp

using namespace std;

int main(int argc, char* argv[])
{
Vertex vertices[] =
{
    Vertex(glm::vec3(-0.5,-0.5,0),glm::vec2(0.0, 0.0)),
    Vertex(glm::vec3(0, 0.5,0),glm::vec2(0.5, 1.0)),
    Vertex(glm::vec3(0.5, -0.5, 0),glm::vec2(1.0, 0.0))
};

Display display(800, 600, "Game Engine!");

Mesh mesh(vertices, sizeof(vertices) / sizeof(vertices[0]));
Shader shader("./res/basicShader");
Texture texture("./res/bricks.jpg");

Transform transform;

float counter = 0.0f;

while (!display.isClosed())
{
    display.Clear(0.0f, 0.0f, 0.3f, 1.0f);

    transform.GetPos().x = sinf(counter);

    cout <<"Counter:" << counter << endl;
    cout << "m_pos.x: " << transform.GetPos().x << endl;

    shader.Bind();
    shader.Update(transform);
    texture.Bind(0);

    mesh.Draw();

    display.Update();
    counter += 0.1f;
}

return 0;
}

更新:这是我的顶点着色器

#version 120

attribute vec3 position;
attribute vec2 texCoord;

varying vec2 texCoord0;

uniform mat4 transform;

void main()
{
 gl_Position = transform * vec4(position, 1.0);
 texCoord0 = texCoord;

}

0 个答案:

没有答案