I wrote some code to display a shape (in this case a pentagon) in a window. The pentagon displays just fine on my computer, but when I create a zip file and send it to others to try on their computers the window still opens but the inside of the window is blank rather than displaying a pentagon as it does on my computer. Can anyone tell me why I see a pentagon in the window when I open the exe file on my computer, but others see nothing?
Here is the "main.cpp" code:
#include <iostream>
#include <fstream>
#include "program.h";
int main()
{
Program myProgram;
myProgram.greetUser();
return 0;
}
Here is the "program.h" code:
#pragma once
class Program {
public:
void greetUser();
private:
char decision;
void promptUser();
void runOpenGL();
};
Here is the "program.cpp" code:
#include "program.h";
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
void Program::greetUser() {
cout << endl;
cout << "Hello. Welcome to this program test." << endl;
cout << endl;
promptUser();
}
void Program::promptUser() {
cout << "Would you like to open the program? (Y/N)" << endl;
cout << endl;
cin >> decision;
cout << endl;
if (decision == 'y' || decision == 'Y' || decision == 'Yes' || decision == 'yes') {
cout << "Opening program... ";
runOpenGL();
}
else if (decision == 'n' || decision == 'N' || decision == 'No' || decision == 'no') {
cout << "Okay. Exiting the program.";
}
else {
cout << "Make up your mind already.";
}
}
void Program::runOpenGL() {
cout << "Program opened.";
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, o.3f, 0.2f, 1.0f);\n"
"}\n\0";
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLfloat vertices[] =
{
-0.4f, -0.5f, 0.0f,
0.4f, -0.5f, 0.0f,
0.6f, 0.1f, 0.0f,
0.0f, 0.6f, 0.0f,
-0.6f, 0.1f, 0.0f
};
GLFWwindow* window = glfwCreateWindow(800, 800, "H6-Project", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
//return -1;
}
glfwMakeContextCurrent(window);
gladLoadGL();
glViewport(0, 0, 800, 800);
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
while (!glfwWindowShouldClose(window))
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_LINE_LOOP, 0, 5);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
glfwDestroyWindow(window);
glfwTerminate();
}