Example #1
0
int32_t main(int32_t argc, char *argv[]) {
    if( init_sdl2() ) {
        return 1;
    }

    int width = 1280;
    int height = 720;

    SDL_Window* window;
    sdl2_window("cute3d: " __FILE__, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, &window);

    SDL_GLContext* context;
    sdl2_glcontext(3, 2, window, &context);

    if( init_shader() ) {
        return 1;
    }

    if( init_canvas(width, height) ) {
        return 1;
    }
    canvas_create("global_dynamic_canvas", &global_dynamic_canvas);
    canvas_create("global_static_canvas", &global_static_canvas);

    struct Vbo vbo = {0};
    vbo_create(&vbo);
    vbo_add_buffer(&vbo, SHADER_ATTRIBUTE_VERTEX, 3, GL_FLOAT, GL_STATIC_DRAW);
    vbo_add_buffer(&vbo, SHADER_ATTRIBUTE_VERTEX_NORMAL, 3, GL_FLOAT, GL_STATIC_DRAW);
    vbo_add_buffer(&vbo, SHADER_ATTRIBUTE_VERTEX_COLOR, 4, GL_UNSIGNED_BYTE, GL_STATIC_DRAW);
    vbo_add_buffer(&vbo, SHADER_ATTRIBUTE_VERTEX_NORMAL, NORMAL_SIZE, GL_FLOAT, GL_STATIC_DRAW);

    struct Ibo ibo = {0};
    ibo_create(GL_TRIANGLES, GL_UNSIGNED_INT, GL_STATIC_DRAW, &ibo);

    struct SolidTetrahedron hard_tetrahedron = {0};
    struct SolidBox hard_cube = {0};
    struct SolidSphere16 hard_sphere16 = {0};
    struct SolidSphere32 hard_sphere32 = {0};
    solid_tetrahedron_create(1.0, (Color){255, 0, 0, 255}, &hard_tetrahedron);
    solid_cube_create(0.5, (Color){0, 255, 0, 255}, &hard_cube);
    solid_sphere16_create(16, 8, 0.75, (Color){0, 255, 255, 255}, &hard_sphere16);
    solid_sphere32_create(32, 16, 0.75, (Color){255, 255, 0, 255}, &hard_sphere32);

    solid_optimize((struct Solid*)&hard_tetrahedron);
    solid_optimize((struct Solid*)&hard_cube);
    solid_optimize((struct Solid*)&hard_sphere16);
    solid_optimize((struct Solid*)&hard_sphere32);

    struct VboMesh hard_tetrahedron_mesh, hard_box_mesh, hard_cube_mesh, hard_sphere16_mesh, hard_sphere32_mesh;
    vbo_mesh_create_from_solid((struct Solid*)&hard_tetrahedron, &vbo, &ibo, &hard_tetrahedron_mesh);
    vbo_mesh_create_from_solid((struct Solid*)&hard_cube, &vbo, &ibo, &hard_cube_mesh);
    vbo_mesh_create_from_solid((struct Solid*)&hard_sphere16, &vbo, &ibo, &hard_sphere16_mesh);
    vbo_mesh_create_from_solid((struct Solid*)&hard_sphere32, &vbo, &ibo, &hard_sphere32_mesh);

    struct SolidTetrahedron smooth_tetrahedron = {0};
    struct SolidBox smooth_cube = {0};
    struct SolidSphere16 smooth_sphere16 = {0};
    struct SolidSphere32 smooth_sphere32 = {0};
    solid_tetrahedron_create(1.0, (Color){255, 0, 0, 255}, &smooth_tetrahedron);
    solid_cube_create(0.5, (Color){0, 255, 0, 255}, &smooth_cube);
    solid_sphere16_create(16, 8, 0.75, (Color){0, 255, 255, 255}, &smooth_sphere16);
    solid_sphere32_create(32, 16, 0.75, (Color){255, 255, 0, 255}, &smooth_sphere32);

    solid_optimize((struct Solid*)&smooth_tetrahedron);
    solid_optimize((struct Solid*)&smooth_cube);
    solid_optimize((struct Solid*)&smooth_sphere16);
    solid_optimize((struct Solid*)&smooth_sphere32);
    solid_smooth_normals((struct Solid*)&smooth_tetrahedron, smooth_tetrahedron.normals, smooth_tetrahedron.normals);
    solid_smooth_normals((struct Solid*)&smooth_cube, smooth_cube.normals, smooth_cube.normals);
    solid_smooth_normals((struct Solid*)&smooth_sphere16, smooth_sphere16.normals, smooth_sphere16.normals);
    solid_smooth_normals((struct Solid*)&smooth_sphere32, smooth_sphere32.normals, smooth_sphere32.normals);

    struct VboMesh smooth_tetrahedron_mesh, smooth_box_mesh, smooth_cube_mesh, smooth_sphere16_mesh, smooth_sphere32_mesh;
    vbo_mesh_create_from_solid((struct Solid*)&smooth_tetrahedron, &vbo, &ibo, &smooth_tetrahedron_mesh);
    vbo_mesh_create_from_solid((struct Solid*)&smooth_cube, &vbo, &ibo, &smooth_cube_mesh);
    vbo_mesh_create_from_solid((struct Solid*)&smooth_sphere16, &vbo, &ibo, &smooth_sphere16_mesh);
    vbo_mesh_create_from_solid((struct Solid*)&smooth_sphere32, &vbo, &ibo, &smooth_sphere32_mesh);

    struct Arcball arcball = {0};
    arcball_create(width, height, (Vec4f){2.5,17.0,17.0,1.0}, (Vec4f){2.5,0.0,0.0,1.0}, 0.1, 100.0, &arcball);

    float circular_motion_angle = 0.0f;
    float circular_motion_speed = (2.0f*PI)/30;
    float circular_motion_radius = 12.0f;

    Vec3f light_position = { circular_motion_radius, 10.0, circular_motion_radius };
    Vec3f light_direction = {0};
    vec_sub((Vec3f){0.0f, 0.0f, 0.0f}, light_position, light_direction);
    vec_normalize(light_direction, light_direction);

    Vec3f eye_position = {0};
    vec_copy3f(arcball.camera.pivot.position, eye_position);

    Color ambiance = {50, 25, 150, 255};
    Color specular = {255, 255, 255, 255};
    float material_shininess = 1.0;
    Vec4f material_coefficients = { 0.8, 0.2, 0.0, 0.0 };

    // flat
    struct Shader flat_shader = {0};
    shader_create(&flat_shader);
    shader_attach(&flat_shader, GL_VERTEX_SHADER, "prefix.vert", 1, "flat_shading.vert");
    shader_attach(&flat_shader, GL_FRAGMENT_SHADER, "prefix.frag", 1, "flat_shading.frag");
    shader_make_program(&flat_shader, SHADER_DEFAULT_NAMES, "flat_shader");

    shader_set_uniform_3f(&flat_shader, flat_shader.program, SHADER_UNIFORM_LIGHT_DIRECTION, 3, GL_FLOAT, light_direction);
    shader_set_uniform_4f(&flat_shader, flat_shader.program, SHADER_UNIFORM_AMBIENT_LIGHT, 4, GL_UNSIGNED_BYTE, ambiance);

    // gouraud
    struct Shader gouraud_shader = {0};
    shader_create(&gouraud_shader);
    shader_attach(&gouraud_shader, GL_VERTEX_SHADER, "prefix.vert", 1, "gouraud_shading.vert");
    shader_attach(&gouraud_shader, GL_FRAGMENT_SHADER, "prefix.frag", 1, "gouraud_shading.frag");
    shader_make_program(&gouraud_shader, SHADER_DEFAULT_NAMES, "gouraud_shader");

    shader_set_uniform_3f(&gouraud_shader, gouraud_shader.program, SHADER_UNIFORM_LIGHT_POSITION, 3, GL_FLOAT, light_position);
    shader_set_uniform_3f(&gouraud_shader, gouraud_shader.program, SHADER_UNIFORM_EYE_POSITION, 3, GL_FLOAT, eye_position);
    shader_set_uniform_4f(&gouraud_shader, gouraud_shader.program, SHADER_UNIFORM_AMBIENT_LIGHT, 4, GL_UNSIGNED_BYTE, ambiance);
    shader_set_uniform_4f(&gouraud_shader, gouraud_shader.program, SHADER_UNIFORM_SPECULAR_LIGHT, 4, GL_UNSIGNED_BYTE, specular);
    shader_set_uniform_1f(&gouraud_shader, gouraud_shader.program, SHADER_UNIFORM_MATERIAL_SHININESS, 1, GL_FLOAT, &material_shininess);
    shader_set_uniform_4f(&gouraud_shader, gouraud_shader.program, SHADER_UNIFORM_MATERIAL_COEFFICIENTS, 4, GL_FLOAT, material_coefficients);
    shader_set_uniform_3f(&gouraud_shader, gouraud_shader.program, SHADER_UNIFORM_EYE_POSITION, 3, GL_FLOAT, &arcball.camera.pivot.position);

    Mat identity = IDENTITY_MAT;
    draw_grid(&global_static_canvas, 0, identity, (Color){127, 127, 127, 255}, 0.03f, 12.0f, 12.0f, 12);

    Mat shading_label_transform = IDENTITY_MAT;
    Quat text_rotation = IDENTITY_QUAT;
    quat_from_axis_angle((Vec3f)X_AXIS, -PI/2.0f, text_rotation);
    mat_rotate(shading_label_transform, text_rotation, shading_label_transform);
    mat_translate(shading_label_transform, (float[4]){ 6.5, 0.0, -2.5, 1.0 }, shading_label_transform);
Example #2
0
void BeforeDrawShader(ShaderProgram *theProgram) {

	RenderState *rs;
	Light *oneLight;
	Material *mat;
	Texture *tex;
	int i;

	if (theProgram == NULL) return;

	rs = RenderStateScene();

	shader_set_uniform_matrix4(theProgram->modeltoCamera_unif, OpenGLRS(rs, MG_MODELVIEW));
	shader_set_uniform_matrix4(theProgram->modeltoWorld_unif, OpenGLRS(rs, MG_MODEL));
	shader_set_uniform_matrix4(theProgram->CameraToClip_unif, OpenGLRS(rs, MG_PROJECTION));
	shader_set_uniform_matrix4(theProgram->modeltoClip_unif, OpenGLRS(rs, MG_MODELVIEW_PROJECTION));

	i = 0;
	oneLight = StartLoop(rs->lightList);
	while (oneLight) {
		if (IsOnLight(oneLight)) {
			if (i == 4) { // MG_MAX_LIGHTS
				fprintf(stderr, "[W] too many active lights. Discarding the rest\n");
				break;
			}
			shader_set_uniform_4fv(theProgram->light_unif[i].position, GetPositionLight(oneLight));
			shader_set_uniform_3fv(theProgram->light_unif[i].diffuse, GetDiffuseLight(oneLight));
			shader_set_uniform_3fv(theProgram->light_unif[i].ambient, GetAmbientLight(oneLight));
			shader_set_uniform_3fv(theProgram->light_unif[i].specular, GetSpecularLight(oneLight));
			shader_set_uniform_3fv(theProgram->light_unif[i].attenuation, GetAttenuationVectorLight(oneLight));
			if (IsSpotLight(oneLight)) {
				shader_set_uniform_3fv(theProgram->light_unif[i].spotDir, GetSpotDirectionLight(oneLight));
				shader_set_uniform_1f(theProgram->light_unif[i].exponent, GetSpotExponentLight(oneLight));
				shader_set_uniform_1f(theProgram->light_unif[i].cosCutOff, cosf(GetSpotCutoffLight(oneLight)));
			} else {
				shader_set_uniform_1f(theProgram->light_unif[i].cosCutOff, 0.0f); // if (cos) cutoff is zero -> no spotLight
			}
			++i;
		}
		oneLight = GetNext(rs->lightList);
	}
	shader_set_uniform_1i(theProgram->activeLights_unif, i);

	mat = rs->frontMaterial;
	if (mat != NULL) {
		shader_set_uniform_3fv(theProgram->mat_unif.ambient, GetAmbientMaterial(mat));
		shader_set_uniform_3fv(theProgram->mat_unif.diffuse, GetDiffuseMaterial(mat));
		shader_set_uniform_3fv(theProgram->mat_unif.specular, GetSpecularMaterial(mat));
		shader_set_uniform_1f(theProgram->mat_unif.shininess, GetShininessMaterial(mat));
		shader_set_uniform_1f(theProgram->mat_unif.alpha, GetAlphaMaterial(mat));

		tex = GetTextureMaterial(mat);
		if (tex != NULL) {
			// Set texture to unit 0
			BindGLTextureTunit(tex, 0);
			shader_set_uniform_1i(theProgram->texSampler_unif, 0); // Texture unit 0
		}
		tex = GetBumpMapTextureMaterial(mat);
		if (tex != NULL) {
			// bumpMapping in texture unit 1
			BindGLTextureTunit(tex, 1);
			shader_set_uniform_1i(theProgram->bumpSampler_unif, 1); // Bump Texture unit 1
		}
	}
}