Hello guys.
I’m drawing 2 triangles on the screen, but they have a weird effect: they always blink
#include <SDL.h>
#include <glew.h>
#include <conio.h>
#include <iostream>
void BindInput(bool &quit_flag)
{
SDL_Event _event;
SDL_PollEvent(&_event);
if (_event.type == SDL_QUIT)
{
quit_flag = true;
}
if(_event.type == SDL_MOUSEMOTION)
{
}
}
void DrawTriangle(SDL_Window* Win, GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2, GLfloat x3, GLfloat y3)
{
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat vertices[]
{
x1, y1,
1.0f, 0.0f, 0.0f,
x2, y2,
0.0f, 1.0f, 0.0f,
x3, y3,
0.0f, 0.0f, 1.0f,
};
// Create a Vertex Buffer Object (VBO)
GLuint vbo;
// Generate a buffer on our VBO
glGenBuffers(1, &vbo);
// Now bind the buffer on our VBO
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
/* SETTING THE POSITION ATTRIBUTE */
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, 0);
/* SETTING THE COLOR ATTRIBUTE */
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (const void*)(sizeof(GLfloat) * 2));
/* DRAWING THE TRIANGLE */
glDrawArrays(GL_TRIANGLES, 0, 3);
// After drawing, disable all the VertexAttribArrays
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
// Unbind buffer
glBindBuffer(GL_ARRAY_BUFFER, 0);
SDL_GL_SwapWindow(Win);
}
int main(int argc, char** argv)
{
bool quit = false;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("GraphicsTest", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768, SDL_WINDOW_OPENGL);
// ... glew initialization ...
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
while (!quit)
{
BindInput(quit);
DrawTriangle(window, -1.0f, -1.0f, +1.0f, -1.0f, +0.0f, +1.0f);
DrawTriangle(window, +0.5f, +0.5f, +0.5f, +1.0f, +1.0f, +1.0f);
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Can you tell me why that happens?
I just want to draw those 2 triangles on the screen and I want them to be static