Created
April 22, 2020 23:01
-
-
Save duzenko/b9c5a2368d0876e4385c4486b01957fe to your computer and use it in GitHub Desktop.
This file contains 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 <glad/glad.h> | |
#include <GLFW/glfw3.h> | |
#include <vector> | |
#include <iostream> | |
#include <Windows.h> | |
void framebuffer_size_callback( GLFWwindow* window, int width, int height ); | |
void processInput( GLFWwindow* window ); | |
// settings | |
const unsigned int SCR_WIDTH = 1280; | |
const unsigned int SCR_HEIGHT = 800; | |
#define floatRandom() (static_cast <float> ( rand() ) / static_cast <float> ( RAND_MAX ) * 2 - 1) | |
#define MULTILINE(...) #__VA_ARGS__ | |
static const char* vertex_shader_text = MULTILINE( | |
#version 430\n | |
in vec4 in_position; | |
out vec4 var_position; | |
void main() | |
{ | |
gl_Position = in_position; | |
var_position = in_position; | |
} | |
); | |
static const char* fragment_shader_text = MULTILINE( | |
#version 430\n | |
in vec4 var_position; | |
layout( location = 0 ) uniform float intensity; | |
layout( location = 1 ) uniform bool limit; | |
layout( std430, binding = 3 ) buffer layoutName | |
{ | |
int N; | |
vec2 points[]; | |
}; | |
bool ccw( vec2 A, vec2 B, vec2 C ) { | |
return ( C.y - A.y ) * ( B.x - A.x ) > ( B.y - A.y ) * ( C.x - A.x ); | |
}; | |
bool intersect( vec2 A, vec2 B, vec2 C, vec2 D ) { | |
return ccw( A, C, D ) != ccw( B, C, D ) && ccw( A, B, C ) != ccw( A, B, D ); | |
} | |
bool intersectsOne(vec2 point1, vec2 point2) { | |
return intersect(vec2(0), var_position.xy, point1, point2); | |
} | |
bool intersectsAny() { | |
int n = limit ? 2 : N; | |
for ( int i = 0; i < n; i += 2 ) { | |
if ( intersectsOne( points[i], points[i + 1] ) ) | |
return true; | |
} | |
return false; | |
} | |
void main() | |
{ | |
vec2 xy = vec2( N, N ); | |
gl_FragColor.rgb = vec3( 1 / ( 1 + length( var_position.xy ) ) ); | |
if ( intersectsAny() ) | |
gl_FragColor = vec4( 1, 0, 1, 1 ); | |
gl_FragColor *= intensity; | |
} | |
); | |
extern "C" { | |
//__declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; | |
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; | |
} | |
const int VertexCount = 2*80; | |
struct SSBO { // SOB aligns to 8 :/ | |
int N; | |
int crap; | |
float points[VertexCount][2]; | |
} ssboData { VertexCount }; | |
void draw() { | |
glClear( GL_COLOR_BUFFER_BIT ); | |
glUniform1f( 0, 0.3f ); | |
glBegin( GL_QUADS ); | |
glVertex2i( -1, -1 ); | |
glVertex2i( 1, -1 ); | |
glVertex2i( 1, 1 ); | |
glVertex2i( -1, 1 ); | |
glEnd(); | |
glUniform1f( 0, 1 ); | |
glLineWidth( 2 ); | |
glBegin( GL_LINES ); | |
for ( int i = 0; i < ssboData.N; i++ ) { | |
glVertex2fv( &ssboData.points[i][0] ); | |
} | |
glEnd(); | |
} | |
void compileShader(int shader) { | |
glCompileShader( shader ); | |
GLint isCompiled = 0; | |
glGetShaderiv( shader, GL_COMPILE_STATUS, &isCompiled ); | |
if ( isCompiled == GL_FALSE ) | |
{ | |
GLint maxLength = 0; | |
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &maxLength ); | |
// The maxLength includes the NULL character | |
std::vector<GLchar> errorLog( maxLength ); | |
glGetShaderInfoLog( shader, maxLength, &maxLength, &errorLog[0] ); | |
for ( auto i : errorLog ) | |
std::cout << i; | |
glDeleteShader( shader ); | |
} | |
} | |
void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods ) | |
{ | |
if ( key == GLFW_KEY_1 && action == GLFW_PRESS ) | |
glUniform1i( 1, 0 ); | |
if ( key == GLFW_KEY_2 && action == GLFW_PRESS ) | |
glUniform1i( 1, 1 ); | |
} | |
int main() | |
{ | |
glfwInit(); | |
glfwWindowHint( GLFW_MAXIMIZED, GLFW_TRUE ); | |
GLFWwindow* window = glfwCreateWindow( SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL ); | |
if ( window == NULL ) | |
{ | |
std::cout << "Failed to create GLFW window" << std::endl; | |
glfwTerminate(); | |
return -1; | |
} | |
glfwSetKeyCallback( window, key_callback ); | |
glfwMakeContextCurrent( window ); | |
glfwSetFramebufferSizeCallback( window, framebuffer_size_callback ); | |
if ( !gladLoadGLLoader( (GLADloadproc)glfwGetProcAddress ) ) | |
{ | |
std::cout << "Failed to initialize GLAD" << std::endl; | |
return -1; | |
} | |
glfwSwapInterval( 1 ); | |
srand( 0xabc ); | |
for ( int i = 0; i < ssboData.N; i += 2 ) { | |
ssboData.points[i][0] = floatRandom(); | |
ssboData.points[i][1] = floatRandom(); | |
float n = ssboData.points[i][0] * ssboData.points[i][0] + ssboData.points[i][1] * ssboData.points[i][1]; | |
float nr = 1 / sqrt( sqrt( n ) ); | |
ssboData.points[i + 1][0] = nr * ssboData.points[i][0] + floatRandom() * .2f; | |
ssboData.points[i + 1][1] = nr * ssboData.points[i][1] + floatRandom() * .2f; | |
} | |
GLuint ssbo; | |
glGenBuffers( 1, &ssbo ); | |
glBindBuffer( GL_SHADER_STORAGE_BUFFER, ssbo ); | |
glBufferData( GL_SHADER_STORAGE_BUFFER, sizeof( ssboData ), &ssboData, GL_DYNAMIC_COPY ); | |
glBindBufferBase( GL_SHADER_STORAGE_BUFFER, 3, ssbo ); | |
glBindBuffer( GL_SHADER_STORAGE_BUFFER, 0 ); | |
auto vertex_shader = glCreateShader( GL_VERTEX_SHADER ); | |
glShaderSource( vertex_shader, 1, &vertex_shader_text, NULL ); | |
compileShader( vertex_shader ); | |
auto fragment_shader = glCreateShader( GL_FRAGMENT_SHADER ); | |
glShaderSource( fragment_shader, 1, &fragment_shader_text, NULL ); | |
compileShader( fragment_shader ); | |
auto program = glCreateProgram(); | |
glAttachShader( program, vertex_shader ); | |
glAttachShader( program, fragment_shader ); | |
glLinkProgram( program ); | |
glUseProgram( program ); | |
//glClearColor( 0, 0, 0, 0 ); | |
while ( !glfwWindowShouldClose( window ) ) | |
{ | |
processInput( window ); | |
draw(); | |
glfwSwapBuffers( window ); | |
glfwPollEvents(); | |
} | |
glfwTerminate(); | |
return 0; | |
} | |
void processInput( GLFWwindow* window ) | |
{ | |
if ( glfwGetKey( window, GLFW_KEY_ESCAPE ) == GLFW_PRESS ) | |
glfwSetWindowShouldClose( window, true ); | |
} | |
void framebuffer_size_callback( GLFWwindow* window, int width, int height ) | |
{ | |
glViewport( 0, 0, width, height ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment