我对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();
}
答案 0 :(得分:0)
您在GL_FLOAT
的调用中将索引数组中的值类型指定为glDrawElements
,而实际上它们的类型为int
。
索引数组的值必须是GL_UNSIGNED_BYTE
,GL_UNSIGNED_SHORT
或GL_UNSIGNED_INT
类型。