Untitled

 avatar
unknown
c_cpp
4 years ago
4.3 kB
3
Indexable
// Standard Libraries
#include <iostream>
#include <fstream>
#include <sstream>

// OpenGL
#include "glad/glad.h"
#include "GLFW/glfw3.h"

static std::string LoadShader(std::string path)
{
	std::stringstream result;
	std::ifstream ifs;
	ifs.open(path);

	if (ifs.is_open())
	{
		std::string line = "";
		while (getline(ifs, line))
		{
			result << line << "\n";
		}
	}

	return result.str();
}

static unsigned int CompileShader(unsigned int type, const std::string& source)
{
	unsigned int id = glCreateShader(type);
	const char* src = source.c_str();

	glShaderSource(id, 1, &src, nullptr);
	glCompileShader(id);

	int result;
	glGetShaderiv(id, GL_COMPILE_STATUS, &result);
	if (!result)
	{
		int length;
		glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);

		char* message = (char*)alloca(sizeof(char) * length);
		glGetShaderInfoLog(id, length, &length, message);

		std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << "shader" << std::endl;
	}

	return id;
}

static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
{
	unsigned int id = glCreateProgram();

	unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
	unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);

	glAttachShader(id, vs);
	glAttachShader(id, fs);

	glLinkProgram(id);

	int result;
	glGetShaderiv(id, GL_LINK_STATUS, &result);
	if (!result)
	{
		int length;
		glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);

		char* message = (char*)alloca(sizeof(char) * length);
		glGetShaderInfoLog(id, length, &length, message);

		std::cout << "Failed to link shader program!\n";
	}

	glDeleteShader(vs);
	glDeleteShader(fs);

	return id;
}

// Callbacks
void framebufferSizeCallback(GLFWwindow*, int w, int h)
{
	glViewport(0, 0, w, h);
}

void processInput(GLFWwindow* window)
{
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
		glfwSetWindowShouldClose(window, true);
}

int main()
{
	// Constants
	const char* title = "Triangle";
	const int WIDTH = 800;
	const int HEIGHT = 600;

	if (!glfwInit())
	{
		std::cout << "Failed to initialize GLFW!\n";
		glfwTerminate();
		return EXIT_FAILURE;
	}

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	// Create window
	GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, title, NULL, NULL);
	glfwMakeContextCurrent(window);
	
	glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);

	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		std::cout << "Failed to initialize GLAD!\n";
		return EXIT_FAILURE;
	}

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

	float indices[] =
	{
		0, 1, 3,   // first triangle
		1, 2, 3    // second triangle
	};

	std::string vertexShader = LoadShader("res/shaders/vertex.glsl");
	std::string fragmentShader = LoadShader("res/shaders/fragment.glsl");

	unsigned int vao;
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	unsigned int shader = CreateShader(vertexShader, fragmentShader);
	glUseProgram(shader);

	unsigned int vbo;
	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);

	unsigned int ibo;
	glGenBuffers(1, &ibo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	while (!glfwWindowShouldClose(window))
	{
		// Procces Input
		processInput(window);

		// Clear buffer
		glClearColor(0.1f, 0.1f, 0.1f, 1.f);
		glClear(GL_COLOR_BUFFER_BIT);

		// Draw
		glUseProgram(shader);
		glBindVertexArray(vao);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

		// Swap buffers
		glfwSwapBuffers(window);

		// Poll events
		glfwPollEvents();
	}

	glfwTerminate();
	return EXIT_SUCCESS;
}
Editor is loading...