Skip to content

Instantly share code, notes, and snippets.

@jnrdrgz
Created June 18, 2020 04:46
Show Gist options
  • Save jnrdrgz/9d0184ae93e73b3e54a07bddd890dbaa to your computer and use it in GitHub Desktop.
Save jnrdrgz/9d0184ae93e73b3e54a07bddd890dbaa to your computer and use it in GitHub Desktop.
opengl_glewglfw
#shader vertex
#version 330 core
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
};
#shader fragment
#version 330 core
layout(location = 0) out vec4 color;
void main()
{
color = vec4(0.3,0.4,0.8,1.0);
};
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
struct ShaderProgramSource
{
std::string VertexSource;
std::string FragmentSource;
};
static ShaderProgramSource ParseShader(std::string filepath)
{
std::ifstream stream(filepath);
enum class ShaderType
{
NONE = -1, VERTEX = 0, FRAGMENT = 1
};
std::string line;
std::stringstream ss[2];
ShaderType type = ShaderType::NONE;
auto _p = [](const std::string& l, const std::string& v) {return l.find(v) != std::string::npos; };
while (getline(stream, line)) {
if (_p(line, "#shader"))
{
if (_p(line, "vertex")) type = ShaderType::VERTEX;
else if (_p(line, "fragment")) type = ShaderType::FRAGMENT;
}
else {
ss[(int)type] << line << "\n";
}
}
return { ss[0].str(),ss[1].str() };
}
static unsigned int CompileShader(unsigned int type, const std::string& source) {
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
//when pass pointer src you are passing a pointer to pointer
//(an array of array), so you pass the pointer of the pointer to src
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
//TODO error handling
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*)_malloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
std::cout << "Failed to compile "
<< (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << "\n";
std::cout << message << "\n";
}
return id;
}
static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
{
unsigned int program = glCreateProgram();
unsigned int vShader = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fShader = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vShader);
glDeleteShader(fShader);
return program;
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
std::cout << "ERROR\n";
}
std::cout << glGetString(GL_VERSION) << "\n";
std::cout << "test\n";
float positions[] = {
-0.5f, -0.5f, // 0
0.5f, -0.5f, // 1
0.5f, 0.5f, // 2
-0.5f, 0.5f // 3
};
unsigned int indices[] = {
0,1,2,
2,3,0
};
unsigned int buffer;
glGenBuffers(1, &buffer);
//BIND == SELECT
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6*2*sizeof(float), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), 0);
unsigned int ibo;
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*sizeof(unsigned int), indices, GL_STATIC_DRAW);
//std::string vertexShader
//std::string fragmentShader
ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");
std::cout << "Vertex:\n";
std::cout << source.VertexSource << "\n";
std::cout << "Fragment:\n";
std::cout << source.FragmentSource << "\n";
unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
glUseProgram(shader);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
//glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glDeleteProgram(shader);
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment