openglsuperbible / sb7code

Source code and supporting material for the 7th Edition of OpenGL SuperBible
727 stars 251 forks source link

spinnycube doesn't show cube/s #29

Open blob84 opened 7 years ago

blob84 commented 7 years ago

The window is green, no cube is displayed. (Debian 8)

`#include

include

// Remove this to draw only a single cube! // #define MANY_CUBES

class singlepoint_app : public sb7::application { void init() { static const char title[] = "OpenGL SuperBible - Spinny Cube";

    sb7::application::init();

    memcpy(info.title, title, sizeof(title));
}

virtual void startup()
{
    static const char * vs_source[] =
    {
        "#version 410 core                                                  \n"
        "                                                                   \n"
        "in vec4 position;                                                  \n"
        "                                                                   \n"
        "out VS_OUT                                                         \n"
        "{                                                                  \n"
        "    vec4 color;                                                    \n"
        "} vs_out;                                                          \n"
        "                                                                   \n"
        "uniform mat4 mv_matrix;                                            \n"
        "uniform mat4 proj_matrix;                                          \n"
        "                                                                   \n"
        "void main(void)                                                    \n"
        "{                                                                  \n"
        "    gl_Position = proj_matrix * mv_matrix * position;              \n"
        "    vs_out.color = position * 2.0 + vec4(0.5, 0.5, 0.5, 0.0);      \n"
        "}                                                                  \n"
    };

    static const char * fs_source[] =
    {
        "#version 410 core                                                  \n"
        "                                                                   \n"
        "out vec4 color;                                                    \n"
        "                                                                   \n"
        "in VS_OUT                                                          \n"
        "{                                                                  \n"
        "    vec4 color;                                                    \n"
        "} fs_in;                                                           \n"
        "                                                                   \n"
        "void main(void)                                                    \n"
        "{                                                                  \n"
        "    color = fs_in.color;                                           \n"
        "}                                                                  \n"
    };

    program = glCreateProgram();
    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fs, 1, fs_source, NULL);
    glCompileShader(fs);

    GLuint vs = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vs, 1, vs_source, NULL);
    glCompileShader(vs);

    glAttachShader(program, vs);
    glAttachShader(program, fs);

    glLinkProgram(program);

    mv_location = glGetUniformLocation(program, "mv_matrix");
    proj_location = glGetUniformLocation(program, "proj_matrix");

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

    static const GLfloat vertex_positions[] =
    {
        -0.25f,  0.25f, -0.25f,
        -0.25f, -0.25f, -0.25f,
         0.25f, -0.25f, -0.25f,

         0.25f, -0.25f, -0.25f,
         0.25f,  0.25f, -0.25f,
        -0.25f,  0.25f, -0.25f,

         0.25f, -0.25f, -0.25f,
         0.25f, -0.25f,  0.25f,
         0.25f,  0.25f, -0.25f,

         0.25f, -0.25f,  0.25f,
         0.25f,  0.25f,  0.25f,
         0.25f,  0.25f, -0.25f,

         0.25f, -0.25f,  0.25f,
        -0.25f, -0.25f,  0.25f,
         0.25f,  0.25f,  0.25f,

        -0.25f, -0.25f,  0.25f,
        -0.25f,  0.25f,  0.25f,
         0.25f,  0.25f,  0.25f,

        -0.25f, -0.25f,  0.25f,
        -0.25f, -0.25f, -0.25f,
        -0.25f,  0.25f,  0.25f,

        -0.25f, -0.25f, -0.25f,
        -0.25f,  0.25f, -0.25f,
        -0.25f,  0.25f,  0.25f,

        -0.25f, -0.25f,  0.25f,
         0.25f, -0.25f,  0.25f,
         0.25f, -0.25f, -0.25f,

         0.25f, -0.25f, -0.25f,
        -0.25f, -0.25f, -0.25f,
        -0.25f, -0.25f,  0.25f,

        -0.25f,  0.25f, -0.25f,
         0.25f,  0.25f, -0.25f,
         0.25f,  0.25f,  0.25f,

         0.25f,  0.25f,  0.25f,
        -0.25f,  0.25f,  0.25f,
        -0.25f,  0.25f, -0.25f
    };

    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER,
                 sizeof(vertex_positions),
                 vertex_positions,
                 GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(0);

    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CW);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
}

virtual void render(double currentTime)
{
    static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
    static const GLfloat one = 1.0f;

    glViewport(0, 0, info.windowWidth, info.windowHeight);
    glClearBufferfv(GL_COLOR, 0, green);
    glClearBufferfv(GL_DEPTH, 0, &one);

    glUseProgram(program);

    glUniformMatrix4fv(proj_location, 1, GL_FALSE, proj_matrix);

ifdef MANY_CUBES

    int i;
    for (i = 0; i < 24; i++)
    {
        float f = (float)i + (float)currentTime * 0.3f;
        vmath::mat4 mv_matrix = vmath::translate(0.0f, 0.0f, -6.0f) *
                                vmath::rotate((float)currentTime * 45.0f, 0.0f, 1.0f, 0.0f) *
                                vmath::rotate((float)currentTime * 21.0f, 1.0f, 0.0f, 0.0f) *
                                vmath::translate(sinf(2.1f * f) * 2.0f,
                                                 cosf(1.7f * f) * 2.0f,
                                                 sinf(1.3f * f) * cosf(1.5f * f) * 2.0f);
        glUniformMatrix4fv(mv_location, 1, GL_FALSE, mv_matrix);
        glDrawArrays(GL_TRIANGLES, 0, 36);
    }

else

    float f = (float)currentTime * 0.3f;
    vmath::mat4 mv_matrix = vmath::translate(0.0f, 0.0f, -4.0f) *
                            vmath::translate(sinf(2.1f * f) * 0.5f,
                                                cosf(1.7f * f) * 0.5f,
                                                sinf(1.3f * f) * cosf(1.5f * f) * 2.0f) *
                            vmath::rotate((float)currentTime * 45.0f, 0.0f, 1.0f, 0.0f) *
                            vmath::rotate((float)currentTime * 81.0f, 1.0f, 0.0f, 0.0f);
    glUniformMatrix4fv(mv_location, 1, GL_FALSE, mv_matrix);
    glDrawArrays(GL_TRIANGLES, 0, 36);

endif

}

virtual void shutdown()
{
    glDeleteVertexArrays(1, &vao);
    glDeleteProgram(program);
    glDeleteBuffers(1, &buffer);
}

void onResize(int w, int h)
{
    sb7::application::onResize(w, h);

    aspect = (float)w / (float)h;
    proj_matrix = vmath::perspective(50.0f, aspect, 0.1f, 1000.0f);
}

private: GLuint program; GLuint vao; GLuint buffer; GLint mv_location; GLint proj_location;

float           aspect;
vmath::mat4     proj_matrix;

};

DECLARE_MAIN(singlepoint_app)`

blob84 commented 7 years ago

The prespective matrix was not set. Resizing the window call the method 'onResize' and will set the matrix.

PhilParisot commented 6 years ago

Resizing the window worked for me.

OdairTrujillo commented 3 years ago

Just initializate the projection matriz inside the render function with an aspect ratio of 4/3:

virtual void render(double currentTime)
{
    static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
    static const GLfloat one = 1.0f;
    glViewport(0, 0, info.windowWidth, info.windowHeight);
    glClearBufferfv(GL_COLOR, 0, green);
    glClearBufferfv(GL_DEPTH, 0, &one);
    glUseProgram(program);
    // initalization of projection matrix
    proj_matrix = vmath::perspective(50.0f, 1.33f, 0.1f, 1000.0f);
    glUniformMatrix4fv(proj_location, 1, GL_FALSE, proj_matrix);

#ifdef MANY_CUBES
...