2

I am learning openGL. I got a problem when I try to translation transformation. The result isn't translation as I wanted. It is just a static image like this enter image description here

And this is my code

TranslationTransformation.cpp

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <stdio.h>
#include <math.h>

#define NUM_VERTICES 3
#define NUM_INDICES 3
#define BUFFER_OFFSET(i) ((char *)NULL + (i))

GLuint vbo, gWorldLocation, indexBufferID, shaderProgramID, positionID;
static float Scale;
struct Vector3f{
    GLfloat x;
    GLfloat y;
    GLfloat z;
    Vector3f(){};
    Vector3f(GLfloat _x, GLfloat _y, GLfloat _z){
        x = _x;
        y = _y;
        z = _z;
    }
};

struct Vector4f{
    GLfloat x;
    GLfloat y;
    GLfloat z;
    GLfloat w;
    Vector4f(){};
    Vector4f(GLfloat _x, GLfloat _y, GLfloat _z, GLfloat _w){
        x = _x;
        y = _y;
        z = _z;
        w = _w;
    }
};

struct Matrix4f{
    float m[4][4];
}; 

static char* readFile(const char* filename) {
    // Open the file
    FILE* fp = fopen (filename, "r");
    // Move the file pointer to the end of the file and determing the length
    fseek(fp, 0, SEEK_END);
    long file_length = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    char* contents = new char[file_length+1];
    // zero out memory
    for (int i = 0; i < file_length+1; i++) {
        contents[i] = 0;
    }
    // Here's the actual read
    fread (contents, 1, file_length, fp);
    // This is how you denote the end of a string in C
    contents[file_length+1] = '\0';
    fclose(fp);
    return contents;
}

bool compiledStatus(GLuint shaderID){
    GLint success = 0;
    gWorldLocation = glGetUniformLocation(shaderProgramID, "gWorld");
    glGetShaderiv(shaderID, GL_COMPILE_STATUS, &success);
    if(success){
        return true;
    }
    else{
        GLint logLength;
        glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &logLength);
        char* msgBuffer = new char[logLength];
        glGetShaderInfoLog(shaderID, logLength, NULL, msgBuffer);
        printf("Error: %s \n", msgBuffer);
        delete(msgBuffer);
        return false;
    }
}

GLuint makeVertexShader(const char* shaderSource){
    GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShaderID, 1, (const GLchar**)&shaderSource, NULL);
    glCompileShader(vertexShaderID);
    if(compiledStatus(vertexShaderID)==true){
        return vertexShaderID;
    }
    else 
        return -1;
}

GLuint makeFragmentShader(const char* shaderSource){
    GLuint fragmenShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmenShaderID, 1, (const char**)&shaderSource, NULL);
    glCompileShader(fragmenShaderID);
    if(compiledStatus(fragmenShaderID)==true){
        return fragmenShaderID;
    }
    else
        return -1;
}

GLuint makeShaderProgram(GLuint vertexShaderID, GLuint fragmentShaderID){
    GLuint shaderID = glCreateProgram();
    glAttachShader(shaderID, vertexShaderID);
    glAttachShader(shaderID, fragmentShaderID);
    glLinkProgram(shaderID);
    return shaderID;
}

void createVertexBuffer(){
    Vector3f Vertices[3];
    Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
    Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
    Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}

void render() {
    glClear(GL_COLOR_BUFFER_BIT);
    glUseProgram(shaderProgramID);

    Scale += 0.001f;
    Matrix4f World;
    World.m[0][0] = 1.0f; World.m[0][1] = 0.0f; World.m[0][2] = 0.0f; World.m[0][3] = sinf(Scale);
    World.m[1][0] = 0.0f; World.m[1][1] = 1.0f; World.m[1][2] = 0.0f; World.m[1][3] = 0.0f;
    World.m[2][0] = 0.0f; World.m[2][1] = 0.0f; World.m[2][2] = 1.0f; World.m[2][3] = 0.0f;
    World.m[3][0] = 0.0f; World.m[3][1] = 0.0f; World.m[3][2] = 0.0f; World.m[3][3] = 1.0f;
    glUniformMatrix4fv(gWorldLocation, 1, GL_TRUE, &World.m[0][0]);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glutSwapBuffers();

}
int main(int argc, char** argv){

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Index Buffers");

    glutDisplayFunc(render);
    glewInit();

    createVertexBuffer();


    Scale = 0.0f;
    Matrix4f World;
    World.m[0][0] = 1.0f; World.m[0][1] = 0.0f; World.m[0][2] = 0.0f; World.m[0][3] = sinf(Scale);
    World.m[1][0] = 0.0f; World.m[1][1] = 1.0f; World.m[1][2] = 0.0f; World.m[1][3] = 0.0f;
    World.m[2][0] = 0.0f; World.m[2][1] = 0.0f; World.m[2][2] = 1.0f; World.m[2][3] = 0.0f;
    World.m[3][0] = 0.0f; World.m[3][1] = 0.0f; World.m[3][2] = 0.0f; World.m[3][3] = 1.0f;
    char* vertexShaderSource = readFile("vertexShader.vsh");
    char* fragmentShaderSource = readFile("fragmentShader.fsh");

    GLuint vertexShaderID = makeVertexShader(vertexShaderSource);
    GLuint fragmenShaderID = makeFragmentShader(fragmentShaderSource);

    GLuint shaderProgramID = makeShaderProgram(vertexShaderID, fragmenShaderID);

    // Find the position of the variables in the shader
    gWorldLocation = glGetUniformLocation(shaderProgramID, "gWorld");
    positionID = glGetAttribLocation(shaderProgramID, "Position");
    glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glUseProgram(shaderProgramID);
    glEnableVertexAttribArray(positionID);
    glutMainLoop();
    return 0;
}

vertexShader.vsh

#version 330

in vec3 Position;

uniform mat4 gWorld;

void main()
{
    gl_Position = gWorld * vec4(Position, 1.0);
}

fragmentShader.fsh

#version 330

out vec4 FragColor;

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

Can you help me solve this problem. Thank you very much!

Robotic Vn
  • 437
  • 2
  • 7
  • 20
  • 1
    It might just move very slowly. At sixty FPS you wouldn't necessarily notice it in one second. [graph of the movement](https://www.google.com/search?q=sin(0.001*x)) – Joonazan Aug 25 '15 at 10:38
  • You can try setting transpose to `GL_FALSE`, if you haven't already. – Joonazan Aug 25 '15 at 10:43
  • Be aware of the fact, that your variable `Scale` is used to translate things in y-direction and not to scale your data. – DanceIgel Aug 25 '15 at 11:40
  • I learn from this tutorial http://ogldev.atspace.co.uk/www/tutorial06/tutorial06.html . I set transpose to GL_TRUE and use Scale similar to above tutorial. But I can not got correct result – Robotic Vn Aug 25 '15 at 16:17
  • Could you help me fix this problem? Thank you – Robotic Vn Aug 27 '15 at 16:38

0 Answers0