Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ nvoglv32.dll访问冲突读取位置0x00000000_C++_Opengl_Sdl - Fatal编程技术网

C++ nvoglv32.dll访问冲突读取位置0x00000000

C++ nvoglv32.dll访问冲突读取位置0x00000000,c++,opengl,sdl,C++,Opengl,Sdl,我最近在使用openGL时遇到了一个问题。在我以前运行良好的程序中,我试图在屏幕上绘制一组多边形,但程序抛出了一个错误,我一生都找不到它的原因。实际上,我没有对代码做任何更改,只是在一个月后返回代码的第二天开始抛出错误。错误是: Unhandled exception at 0x69F2883E (nvoglv32.dll) in openGLtest.exe: 0xC0000005: Access violation reading location 0x00000000. 我用下面的代码

我最近在使用openGL时遇到了一个问题。在我以前运行良好的程序中,我试图在屏幕上绘制一组多边形,但程序抛出了一个错误,我一生都找不到它的原因。实际上,我没有对代码做任何更改,只是在一个月后返回代码的第二天开始抛出错误。错误是:

Unhandled exception at 0x69F2883E (nvoglv32.dll) in openGLtest.exe: 
0xC0000005: Access violation reading location 0x00000000.
我用下面的代码复制了这个错误。我注意到的一些更奇怪的行为是,如果我将对glDrawArrays的调用更改为:

glDrawArrays(GL_TRIANGLES, 3, numV);

然后程序不会抛出错误,尽管它显然没有正确渲染,只渲染了矩形的一半,或者渲染了矩形加上一些垃圾输入

这是非常奇怪的,如果它渲染超过255点,它工作正常,但任何更少,它抛出的错误

#include <SDL.h>
#include <GL/glew.h>
#include <math.h>
#include <vector>

struct Vector2f{
public:
    float x, y;
    Vector2f() {}
    Vector2f(float _x, float _y) { x = _x; y = _y; }
};
struct Vector3f {
public:
    float x, y, z;
    Vector3f() { x = 0.0, y = 0.0, z = 0.0; }
    Vector3f(float _x, float _y, float _z) { x = _x; y = _y; z = _z; }
};
struct Vertex {
    Vector3f m_pos;
    Vector2f m_tex;
    Vector3f m_normal;
    Vertex() {}
    Vertex(Vector3f pos, Vector2f tex) {
        m_pos = pos;
        m_tex = tex;
        m_normal = Vector3f(0.0f, 0.0f, 0.0f);
    }
    Vertex(Vector3f pos, Vector2f tex, Vector3f norm) {
        m_pos = pos;
        m_tex = tex;
        m_normal = norm;
    }
};

SDL_Window*         Window_Display  = NULL;
SDL_GLContext       glcontext;
GLuint              VBO;
int                 width           = 800;
int                 height          = 600;
float               ratio           = (float)width / (float)height;
int                 numV;
std::vector<Vertex> _VBO;

