X-Git-Url: https://git.lukelau.me/?p=wasm.git;a=blobdiff_plain;f=test.cpp;h=cbdf3707e6cd47a2a6259809024a112009c61135;hp=d59323af877cb9a5b084d770b824e96d3a05d51a;hb=HEAD;hpb=aed1f5271c857c3afa085c593d2ca6159770395c diff --git a/test.cpp b/test.cpp index d59323a..cbdf370 100644 --- a/test.cpp +++ b/test.cpp @@ -1,18 +1,62 @@ -#include +#include #include +#include +#include +#include +#include const char* vertShaderSrc = R"( attribute vec4 vertPos; +uniform mat4 model; void main() { - gl_Position = vertPos; + gl_Position = model * vertPos; } )"; -extern "C" int foo() { +const char *fragShaderSrc = R"( +void main() { + gl_FragColor = vec4(1, 0, 1, 1); +} +)"; + +GLuint program; + +extern "C" void setup() { GLuint vertShader = glCreateShader(GL_VERTEX_SHADER); const char* sources[1] = { vertShaderSrc }; const GLint lengths[1] = { (GLint)strlen(vertShaderSrc) }; glShaderSource(vertShader, 1, sources, lengths); glCompileShader(vertShader); - return 0; + + GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragShader, 1, + new const char*[1] { fragShaderSrc }, + new const GLint[1] { (GLint)strlen(fragShaderSrc) }); + glCompileShader(fragShader); + + program = glCreateProgram(); + glAttachShader(program, vertShader); + glAttachShader(program, fragShader); + glLinkProgram(program); + glUseProgram(program); + + GLuint vertPosBuffer; + glGenBuffers(1, &vertPosBuffer); + glBindBuffer(GL_ARRAY_BUFFER, vertPosBuffer); + std::vector vertPositions = { + 0, 1, 0, + 1, -1, 0, + -1, -1, 0 + }; + glBufferData(GL_ARRAY_BUFFER, vertPositions.size(), vertPositions.data(), GL_STATIC_DRAW); + + GLuint vertPosLoc = glGetAttribLocation(program, "vertPos"); + glEnableVertexAttribArray(vertPosLoc); + glVertexAttribPointer(vertPosLoc, 3, GL_FLOAT, false, 0, 0); +} + +extern "C" void update(int tick) { + GLuint modelLoc = glGetUniformLocation(program, "model"); + const glm::mat4 model = glm::rotate(glm::mat4(1), (float)tick * 0.001f, glm::vec3(0, 0, 1)); + glUniformMatrix4fv(modelLoc, 1, false, glm::value_ptr(model)); }