简体   繁体   中英

opengl cube won't render

I'm trying to draw a cube on OpenGL.

When I compile and run this code, all I see is a black window. Which part of my code causes this problem? Any help would be appreciated as I am very new to opengl Here is my source file:

#include <GL/glew.h>
#include <GL/glfw.h>
#include <iostream>
#include <cmath>

void drawCube() {
//vertices of the triangle  
GLfloat vertices[] = {
    -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f,
    1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
    -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
    -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
    -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
    -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f
};

//colors of the triangle
GLfloat colors[] = {
    0.410f, 0.481f, 0.675f,
    0.177f, 0.823f, 0.970f,
    0.604f, 0.516f, 0.611f,
    0.676f, 0.779f, 0.331f,
    0.179f, 0.275f, 0.338f,
    0.041f, 0.616f, 0.984f,
    0.799f, 0.315f, 0.460f,
    0.945f, 0.719f, 0.295f
};

static float alpha = 0;
//attempt to rotate cube
glRotatef(alpha, 0, 1, 0);

/* We have a color array and a vertex array */
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);

/* Send data : 24 vertices */
glDrawArrays(GL_TRIANGLES, 0, 24);

/* Cleanup states */
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
alpha += 1;


};

int main() {
if (!glfwInit()) {
    std::cerr << "Unable to initialize OpenGL!\n";
    return -1;
}

if (!glfwOpenWindow(1024, 768, //width and height of the screen
    8, 8, 8, 0, //Red, Green, Blue and Alpha bits
    0, 0, //Depth and Stencil bits
    GLFW_WINDOW)) {
    std::cerr << "Unable to create OpenGL window.\n";   
    glfwTerminate();
    return -1;
}

glfwSetWindowTitle("GLFW Simple Example");

// Ensure we can capture the escape key being pressed below
glfwEnable(GLFW_STICKY_KEYS);


do {
    GLint width, height;
    // Get window size (may be different than the requested size)
    //we do this every frame to accommodate window resizing.
    glfwGetWindowSize(&width, &height);
    glViewport(0, 0, width, height);
    glEnable(GL_DEPTH_TEST); // Depth Testing
    glDepthFunc(GL_LEQUAL);
    glDisable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glfwSwapInterval(1);

    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION_MATRIX);
    glLoadIdentity();

    glMatrixMode(GL_MODELVIEW_MATRIX);
    glTranslatef(0, 0, -5);
    gluPerspective(60, (double)width / (double)height, 0.1, 100);

    drawCube();

    //VERY IMPORTANT: displays the buffer to the screen
    glfwSwapBuffers();

} while (glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS &&
    glfwGetWindowParam(GLFW_OPENED));

glfwTerminate();
return 0;
}

The problem is that you don't use a perspective projection, so you are basically seeing one face of the cube in an orthogonal view.

To use perspective, you can use gluPerspective :

glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
gluPerspective(60, (double)width / (double)height, 0.1, 100); // Set up perspective matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM