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