opengl红宝书第七版问题

fuhuasuiyue 发布于 2013/12/04 21:15
阅读 451
收藏 0

opengl红宝书第七版中2-15的例子,代码如下  我怎么也调不过去,希望大牛给点建议,在此谢过,希望有朋友可以加我QQ267085308,希望向各位请教

#include <GL\glew.h>
#include <GL\freeglut.h>
//#include <iostream>
#include <cstdio>
#include <malloc.h>

#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu.lib")
#pragma comment(lib, "glut.lib")
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "freeglut.lib")

 

#define BUFFER_OFFSET(offset) ((GLvoid *) NULL + offset)

#define XStart  -0.8
#define XEnd  0.8
#define YStart  -0.8
#define YEnd  0.8
#define NumXPoints 11
#define NumYPoints 11
#define NumPoints (NumXPoints * NumYPoints)
#define NumPointsPerStrip (2*NumXPoints)
#define NumStrips (NumYPoints-1)
#define RestartIndex 0xffff

GLuint vbo, ebo;
GLfloat *vertices;
GLushort *indices;

void init(void)
{
 glClearColor(0.0, 0.0, 0.0, 0.0);
 glGenBuffers(1, &vbo);
 glBindBuffer(GL_ARRAY_BUFFER, vbo);
 glBufferData(GL_ARRAY_BUFFER, 2 * NumPoints*sizeof(GLfloat),
  NULL, GL_STATIC_DRAW);
 vertices = (GLfloat *)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
 vertices = (GLfloat *)malloc(2 * NumPoints*sizeof(GLfloat));
 /* Set up vertex data */
 if (vertices == NULL)
 {
  fprintf(stderr, "Unable to map vertices buffer\n");
  exit(EXIT_FAILURE);
 }
 else
 {
  int i, j;
  GLfloat dx = (XEnd - XStart) / (NumXPoints - 1);
  GLfloat dy = (YEnd - YStart) / (NumYPoints - 1);
  GLfloat *tmp = vertices;
  int n = 0;

  for (j = 0; j < NumYPoints; j++)
  {
   GLfloat y = YStart + j*dy;

   for (i = 0; i < NumXPoints; i++)
   {
    GLfloat x = XStart + i*dx;
    *tmp++ = x;
    *tmp++ = y;
   }
  }

  glUnmapBuffer(GL_ARRAY_BUFFER);
//  glVertexPointer(1, GL_FLOAT, 0, BUFFER_OFFSET(0)); //书上的函数
  glVertexPointer(1, GL_FLOAT, 0, vertices);
  glEnableClientState(GL_VERTEX_ARRAY);

 }
 /* Set up index data */
 glGenBuffers(1, &ebo);
 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

 /* We allocate an extra restart index because it simplifies the element-array loop logic */
 glBufferData(GL_ELEMENT_ARRAY_BUFFER, NumStrips*(NumPointsPerStrip + 1)*sizeof(GLushort),
  NULL, GL_STATIC_DRAW);
 indices = (GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);

 

 if (indices == NULL)
 {
  fprintf(stderr, "Unable to map index buffer\n");
  exit(EXIT_FAILURE);
 }
 else
 {
  int i, j;
  GLushort *index = indices;
  for (j = 0; j < NumStrips; j++)
  {
   GLushort bottomRow = j*NumYPoints;
   GLushort topRow = bottomRow + NumYPoints;

   for (i = 0; i < NumXPoints; i++)
   {
    *index++ = topRow + i;
    *index++ = bottomRow + i;

   }
  *index++ = RestartIndex;
  }

  glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
 }

 glPrimitiveRestartIndex(RestartIndex);
 glEnableClientState(GL_PRIMITIVE_RESTART);

}
void display(void)
{
 glClear(GL_COLOR_BUFFER_BIT);

 glColor3f(1, 1, 1);
// glDrawElements(GL_TRIANGLE_STRIP, NumStrips*(NumPointsPerStrip + 1), GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));  //书上的函数
 glDrawElements(GL_TRIANGLE_STRIP, NumStrips*(NumPointsPerStrip + 1), GL_UNSIGNED_SHORT, indices);

 glutSwapBuffers();
}

void reshape(int w, int h)
{
 glViewport(0, 0, (GLsizei)w, (GLsizei)h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
}

int main(int argc, char **argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
 glutInitWindowSize(640, 480);
 glutInitWindowPosition(100, 100);
 glutCreateWindow(argv[0]);
 glutDisplayFunc(display);
 glutReshapeFunc(reshape);

 GLenum err = glewInit();

 if (GLEW_OK != err)
 {
  fprintf(stderr, "Glew init err\n");
  exit(EXIT_FAILURE);
 }
 init();
 glutMainLoop();

 return 0;

}

 

加载中
返回顶部
顶部