Home>

The code below shows that I implement a rotation, scale, and translation matrix in a shader, prepare a view matrix, and calculate everything. Why is nothing displayed on the screen? The coordinates of the vertices are correct in the glVertex3f ();function. Am I forgetting something?

Implements a matrix of rotation, scale, and translation
Implement view matrix
Pass the value in the shader
Calculated with shader
GlDrawArrays ();

* The code enclosed in the comment section ///// is the corresponding code.
Reference site: https://tokoik.github.io/GLFWdraft.pdf (Formula moved to page 163 (view matrix))
Github: https://github.com/Shigurechan/project

#version 400
// Vertex shader
in vec3 position;// Vertex coordinates
uniform mat4 scale;// scale matrix
uniform mat4 rotate;// rotation matrix
uniform mat4 move;// translation matrix
uniform mat4 MP;

void main ()
{
    vec4 t = vec4 (position, 1.0);
    mat4 M = scale * rotate * move;

    gl_Position = MP * M * t;
}
#ifndef ___GMAE_H_
#define ___GMAE_H_
#define WIDTH 640
#define HEIGHT 400
#include "stdio.h"
#include<fstream>#include<sstream>#include<iostream>
#include "GLEW/include/GL/glew.h"
#include "gl/GL.h"
#include "GLFW/include/GLFW/glfw3.h"
#include "Vector.hpp"
// #include "Vector.hpp"

class Game
{
public:
    Vector3 cv;
    Game ();
    bool Initialization ();
    bool RunLoop ();
    void Update ();
    void GenerateOutput ();

private: private:
    bool mIsRunLoop;// Main loop

    Vector3 pos;
////////////////////////////////////////////////// ////////////////////////////////////////////////// /////
    // View matrix
    GLfloat view [4] [4] = {
        {1,2,6,0},


        {0,0,0,0},


        {0,0,0,0},


        {0,0,0,1}
    };
    // projection
    GLfloat projection [4] [4] = {
        {0,0,0,0},


        {0,0,0,0},


        {0,0,0,0},


        {0,0,0,0},


    };



    GLfloat vet [6] [3] = {0};
    Vector3 v [6] = {0};

    // Vertex buffer
    GLfloat Vertex [6] [3] =
    {
        {-0.5, 0.5, 1.0},


        {-0.5, -0.5, 1.0},


        {0.5, -0.5, 1.0},


        {-0.5, 0.5, 1.0},{0.5, 0.5, 1.0},


        {0.5, -0.5, 1.0}
    };



     // Scale matrix
     const GLfloat scale [4] [4] = {
         {1,0,0,0},


         {0,1,0,0},


         {0,0,1,0},


         {0,0,0,1}
     };

     //rotation
     float r = 1;
     const GLfloat rotate [4] [4] = {
             (pos.x * pos.x + (1 --cos (r)) + cos (r)),
             (pos.x * pos.y + (1 --cos (r)) + pos.z * sin (r))
            , (pos.x * pos.z + (1 --cos (r)) --pos.y * sin (r)), 0,

             (pos.x * pos.x + (1 --cos (r)) + sin (r)),
             (pos.y * pos.y + (1 --cos (r)) + cos (r)),
             (pos.y * pos.z + (1 --cos (r)) + pos.x * sin (r)), 0,

             (pos.x * pos.z + (1 --cos (r)) --pos.z * sin (r)),
             (pos.y * pos.z + (1 --cos (r)) --pos.x * sin (r)),
             (pos.z * pos.z + (1 --cos (r)) + cos (r)), 0,
     };

     // Translate
     float mx = 0;
     float my = 0;
     float mz = 0;
     const GLfloat move [4] [4] =
     {
         1,0,0,mx,
         0,1,0, my,
         0,0,1, mz,
         0,0,0,1,
     };


////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////
    GLuint vbo = 0;// Vertex array buffer object name
    GLuint vao = 0;// Vertex buffer object name
    GLFWwindow * Window = nullptr;// OpenGL context
    GLuint program = 0;
    bool Shader ();// Shader loading
    void Compile_log (GLuint Shader);// Shader compilation log
    void Link_log (GLuint Shader);// Shader link log
    void GetShader_Log (GLuint shader_type, GLuint Log_type);

