c++ - OpenGL, Access violation -


i making opengl project in visual studio 2010. following tutorial youtube. don't know error is, , have error:

unhandled exception @ 0x00000000 in opengl first game.exe: 0xc0000005: access violation. 

main.cpp

#include <gl/glew.h> #include <iostream> #include <gl/glut.h> #include "sprite.h"  sprite _sprite;  void init() {     glclearcolor(0.0f, 1.0f, 1.0f, 1.0f);     _sprite.init(-1.0f, -1.0f, 1.0f, 1.0f); }  void render() {     glcleardepth(1.0);     glclear(gl_color_buffer_bit | gl_depth_buffer_bit);      _sprite.render();      glutswapbuffers(); }  int main(int argc, char** argv) {     glutinit(&argc, argv);     glutinitdisplaymode(glut_depth | glut_double | glut_rgba);     glutinitwindowposition(520, 200);     glutinitwindowsize(800, 600);     glutinitdisplaymode(glut_double);     glutcreatewindow("opengl [ 1 ]");     glutdisplayfunc(render);     init();     glutmainloop(); } 

sprite.cpp

#include <gl/glew.h> #include "sprite.h"  sprite::sprite(void){     _vboid = 0; }   sprite::~sprite(void){     if(_vboid != 0)         gldeletebuffers(1, &_vboid); }  void sprite::init(float x, float y, float width, float height){     _x = x;     _y = y;     _width = width;     _height = height;      if(_vboid == 0){         glgenbuffers(1, &_vboid);     }      float vertexdata[12];     // prvi trougao     vertexdata[0] = x + width;     vertexdata[1] = y + height;     vertexdata[2] = x;     vertexdata[3] = y + height;     vertexdata[4] = x;     vertexdata[5] = y;      // drugi trougao     vertexdata[6] = x;     vertexdata[7] = y;     vertexdata[8] = x + width;     vertexdata[9] = y;     vertexdata[10] = x + width;     vertexdata[11] = y + height;      glbindbuffer(gl_array_buffer, _vboid);     glbufferdata(gl_array_buffer, sizeof(vertexdata), vertexdata, gl_static_draw);     glbindbuffer(gl_array_buffer, 0); }  void sprite::render(){     glbindbuffer(gl_array_buffer, _vboid);      glenablevertexattribarray(0);      glvertexattribpointer(0, 2, gl_float, gl_false, 0, 0);      gldrawarrays(gl_triangles, 0, 6);      gldisablevertexattribarray(0);      glbindbuffer(gl_array_buffer, 0); } 

sprite.h

#pragma once #include <gl/glut.h>  class sprite{     public:         sprite(void);         ~sprite(void);         void init(float x, float y, float width, float height);         void render();     private:         float _x;         float _y;         float _width;         float _height;         gluint _vboid; // gluint na garantira da ce int biti 32 bita }; 

you did not initialize glew. without doing entry points provided glew (which beyond opengl-1.1) left uninitialized , calling them crashes program.

add

if( glew_ok != glewinit() ) { return 1; } while( gl_no_error != glgeterror() ); /* glewinit may cause opengl errors -- flush error state */ 

after glutcreatewindow.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -