oglc/src/main.c

88 lines
2.2 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-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-10 16:16:01 +01:00
// create a texture for the compute shader to write to
2021-08-02 09:35:39 +01:00
GLuint textureOutput = createWriteOnlyTexture(WIDTH, HEIGHT);
2021-07-10 16:16:01 +01:00
printWorkGroupLimits();
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-07-10 16:16:01 +01:00
// initialise quad
initBuffers();
setVertexAttributes();
2021-07-05 08:51:55 +01:00
2021-07-05 01:05:37 +01:00
// render loop
2021-07-04 02:53:37 +01:00
while (!checkQuit())
{
2021-07-10 16:16:01 +01:00
glUseProgram(computeProgram);
2021-08-02 09:35:39 +01:00
updateUniforms(computeProgram);
2021-07-10 16:16:01 +01:00
// dispatch compute shader
2021-08-02 09:35:39 +01:00
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
glActiveTexture(GL_TEXTURE0); // use computed texture
2021-07-10 16:16:01 +01:00
glBindTexture(GL_TEXTURE_2D, textureOutput);
2021-07-24 01:31:35 +01:00
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);
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
}