    void create_maxtri_mp (float top, float bottom, float left, float right,
                                            float far, float near, float result [4] [4]);

};

#endif
#include "Game.hpp"
#include<iostream>// #include "stdafx.h"
#include<iostream>#include<string>#include<fstream>#include<sstream>#include "Vector.hpp"

// Create a perspective projection transformation matrix
void Game :: create_maxtri_mp (float top, float bottom, float left, float right,
    float near, float far, float result [4] [4])
{
    result [0] [0] = (2 * near)/(right --left);
    result [0] [1] = 0;
    result [0] [2] = (right + left)/(right --left);
    result [0] [3] = 0;
    result [1] [0] = 0;
    result [1] [1] = (2 * near)/(top --bottom);
    result [1] [2] = (top + bottom)/(top --bottom);
    result [1] [3] = 0;
    result [2] [0] = 0;
    result [2] [1] = 0;
    result [2] [2] =-(far + near)/(far --near);
    result [2] [3] =-(2 * far + near)/(far --near);
    result [3] [0] = 0;
    result [3] [1] = 0;
    result [3] [2] = -1;
    result [3] [3] = 0;
}

//constructor
Game :: Game ()
{mIsRunLoop = true;
}
//Main loop
bool Game :: RunLoop ()
{
    if (mIsRunLoop == true)
    {
        Update ();
        GenerateOutput ();
    }
    else {
        return false;
    }
    return true;
}

// Get shader related logs
void Game :: GetShader_Log (GLuint shader_type, GLuint Log_type)
{
    GLint bufferSize = 0;
    GLchar * infoLog = nullptr;
    // Was it successful?
    glGetShaderiv (shader_type, Log_type,&bufferSize);
    if (bufferSize == GL_FALSE)
    {
        // printf ("");
    }
    // Get the log length
    glGetShaderiv (shader_type, GL_INFO_LOG_LENGTH,&bufferSize);
    infoLog = (GLchar *) malloc (bufferSize);
    if (infoLog! = NULL)
    {
        GLsizei length;// Log length
        glGetShaderInfoLog (shader_type, bufferSize,&length, infoLog);
        if (length>1) // Log output if there is more than one character because it contains null characters
        {
            fprintf (stderr, "InfoLog:% s \ n \ n", infoLog);
        }
        free (infoLog);// Free memory
        infoLog = NULL;
    }
    // return true;
}
// implement shader
bool Game :: Shader ()
{
    program = glCreateProgram ();
    GLuint out_vert = glCreateShader (GL_VERTEX_SHADER);
    GLuint out_frag = glCreateShader (GL_FRAGMENT_SHADER);
    // Load the vertex shader
    std :: string fileName = "Basic.vert";
    std :: ifstream shaderfile (fileName);
    if (shaderfile.is_open ())
    {
        std :: stringstream sstream;
        sstream<<shaderfile.rdbuf ();
        std :: string str = sstream.str ();
        const char * cc = str.c_str ();
        out_vert = glCreateShader (GL_VERTEX_SHADER);
        glShaderSource (out_vert, 1,&cc, nullptr);
        glCompileShader (out_vert);
        // printf ("Basic.vert file reading \ n");
        GetShader_Log (out_vert, GL_COMPILE_STATUS);
        glAttachShader (program, out_vert);
        shaderfile.close ();
    }
    else {
        shaderfile.close ();
        printf ("File:% s could not be read.", fileName.c_str ());
    }

    // Load the vertex shader
    fileName = "Basic.frag";
    shaderfile.open (fileName);
    if (shaderfile.is_open ())
    {
        std :: stringstream sstream;
        sstream<<shaderfile.rdbuf ();
        std :: string str = sstream.str ();
        const char * cc = str.c_str ();
        out_frag = glCreateShader (GL_FRAGMENT_SHADER);
        glShaderSource (out_frag, 1,&cc, nullptr);
        glCompileShader (out_frag);
        GetShader_Log (out_frag, GL_COMPILE_STATUS);
        glAttachShader (program, out_frag);
        shaderfile.close ();
        // printf ("Basic.frag file reading \ n");
    }
    else {
        shaderfile.close ();
        printf ("File:% s could not be read.", fileName.c_str ());
    }

    glLinkProgram (program);
    // Delete shader objects that are no longer in use
    glDeleteShader (out_frag);
    glDeleteShader (out_vert);
    glUseProgram (program);

    // pass the value to the shader
////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////
//**************************************************** *****************************float mp [4] [4];
    create_maxtri_mp (HEIGHT, -HEIGHT, -WIDTH, WIDTH, 0.1, 0.0000000001, mp);// Create a perspective matrix
    // Fragment shader
    glBindAttribLocation (program, 0, "Vertex");

    // Vertex shader
    glBindAttribLocation (program, 0, "Vertex");
    glUniformMatrix4fv (glGetUniformLocation (program, "scale"), 0, GL_TRUE,&scale [0] [0]);
    glUniformMatrix4fv (glGetUniformLocation (program, "rotate"), 1, GL_TRUE,&rotate [0] [0]);
    glUniformMatrix4fv (glGetUniformLocation (program, "move"), 2, GL_TRUE,&move [0] [0]);

    glUniformMatrix4fv (glGetUniformLocation (program, "MP"), 3, GL_TRUE,&mp [0] [0]);
    //**************************************************** *****************************
    ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////
    glUseProgram (program);

    return true;
}