bool initWindow() {
    //draw a square
    _VBO.push_back(Vertex(Vector3f(-0.8f, -0.8f, 0.0f), Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    _VBO.push_back(Vertex(Vector3f(0.8f, -0.8f, 0.0f),  Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    _VBO.push_back(Vertex(Vector3f(0.8f, 0.8f, 0.0f),   Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    _VBO.push_back(Vertex(Vector3f(-0.8f, -0.8f, 0.0f), Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    _VBO.push_back(Vertex(Vector3f(-0.8f, 0.8f, 0.0f),  Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    _VBO.push_back(Vertex(Vector3f(0.8f, 0.8f, 0.0f),   Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    numV = 6;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) return false;
    if ((Window_Display = SDL_CreateWindow("OpenGL Drawing Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL)) == NULL) return false;

    glcontext = SDL_GL_CreateContext(Window_Display);

    glewInit();

    return true;
}

bool drawScene() {
    glEnableClientState(GL_VERTEX_ARRAY);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
    glLoadIdentity(); //Reset the drawing perspective

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, numV * sizeof(Vertex), &_VBO[0], GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)12);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)20);
    glDrawArrays(GL_TRIANGLES, 0, numV);    //Unhandled exception at 0x69F2883E (nvoglv32.dll) in openGLtest.exe: 0xC0000005: Access violation reading location 0x00000000.

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);

    SDL_GL_SwapWindow(Window_Display); //Send the 3D scene to the screen

    glDisableClientState(GL_VERTEX_ARRAY);

    return true;
}

int main(int argc, char* args[]) {
    if (initWindow()) {
        while (drawScene()) {}
    }
    return 0;
}
#包括
#包括
#包括
#包括
结构向量2f{
公众:
浮动x,y;
向量2f(){}
向量2f(float x,float y){x=x;y=y;}
};
结构向量3f{
公众:
浮动x,y,z;
向量3f(){x=0.0,y=0.0,z=0.0;}
向量3f(float x,float y,float z){x=x;y=y;z=z;}
};
结构顶点{
向量3f m_pos;
矢量2f m_tex;
向量3f m_法线;
顶点(){}
顶点(矢量3F位置,矢量2F tex){
m_pos=pos;
m_tex=tex;
m_法线=矢量3f(0.0f,0.0f,0.0f);
}
顶点(向量3f位置、向量2f tex、向量3f范数){
m_pos=pos;
m_tex=tex;
m_normal=正常值;
}
};
SDL_窗口*窗口显示=NULL;
SDL_GLContext GLContext;
GLuint VBO;
整数宽度=800;
内部高度=600;
浮动比率=(浮动)宽度/(浮动)高度;
int numV;
std::vector_VBO;
bool initWindow(){
//画正方形
_VBO.向后推(顶点(向量3F(-0.8f,-0.8f,0.0f),向量2F(0.0f,0.0f),向量3F(0.0f,0.0f,0.0f));
_VBO.向后推(顶点(向量3F(0.8f,-0.8f,0.0f),向量2F(0.0f,0.0f),向量3F(0.0f,0.0f,0.0f));
_VBO.向后推(顶点(向量3F(0.8f,0.8f,0.0f),向量2F(0.0f,0.0f),向量3F(0.0f,0.0f,0.0f));
_VBO.向后推(顶点(向量3F(-0.8f,-0.8f,0.0f),向量2F(0.0f,0.0f),向量3F(0.0f,0.0f,0.0f));
_VBO.向后推(顶点(向量3F(-0.8f,0.8f,0.0f),向量2F(0.0f,0.0f),向量3F(0.0f,0.0f,0.0f));
_VBO.向后推(顶点(向量3F(0.8f,0.8f,0.0f),向量2F(0.0f,0.0f),向量3F(0.0f,0.0f,0.0f));
numV=6;
if(SDL_Init(SDL_Init_VIDEO)<0)返回false;
如果((Window\u Display=SDL\u CreateWindow(“OpenGL绘图测试”,SDL\u WINDOWPOS\u未定义,SDL\u WINDOWPOS\u未定义,宽度,高度,SDL\u Window\u OpenGL))==NULL)返回false;
glcontext=SDL\u GL\u CreateContext(窗口显示);
glewInit();
返回true;
}
bool drawsecene(){
glEnableClientState(GL_顶点_数组);
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
glMatrixMode(GL_MODELVIEW);//切换到绘图透视图
glLoadIdentity();//重置图形透视图
GlenableVertexAttributeArray(0);
GlenableVertexAttributeArray(1);
GlenableVertexAttributeArray(2);
glGenBuffers(1,&VBO);
glBindBuffer(GL_数组_BUFFER,VBO);
glBufferData(GL_数组_缓冲区,numV*sizeof(顶点),&U VBO[0],GL_静态_绘制);
glBindBuffer(GL_数组_BUFFER,VBO);
glvertexattributepointer(0,3,GL_FLOAT,GL_FALSE,sizeof(顶点),0);
glvertexattributepointer(1,2,GL_FLOAT,GL_FALSE,sizeof(顶点),(常量GLvoid*)12);
glvertexattributepointer(2,3,GL_FLOAT,GL_FALSE,sizeof(顶点),(常量GLvoid*)20);
glDrawArrays(GL_三角形,0,numV);//openGLtest.exe中0x69F2883E(nvoglv32.dll)处未处理的异常:0xC0000005:访问冲突读取位置0x00000000。
glDisableVertexAttributeArray(0);
glDisableVertexAttributeArray(1);
GLDisableVertexAttributeArray(2);
SDL_GL_SwapWindow(窗口显示);//将3D场景发送到屏幕
glDisableClientState(GL_顶点_数组);
返回true;
}
int main(int argc,char*args[]{
if(initWindow()){
while(drawsecene()){}
}
返回0;
}

为什么你想用
glVertexAttribPointer()
GL\u VERTEX\u数组一起而不是
glVertexPointer()
?嗯,我不认为只是删除GL\u VERTEX\u数组就可以了。为什么你想用
glVertexAttribPointer()
GL\u vertext\u数组一起而不是
glexportinter()
?嗯,我没想到只删除GL_VERTEX_数组,现在就可以了。
#include <SDL.h>
#include <GL/glew.h>
#include <math.h>
#include <vector>

struct Vector2f{
public:
    float x, y;
    Vector2f() {}
    Vector2f(float _x, float _y) { x = _x; y = _y; }
};
struct Vector3f {
public:
    float x, y, z;
    Vector3f() { x = 0.0, y = 0.0, z = 0.0; }
    Vector3f(float _x, float _y, float _z) { x = _x; y = _y; z = _z; }
};
struct Vertex {
    Vector3f m_pos;
    Vector2f m_tex;
    Vector3f m_normal;
    Vertex() {}
    Vertex(Vector3f pos, Vector2f tex) {
        m_pos = pos;
        m_tex = tex;
        m_normal = Vector3f(0.0f, 0.0f, 0.0f);
    }
    Vertex(Vector3f pos, Vector2f tex, Vector3f norm) {
        m_pos = pos;
        m_tex = tex;
        m_normal = norm;
    }
};

SDL_Window*         Window_Display  = NULL;
SDL_GLContext       glcontext;
GLuint              VBO;
int                 width           = 800;
int                 height          = 600;
float               ratio           = (float)width / (float)height;
int                 numV;
std::vector<Vertex> _VBO;

bool initWindow() {
    //draw a square
    _VBO.push_back(Vertex(Vector3f(-0.8f, -0.8f, 0.0f), Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    _VBO.push_back(Vertex(Vector3f(0.8f, -0.8f, 0.0f),  Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    _VBO.push_back(Vertex(Vector3f(0.8f, 0.8f, 0.0f),   Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    _VBO.push_back(Vertex(Vector3f(-0.8f, -0.8f, 0.0f), Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    _VBO.push_back(Vertex(Vector3f(-0.8f, 0.8f, 0.0f),  Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    _VBO.push_back(Vertex(Vector3f(0.8f, 0.8f, 0.0f),   Vector2f(0.0f, 0.0f), Vector3f(0.0f, 0.0f, 0.0f)));
    numV = 6;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) return false;
    if ((Window_Display = SDL_CreateWindow("OpenGL Drawing Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL)) == NULL) return false;

    glcontext = SDL_GL_CreateContext(Window_Display);

    glewInit();

    return true;
}

bool drawScene() {
    glEnableClientState(GL_VERTEX_ARRAY);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
    glLoadIdentity(); //Reset the drawing perspective

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, numV * sizeof(Vertex), &_VBO[0], GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)12);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)20);
    glDrawArrays(GL_TRIANGLES, 0, numV);    //Unhandled exception at 0x69F2883E (nvoglv32.dll) in openGLtest.exe: 0xC0000005: Access violation reading location 0x00000000.

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);

    SDL_GL_SwapWindow(Window_Display); //Send the 3D scene to the screen

    glDisableClientState(GL_VERTEX_ARRAY);

    return true;
}

int main(int argc, char* args[]) {
    if (initWindow()) {
        while (drawScene()) {}
    }
    return 0;
}