Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to draw 4 points (box corners) with specific color using OpenGL with GLFW and Glad in C++

1 Answer

0 votes
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

#define SCR_WIDTH 800
#define SCR_HEIGHT 800

// Vertex Shader source code // point size = 20px
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"
"	gl_PointSize = 20.0;\n"
"}\n\0";

// Fragment Shader source code
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"
"}\n\0";


int main()
{
	glfwInit();

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
	// CORE profile =  modern OpenGL functions
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "OpenGL Points", NULL, NULL);

	if (window == NULL) {
		std::cout << "Failed to create GLFW window (glfwCreateWindow)\n";
		glfwTerminate();
		return -1;
	}
	// Connect the window into the current context
	glfwMakeContextCurrent(window);

	gladLoadGL();
	// Set viewport of OpenGL in the Window
	glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);

	// Create Vertex Shader Object
	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
	// Attach Vertex Shader source to Vertex Shader Object
	glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
	// Compile the Vertex Shader into machine code
	glCompileShader(vertexShader);

	// Create Fragment Shader Object
	GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	// Attach Fragment Shader source to Fragment Shader Object
	glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
	// Compile the Fragment Shader into machine code
	glCompileShader(fragmentShader);

	// Create Shader Program Object 
	GLuint shaderProgram = glCreateProgram();
	// Attach the Vertex and Fragment Shaders to Shader Program
	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);
	// Link all the shaders together into the Shader Program
	glLinkProgram(shaderProgram);

	// After Attach and Link Delete the Vertex and Fragment Shader objects
	glDeleteShader(vertexShader);
	glDeleteShader(fragmentShader);

	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   // top left
	};

	// Create reference for the Vartex Array Object (VAO) and Vertex Buffer Object (VBO)
	GLuint VAO, VBO;

	// Generate 1 VAO object and 1 VBO object
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);

	// Make the VAO the current Vertex Array Object
	glBindVertexArray(VAO);

	// Bind the VBO 
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	// Conect the vertices into the VBO
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	// Configure the Vertex Attribute to let OpenGL to know how to read the VBO
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
	// Enable the Vertex Attribute to let OpenGL know that he need use it
	glEnableVertexAttribArray(0);

	// Bind the VBO and VAO to 0 to prevent accidentally modify the VAO and VBO 
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

	while (!glfwWindowShouldClose(window)) {
		// Specify the background color
		glClearColor(0.3f, 0.4f, 0.5f, 1.0f);
		// Clean the back buffer and assign the background color to it
		glClear(GL_COLOR_BUFFER_BIT);
		// Tell OpenGL to use the Shader Program Object we create (line 64)
		glUseProgram(shaderProgram);
		// Bind the VAO to let OpenGL know to use it
		glBindVertexArray(VAO);
		// Draw 4 points using primitive GL_POINTS 
		glDrawArrays(GL_POINTS, 0, 4);
		// For gl_PointSize in Vertex Shader source code 
		glEnable(GL_PROGRAM_POINT_SIZE);

		// Swap the back buffer with the front buffer
		glfwSwapBuffers(window);

		glfwPollEvents();
	}

	// Delete all the objects 
	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);
	glDeleteProgram(shaderProgram);
	
	glfwDestroyWindow(window);
	glfwTerminate();
}



/*
run:


*/

 



answered Sep 22, 2023 by avibootz
edited Sep 22, 2023 by avibootz
...