+ VBO by default
+ some error-handling + transparency
This commit is contained in:
parent
cce4c4bb1a
commit
1d55b58b1d
5 changed files with 32 additions and 10 deletions
7
main.cpp
7
main.cpp
|
@ -30,7 +30,9 @@ bool TestGame::init()
|
||||||
cerr << "Could not bind attribute " << attribute_name << endl;
|
cerr << "Could not bind attribute " << attribute_name << endl;
|
||||||
throw nullptr;
|
throw nullptr;
|
||||||
}
|
}
|
||||||
|
t1.init();
|
||||||
|
t2.init();
|
||||||
|
t3.init();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
void TestGame::stop()
|
void TestGame::stop()
|
||||||
|
@ -52,7 +54,8 @@ void TestGame::render()
|
||||||
//Clear Background
|
//Clear Background
|
||||||
glClearColor(1.0, 1.0, 1.0, 1.0);
|
glClearColor(1.0, 1.0, 1.0, 1.0);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
glEnable(GL_BLEND);
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
glUseProgram(program);
|
glUseProgram(program);
|
||||||
glEnableVertexAttribArray(attribute_coord2d);
|
glEnableVertexAttribArray(attribute_coord2d);
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,16 @@ MainClass::MainClass(int width, int height)
|
||||||
{
|
{
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
window = SDL_CreateWindow("Testgame", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
|
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()
|
void MainClass::start()
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,20 +13,27 @@ Triangle::Triangle(float ax, float ay, float bx, float by, float cx, float cy)
|
||||||
B.y=by-1.0;
|
B.y=by-1.0;
|
||||||
C.x=cx-1.0;
|
C.x=cx-1.0;
|
||||||
C.y=cy-1.0;
|
C.y=cy-1.0;
|
||||||
}
|
|
||||||
|
|
||||||
Triangle::~Triangle()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
}
|
||||||
bool Triangle::render(GLint attrib)
|
auto Triangle::init() -> void {
|
||||||
{
|
|
||||||
GLfloat triangle_vertices[] = {
|
GLfloat triangle_vertices[] = {
|
||||||
A.x, A.y,
|
A.x, A.y,
|
||||||
B.x, B.y,
|
B.x, B.y,
|
||||||
C.x, C.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);
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <renderer/shapes/float2D.h>
|
#include <renderer/shapes/float2D.h>
|
||||||
class Triangle {
|
class Triangle {
|
||||||
private:
|
private:
|
||||||
|
GLuint vbo_triangle;
|
||||||
float2D A;
|
float2D A;
|
||||||
float2D B;
|
float2D B;
|
||||||
float2D C; //The three points
|
float2D C; //The three points
|
||||||
|
@ -11,6 +12,7 @@ public:
|
||||||
Triangle(float x, float y, float alen, float blen, float clen);
|
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(float ax, float ay, float bx, float by, float cx, float cy);
|
||||||
~Triangle();
|
~Triangle();
|
||||||
|
auto init() -> void;
|
||||||
auto render(GLint attrib) -> bool;
|
auto render(GLint attrib) -> bool;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
|
@ -2,4 +2,5 @@ void main(void) {
|
||||||
gl_FragColor[0] = 1.0;
|
gl_FragColor[0] = 1.0;
|
||||||
gl_FragColor[1] = 1.0;
|
gl_FragColor[1] = 1.0;
|
||||||
gl_FragColor[2] = 0.0;
|
gl_FragColor[2] = 0.0;
|
||||||
|
gl_FragColor[3] = floor(mod(gl_FragCoord.y, 3.0));
|
||||||
}
|
}
|
Loading…
Reference in a new issue