void GFX_scale( float x, float y, float z ) { static vec3 scale = { 1.0f, 1.0f, 1.0f }; vec3 v = { x, y, z }; if( !memcmp( &v, &scale, sizeof( vec3 ) ) ) return; switch( gfx.matrix_mode ) { case MODELVIEW_MATRIX: { mat4_scale( GFX_get_modelview_matrix(), GFX_get_modelview_matrix(), &v ); break; } case PROJECTION_MATRIX: { mat4_scale( GFX_get_projection_matrix(), GFX_get_projection_matrix(), &v ); break; } case TEXTURE_MATRIX: { mat4_scale( GFX_get_texture_matrix(), GFX_get_texture_matrix(), &v ); break; } } }
// Private functions static void recalculate_matrix(transform2D* trans) { mat4_reset(&trans->matrix); mat4_translate(&trans->matrix, trans->x, trans->y, 0); mat4_rotate(&trans->matrix, trans->angle, 0); mat4_scale(&trans->matrix, trans->scale_x, trans->scale_y, 0); }
// ---------------------------------------------------------------- display --- void display( GLFWwindow* window ) { glClearColor( 1, 1, 1, 1 ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); GLint viewport[4]; glGetIntegerv( GL_VIEWPORT, viewport ); GLint width = viewport[2]; GLint height = viewport[3]; srand(4); vec4 color = {{0.067,0.333, 0.486, 1.0}}; size_t i; for( i=0; i<40; ++i) { float scale = .25 + 4.75 * pow(rand()/(float)(RAND_MAX),2); float angle = 90*(rand()%2); float x = (.05 + .9*(rand()/(float)(RAND_MAX)))*width; float y = (-.05 + .9*(rand()/(float)(RAND_MAX)))*height; float a = 0.1+.8*(pow((1.0-scale/5),2)); mat4_set_identity( &model ); mat4_rotate( &model, angle,0,0,1); mat4_scale( &model, scale, scale, 1); mat4_translate( &model, x, y, 0); glUseProgram( shader ); { glUniform1i( glGetUniformLocation( shader, "u_texture" ), 0 ); glUniform4f( glGetUniformLocation( shader, "u_color" ), color.r, color.g, color.b, a); glUniformMatrix4fv( glGetUniformLocation( shader, "u_model" ), 1, 0, model.data); glUniformMatrix4fv( glGetUniformLocation( shader, "u_view" ), 1, 0, view.data); glUniformMatrix4fv( glGetUniformLocation( shader, "u_projection" ), 1, 0, projection.data); vertex_buffer_render( buffer, GL_TRIANGLES ); } } glfwSwapBuffers( window ); }
// ---------------------------------------------------------------- display --- void display( GLFWwindow* window ) { glClearColor(1.0,1.0,1.0,1.0); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, atlas->id); glEnable( GL_TEXTURE_2D ); glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); texture_glyph_t * glyph = texture_font_get_glyph( font, "@"); int width = 512; int height = 512; float glyph_height = glyph->height * width/(float)glyph->width; float glyph_width = glyph->width * height/(float)glyph->height; int x = -glyph_width/2 + 512/2.; int y = -glyph_height/2 + 512/2.; float s = .025+.975*(1+cos(angle/100.0))/2.; vec4 color = {{1.0, 1.0, 1.0, 1.0 }}; mat4_set_identity( &model ); mat4_scale( &model, width * s, width * s, 1 ); mat4_rotate( &model, angle, 0, 0, 1 ); mat4_translate( &model, 256, 256, 0 ); glUseProgram( program ); { glUniform1i( glGetUniformLocation( program, "u_texture" ), 0); glUniform4f( glGetUniformLocation( program, "u_color" ), color.r, color.g, color.b, color.a); glUniformMatrix4fv( glGetUniformLocation( program, "u_model" ), 1, 0, model.data); glUniformMatrix4fv( glGetUniformLocation( program, "u_view" ), 1, 0, view.data); glUniformMatrix4fv( glGetUniformLocation( program, "u_projection" ), 1, 0, projection.data); vertex_buffer_render( buffer, GL_TRIANGLES ); } glfwSwapBuffers( window ); }
Mat4 Entity_GetModelTransform(Entity* entity) { Mat4 temp; Mat4 rotation = mat4_rotate_x(entity->rotX); temp = mat4_rotate_y(entity->rotY); rotation = mat4_mul(&rotation, &temp); temp = mat4_rotate_z(entity->rotZ); rotation = mat4_mul(&rotation, &temp); temp = mat4_translate(&entity->position); Mat4 modelTransform = mat4_mul(&temp, &rotation); temp = mat4_scale(entity->scale[0], entity->scale[1], entity->scale[2]); modelTransform = mat4_mul(&modelTransform, &temp); return modelTransform; }
static void test3_mag(enum grate_textute_filter min_filter, enum grate_textute_filter mag_filter) { struct mat4 mvp, modelview, transform, scale; mat4_scale(&scale, 10.0f, 10.0f, 1.0f); mat4_translate(&transform, 0.0f, 0.0f, -1.0f - powf(cosf(elapsed), 2.0f) * 4.0f); mat4_multiply(&modelview, &transform, &scale); mat4_multiply(&mvp, &projection, &modelview); location = grate_get_vertex_uniform_location(quad_program, "mvp"); grate_3d_ctx_set_vertex_mat4_uniform(ctx, location, &mvp); grate_texture_set_min_filter(tex[active_tex], min_filter); grate_texture_set_mag_filter(tex[active_tex], mag_filter); grate_texture_set_max_lod(tex[active_tex], max_lod); grate_3d_ctx_bind_texture(ctx, 0, tex[active_tex]); location = grate_get_fragment_uniform_location(quad_program, "lod_bias"); grate_3d_ctx_set_fragment_float_uniform(ctx, location, 0.0f); grate_3d_draw_elements(ctx, TGR3D_PRIMITIVE_TYPE_TRIANGLES, idx_bo, TGR3D_INDEX_MODE_UINT16, ARRAY_SIZE(indices)); grate_flush(grate); grate_3d_printf(grate, ctx, font, 1, -0.85f, 0.85f, font_scale, "LOD BIAS: 0\nMAX LOD: %d\nMIN: %s\nMAG: %s\nMIPMAP: %s\n" "Texture id: %d", max_lod, texfilter[min_filter], texfilter[mag_filter], mipdesc[active_tex], active_tex); grate_3d_printf(grate, ctx, font, 1, 0.55f, 0.85f, font_scale, "Test #3"); }
int main(int argc, char **argv){ /* vertices for a triangle */ /* Why does triangle[] = { vec4_new(...) } result in a segfault when returning * from _mm_load_ps? */ /* Just want a sort of 3D object, but not a closed object otherwise it's hard * to tell what's going on w/ flat shading */ vec4_t object[18]; //+Z face object[0] = vec4_new(-1, -1, 1, 1); object[1] = vec4_new(1, -1, 1, 1); object[2] = vec4_new(1, 1, 1, 1); object[3] = vec4_new(1, 1, 1, 1); object[4] = vec4_new(-1, 1, 1, 1); object[5] = vec4_new(-1, -1, 1, 1); //+X face object[6] = vec4_new(1, -1, 1, 1); object[7] = vec4_new(1, -1, -1, 1); object[8] = vec4_new(1, 1, -1, 1); object[9] = vec4_new(1, 1, -1, 1); object[10] = vec4_new(1, 1, 1, 1); object[11] = vec4_new(1, -1, 1, 1); //-X face object[12] = vec4_new(-1, -1, 1, 1); object[13] = vec4_new(-1, -1, -1, 1); object[14] = vec4_new(-1, 1, -1, 1); object[15] = vec4_new(-1, 1, -1, 1); object[16] = vec4_new(-1, 1, 1, 1); object[17] = vec4_new(-1, -1, 1, 1); if (SDL_Init(SDL_INIT_EVERYTHING) != 0){ fprintf(stderr, "SDL Init error: %s\n", SDL_GetError()); return 1; } SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); #ifdef DEBUG SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); #endif SDL_Window *win = SDL_CreateWindow("SSE GL Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_OPENGL); SDL_GLContext context = SDL_GL_CreateContext(win); if (check_GL_error("Opened win + context")){ SDL_GL_DeleteContext(context); SDL_DestroyWindow(win); return 1; } glewExperimental = GL_TRUE; GLenum err = glewInit(); if (err != GLEW_OK){ fprintf(stderr, "GLEW init error %d\n", err); SDL_GL_DeleteContext(context); SDL_DestroyWindow(win); return 1; } check_GL_error("Post GLEW init"); #ifdef DEBUG glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB); glDebugMessageCallbackARB(gl_debug_callback, NULL); glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE); #endif glClearColor(0, 0, 0, 1); glClearDepth(1); glEnable(GL_DEPTH_TEST); //Model's vao and vbo GLuint model[2]; glGenVertexArrays(1, model); glBindVertexArray(model[0]); glGenBuffers(1, model + 1); glBindBuffer(GL_ARRAY_BUFFER, model[1]); glBufferData(GL_ARRAY_BUFFER, 4 * 6 * 3 * sizeof(GLfloat), object, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); if (check_GL_error("Setup buffers")){ return 1; } GLint vshader = make_shader(vert_shader_src, GL_VERTEX_SHADER); GLint fshader = make_shader(frag_shader_src, GL_FRAGMENT_SHADER); if (vshader == -1 || fshader == -1){ return 1; } GLint program = make_program(vshader, fshader); if (program == -1){ return 1; } glDeleteShader(vshader); glDeleteShader(fshader); mat4_t model_mat = mat4_mult(mat4_rotate(45, vec4_new(1, 1, 0, 0)), mat4_scale(2, 2, 2)); model_mat = mat4_mult(mat4_translate(vec4_new(0, 2, -5, 1)), model_mat); mat4_t view_mat = mat4_look_at(vec4_new(0, 0, 5, 0), vec4_new(0, 0, 0, 0), vec4_new(0, 1, 0, 0)); mat4_t proj_mat = mat4_perspective(75, ((float)WIN_WIDTH) / WIN_HEIGHT, 1, 100); glUseProgram(program); GLuint model_unif = glGetUniformLocation(program, "model"); GLuint view_unif = glGetUniformLocation(program, "view"); GLuint proj_unif = glGetUniformLocation(program, "proj"); glUniformMatrix4fv(model_unif, 1, GL_FALSE, (GLfloat*)&model_mat); glUniformMatrix4fv(view_unif, 1, GL_FALSE, (GLfloat*)&view_mat); glUniformMatrix4fv(proj_unif, 1, GL_FALSE, (GLfloat*)&proj_mat); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawArrays(GL_TRIANGLES, 0, 18); SDL_GL_SwapWindow(win); check_GL_error("Post Draw"); SDL_Event e; int quit = 0; while (!quit){ while (SDL_PollEvent(&e)){ if (e.type == SDL_QUIT || e.type == SDL_KEYDOWN){ quit = 1; } } } glDeleteProgram(program); glDeleteVertexArrays(1, model); glDeleteBuffers(1, model + 1); SDL_GL_DeleteContext(context); SDL_DestroyWindow(win); return 0; }
static int _llfunc_mat4_scale(lua_State *L) { mat4 *m = (mat4*)userdata_get_or_die(L, 1); vec3 *v = (vec3*)userdata_get_or_die(L, 2); mat4_scale(m, v); return 0; }