Additional radius is visible when drawing hollow circle

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);
	}
}

na forum

vertices.size() is the number of floats; glDrawArrays needs the number of vertices.

So you’re telling it to draw three times as many vertices as you actually have.

Also: you’ll want to either change the <numOfVertices to <=numOfVertices in genHollowCircleVertices, or change GL_LINE_STRIP to GL_LINE_LOOP to avoid a gap where the last line segment should be.

Hello, I have divided number of vertices by 3 and it have solved my problem. Changing <numOfVertices to <=numOfVertices doesn’t affects my program and it also works correctly. Why?

I overlooked this part:

which basically does the same thing.

Thanks btw I have now I have an issue with texturing a filled circle. I have system that that generates a relevant coords but I see than texture is repeating. I guess(but I’m not sure) it is because texture coords are in range[0, 1] and in local space range is between [-1, 1] .How can I convert it to range [0, 1]?

void genCircleVertices(std::vector<float>& vertices, float radius, int n)
{
	int numOfVertices = n + 2;
	const float TWO_PI = 2 * 3.14159;
	float step = TWO_PI / (float)n;;

	vertices.push_back(0.0f);
	vertices.push_back(0.0f);
	vertices.push_back(0.0f);

	vertices.push_back(0.0f);
	vertices.push_back(0.0f);

	for (int i = 1; i < numOfVertices; i++)
	{
		float x = radius * cos(i * step);
		float y = radius * sin(i * step);
		float z = 0.0f;

		// Texture coords
		float textureX = cos(i * step);
		float textureY = sin(i * step);


		vertices.push_back(x);
		vertices.push_back(y);
		vertices.push_back(z);

		vertices.push_back(textureX);
		vertices.push_back(textureY);
	}
}

(... + 1) / 2

		float textureX = (cos(i * step) + 1) / 2;
		float textureY = (sin(i * step) + 1) / 2;

This really should have been obvious, so I’d suggest focusing on improving your basic math skills before getting into graphics programming.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.