write a complete program in openGL with glad and C++ two bouncing balls interacting with GLSL and glfw3 that will compile on MacOS. Do not use GLEW . Do not use GLUT. Use a struct data for the balls using projection matrix for 2D rendering.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Here is a complete program in OpenGL with GLFW3, GLSL, and C++ that renders two bouncing balls interacting with each other in a 2D space. The program uses a struct data type for the balls and employs a projection matrix for 2D rendering.
First, let's start by setting up the GLFW3 window and context:
c++
#include <GLFW/glfw3.h>
int main() {
// Initialize GLFW3
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Create a GLFW window
GLFWwindow* window = glfwCreateWindow(800, 600, "Bouncing Balls", NULL, NULL);
if (!window) {
glfwTerminate();
return 1;
}
// Make the window current
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Set up the GLSL program
const char* vertexShaderSource = "#version 450 core\n\
in vec2 vertexPosition;\n\
in vec2 vertexColor;\n\
out vec2 uvCoord;\n\
void main()\n\
{\n\
gl_Position = vec4(vertexPosition, 0.0, 1.0);\n\
uvCoord = vertexPosition;\n\
}";
const char* fragmentShaderSource = "#version 450 core\n\
in vec2 uvCoord;\n\
out vec4 color;\n\
void main()\n\
{\n\
color = vec4(uvCoord, 0.5, 0.5, 1.0);\n\
}";
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, 0);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, 0);
glCompileShader(fragmentShader);
GLuint program = glCreateProgram();
glProgramParameteri(program, GL_PROGRAM_TYPE, GL_PROGRAM_TYPE_VERTEX_AND_FRAGMENT);
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
// Set up the projection matrix
glGetFloatv(GL_PROJECTION_MATRIX_fv, &projectionMatrix[0][0]);
glGetFloatv(GL_MODELVIEW_MATRIX_fv, &modelviewMatrix[0][0]);
// Create the balls
struct Ball {
glm::vec2 position;
glm::vec2 velocity;
glm::vec2 size;
glm::vec4 color;
};
Ball ball1 = {
glm::vec2(-5.0f, -5.0f),
glm::vec2(0.5f, 0.5f),
glm::vec2(0.5f, 0.25f),
glm::vec4(1.0