使用openGL顶点阵列的白屏
White screen using openGL vertex array
我对openGL相当陌生。一个练习是使用顶点数组重写一段代码。这就是我想出的。当我编译然后运行.exe时,我得到的只是一个白色窗口。我认为这可能是我搞砸的索引变量。我认为。
#include <cmath>
#include <iostream>
#ifdef __APPLE__
# include <GL/glew.h>
# include <GL/freeglut.h>
# include <OpenGL/glext.h>
#else
# include <GL/glew.h>
# include <GL/freeglut.h>
# include <GL/glext.h>
#pragma comment(lib, "glew32.lib")
#endif
#define PI 3.14159265
using namespace std;
// Globals.
static float R = 5.0; // Radius of hemisphere.
static int p = 6; // Number of longitudinal slices.
static int q = 4; // Number of latitudinal slices.
static float Xangle = 0.0, Yangle = 0.0, Zangle = 0.0; // Angles to rotate hemisphere.
static float *vert;
static int *ind;
// Fill the vertex array with co-ordinates of the sample points.
void fillVerArr(void)
{
int k = 0;
for (int j = 0; j <= q; j++)
{
for (int i = 0; i <= p; i++)
{
vert[k++] = R * sin( (float)j/q * PI/2.0 ) * cos( 2.0 * (float)i/p * PI );
vert[k++] = R * sin( (float)j/q * PI/2.0 ) * sin( 2.0 * (float)i/p * PI );
vert[k++] = R * cos( (float)j/q * PI/2.0 );
}
}
}
// Fill the array of index arrays.
void fillIndArr(int j)
{
for(int i = 0; i <= p; i++)
{
ind[2*i] = (j+1)*p+(i+1);
ind[2*i+1] = j*p + i;
}
}
// Initialization routine.
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
// Enable vertex array.
glEnableClientState(GL_VERTEX_ARRAY);
}
// Drawing routine.
void drawScene(void)
{
int i, j;
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Command to push the hemisphere, which is drawn centered at the origin,
// into the viewing frustum.
glTranslatef(0.0, 0.0, -10.0);
vert = new float[3 * (p+1) * (q+1)];
fillVerArr();
// Commands to turn the hemisphere.
glRotatef(Zangle, 0.0, 0.0, 1.0);
glRotatef(Yangle, 0.0, 1.0, 0.0);
glRotatef(Xangle, 1.0, 0.0, 0.0);
// Hemisphere properties.
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(0.0, 0.0, 0.0);
glVertexPointer(3,GL_FLOAT,0,vert);
for(j = 0; j < q; j++)
{
ind = new int[2*p];
fillIndArr(j);
glDrawElements(GL_TRIANGLE_STRIP,2*(p+1) + 1,GL_FLOAT,ind );
}
glFlush();
}
// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
case 'P':
p += 1;
glutPostRedisplay();
break;
case 'p':
if (p > 3) p -= 1;
glutPostRedisplay();
break;
case 'Q':
q += 1;
glutPostRedisplay();
break;
case 'q':
if (q > 3) q -= 1;
glutPostRedisplay();
break;
case 'x':
Xangle += 5.0;
if (Xangle > 360.0) Xangle -= 360.0;
glutPostRedisplay();
break;
case 'X':
Xangle -= 5.0;
if (Xangle < 0.0) Xangle += 360.0;
glutPostRedisplay();
break;
case 'y':
Yangle += 5.0;
if (Yangle > 360.0) Yangle -= 360.0;
glutPostRedisplay();
break;
case 'Y':
Yangle -= 5.0;
if (Yangle < 0.0) Yangle += 360.0;
glutPostRedisplay();
break;
case 'z':
Zangle += 5.0;
if (Zangle > 360.0) Zangle -= 360.0;
glutPostRedisplay();
break;
case 'Z':
Zangle -= 5.0;
if (Zangle < 0.0) Zangle += 360.0;
glutPostRedisplay();
break;
default:
break;
}
}
// Routine to output interaction instructions to the C++ window.
void printInteraction(void)
{
cout << "Interaction:" << endl;
cout << "Press P/p to increase/decrease the number of longitudinal slices." << endl
<< "Press Q/q to increase/decrease the number of latitudinal slices." << endl
<< "Press x, X, y, Y, z, Z to turn the hemisphere." << endl;
}
// Main routine.
int main(int argc, char **argv)
{
printInteraction();
glutInit(&argc, argv);
glutInitContextVersion(2, 1);
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("hemisphere.cpp");
// registers callback routines
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
glewExperimental = GL_TRUE;
glewInit();
// call setup()
setup();
// run the event processing loop, calling callback routines as needed.
glutMainLoop();
}
您将索引数组中的值类型指定为 GL_FLOAT
在调用 glDrawElements
时,而它们实际上是 int
的类型。
索引数组的值必须为 GL_UNSIGNED_BYTE
、 GL_UNSIGNED_SHORT
或 GL_UNSIGNED_INT
的类型
相关文章:
- OpenMP阵列性能较差
- 如何在全屏模式下(在OpenGL中)使背景透明
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 当我的阵列太大时出现分段错误
- 位阵列上的快速AND运算
- OpenSSL TLS服务器-使用客户端证书白名单
- 阵列必须使用大括号封闭的初始器进行初始化
- 没有从阵列<float>到阵列<int>的可行转换
- 如何为窗口截屏
- C++动态安全 2D 交错阵列
- 将平面阵列重塑为复杂的特征类型
- 如何使用英特尔 PIN 捕获阵列的所有负载?
- Qt QML相机在部署后显示白屏
- openGL GLFW 白屏,带加载光标正在运行
- OpenGL显示空白的白屏
- D3D11 着色器编译成功,但出现白屏
- 简单的Qt视频流应用程序,只有声音,没有视频(白屏)
- OpenGL白屏,无响应
- 使用openGL顶点阵列的白屏
- OpenGL 4.4渲染只是白屏