oglc/src/main.c

108 lines
2.7 KiB
C
Raw Normal View History

2021-07-04 02:53:37 +01:00
#include "main.h"
#include "gfx.h"
2021-08-08 16:47:25 +01:00
#include "clock.h"
#include "random.h"
2021-07-04 02:53:37 +01:00
2021-08-06 19:25:52 +01:00
#include "sphere.h"
2021-08-07 15:19:22 +01:00
#include "cam.h"
2021-08-06 19:25:52 +01:00
2021-08-02 09:35:39 +01:00
const int WIDTH = 420;
const int HEIGHT = 420;
void updateUniforms(GLuint shaderProgram);
2021-07-04 02:53:37 +01:00
int main()
{
2021-08-09 18:15:43 +01:00
printf("GL_TEXTURE0: %d\n", GL_TEXTURE0);
printf("GL_TEXTURE1: %d\n", GL_TEXTURE1);
randomInit();
2021-07-10 16:16:01 +01:00
// create a window and opengl context
2021-08-02 09:35:39 +01:00
SDL_Window* window = gfxInit(WIDTH, HEIGHT);
2021-07-06 21:20:01 +01:00
2021-07-04 02:53:37 +01:00
2021-07-10 16:16:01 +01:00
// compile shader programs
2021-07-29 23:39:04 +01:00
unsigned int computeProgram = compileComputeShaderProgram(
2021-08-02 19:43:05 +01:00
"bin/rt.compute");
2021-07-10 16:16:01 +01:00
unsigned int quadProgram = compileQuadShaderProgram(
2021-08-02 19:43:05 +01:00
"bin/shader.vert",
"bin/shader.frag");
2021-07-04 02:53:37 +01:00
2021-08-09 18:15:43 +01:00
// generate noise
GLuint noise = createNoiseTexture(WIDTH, HEIGHT);
glBindTexture(GL_TEXTURE_2D, noise);
int noiseLoc = glGetUniformLocation(computeProgram, "_noise");
glUniform1i(noiseLoc, noise);
2021-08-09 18:15:43 +01:00
// create a texture for the compute shader to write to
GLuint textureOutput = createWriteOnlyTexture(WIDTH, HEIGHT);
printWorkGroupLimits();
2021-07-10 16:16:01 +01:00
// initialise quad
initBuffers();
setVertexAttributes();
2021-07-05 08:51:55 +01:00
int frames = 0;
2021-07-05 01:05:37 +01:00
// render loop
2021-07-04 02:53:37 +01:00
while (!checkQuit())
{
// dispatch compute shader
2021-07-10 16:16:01 +01:00
glUseProgram(computeProgram);
2021-08-02 09:35:39 +01:00
updateUniforms(computeProgram);
glDispatchCompute((GLuint)WIDTH, (GLuint)HEIGHT, 1);
2021-07-10 16:16:01 +01:00
// 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 01:31:35 +01:00
// bind texture written to by compute stage to 2d target
glBindTexture(GL_TEXTURE_2D, textureOutput);
2021-07-05 08:51:55 +01:00
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
2021-07-10 16:16:01 +01:00
// swip swap
2021-07-05 01:05:37 +01:00
SDL_GL_SwapWindow(window);
frames++;
2021-07-04 02:53:37 +01:00
}
float elapsed = now();
printf("%d frames in %f seconds (avg: %f fps)\n",
frames,
elapsed,
(float)frames/elapsed);
2021-07-04 02:53:37 +01:00
return 0;
}
2021-08-02 09:35:39 +01:00
void updateUniforms(GLuint shaderProgram)
{
2021-08-08 16:47:25 +01:00
// update random values
vec4 seed =
{
randomFloat(),
randomFloat(),
randomFloat(),
randomFloat()
};
int loc = glGetUniformLocation(shaderProgram, "_seed");
glUniform4fv(loc, 1, seed);
2021-08-07 03:44:08 +01:00
float t = now();
2021-08-02 09:35:39 +01:00
float sin_t = sin(t);
2021-08-02 23:32:47 +01:00
int tLocation = glGetUniformLocation(shaderProgram, "_t");
2021-08-02 09:35:39 +01:00
glUniform4f(tLocation, t, sin_t, (1.0 + sin_t)*0.5, 0.0f);
2021-08-07 15:19:22 +01:00
float aspect = (float)WIDTH/(float)HEIGHT;
updateCameraUniforms(shaderProgram, aspect);
2021-08-06 19:25:52 +01:00
const int sphereCount = 42;
struct Sphere spheres[sphereCount];
makeSpheres(spheres, sphereCount);
updateSphereUniforms(shaderProgram, spheres, sphereCount);
2021-08-02 09:35:39 +01:00
}