OpenGL glDrawElements只有黑屏 [英] OpenGL glDrawElements only Black Screen
问题描述
我试图绘制一个立方体,使用glDrawElements函数,但即使下面的简单代码只给了我一个黑色的屏幕。如果有帮助,我在XCode 6上编程。
//
// main.cpp
//版权所有(c)2014 Guilherme Cardoso。版权所有。
//
#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 char(){
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 void(){
}
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();
}
我已经检查过一些教程, 。
实际上,你的代码正在绘制多维数据集。仔细观察:P
/ p>
主要的问题是你的投影。初始GL查看量是一个-1到1的立方体,但是你正在绘制的立方体是 -1到1.调用 gluOrtho2D
在OpenGL坐标中定义投影,它与像素相同,但是因为您的立方体是-1到1,它只有两个像素,其余的是在屏幕外。
而是将 c> gluOrtho2D ,将 Z 维度-1设为1,只允许您设定 / ,并创建一个略大的投影...
glOrtho(-2, -2,2,-2,2);
注意:如@derhass建议,调用 glClear
是重要的,特别是在启用深度测试的情况下(没有它,上次绘图调用的多维数据集将隐藏更新的多维数据集)。
i was trying to draw a cube, using the glDrawElements function, but even the simple code below only gives me a black screen. If helps, i'm programming on 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.
Actually your code is drawing the cube just fine. Look closely :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.
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...
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).
这篇关于OpenGL glDrawElements只有黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!