2 wendyanna wendyanna 于 2015.06.03 14:34 提问

opengl VBO(vertex buffer object), 在线等 5C

初学opengl,代码如下,不知哪里出了问题,没有办法显示正方体,一片空白。
// myFirstOpengl.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include
#define FREEGLUT_STATIC
#include
#include
#include

extern GLfloat vertexData[8][3]; //顶点数据指针
extern GLubyte colorData[8][4]; //颜色指针
extern GLuint indexData[12][3]; //索引指针
GLuint BufferName[3]; //顶点缓存和索引缓存的数组id
GLFrustum viewFrustum;
GLShaderManager shaderManager;

void SetupRC()
{
//glClearColor(0.0f, 0.0f, 0.75f, 1.0f); //设置背景颜色
glEnable(GL_DEPTH_TEST);
printf("setupRc");

shaderManager.InitializeStockShaders();

}

void ShutdownRC()
{
glGenBuffers(3, BufferName);

glBindBuffer(GL_ARRAY_BUFFER, BufferName[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, BufferName[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(colorData), colorData, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferName[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData), indexData, GL_STATIC_DRAW);

}

void ChangeSize(int w, int h)
{
printf("changesize");
if(h == 0)
h = 1;
glViewport(0, 0, w, h); // set viewport to window dimensions

viewFrustum.SetPerspective(35.0f, float(w)/float(h), 1.0f, 1000.0f); //angle, w/h, near, far

}
void SpecialKeys(int key, int x, int y)
{
}
void RenderScene()
{
printf("render");
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

glBindBuffer(GL_ARRAY_BUFFER, BufferName[0]);
//glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, BufferName[1]);
//glBufferData(GL_ARRAY_BUFFER, sizeof(colorData), colorData, GL_STATIC_DRAW);
glColorPointer(4, GL_FLOAT, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferName[2]);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData), indexData, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);
//glDisableClientState(GL_VERTEX_ARRAY);

}
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA |GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("My first Opengl Example");
glutReshapeFunc(ChangeSize);//原型函数
glutSpecialFunc(SpecialKeys);//原型函数
glutDisplayFunc(RenderScene);

GLenum err = glewInit();
if(GLEW_OK != err)
{
    fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
    return 1;
}

SetupRC();

glutMainLoop();
ShutdownRC();
return 0;

}

1个回答

happy__888
happy__888   2015.06.10 09:49

看不到全貌十有八九是相机的位置距离物体的位置关系不对

物体不在视线范围内,或者距离物体太近,全屏都被物体充满, 让你误以为没看见

Csdn user default icon
上传中...
上传图片
插入图片