简体   繁体   English

仅限OpenGL glDrawElements黑屏

[英]OpenGL glDrawElements only Black Screen

i was trying to draw a cube, using the glDrawElements function, but even the simple code below only gives me a black screen. 我试图使用glDrawElements函数绘制一个立方体,但是即使是下面的简单代码也只能给我黑屏。 If helps, i'm programming on XCode 6. 如果有帮助,我正在使用XCode 6进行编程。

//
//  main.cpp
//  Copyright (c) 2014 Guilherme Cardoso. All rights reserved.
//

#include <iostream>
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <vector>
#include <math.h>

const GLfloat width = 500;
const GLfloat height = 500;

GLubyte cubeIndices[24] = {0,3,2,1,2,3,7,6
    ,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};

GLfloat vertices[][3] =
{{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
    {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
    {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat colors[][3] =
{{0.0,0.0,0.0},{1.0,0.0,0.0},
    {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0},
    {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};

void display(){
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(3, GL_FLOAT, 0, colors);
    glVertexPointer(3, GL_FLOAT, 0, vertices);

    //glDrawArrays(GL_QUADS, 0, 24);
    glDrawElements(GL_QUADS, 24,GL_UNSIGNED_BYTE, cubeIndices);
    glDisableClientState(GL_VERTEX_ARRAY);
    glutSwapBuffers();
}

void mouse(int button, int state, int x, int y){

}

void keyboard(unsigned char key, int x, int y){
    if(key=='q' || key == 'Q') exit(0);
}

void init(){
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,width ,height, 0);
    glMatrixMode(GL_MODELVIEW);

    glClearColor (1.0, 1.0, 1.0,1.0);
    //glColor3f(0.0,0.0,0.0);
}

void idle(){

}

int main(int argc,  char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(width, height);
    glutCreateWindow("Assignment 3");
    glutPositionWindow(0, 0);


    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutIdleFunc(idle);

    init();
    glutMainLoop();
}

i already checked some tutorials, and is no much different of what i'm doing. 我已经检查了一些教程,与我所做的没有太大不同。

You never clear the color and especially the depth buffer. 您永远不会清除颜色,尤其是深度缓冲区。 You should do that at the beginning of your display() function. 您应该在display()函数的开头执行此操作。

Also, your matrix setup is a bit weird. 另外,您的矩阵设置有点怪异。 You set up some ortho projection for the pixels, but try to draw a cube in the range [-1,1], so it will be 2 pixel wide on the screen. 您为像素设置了一些正交投影,但是尝试在[-1,1]范围内绘制一个立方体,因此它在屏幕上将为2像素宽。

Actually your code is drawing the cube just fine. 实际上,您的代码正在绘制多维数据集。 Look closely :P 仔细看:P

在此处输入图片说明

The main issue is your projection. 主要问题是您的预测。 The initial GL viewing volume is a -1 to 1 cube, but the cube you're drawing is -1 to 1. The call to gluOrtho2D defines the projection in OpenGL coordinates, which you make the same as pixels, but since your cube is -1 to 1 it is only two pixels big, the rest being offscreen. 初始GL观看体积是-1到1的多维数据集,但是绘制的多维数据集 -1到1。对gluOrtho2D的调用定义了OpenGL坐标中的投影,该投影与像素相同,但是由于您的立方体是-1比1只有2个像素大,其余都在屏幕外。

Instead, drop the gluOrtho2D , which sets the Z dimension -1 to 1 and only allows you to set X / Y , and create a slightly bigger projection... 而是放下gluOrtho2D ,它将Z尺寸-1设置为1,只允许您设置X / Y ,并创建一个稍大的投影...

glOrtho(-2, 2, -2, 2, -2, 2);

Note: As @derhass suggests, calling glClear is important especially with depth testing enabled (without it, the cube from the last draw call will hide the updated cube). 注意:正如@derhass所建议的那样,调用glClear非常重要,尤其是在启用深度测试的情况下(不启用它,上一次绘制调用中的多维数据集将隐藏更新的多维数据集)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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