diff --git a/main.cpp b/main.cpp index a0fb3de..b035381 100644 --- a/main.cpp +++ b/main.cpp @@ -30,7 +30,9 @@ bool TestGame::init() cerr << "Could not bind attribute " << attribute_name << endl; throw nullptr; } - + t1.init(); + t2.init(); + t3.init(); return true; } void TestGame::stop() @@ -52,7 +54,8 @@ void TestGame::render() //Clear Background glClearColor(1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); - + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glUseProgram(program); glEnableVertexAttribArray(attribute_coord2d); diff --git a/renderer/main.cpp b/renderer/main.cpp index f856f61..e883ee1 100644 --- a/renderer/main.cpp +++ b/renderer/main.cpp @@ -6,7 +6,16 @@ MainClass::MainClass(int width, int height) { SDL_Init(SDL_INIT_VIDEO); window = SDL_CreateWindow("Testgame", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL); - SDL_GL_CreateContext(window); + if(!window) { + cerr << "Error: can't create window: " << SDL_GetError() << endl; + throw nullptr; + } + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1); + if(!SDL_GL_CreateContext(window)) { + cerr << "Error: SDL_GL_CreateContext: " << SDL_GetError() << endl; + throw nullptr; + } } void MainClass::start() { diff --git a/renderer/shapes/triangle.cpp b/renderer/shapes/triangle.cpp index b6d2ba7..4f926a2 100644 --- a/renderer/shapes/triangle.cpp +++ b/renderer/shapes/triangle.cpp @@ -13,20 +13,27 @@ Triangle::Triangle(float ax, float ay, float bx, float by, float cx, float cy) B.y=by-1.0; C.x=cx-1.0; C.y=cy-1.0; -} - -Triangle::~Triangle() -{ } -bool Triangle::render(GLint attrib) -{ +auto Triangle::init() -> void { GLfloat triangle_vertices[] = { A.x, A.y, B.x, B.y, C.x, C.y }; - glVertexAttribPointer(attrib, 2, GL_FLOAT, GL_FALSE, 0, triangle_vertices); + glGenBuffers(1, &vbo_triangle); + glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle); //Create VBO + //Fill VBO + glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW); +} +Triangle::~Triangle() +{ + glDeleteBuffers(1, &vbo_triangle); +} +bool Triangle::render(GLint attrib) +{ + glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle); + glVertexAttribPointer(attrib, 2, GL_FLOAT, GL_FALSE, 0, 0); glDrawArrays(GL_TRIANGLES, 0, 3); return true; } diff --git a/renderer/shapes/triangle.h b/renderer/shapes/triangle.h index 02dbb8d..424499c 100644 --- a/renderer/shapes/triangle.h +++ b/renderer/shapes/triangle.h @@ -4,6 +4,7 @@ #include class Triangle { private: + GLuint vbo_triangle; float2D A; float2D B; float2D C; //The three points @@ -11,6 +12,7 @@ public: Triangle(float x, float y, float alen, float blen, float clen); Triangle(float ax, float ay, float bx, float by, float cx, float cy); ~Triangle(); + auto init() -> void; auto render(GLint attrib) -> bool; }; #endif \ No newline at end of file diff --git a/shaders/triangle.f.glsl b/shaders/triangle.f.glsl index 498d374..bf95289 100644 --- a/shaders/triangle.f.glsl +++ b/shaders/triangle.f.glsl @@ -2,4 +2,5 @@ void main(void) { gl_FragColor[0] = 1.0; gl_FragColor[1] = 1.0; gl_FragColor[2] = 0.0; + gl_FragColor[3] = floor(mod(gl_FragCoord.y, 3.0)); } \ No newline at end of file