Help with coding a vertex/point plotter

Hello,

I’ve been trying to make a vertex/point plotter with OpenGl/GLFW and I’m not sure where to begin with it. I’ve tried manipulating the VBO and VAO of a default set of vertices to try to include the added point after it was captured from a mouse click but I don’t think my procedure is the right direction for it. When I start off I have a default set of vertices from range -.5 to .5 creating a rectangle with two triangles. I added functionality to capture mouse clicks to add to the vertices vector and it also updates the indices. When the point is drawn it is off screen and the coordinates aren’t matching the mouse click location coordinates. I think it gets shifted because the small range of -.5 to .5 creates a big rectangle on screen but when I use the click coordinates it doesn’t align with the vertex set up that was created by default since I can click on screen and get a (10,100,0) when I apply it to the object it’s all the way off screen as opposed to the default (0.5,0,0)

#include<iostream>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include <windows.h>
#include"shaderClass.h"
#include"VAO.h"
#include"VBO.h"
#include"EBO.h"
#include <vector>
#include <glm/glm.hpp>

const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"	gl_Position = vec4(aPos.x,aPos.y,aPos.z,1.0);\n"
"}\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"	FragColor = vec4(0.8f,0.3f,0.02f,1.0f);\n"
"}\0";

int main()
{
	glfwInit(); 
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	std::vector<GLfloat> vertices{
     0.5f,  0.5f, 0.0f,  // top right
	 0.5f, -0.5f, 0.0f,  // bottom right
	-0.5f, -0.5f, 0.0f,  // bottom left
	-0.5f,  0.5f, 0.0f 
	};

std::vector<GLuint> indices
	{
	  0, 1, 3,   // first triangle
	  1, 2, 3 ,
	   // second triangl
	};

GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL Practice", NULL, NULL);
	if (window == NULL)
	{
		std::cout << "failed to create GLFW window" << std::endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);

	gladLoadGL();

	glViewport(0, 0, 800, 600);


	Shader shaderProg("defaultVertexShader.shader", "defaultFragmentShader.shader");
	// Generates Vertex Array Object and binds it
	VAO VAO1;
	VAO1.Bind();

	// Generates Vertex Buffer Object and links it to vertices
	VBO VBO1(vertices, sizeof(vertices.size()));

	// Generates Element Buffer Object and links it to indices
	EBO EBO1(indices, sizeof(indices));

	// Links VBO to VAO
	VAO1.LinkVBO(VBO1, 0);
	// Unbind all to prevent accidentally modifying them
	VAO1.Unbind();
	VBO1.Unbind();
	EBO1.Unbind();
double xpos, ypos;
	while (!glfwWindowShouldClose(window))
	{

		glClearColor(0.06f, 0.13f, 0.17f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		shaderProg.Activate();
		VAO1.Bind();
		//glDrawArrays(GL_TRIANGLES, 0, 3);
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
		glfwSwapBuffers(window);
		glfwPollEvents();

		if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
		{
		
		glfwGetCursorPos(window, &xpos, &ypos);
		std::cout << "Position: (" << xpos << ":" << ypos << ")" << std::endl;
		int windowHeight, windowWidth;
		
		glfwGetWindowSize(window, &windowHeight, &windowWidth);
		int windowYPos = ypos + (windowHeight / 2);
		int windowXPos = xpos + (windowWidth / 2);
		vertices.push_back(xpos);
		vertices.push_back(ypos);
		vertices.push_back(0);

		indices.push_back(0);
		indices.push_back(1);
		indices.push_back(vertices.size()/3 - 1);
		std::cout << "Vertices" << std::endl;

		for (int i = 0; i < vertices.size(); i++)
		{
			
			std::cout << vertices.at(i) << std::endl;
		
		}std::cout << "Indices" << std::endl;
		for (int i = 0; i < indices.size(); i++)
		{
			
			std::cout << indices.at(i) << std::endl;
			
		}	

	
		VAO1.Bind();
		
		VBO VBO1(vertices, sizeof(vertices.size()));
		

		// Generates Element Buffer Object and links it to indices
		EBO EBO1(indices, sizeof(indices));
		// Links VBO to VAO
		VAO1.LinkVBO(VBO1, 0);
		// Unbind all to prevent accidentally modifying them
		VAO1.Unbind();
		VBO1.Unbind();
		EBO1.Unbind();

		Sleep(100);
		}

	
	
	}

	// Delete all the objects we've created
	VAO1.Delete();
	VBO1.Delete();
	EBO1.Delete();
	shaderProg.Delete();

	glfwDestroyWindow(window);
	glfwTerminate();

	return 0;
}

I also want to know if what I’m doing to add points to the screen in my render loop is correct. I don’t think it is. Btw when I try clicking I get this:

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