static void update(void) { uint8_t tmp[6] = {0}; int16_t a[3] = {0}; uint8_t tmpreg = 0; L3GD20_Read(&tmpreg, L3GD20_CTRL_REG4_ADDR, 1); L3GD20_Read(tmp, L3GD20_OUT_X_L_ADDR, 6); /* check in the control register 4 the data alignment (Big Endian or Little Endian)*/ if (!(tmpreg & 0x40)) { for (int i = 0; i < 3; i++) a[i] = (int16_t)(((uint16_t)tmp[2 * i + 1] << 8) | (uint16_t)tmp[2 * i]); } else { for (int i = 0; i < 3; i++) a[i] = (int16_t)(((uint16_t)tmp[2 * i] << 8) | (uint16_t)tmp[2 * i + 1]); } if (STM_EVAL_PBGetState(BUTTON_USER)) { mesh = (mesh + 1) % 3; // next mesh r3d_primitive_winding = windings[mesh]; itoa(meshes[mesh].count / 3, info_str, 10); strcat(info_str, " tris"); while (STM_EVAL_PBGetState(BUTTON_USER)); // debounce } float delta = frametime - time; frametime = time; // make frametime a consistent time value during the frames for (int i = 0; i < 3; i++) axes[i] += (float)a[i] * delta / 114.285f; if (axes[0] < 0) axes[0] = 0; if (axes[0] > 180) axes[0] = 180; model = mat4_mul( mat4_rotation(axes[0], vec3(1.0f, 0.0f, 0.0f)), mat4_rotation(axes[1], vec3(0.0f, 1.0f, 0.0f))); if (mesh == 1) model = mat4_mul(model, mat4_scaling(vec3(0.5f, 0.5f, 0.5f))); mv = mat4_mul(view, model); mvp = mat4_mul(projection, mv); }
int main(int argc, char *argv[]) { uint32_t width = 1920, height = 1080; LoadOpenGLESWindow(width, height); // initial opengl state glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ONE); glDisable(GL_CULL_FACE); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // load textures GLfloat max_anisotropy; glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &max_anisotropy); GLuint textures[TEXTURES_NUM]; glGenTextures(TEXTURES_NUM, textures); int i; for (i = 0; i < TEXTURES_NUM; i++) { int x, y, n; unsigned char *data = stbi_load(texture_files[i], &x, &y, &n, 3); if (data == NULL) { fprintf(stderr, "Could not load file: %s", texture_files[i]); exit(-1); } glBindTexture(GL_TEXTURE_2D, textures[i]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, x, y, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, max_anisotropy); stbi_image_free(data); } // load program GLuint programObject = LoadProgram(vertex_shader, fragment_shader); glUseProgram(programObject); glUniform1i(glGetUniformLocation(programObject, "tex"), 0); GLint colorUniformLocation = glGetUniformLocation(programObject, "color"); GLint transformUniformLocation = glGetUniformLocation(programObject, "transform"); // load quad data glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, quad_vertices); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, quad_texcoord); glEnableVertexAttribArray(1); unsigned int frames = 0; while(1) { glViewport(0, 0, width, height); // animation params double t = (frames++) * 0.02f; float a = 0.5f + 0.5f * sinf(t * 13.0f); float x = 0.2f * sinf(32.0f * t) * sinf(3.5f * t); float y = 0.1f * sinf(23.0f * t) * sinf(3.5f * t); // camera config mat4_t proj = mat4_perspective(50.0f, (float)width / (float)height, 0.1f, 1000.0f); mat4_t view = mat4_mul(//mat4_mul( mat4_translation(vec3(x, y, -3.0f)), // positioning mat4_rotation(-90.0f, vec3(0.0f, 0.0f, 1.0f)));//, // beamer rotation //mat4_rotation(180.0f, vec3(0.0f, 1.0f, 0.0f))); // horizontal "flip" mat4_t vp = mat4_mul(proj, view); // color glUniform3f(colorUniformLocation, a * (0.5f + 0.5f * sinf(t)), a * (0.5f + 0.5f * sinf(1.3f * t)), a * (0.5f + 0.5f * sinf(1.7f * t))); // draw radials int i; for (i = 0; i < 3; i++) { mat4_t mvp = mat4_mul(vp, mat4_rotation(20.0f * cos(t * 2.0f), vec3(0.0f, 1.0f, 0.0f))); mvp = mat4_mul(mvp, mat4_translation(vec3(0.0f, 0.8f, 0.0f))); mvp = mat4_mul(mvp, mat4_rotation(t * 32.0f * (0.5f + (i - 1)), vec3(0.0f, 0.0f, 1.0f))); glUniformMatrix4fv(transformUniformLocation, 1, GL_FALSE, mvp.m); glBindTexture(GL_TEXTURE_2D, textures[RADIAL0 + i]); glDrawArrays(GL_TRIANGLES, 0, 6); } // draw text mat4_t mvp = mat4_mul(vp, mat4_rotation(60.0f * sin(t), vec3(0.0f, 1.0f, 0.0f))); mvp = mat4_mul(mvp, mat4_translation(vec3(0.0f, -0.8f, 0.0f))); glUniformMatrix4fv(transformUniformLocation, 1, GL_FALSE, mvp.m); glBindTexture(GL_TEXTURE_2D, textures[TEXT]); glDrawArrays(GL_TRIANGLES, 0, 6); // present eglSwapBuffers(display, surface); } }