c++ - glCreateShader fails and return 0 -
i'm having problem glcreateshaders. returns 0. i'm using glew sdl , whenever run program, says:
0(1) : error c0000: syntax error, unexpected '}' @ token "}" shader shaders/colorshading.vert failed compile!
main.cpp:
#include <iostream> #include "maingame.h" int main(int argc, char** argv) { maingame maingame; maingame.run(); homecoming 0; }
maingame.h:
//core -> initializing glew, sdl etc... //just ignore sprite class #pragma 1 time #include <sdl/sdl.h> #include <gl/glew.h> #include <iostream> #include <string> #include "errors.h" #include "glslprogram.h" enum class gamestate {play, exit}; #include "sprite.h" class maingame { public: maingame(void); ~maingame(void); void run(); private: void initsystems(); void initshaders(); void gameloop(); void processinput(); void drawgame(); sdl_window* _window; int _screenwidth; int _screenheight; gamestate _gamestate; sprite _sprite; glslprogram _colorprogram; };
maingame.cpp:
#include "maingame.h" maingame::maingame(void) { _window = nullptr; _screenwidth = 1024; _screenheight = 700; _gamestate = gamestate::play; } maingame::~maingame(void) { } void maingame::run() { initsystems(); _sprite.init(-1.0f, -1.0f, 1.0f, 1.0f); gameloop(); } void maingame::initsystems() { //initialize sdl sdl_init(sdl_init_everything); _window = sdl_createwindow("game engine", sdl_windowpos_centered, sdl_windowpos_centered, _screenwidth, _screenheight, sdl_window_opengl); if (_window == nullptr) { fatalerror("sdl window not created!"); } sdl_glcontext glcontext = sdl_gl_createcontext(_window); if (glcontext == nullptr) fatalerror("sdl_gl context not created!"); glenum error = glewinit(); if (error != glew_ok) fatalerror("could not initialize glew!"); sdl_gl_setattribute(sdl_gl_doublebuffer, 1); glclearcolor(0.0f, 0.0f, 1.0f, 1.0f); initshaders(); } void maingame::initshaders() { _colorprogram.compileshaders("shaders/colorshading.vert", "shaders/colorshading.frag"); _colorprogram.addattribute("vertexposition"); _colorprogram.linkshaders(); } void maingame::gameloop() { while (_gamestate != gamestate::exit) { processinput(); drawgame(); } } void maingame::processinput() { sdl_event evnt; while (sdl_pollevent(&evnt)) { switch (evnt.type) { case sdl_quit: _gamestate = gamestate::exit; break; case sdl_mousemotion: std::cout << "new coords: " << evnt.motion.x << " " << evnt.motion.y << std::endl; break; } } } void maingame::drawgame() { glcleardepth(1.0); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); _colorprogram.use(); _sprite.draw(); _colorprogram.unuse(); sdl_gl_swapwindow(_window); }
glslprogram.h:
//used #pragma 1 time #include <string> #include <gl/glew.h> //#include <sdl/sdl.h> #include "errors.h" class glslprogram { public: glslprogram(); ~glslprogram(); void compileshaders(const std::string& vertextshaderfilepath, const std::string& fragmentshaderfilepath); void linkshaders(); void addattribute(const std::string&); void use(); void unuse(); private: int _numattributes; void _compileshader(const std::string&, gluint); gluint _programid; gluint _vertexshaderid; gluint _fragmentshaderid; };
glslprogram.cpp:
//used compiling shaders #include "glslprogram.h" #include <fstream> #include <vector> glslprogram::glslprogram() : _numattributes(0), _programid(0), _vertexshaderid(0), _fragmentshaderid(0) { } glslprogram::~glslprogram() { } void glslprogram::compileshaders(const std::string& vertexshaderfilepath, const std::string& fragmentshaderfilepath) { _vertexshaderid = glcreateshader(gl_vertex_shader); if (_vertexshaderid == 0) { fatalerror("vertex shader failed created!"); sdl_quit(); } _fragmentshaderid = glcreateshader(gl_fragment_shader); if (_fragmentshaderid == 0) { fatalerror("fragment shader failed created!"); sdl_quit(); } _compileshader(vertexshaderfilepath, _vertexshaderid); _compileshader(fragmentshaderfilepath, _fragmentshaderid); } void glslprogram::addattribute(const std::string& attributename) { glbindattriblocation(_programid, _numattributes++, attributename.c_str()); } void glslprogram::use() { gluseprogram(_programid); (int x = 0; x < _numattributes; x++) { glenablevertexattribarray(x); } } void glslprogram::unuse() { gluseprogram(0); (int x = 0; x < _numattributes; x++) { gldisablevertexattribarray(x); } } void glslprogram::linkshaders() { //vertex , fragment shaders compiled. //now time link them program. //get programme object. _programid = glcreateprogram(); //attach our shaders our programme glattachshader(_programid, _vertexshaderid); glattachshader(_programid, _fragmentshaderid); //link our programme gllinkprogram(_programid); //note different functions here: glgetprogram* instead of glgetshader*. glint islinked = 0; glgetprogramiv(_programid, gl_link_status, (int *)&islinked); if (islinked == gl_false) { glint maxlength = 0; glgetprogramiv(_programid, gl_info_log_length, &maxlength); //the maxlength includes null character std::vector<char> infolog(maxlength); glgetprograminfolog(_programid, maxlength, &maxlength, &infolog[0]); //we don't need programme anymore. gldeleteprogram(_programid); //don't leak shaders either. gldeleteshader(_vertexshaderid); gldeleteshader(_fragmentshaderid); printf("%s\n", &(infolog[0])); fatalerror("shaders failed link!"); } //always detach shaders after successful link. gldetachshader(_programid, _vertexshaderid); gldetachshader(_programid, _fragmentshaderid); gldeleteprogram(_programid); gldeleteshader(_vertexshaderid); gldeleteshader(_fragmentshaderid); } void glslprogram::_compileshader(const std::string &filepath, gluint id) { std::ifstream vertexfile(filepath); if (vertexfile.fail()) { perror(filepath.c_str()); fatalerror("failed open " + filepath); } std::string filecontents; std::string line; while (std::getline(vertexfile, line)); { filecontents += line + "\n"; } vertexfile.close(); const char *contentsptr = filecontents.c_str(); glshadersource(id, 1, &contentsptr, nullptr); glcompileshader(id); glint iscompiled = 0; glgetshaderiv(id, gl_compile_status, &iscompiled); if (iscompiled == gl_false) { glint maxlength = 0; glgetshaderiv(id, gl_info_log_length, &maxlength); //the maxlength includes null character std::vector<char> errorlog(maxlength); glgetshaderinfolog(id, maxlength, &maxlength, &errorlog[0]); //provide infolog in whatever manor deem best. //exit failure. gldeleteshader(id); //don't leak shader. printf("%s\n", &(errorlog[0])); fatalerror("shader" + filepath + "failed compile!"); } }
vertex shader:
#version 130 //the vertex shader operates on each vertex //input info vbo. each vertex 2 floats in vec2 vertexposition; void main() { //set x,y position on screen gl_position.xy = vertexposition; //the z position 0 since in 2d gl_position.z = 0.0; //indicate coordinates normalized gl_position.w = 1.0; }
and fragment shader:
#version 130 //the fragment shader operates on each pixel in given polygon //this 3 component float vector gets outputted screen //for each pixel. out vec3 color; void main() { //just hardcode color reddish color = vec3(1.0, 0.0, 1.0); }
i don't have thought why happening. :(
ps: i'm beginner glew please don't reply advance stuff:d
edit 1: 02/11/2014 (11/02/2014 americans) downloaded source code tutorial using , working. there's code. edit post when find problem.
remove semicolon in file glslprogram.cpp @ line:
while (std::getline(vertexfile, line));
you go through file , add together empty string filecontents lastly line of shader code, '}'.
c++ opengl sdl shader glew
No comments:
Post a Comment