diff --git a/main.cpp b/main.cpp index 8e53453..7e2601a 100644 --- a/main.cpp +++ b/main.cpp @@ -15,8 +15,8 @@ #define SCREEN_WIDTH 1600 #define SCREEN_HEIGHT 720 -#define BOX_WIDTH 250 -#define BOX_HEIGHT 250 +#define BOX_WIDTH 100 +#define BOX_HEIGHT 100 static SDL_Window *window = NULL; @@ -25,45 +25,26 @@ static bool running = true; static Uint8 color = 0x18; static Uint64 elapsed = 0; -static float boxX = 0.f; -static float boxY = 0.f; -static float boxVelX = 1.f; -static float boxVelY = 1.f; +static float boxVelX = 0.5f; +static float boxVelY = 0.5f; -static float boxSpeed = 250.f; +static float boxSpeed = 300.f; const char* vShader = "#version 300 es\n" "layout (location = 0) in vec2 aPos;\n" -"uniform mat4 uTransform;\n" -"uniform vec4 uColor;\n" -"out vec4 color;\n" "void main()\n" "{\n" -" gl_Position = uTransform * vec4(aPos, 0.0, 1.0);\n" -" color = uColor;\n" +" gl_Position = vec4(aPos, 0.0, 1.0);\n" "}"; const char* fShader = "#version 300 es\n" "precision mediump float;\n" "out vec4 fragColor;\n" -"in vec4 color;\n" "void main()\n" "{\n" -" fragColor = color;\n" +" fragColor = vec4(0.8, 0.8, 0.8, 1.0);\n" "}"; -void makeRotationZ(float angle, float* m) -{ - float c = cosf(angle); - float s = sinf(angle); - - // column-major (OpenGL style) - m[0] = c; m[4] = -s; m[8] = 0; m[12] = 0; - m[1] = s; m[5] = c; m[9] = 0; m[13] = 0; - m[2] = 0; m[6] = 0; m[10] = 1; m[14] = 0; - m[3] = 0; m[7] = 0; m[11] = 0; m[15] = 1; -} - void computeLetterbox(int windowW, int windowH, int logicalW, int logicalH, int* x, int* y, int* w, int* h) @@ -80,6 +61,13 @@ void computeLetterbox(int windowW, int windowH, *y = (windowH - *h) / 2; } +struct Rect { + float x; + float y; + float w; + float h; +}; + extern "C" int APP_MAIN(int argc, char **argv) { SDL_SetAppMetadata("Example Renderer Primitives", "1.0", "com.example.renderer-primitives"); SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight"); @@ -141,15 +129,14 @@ extern "C" int APP_MAIN(int argc, char **argv) { glDeleteShader(vertexShader); glDeleteShader(fragmentShader); - float vertices[] = { - // positions (x, y) - -0.5f, -0.5f, - 0.5f, -0.5f, - 0.5f, 0.5f, - - -0.5f, -0.5f, - 0.5f, 0.5f, - -0.5f, 0.5f}; + float vertices[12] = {0}; + + Rect rect = { + .x = 50.f, + .y = 50.f, + .w = BOX_WIDTH, + .h = BOX_HEIGHT, + }; GLuint vao, vbo; glGenVertexArrays(1, &vao); @@ -157,7 +144,7 @@ extern "C" int APP_MAIN(int argc, char **argv) { glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0); glEnableVertexAttribArray(0); @@ -168,21 +155,18 @@ extern "C" int APP_MAIN(int argc, char **argv) { int drawableW, drawableH; SDL_GetWindowSizeInPixels(window, &drawableW, &drawableH); +#if defined(__ANDROID__) int vx, vy, vw, vh; computeLetterbox(drawableW, drawableH, SCREEN_WIDTH, SCREEN_HEIGHT, &vx, &vy, &vw, &vh); glViewport(vx, vy, vw, vh); +#else + glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); +#endif glEnable(GL_DEPTH_TEST); - float transform[16]; - float color[4]; - color[0] = 0x18 / 255.f; - color[1] = 0xFF / 255.f; - color[2] = 0x18 / 255.f; - color[3] = 0xFF / 255.f; - while (running) { SDL_Event event; while (SDL_PollEvent(&event)) { @@ -197,31 +181,46 @@ extern "C" int APP_MAIN(int argc, char **argv) { // SDL_Log("dt: %f\n", dt); dt /= 1000.f; - boxX += boxSpeed * boxVelX * dt; - boxY += boxSpeed * boxVelY * dt; - boxSpeed += 50.f * dt; + rect.x += boxSpeed * boxVelX * dt; + rect.y += boxSpeed * boxVelY * dt; + // boxSpeed += 50.f * dt; - if (boxX + BOX_WIDTH >= SCREEN_WIDTH || boxX <= 0) boxVelX *= -1.f; - if (boxY + BOX_HEIGHT >= SCREEN_HEIGHT || boxY <= 0) boxVelY *= -1.f; + if (rect.x + BOX_WIDTH >= SCREEN_WIDTH || rect.x <= 0) boxVelX *= -1.f; + if (rect.y + BOX_HEIGHT >= SCREEN_HEIGHT || rect.y <= 0) boxVelY *= -1.f; + + // float xProj = -1 + rect.x / ((float)SCREEN_WIDTH / 2); + // float yProj = rect.y / ((float)SCREEN_HEIGHT / 2); + + float xProj = (rect.x / SCREEN_WIDTH) * 2.0f - 1.0f; + float yProj = 1.0f - (rect.y / SCREEN_HEIGHT) * 2.0f; + + float wProj = (rect.w / SCREEN_WIDTH) * 2.0f; + float hProj = (rect.h / SCREEN_HEIGHT) * 2.0f; + SDL_Log("%f::%f (%f:%f)\n", xProj, yProj, wProj, hProj); + + vertices[0] = xProj; + vertices[1] = yProj; + vertices[2] = xProj + wProj; + vertices[3] = yProj; + vertices[4] = xProj + wProj; + vertices[5] = yProj - hProj; + + vertices[6] = xProj; + vertices[7] = yProj; + vertices[8] = xProj; + vertices[9] = yProj - hProj; + vertices[10] = xProj + wProj; + vertices[11] = yProj - hProj; glClearColor(0x18/255.f, 0x18/255.f, 0x18/255.f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw your 3D stuff here - - float angle = SDL_GetTicks() / 1000.0f; // rotates over time - - makeRotationZ(angle, transform); - - GLuint tranformLoc = glGetUniformLocation(program, "uTransform"); - GLuint colorLoc = glGetUniformLocation(program, "uColor"); + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW); glUseProgram(program); - glUniformMatrix4fv(tranformLoc, 1, GL_FALSE, transform); - - color[1] = (float)((int)angle * 10 % 255) / 255.f; - glUniform4fv(colorLoc, 1, color); - glBindVertexArray(vao); glDrawArrays(GL_TRIANGLES, 0, 6);