1、代码
// Main.cpp
// Created by lmq, 2014/07/23
// Chap02_11, glDrawElements
// Chap02_12, glDrawElements
// Chap02_14, glMultiDrawelements
#include <iostream>
#include "GL\glew.h"
#include "GL\freeglut.h"
#define CHAP02_11
#define CHAP02_12
#define CHAP02_14
#undef CHAP02_11
#undef CHAP02_12
//#undef CHAP02_14
void init(void);
void reshape(int w, int h);
void display(void);
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(250, 250);
glutCreateWindow("Create Chap02_11 window");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
void init(void)
{
glewInit();
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLfloat)w, 0.0, (GLfloat)h);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
static GLfloat vertexs[] = {
25.0, 25.0,
50.0, 25.0,
50.0, 50.0,
25.0, 50.0,
75.0, 25.0,
100.0, 25.0,
100.0, 50.0,
75.0, 50
};
glEnableClientState(GL_VERTEX_ARRAY); // Enable vertex array
glVertexPointer(2, GL_FLOAT, 0, vertexs);
static GLubyte first[] = {0, 1, 2, 3};
static GLubyte second[] = {4, 5, 6, 7};
// Chap02_11,分别调用
#ifdef CHAP02_11
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, first);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, second);
#endif // end CHAP02_11
// Chap02_12,压缩glDrawElemnts()
#ifdef CHAP02_12
glColor3f(1.0, 0.0, 0.0);
static GLubyte allIndices[] = {0, 1, 2, 3, 4, 5, 6, 7};
glDrawElements(GL_QUADS, 8, GL_UNSIGNED_BYTE, allIndices);
#endif // end CHAP02_12
// Chap02_14,glMultiDrawElements
#ifdef CHAP02_14
glColor3f(1.0, 1.0, 0.0);
static GLsizei count[] = {4, 4};
static GLvoid *indices[2] = {first, second};
glMultiDrawElements(GL_QUADS, count, GL_UNSIGNED_BYTE, indices, 2);
#endif // end CHAP02_14
glFlush();
}
2、注意事项
glMultiDrawElements此函数必须包含"GL\glew.h",而且必须添加glew32.lib库。
但是即便是添加了头文件、库文件,程序运行依然报错
0x00000000 处有未经处理的异常(在 Chap02_11_d.exe 中): 0xC0000005: 执行位置 0x00000000 时发生访问冲突。
解决此问题,必须在程序初始化时添加 "glewInit();"