//Initialization
bool Game :: Initialization ()
{
    // Screen initialization related
    ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///////
    // glfw initialization failed
    if (glfwInit ()! = GL_TRUE)
    {
        printf ("glfwInit () failed \ n");
        int _c = getchar ();
        return 0;
    }
    // create context
    Window = glfwCreateWindow (640, 400, "Hello", NULL, NULL);
    // Failed to create OpenGL context
    if (Window == NULL)
    {
        printf ("glfCreateWindow () failed \ n");
        int c = getchar ();
        return 0;
    }
    // Make the context an OpenGL drawing target.
    glfwMakeContextCurrent (Window);
    // Initialize glew.
    glewExperimental = GL_TRUE;
    if (glewInit ()! = GLEW_OK)
    {
        printf ("Failed to initialize glewInit ().");
        return 0;
    }
    // OpenGL version specification
    glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 0);
    glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    // Wait for the timing of vertical synchronization
    glfwSwapInterval (1);
    ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///////

    if (Shader () == true) // Load the shader
    {
        printf ("Load shader \ n");
    }

    glGenVertexArrays (1,&vbo);
    glBindVertexArray (vbo);
    glGenBuffers (1,&vbo);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glBufferData (GL_ARRAY_BUFFER, sizeof (Vertex), Vertex, GL_STATIC_DRAW);///////
    glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray (0);
    // glEnableClientState (GL_VERTEX_ARRAY);// Enable vertices on the client.


    mIsRunLoop = true;
    return mIsRunLoop;
}
//update
void Game :: Update ()
{
    if (glfwGetKey (Window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose (Window)! = GL_FALSE)
    {
        mIsRunLoop = false;
    }

}
// Drawing update
void Game :: GenerateOutput ()
{
    glClearColor (1.0f, 0.0f, 0.0f, 1.0f);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUseProgram (program);

    glDrawArrays (GL_POLYGON, 0,18);



    glViewport (0, 0, WIDTH, HEIGHT);
    glfwSwapBuffers (Window);
    glfwWaitEvents ();
};
  • Answer # 1

    Adjusted the argument of create_maxtri_mp.

    Since the second argument of glUniformMatrix4fv is the number, specify 1

    Adjust the translation matrix and move to the back

    After correcting the above for the time being, it was displayed.

    After that, the part of the rotation matrix is ​​unknown what the original is trying to do, so
    For the time being, I tried to rotate it by updating the screen.

       // Translate
         float mx = 0;
         float my = 0;
         float mz = -3;// move to the back
         const GLfloat move [4] [4] =
         {
             1,0,0,mx,
             0,1,0, my,
             0,0,1, mz,
             0,0,0,1,
         };
    #include "Game.hpp"
    #include<iostream>// #include "stdafx.h"
    #include<iostream>#include<string>#include<fstream>#include<sstream>#include "Vector.hpp"
    // Create a perspective projection transformation matrix
    void Game :: create_maxtri_mp (float top, float bottom, float left, float right,
        float near, float far, float result [4] [4])
    {
        result [0] [0] = (2 * near)/(right --left);
        result [0] [1] = 0;
        result [0] [2] = (right + left)/(right --left);
        result [0] [3] = 0;
        result [1] [0] = 0;
        result [1] [1] = (2 * near)/(top --bottom);
        result [1] [2] = (top + bottom)/(top --bottom);
        result [1] [3] = 0;
        result [2] [0] = 0;
        result [2] [1] = 0;
        result [2] [2] =-(far + near)/(far --near);
        result [2] [3] =-(2 * far + near)/(far --near);
        result [3] [0] = 0;
        result [3] [1] = 0;
        result [3] [2] = -1;
        result [3] [3] = 0;
    }
    //constructor
    Game :: Game ()
    {
        mIsRunLoop = true;
    }
    //Main loop
    bool Game :: RunLoop ()
    {
        if (mIsRunLoop == true)
        {
            Update ();
            GenerateOutput ();
        }
        else {
            return false;
        }
        return true;
    }
    // Get shader related logs
    void Game :: GetShader_Log (GLuint shader_type, GLuint Log_type)
    {
        GLint bufferSize = 0;
        GLchar * infoLog = nullptr;
        // Was it successful?
        glGetShaderiv (shader_type, Log_type,&bufferSize);
        if (bufferSize == GL_FALSE)
        {
            // printf ("");
        }
        // Get the log length
        glGetShaderiv (shader_type, GL_INFO_LOG_LENGTH,&bufferSize);
        infoLog = (GLchar *) malloc (bufferSize);
        if (infoLog! = NULL)
        {
            GLsizei length;// Log length
            glGetShaderInfoLog (shader_type, bufferSize,&length, infoLog);
            if (length>1) // Log output if there is more than one character because it contains null characters
            {
                fprintf (stderr, "InfoLog:% s \ n \ n", infoLog);
            }
            free (infoLog);// Free memory
            infoLog = NULL;}
        // return true;
    }
    // implement shader
    bool Game :: Shader ()
    {
        program = glCreateProgram ();
        GLuint out_vert = glCreateShader (GL_VERTEX_SHADER);
        GLuint out_frag = glCreateShader (GL_FRAGMENT_SHADER);
        // Load the vertex shader
        std :: string fileName = "Basic.vert";
        std :: ifstream shaderfile (fileName);
        if (shaderfile.is_open ())
        {
            std :: stringstream sstream;
            sstream<<shaderfile.rdbuf ();
            std :: string str = sstream.str ();
            const char * cc = str.c_str ();
            out_vert = glCreateShader (GL_VERTEX_SHADER);
            glShaderSource (out_vert, 1,&cc, nullptr);
            glCompileShader (out_vert);
            // printf ("Basic.vert file reading \ n");
            GetShader_Log (out_vert, GL_COMPILE_STATUS);
            glAttachShader (program, out_vert);
            shaderfile.close ();
        }
        else {
            shaderfile.close ();
            printf ("File:% s could not be read.", fileName.c_str ());
        }
        // Load the vertex shader
        fileName = "Basic.frag";
        shaderfile.open (fileName);
        if (shaderfile.is_open ())
        {
            std :: stringstream sstream;
            sstream<<shaderfile.rdbuf ();
            std :: string str = sstream.str ();
            const char * cc = str.c_str ();
            out_frag = glCreateShader (GL_FRAGMENT_SHADER);
            glShaderSource (out_frag, 1,&cc, nullptr);
            glCompileShader (out_frag);
            GetShader_Log (out_frag, GL_COMPILE_STATUS);
            glAttachShader (program, out_frag);
            shaderfile.close ();
            // printf ("Basic.frag file reading \ n");
        }
        else {
            shaderfile.close ();
            printf ("File:% s could not be read.", fileName.c_str ());
        }
        glBindAttribLocation (program, 0, "position");
        glLinkProgram (program);
        // Delete shader objects that are no longer in use
        glDeleteShader (out_frag);
        glDeleteShader (out_vert);
        glUseProgram (program);
        // pass the value to the shader
    ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////
    //**************************************************** *****************************
        float mp [4] [4];
        create_maxtri_mp (1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 10.0f, mp);// Create a perspective matrix
        const GLfloat rot [4] [4] = {
            {1,0,0,0},
            {0,1,0,0},
            {0,0,1,0},
            {0,0,0,1}
        };
        // Vertex shader
        glUniformMatrix4fv (glGetUniformLocation (program, "scale"), 1, GL_TRUE,&scale [0] [0]);
        glUniformMatrix4fv (glGetUniformLocation (program, "rotate"), 1, GL_TRUE,&rot [0] [0]);
        glUniformMatrix4fv (glGetUniformLocation (program, "move"), 1, GL_TRUE,&move [0] [0]);
        glUniformMatrix4fv (glGetUniformLocation (program, "MP"), 1, GL_TRUE,&mp [0] [0]);
        //**************************************************** *****************************
        ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////
        glUseProgram (program);
        return true;
    }
    //Initialization
    bool Game :: Initialization (){
        // Screen initialization related
        ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///////
        // glfw initialization failed
        if (glfwInit ()! = GL_TRUE)
        {
            printf ("glfwInit () failed \ n");
            int _c = getchar ();
            return 0;
        }
        // create context
        Window = glfwCreateWindow (640, 400, "Hello", NULL, NULL);
        // Failed to create OpenGL context
        if (Window == NULL)
        {
            printf ("glfCreateWindow () failed \ n");
            int c = getchar ();
            return 0;
        }
        // Make the context an OpenGL drawing target.
        glfwMakeContextCurrent (Window);
        // Initialize glew.
        glewExperimental = GL_TRUE;
        if (glewInit ()! = GLEW_OK)
        {
            printf ("Failed to initialize glewInit ().");
            return 0;
        }
        // OpenGL version specification
        glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
        glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 0);
        glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
        glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        // Wait for the timing of vertical synchronization
        glfwSwapInterval (1);
        ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///////
        if (Shader () == true) // Load the shader
        {
            printf ("Load shader \ n");
        }
        glGenVertexArrays (1,&vbo);
        glBindVertexArray (vbo);
        glGenBuffers (1,&vbo);
        glBindBuffer (GL_ARRAY_BUFFER, vbo);
        glBufferData (GL_ARRAY_BUFFER, sizeof (Vertex), Vertex, GL_STATIC_DRAW);///////
        glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, 0);
        glEnableVertexAttribArray (0);
        // glEnableClientState (GL_VERTEX_ARRAY);// Enable vertices on the client.
        mIsRunLoop = true;
        return mIsRunLoop;
    }
    //update
    void Game :: Update ()
    {
        if (glfwGetKey (Window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose (Window)! = GL_FALSE)
        {
            mIsRunLoop = false;
        }
    }
    static void rotateX (GLfloat radian, GLfloat m [])
    {
        float s = sin (radian);
        float c = cos (radian);
        m [0] = 1.0;m [1] = 0.0;m [2] = 0.0;m [3] = 0.0;
        m [4] = 0.0;m [5] = c;m [6] = -s;m [7] = 0.0;
        m [8] = 0.0;m [9] = s;m [10] = c;m [11] = 0.0;
        m [12] = 0.0;m [13] = 0.0;m [14] = 0.0;m [15] = 1.0;
    }
    // Drawing update
    void Game :: GenerateOutput ()
    {
        glClearColor (1.0f, 0.0f, 0.0f, 1.0f);
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUseProgram (program);
        static float rotX = 0;
        float rot [16];
        rotateX (rotX, rot);
        rotX + = 0.01f;
        glUniformMatrix4fv (glGetUniformLocation (program, "rotate"), 1, GL_FALSE, rot);
        glDrawArrays (GL_POLYGON, 0, 18);
        glViewport (0, 0, WIDTH, HEIGHT);
        glfwSwapBuffers (Window);
        glfwPollEvents ();
    }