2021-07-04 03:53:37 +02:00
|
|
|
#include "main.h"
|
|
|
|
#include "gfx.h"
|
|
|
|
|
|
|
|
// forward declarations
|
2021-07-05 09:51:55 +02:00
|
|
|
|
|
|
|
// input
|
2021-07-04 03:53:37 +02:00
|
|
|
int checkQuit();
|
|
|
|
|
2021-07-05 09:51:55 +02:00
|
|
|
// time
|
|
|
|
float time();
|
|
|
|
|
2021-07-04 03:53:37 +02:00
|
|
|
int main()
|
|
|
|
{
|
2021-07-24 02:31:35 +02:00
|
|
|
int width = 420;
|
|
|
|
int height = 420;
|
2021-07-04 03:53:37 +02:00
|
|
|
|
2021-07-10 17:16:01 +02:00
|
|
|
// create a window and opengl context
|
|
|
|
SDL_Window* window = gfxInit(width, height);
|
2021-07-06 22:20:01 +02:00
|
|
|
|
2021-07-10 17:16:01 +02:00
|
|
|
// create a texture for the compute shader to write to
|
|
|
|
GLuint textureOutput = createWriteOnlyTexture(width, height);
|
|
|
|
printWorkGroupLimits();
|
2021-07-04 03:53:37 +02:00
|
|
|
|
2021-07-10 17:16:01 +02:00
|
|
|
// compile shader programs
|
2021-07-30 00:39:04 +02:00
|
|
|
unsigned int computeProgram = compileComputeShaderProgram(
|
2021-07-30 03:06:59 +02:00
|
|
|
"bin/res/rt.compute");
|
2021-07-10 17:16:01 +02:00
|
|
|
unsigned int quadProgram = compileQuadShaderProgram(
|
2021-07-30 00:39:04 +02:00
|
|
|
"bin/res/shader.vert",
|
|
|
|
"bin/res/shader.frag");
|
2021-07-04 03:53:37 +02:00
|
|
|
|
2021-07-10 17:16:01 +02:00
|
|
|
// initialise quad
|
|
|
|
initBuffers();
|
|
|
|
setVertexAttributes();
|
2021-07-05 09:51:55 +02:00
|
|
|
|
2021-07-30 03:06:59 +02:00
|
|
|
|
2021-07-05 02:05:37 +02:00
|
|
|
// render loop
|
2021-07-04 03:53:37 +02:00
|
|
|
while (!checkQuit())
|
|
|
|
{
|
2021-07-10 17:16:01 +02:00
|
|
|
glUseProgram(computeProgram);
|
|
|
|
|
|
|
|
// update uniforms
|
2021-07-30 03:06:59 +02:00
|
|
|
float t = time(); // time
|
2021-07-24 02:31:35 +02:00
|
|
|
float sin_t = sin(t);
|
2021-07-10 17:16:01 +02:00
|
|
|
int tLocation = glGetUniformLocation(computeProgram, "t");
|
2021-07-30 03:06:59 +02:00
|
|
|
glUniform4f(tLocation, t, sin_t, (1.0 + sin_t)*0.5, 0.0f);
|
|
|
|
|
|
|
|
// form view space axes
|
|
|
|
vec3 u,v;
|
|
|
|
vec3 up = {0,1.0,0};
|
|
|
|
vec3 w = {0,sin_t*0.1,-1.0};
|
|
|
|
glm_vec3_norm(w);
|
|
|
|
glm_vec3_cross(up,w,u);
|
|
|
|
glm_vec3_norm(u);
|
|
|
|
glm_vec3_cross(w, u, v);
|
|
|
|
int wLocation = glGetUniformLocation(computeProgram, "w");
|
|
|
|
glUniform3f(wLocation+0, w[0], w[1], w[2]); // w
|
|
|
|
glUniform3f(wLocation+1, u[0], u[1], u[2]); // u
|
|
|
|
glUniform3f(wLocation+2, v[0], v[1], v[2]); // v
|
2021-07-10 17:16:01 +02:00
|
|
|
|
|
|
|
// dispatch compute shader
|
|
|
|
glDispatchCompute((GLuint)width, (GLuint)height, 1);
|
|
|
|
|
|
|
|
// make sure we're finished writing to the texture before trying to read it
|
|
|
|
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
|
|
|
|
|
|
|
|
// normal drawing pass
|
|
|
|
glUseProgram(quadProgram);
|
2021-07-24 02:31:35 +02:00
|
|
|
glActiveTexture(GL_TEXTURE0); // use computed texture
|
2021-07-10 17:16:01 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, textureOutput);
|
2021-07-24 02:31:35 +02:00
|
|
|
|
2021-07-05 09:51:55 +02:00
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
2021-07-10 17:16:01 +02:00
|
|
|
|
|
|
|
// swip swap
|
2021-07-05 02:05:37 +02:00
|
|
|
SDL_GL_SwapWindow(window);
|
2021-07-04 03:53:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-05 09:51:55 +02:00
|
|
|
float time()
|
|
|
|
{
|
|
|
|
return (float)SDL_GetTicks() / 1000.0f; // ms / 1000.0 = seconds since start
|
|
|
|
}
|
|
|
|
|
2021-07-04 03:53:37 +02:00
|
|
|
int checkQuit()
|
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
|
|
|
|
if (SDL_PollEvent(&event) && event.type == SDL_QUIT) return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|