mirror of
https://github.com/guilhermewerner/opengl
synced 2025-06-15 13:04:17 +00:00
148 lines
3.2 KiB
C++
148 lines
3.2 KiB
C++
#include <GL/glew.h>
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
static string ParseShader(const string filePath)
|
|
{
|
|
ifstream stream(filePath);
|
|
|
|
string line;
|
|
stringstream ss;
|
|
|
|
while (getline(stream, line))
|
|
{
|
|
ss << line << "\n";
|
|
}
|
|
|
|
return ss.str();
|
|
}
|
|
|
|
static unsigned int CompileShader(unsigned int type, const 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 == GL_FALSE)
|
|
{
|
|
int lenght;
|
|
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &lenght);
|
|
|
|
char *message = (char *)malloc(lenght + sizeof(char));
|
|
|
|
glGetShaderInfoLog(id, lenght, &lenght, message);
|
|
|
|
cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader!" << endl;
|
|
cout << message << endl;
|
|
|
|
glDeleteShader(id);
|
|
|
|
return 0;
|
|
}
|
|
|
|
return id;
|
|
}
|
|
|
|
static unsigned int CreateShader(const string &vertexShader, const string &fragmentShader)
|
|
{
|
|
unsigned int program = glCreateProgram();
|
|
|
|
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
|
|
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
|
|
|
|
glAttachShader(program, vs);
|
|
glAttachShader(program, fs);
|
|
|
|
glLinkProgram(program);
|
|
glValidateProgram(program);
|
|
|
|
glDeleteShader(vs);
|
|
glDeleteShader(fs);
|
|
|
|
return program;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
GLFWwindow *window;
|
|
|
|
/* Initialize the library */
|
|
if (!glfwInit())
|
|
return -1;
|
|
|
|
/* Create a windowed mode window and its OpenGL context */
|
|
window = glfwCreateWindow(1280, 720, "Hello World", NULL, NULL);
|
|
if (!window)
|
|
{
|
|
glfwTerminate();
|
|
return -1;
|
|
}
|
|
|
|
/* Make the window's context current */
|
|
glfwMakeContextCurrent(window);
|
|
|
|
if (glewInit() != GLEW_OK)
|
|
{
|
|
cout << "Error!" << endl;
|
|
}
|
|
|
|
cout << glGetString(GL_VERSION) << endl;
|
|
|
|
float positions[6] = {
|
|
-0.5f, -0.5f,
|
|
0.0f, 0.5f,
|
|
0.5f, -0.5f};
|
|
|
|
unsigned int buffer;
|
|
glGenBuffers(1, &buffer);
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
|
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
|
|
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
string vertexShader = ParseShader("Shaders/Vertex.glsl");
|
|
cout << "VERTEX:" << endl;
|
|
cout << vertexShader << endl;
|
|
|
|
string fragmentShader = ParseShader("Shaders/Fragment.glsl");
|
|
cout << "FRAGMENT:" << endl;
|
|
cout << fragmentShader << endl;
|
|
|
|
unsigned int shader = CreateShader(vertexShader, fragmentShader);
|
|
|
|
glUseProgram(shader);
|
|
|
|
/* Loop until the user closes the window */
|
|
while (!glfwWindowShouldClose(window))
|
|
{
|
|
/* Render here */
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
/* Swap front and back buffers */
|
|
glfwSwapBuffers(window);
|
|
|
|
/* Poll for and process events */
|
|
glfwPollEvents();
|
|
}
|
|
|
|
glDeleteProgram(shader);
|
|
|
|
glfwTerminate();
|
|
|
|
return 0;
|
|
}
|