Hello, I have written function that generate vertices for hollow circle. Generally it works fine but there is a problem with additional radius. I don’t want this to be shown and don’t understand why see it. I tried to change number of vertices and set initial value of iterator to 1 instead of 0 but it also doesn’t help.
Why do I see it and how to fix it?
#include <iostream>
#include <math.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "VBO.h"
#include "VAO.h"
#include "EBO.h"
#include "Camera.h"
#include "Shader.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);
void genHollowCircleVertices(std::vector<float>& vertices, float radius, int n);
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "Hello Window!", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to initialize GLFW" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glEnable(GL_DEPTH_TEST);
std::vector<float> vertices;
//genDonutVertices(vertices, 1.5f , 0.5f, 72);
genHollowCircleVertices(vertices, 1.0f, 64);
Shader shaderProgram("vertexShader.vs", "fragmentShader.fs");
VAO VAO1;
VAO1.Bind();
VBO VBO(&vertices[0], vertices.size() * sizeof(float));
VAO1.LinkAttrib(VBO, 0, 3, GL_FLOAT, 3 * sizeof(float), (void*)0);
shaderProgram.use();
while (!glfwWindowShouldClose(window))
{
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shaderProgram.use();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
shaderProgram.setMat4("projection", projection);
glm::mat4 view = camera.GetViewMatrix();
shaderProgram.setMat4("view", view);
VAO1.Bind();
glDrawArrays(GL_LINE_STRIP, 0, vertices.size());
glfwSwapBuffers(window);
glfwPollEvents();
}
VAO1.Unbind();
VBO.Unbind();
VAO1.Delete();
VBO.Delete();
shaderProgram.~Shader();
glfwTerminate();
}
void genHollowCircleVertices(std::vector<float>& vertices, float radius, int n)
{
int numOfVertices = n + 1;
const float TWO_PI = 2 * 3.14159;
float step = TWO_PI / (float)n;;
for (int i = 0; i < numOfVertices; i++)
{
float x = radius * cos(i * step);
float y = radius * sin(i * step);
float z = 0.0f;
vertices.push_back(x);
vertices.push_back(y);
vertices.push_back(z);
}
}