菜鸟学习OpenGL,做了下书上例子编译通过,运行到glGenBuffers(1,&vbo)出错;求指导
错误提示:text2.exe中的0x00000000处有未经处理的异常:0xC0000005:Accessviolation以下为源码,编程环境VS2010#pragma...
错误提示:text2.exe 中的 0x00000000 处有未经处理的异常: 0xC0000005: Access violation
以下为源码,编程环境VS2010
#pragma comment(lib,"glew32.lib")
#include "windows.h"
#include <stdio.h>
#include <gl/glew.h>
#include <gl/glut.h>
#define BUFFER_OFFSET(offset) ((GLvoid*)offset)
#define XStart -0.8
#define XEnd 0.8
#define YStart -0.8
#define YEnd 0.0
#define NumXPoints 11
#define NumYPoints 11
#define NumPoints (NumXPoints * NumYPoints)
#define NumPointsPerStrip (2*NumPoints)
#define NumStrips (NumXPoints-1)
#define RestartIndex 0xffff
void init()
{
GLuint vbo, ebo;
GLfloat *vertices;
GLushort *indices;
//--------Set up vertex data---------//
//创建缓冲区对象
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);
if (vertices == NULL)
{
fprintf(stderr, "Unable to map vertex buffer\n");
exit(EXIT_FAILURE);
}
else
{
int i, j;
GLfloat dx = (XEnd - XStart) / (NumPoints - 1);
GLfloat dy = (YEnd - YStart) / (NumPoints - 1);
GLfloat *tmp = vertices;
int n = 0;
for (j = 0; j < NumPoints; ++j)
{
GLfloat y = YStart + j*dy;
for (i = 0; i < NumPoints; ++i)
{
GLfloat x = XStart + i*dx;
*tmp++ = x;
*tmp++ = y;
}
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glVertexPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
}
//--------Set up index data---------//
glGenBuffers(1,&ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);
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);
glEnable(GL_PRIMITIVE_RESTART);
}
void display()
{
//int i, start;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glDrawElements(GL_TRIANGLE_STRIP, NumStrips*(NumPointsPerStrip+1), GL_UNSIGNED_SHORT,BUFFER_OFFSET(0));
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(250,250);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
} 展开
以下为源码,编程环境VS2010
#pragma comment(lib,"glew32.lib")
#include "windows.h"
#include <stdio.h>
#include <gl/glew.h>
#include <gl/glut.h>
#define BUFFER_OFFSET(offset) ((GLvoid*)offset)
#define XStart -0.8
#define XEnd 0.8
#define YStart -0.8
#define YEnd 0.0
#define NumXPoints 11
#define NumYPoints 11
#define NumPoints (NumXPoints * NumYPoints)
#define NumPointsPerStrip (2*NumPoints)
#define NumStrips (NumXPoints-1)
#define RestartIndex 0xffff
void init()
{
GLuint vbo, ebo;
GLfloat *vertices;
GLushort *indices;
//--------Set up vertex data---------//
//创建缓冲区对象
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);
if (vertices == NULL)
{
fprintf(stderr, "Unable to map vertex buffer\n");
exit(EXIT_FAILURE);
}
else
{
int i, j;
GLfloat dx = (XEnd - XStart) / (NumPoints - 1);
GLfloat dy = (YEnd - YStart) / (NumPoints - 1);
GLfloat *tmp = vertices;
int n = 0;
for (j = 0; j < NumPoints; ++j)
{
GLfloat y = YStart + j*dy;
for (i = 0; i < NumPoints; ++i)
{
GLfloat x = XStart + i*dx;
*tmp++ = x;
*tmp++ = y;
}
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glVertexPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
}
//--------Set up index data---------//
glGenBuffers(1,&ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);
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);
glEnable(GL_PRIMITIVE_RESTART);
}
void display()
{
//int i, start;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glDrawElements(GL_TRIANGLE_STRIP, NumStrips*(NumPointsPerStrip+1), GL_UNSIGNED_SHORT,BUFFER_OFFSET(0));
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(250,250);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询