I'm trying to draw a triangle with it's top point in the middle of a circle. Unfortunately I keep getting an error in visual studio, 0x80070003. It still shows the triangle I have drawn but it doesn't show the circle. I'd really appreciate any help I could get. I am using GLEW and GLFW as well.
I've tried to create multiple VAO's to store two different VBO's and then draw them separately, but that didn't worked.
#define PI 3.14159265
#define MAX_SLICES 32
#define MIN_SLICES 8
#define MAX_VERTICES (MAX_SLICES+2)*3
#define CIRCLE_RADIUS 0.5
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
GLfloat c_vertices[MAX_VERTICES] = {
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
};
GLfloat t_vertices[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
GLuint c_slices = MIN_SLICES; // number of circle slices
GLuint g_VBO[2]; // vertex buffer object identifier
GLuint g_VAO[2]; // vertex array object identifier
GLuint g_shaderProgramID = 0; // shader program identifier
void generate_circle()
{
float slice_angle = PI * 2 / static_cast<float>(c_slices); // angle for each fan slice
float angle = slice_angle; // angle used to generate x and y coordinates
float scale_factor = static_cast<float>(WINDOW_HEIGHT) / WINDOW_WIDTH; // scale to make it a circle instead of an elipse
int index = 0; // vertex index
c_vertices[3] = CIRCLE_RADIUS * scale_factor; // set x coordinate of vertex 1
// generate vertex coordinates for triangle fan
for (int i = 2; i < c_slices + 2; i++)
{
// multiply by 3 because a vertex has x, y, z coordinates
index = i * 3;
c_vertices[index] = CIRCLE_RADIUS * cos(angle) * scale_factor;
c_vertices[index + 1] = CIRCLE_RADIUS * sin(angle);
c_vertices[index + 2] = 0.0f;
// update to next angle
angle += slice_angle
}
}
static void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0); // set clear background colour
// create and compile our GLSL program from the shader files
g_shaderProgramID = loadShaders("SimpleVS.vert", "SimpleFS.frag");
// create VBO and buffer the data
glGenBuffers(2, g_VBO);
glBindBuffer(GL_ARRAY_BUFFER, g_VBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(t_vertices), t_vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, g_VBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*(c_slices + 2), c_vertices, GL_DYNAMIC_DRAW);
// create VAO and specify VBO data
glGenVertexArrays(2, g_VAO);
glBindVertexArray(g_VAO[0]);
glBindBuffer(GL_ARRAY_BUFFER, g_VBO[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); // specify the form of the data
glBindVertexArray(g_VAO[1]);
glBindBuffer(GL_ARRAY_BUFFER, g_VBO[1]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0); // enable vertex attributes
glDisableVertexAttribArray(1);
}
static void render_scene()
{
glClear(GL_COLOR_BUFFER_BIT); // clear colour buffer
glUseProgram(g_shaderProgramID); // use the shaders associated with the shader program
glBindVertexArray(g_VAO[0]); // make VAO active
glDrawArrays(GL_TRIANGLES, 0, 3); // display the vertices based on the primitive type
glBindVertexArray(g_VAO[1]);
glDrawArrays(GL_TRIANGLE_FAN, 0, c_slices + 2);
glFlush(); // flush the pipeline
}
User contributions licensed under CC BY-SA 3.0