Last active
January 2, 2020 06:22
-
-
Save orazdow/1f444f2027157461edcd951b33470039 to your computer and use it in GitHub Desktop.
GLFW cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef GL_PROG | |
#define GL_PROG | |
#include <glad/glad.h> | |
#include <glad/glad.h> | |
#include <GLFW/glfw3.h> | |
#include <cstdio> | |
#define gl_err(str) glfw_err(0, (str)); | |
void glfw_err(int error, const char* description); | |
void processInput(GLFWwindow *window); | |
void framebuffer_size_callback(GLFWwindow* window, int width, int height); | |
void keyCB(GLFWwindow* window, int key, int scancode, int action, int mods); | |
GLFWwindow* glfw_init_window(int w, int h, const char* str); | |
class GlProg{ | |
public: | |
GLFWwindow* window; | |
int w, h; | |
GlProg(int _w, int _h, const char* str = NULL){ | |
w = _w; h = _h; | |
this->window = glfw_init_window(w, h, str); | |
} | |
~GlProg(){ | |
glfwTerminate(); | |
} | |
}; | |
// glfw init | |
GLFWwindow* glfw_init_window(int w, int h, const char* str = NULL){ | |
glfwSetErrorCallback(glfw_err); | |
if(!glfwInit()){ | |
glfw_err(GLFW_NOT_INITIALIZED, "no init"); | |
return NULL; | |
} | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); | |
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
#ifdef __APPLE__ | |
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | |
#endif | |
// glfwWindowHint(GLFW_RESIZABLE, true); | |
// glfwWindowHint(GLFW_SAMPLES, 4); // MSAA | |
GLFWwindow* window = glfwCreateWindow(w, h, str ? str : "", NULL, NULL); | |
if(!window){ | |
gl_err("failed to create window"); | |
glfwTerminate(); | |
return NULL; | |
} | |
glfwMakeContextCurrent(window); | |
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); | |
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){ | |
gl_err("failed initialize glad"); | |
return NULL; | |
} | |
return window; | |
} | |
void keyCB(GLFWwindow* window, int key, int scancode, int action, int mods){ | |
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){ | |
glfwSetWindowShouldClose(window, 1); | |
} | |
} | |
void processInput(GLFWwindow *window){ | |
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) | |
glfwSetWindowShouldClose(window, 1); | |
} | |
void framebuffer_size_callback(GLFWwindow* window, int width, int height){ | |
glViewport(0, 0, width, height); | |
} | |
void glfw_err(int error, const char* description){ | |
fprintf(stderr, "GFLW error: %s\n", description); | |
} | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "gl_prog.h" | |
#include "shader.h" | |
#include "time.h" | |
#include "windows.h" | |
#define ww 640 | |
#define wh 480 | |
#define FRAND(max) ((float)rand()/(float)RAND_MAX)*(max) | |
void preturb(float* arr, int num, float amp){ | |
for(int i = 0; i < num; i++){ | |
float a = arr[i]; | |
a += FRAND(amp) - amp/2.0f; | |
if(a > 1.0f) a = 1.0f; | |
else if(a < -1.0f) a = -1.0f; | |
arr[i] = a; | |
} | |
} | |
int main(){ | |
GLFWwindow* window = glfw_init_window(ww, wh); | |
int seed = time(NULL)%300; | |
srand(seed); | |
float pointVertices[256]; | |
for(int i = 0; i < 256; i++){ | |
pointVertices[i] = (FRAND(2)-1.0f);//*0.33; | |
} | |
unsigned int VAO, VBO; | |
glGenVertexArrays(1, &VAO); | |
glGenBuffers(1, &VBO); | |
glBindVertexArray(VAO); | |
glBindBuffer(GL_ARRAY_BUFFER, VBO); | |
// fill VBO | |
glBufferData(GL_ARRAY_BUFFER, sizeof(pointVertices), pointVertices, GL_STATIC_DRAW); | |
// position attribute | |
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); | |
glEnableVertexAttribArray(0); | |
// glEnable(GL_DEPTH_TEST); | |
glEnable(GL_BLEND); | |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
glPointSize(20.0); | |
glClearColor(0.3f, 0.8f, 0.9f, 1.0f); // greenish | |
Shader pointShader("./points.vs", "./points.fs"); | |
if(pointShader.valid()){ | |
pointShader.use(); | |
} | |
while(!glfwWindowShouldClose(window)){ | |
processInput(window); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
preturb(pointVertices, 80, 0.009f); | |
glBufferSubData(GL_ARRAY_BUFFER, 0, 80*sizeof(float), pointVertices); | |
glDrawArrays(GL_POINTS, 0, 40); | |
glfwSwapBuffers(window); | |
glfwPollEvents(); | |
Sleep(10); | |
} | |
glfwTerminate(); | |
printf("bye\n"); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TARGET = out | |
SRC = main.cpp | |
SRC2 = D:/Libraries/glad/src | |
SRC += $(SRC2)/glad.c | |
INCLUDE = -I D:/Libraries/glad/include | |
INCLUDE += -I D:/Libraries/glfw/include | |
LIBS = -L D:/Libraries/glfw/lib-mingw | |
LIBS += -lglfw3 -lgdi32 -lopengl32 -lpthread | |
CXX = g++ | |
# FLAGS = -Wall | |
OBJ = $(notdir $(patsubst %.c, %.o, $(SRC))) | |
OBJ := $(notdir $(patsubst %.cpp, %.o, $(OBJ))) | |
DEPS = $(wildcard *.h) | |
%.o : $(SRC2)/%.c | |
$(CXX) -x c $(INCLUDE) $(FLAGS) -c $< -o $@ | |
%.o : %.cpp $(DEPS) | |
$(CXX) $(INCLUDE) $(FLAGS) -c $< -o $@ | |
all: $(TARGET) | |
$(TARGET): $(OBJ) | |
$(CXX) $(INCLUDE) $(FLAGS) $^ -o $@ $(LIBS) | |
clean: | |
rm -f $(TARGET) $(wildcard *.o) | |
run: | |
@./$(TARGET) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 core | |
out vec4 FragColor; | |
vec2 res = vec2(640, 480); | |
void main(){ | |
FragColor = vec4(1.0,0,0, 0.76-pow(distance(gl_PointCoord, vec2(0.5)), 0.48) ); | |
// FragColor = vec4(0,0,0,1.0); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 core | |
layout (location = 0) in vec2 aPos; | |
void main(){ | |
gl_Position = vec4(aPos, 1.0, 1.0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SHADER_H | |
#define SHADER_H | |
#include <glad/glad.h> | |
#include <GLFW/glfw3.h> | |
#include <stdlib.h> | |
#include <cstdio> | |
class Shader{ | |
public: | |
unsigned int program = 0; | |
enum UType { | |
UNIFORM_FLOAT = 0, | |
UNIFORM_VEC2, | |
UNIFORM_VEC3, | |
UNIFORM_VEC4, | |
UNIFORM_INT, | |
UNIFORM_IVEC2, | |
UNIFORM_IVEC3, | |
UNIFORM_IVEC4, | |
UNIFORM_SAMPLER2D | |
}; | |
Shader(const char* vPath, const char* fPath){ | |
int rtn = init(vPath, fPath); | |
if(rtn) printf("shader compiled\n"); | |
else printf("shader not compiled\n"); | |
} | |
void use(){ glUseProgram(program); } | |
unsigned int valid(){ | |
return program; | |
} | |
unsigned int makeUniform(const char* name){ | |
return glGetUniformLocation(program, name); | |
} | |
// from raylib: | |
void setUniform(int uniformLoc, void *value, int uniformType){ | |
SetShaderValueV(uniformLoc, value, uniformType, 1); | |
} | |
void setUniformV(int uniformLoc, void *value, int uniformType, int count){ | |
SetShaderValueV(uniformLoc, value, uniformType, count); | |
} | |
private: | |
int init(const char* vPath, const char* fPath){ | |
if(!glfwGetCurrentContext()){ | |
printf("no glfw context\n"); | |
return 0; | |
} | |
if(!glad_glDrawArrays){ | |
printf("glad extensions not loaded\n"); | |
return 0; | |
} | |
FILE* f; | |
f = fopen(vPath, "rb" ); | |
if(f == NULL){ | |
printf("error opening %s\n", vPath); | |
return 0; | |
} | |
fseek(f , 0 , SEEK_END); | |
unsigned long len = ftell(f); | |
rewind(f); | |
char* vShaderSrc = (char*)calloc(len + 1, 1); | |
fread(vShaderSrc, 1, len, f); | |
fclose(f); | |
freopen(fPath, "rb", f); | |
if(f == NULL){ | |
printf("error opening %s\n", fPath); | |
return 0; | |
} | |
fseek(f , 0 , SEEK_END); | |
len = ftell(f); | |
rewind(f); | |
char* fShaderSrc = (char*)calloc(len + 1, 1); | |
fread(fShaderSrc, 1, len, f); | |
fclose(f); | |
// printf("%s\n%s\n", vShaderSrc, fShaderSrc); | |
int rtn = compile(vShaderSrc, fShaderSrc); | |
free(vShaderSrc); | |
free(fShaderSrc); | |
return rtn; | |
} | |
// from raylib: | |
void SetShaderValueV(int uniformLoc, void *value, int uniformType, int count){ | |
switch (uniformType){ | |
case UNIFORM_FLOAT: glUniform1fv(uniformLoc, count, (float *)value); break; | |
case UNIFORM_VEC2: glUniform2fv(uniformLoc, count, (float *)value); break; | |
case UNIFORM_VEC3: glUniform3fv(uniformLoc, count, (float *)value); break; | |
case UNIFORM_VEC4: glUniform4fv(uniformLoc, count, (float *)value); break; | |
case UNIFORM_INT: glUniform1iv(uniformLoc, count, (int *)value); break; | |
case UNIFORM_IVEC2: glUniform2iv(uniformLoc, count, (int *)value); break; | |
case UNIFORM_IVEC3: glUniform3iv(uniformLoc, count, (int *)value); break; | |
case UNIFORM_IVEC4: glUniform4iv(uniformLoc, count, (int *)value); break; | |
case UNIFORM_SAMPLER2D: glUniform1iv(uniformLoc, count, (int *)value); break; | |
default: printf("uniform enum type not recognized: %u\n", uniformType); | |
} | |
} | |
int compile(const char* vSrc, const char* fSrc){ | |
int vertexShader, fragmentShader, success = 0; | |
char infoLog[512]; | |
// compile vetex shader | |
vertexShader = glCreateShader(GL_VERTEX_SHADER); | |
glShaderSource(vertexShader, 1, &vSrc, NULL); | |
glCompileShader(vertexShader); | |
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); | |
if(!success) | |
{ | |
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); | |
printf("vertex shader compilation failed:\n%s\n", infoLog); | |
return 0; | |
} | |
// compile fragment shader | |
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(fragmentShader, 1, &fSrc, NULL); | |
glCompileShader(fragmentShader); | |
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); | |
if(!success) | |
{ | |
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); | |
printf("fragment shader compilation failed:\n%s\n", infoLog); | |
return 0; | |
} | |
// link program | |
program = glCreateProgram(); | |
glAttachShader(program, vertexShader); | |
glAttachShader(program, fragmentShader); | |
glLinkProgram(program); | |
// check for linking errors | |
glGetProgramiv(program, GL_LINK_STATUS, &success); | |
if(!success) | |
{ | |
glGetProgramInfoLog(program, 512, NULL, infoLog); | |
printf("shader program linking failed:\n%s\n", infoLog); | |
return 0; | |
} | |
// delete shaders | |
glDeleteShader(vertexShader); | |
glDeleteShader(fragmentShader); | |
return 1; | |
} | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment