使用openGL顶点数组的白屏

Posted

技术标签:

【中文标题】使用openGL顶点数组的白屏【英文标题】:White screen using openGL vertex array 【发布时间】:2016-01-26 23:11:13 【问题描述】:

我对 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 的颜色清除缓冲区。尝试将其更改为 1.0:glClearColor(1.0, 1.0, 1.0, 1.0); @fordcars我不认为颜色缓冲区中的 alpha 值可能会产生影响。尤其是在关闭混合的情况下。 你在什么平台上测试这个? Ubuntu 14 的东西。如果这就是我所理解的平台。对不起,我对术语不太了解,你对着色器有什么看法? 【参考方案1】:

您在对glDrawElements 的调用中将索引数组中值的类型指定为GL_FLOAT,而它们实际上是int 类型。

索引数组的值必须是 GL_UNSIGNED_BYTEGL_UNSIGNED_SHORTGL_UNSIGNED_INT 类型。

【讨论】:

我不敢相信我忽略了这一点。谢谢哈哈。

以上是关于使用openGL顶点数组的白屏的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL ES 2.0 vbo 白屏

运行时带有加载光标的openGL GLFW白屏

Opengl 顶点数组对象

Modern OpenGL - 顶点数组、属性与绑定点(OpenGL 4.5+)

我的OpenGL学习进阶之旅顶点属性顶点数组

我的OpenGL学习进阶之旅顶点属性顶点数组