user4388084
user4388084

Reputation:

White screen using openGL vertex array

I am fairly new to openGL. One exercise was to rewrite a piece of code using vertex arrays. This is what I come up with. When I compile and then run the .exe all I get is a white window. I think it could be an index variable I have messed up. I think.

    #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(); 
}

Upvotes: 1

Views: 105

Answers (1)

johannes_lalala
johannes_lalala

Reputation: 595

Your specify the types of the values in the index array as GL_FLOAT in the call to glDrawElements, while they are in fact of type int.

Values of the index array must be of type GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT.

Upvotes: 0

Related Questions