Exemple #1
0
void App::update(float delta_time) {
	if (anim_play) frame_count++;

	if (!hide_gui) gui();

	// autoreload frag shader (every 60 frames)
	if (shader_filepath && shader_file_autoreload && (frame_count % 60) == 0) {
		struct stat attr;
		if (!stat(shader_filepath, &attr)) { // file exists
			if (attr.st_mtime > shader_file_mtime) { // file has been modified
				shader_file_mtime = (int)attr.st_mtime;
				reloadShader();
			}
		}
	}

	// update camera (-z: forward, y: up)
	camera_euler_angles += 2.0f*delta_time
		* v3(-movement_command.rotate.x, -movement_command.rotate.y, 0.0f);
	camera_euler_angles.x = fminf(fmaxf(-0.5f*(float)M_PI, camera_euler_angles.x), 0.5f*(float)M_PI); // clamp
	mat3 rot_x = rotationMatrix(v3(1.0f, 0.0f, 0.0f), camera_euler_angles.x);
	mat3 rot_y = rotationMatrix(v3(0.0f, 1.0f, 0.0f), camera_euler_angles.y);
	mat3 rot_z = rotationMatrix(v3(0.0f, 0.0f, 1.0f), camera_euler_angles.z);
	mat3 rot = rot_y * rot_x * rot_z;
	camera_location += rot * (8.0f*delta_time*movement_command.move);
	mat4 view_to_world = translationMatrix(camera_location) * m4(rot);
	mat4 world_to_view = m4(transpose(rot)) * translationMatrix(-camera_location);

	// bind textures
	for (int tsi = 0; tsi < (int)ARRAY_COUNT(texture_slots); tsi++) {
		glActiveTexture(GL_TEXTURE0+tsi);
		glBindTexture(texture_slots[tsi].target, texture_slots[tsi].texture);
	}

	// draw fullscreen triangle(s)
	glClearColor(0.2f, 0.21f, 0.22f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	{ BindShader bind_shader(shader);
		if (!compile_error_log) {
			for (int i = 0; i < uniform_count; i++) {
				uniforms[i].apply();
			}
		}

		// apply builtin uniforms
		u_time = (float)frame_count / 60.0f;
		glUniform1f(shader.getUniformLocation(u_time_name), u_time);
		vec2 u_resolution = v2(video.pixel_scale*video.width, video.pixel_scale*video.height);
		glUniform2fv(shader.getUniformLocation(u_resolution_name), 1, u_resolution.e);
		glUniformMatrix4fv(shader.getUniformLocation(u_view_to_world_name), 1, GL_FALSE, view_to_world.e);
		glUniformMatrix4fv(shader.getUniformLocation(u_world_to_view_name), 1, GL_FALSE, world_to_view.e);

		if (single_triangle_mode) {
			{ BindArrayBuffer bind_array_buffer(single_triangle_vbo);
				glEnableVertexAttribArray(VAT_POSITION);
				glVertexAttribPointer(VAT_POSITION, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
				glDrawArrays(GL_TRIANGLES, 0, 3);
				glDisableVertexAttribArray(VAT_POSITION);
			}
		} else {
			{ BindArrayBuffer bind_array_buffer(two_triangles_vbo);
				glEnableVertexAttribArray(VAT_POSITION);
				glVertexAttribPointer(VAT_POSITION, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
				glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
				glDisableVertexAttribArray(VAT_POSITION);
			}
		}
	}
}
Exemple #2
0
void App::update(float delta_time) {
	if (anim_play) frame_count++;

	// builtin uniform names
	static char u_time_name[64]       = "u_time";
	static char u_resolution_name[64] = "u_resolution";
	static char u_view_mat_name[64]   = "u_view_mat";

	if (ImGui::BeginMainMenuBar()) {
		if (ImGui::BeginMenu("File")) {
			if (ImGui::MenuItem("Open fragment shader"/*, "Ctrl+O"*/)) {
				openShaderDialog();
			}
			if (ImGui::IsItemHovered() && file_path) {
				ImGui::SetTooltip("%s", file_path);
			}
			if (ImGui::MenuItem("Autoreload", nullptr, file_autoreload)) {
				file_autoreload = !file_autoreload;
			}
			ImGui::EndMenu();
		}
		if (ImGui::BeginMenu("Animation")) {
			if (ImGui::MenuItem(anim_play ? "Pause" : "Play")) {
				anim_play = !anim_play;
			}
			if (ImGui::MenuItem("Reset")) {
				frame_count = 0;
			}
			ImGui::EndMenu();
		}
		if (ImGui::BeginMenu("Window")) {
			if (ImGui::MenuItem("Uniforms", nullptr, show_uniforms_window)) {
				show_uniforms_window = !show_uniforms_window;
			}
			if (ImGui::MenuItem("Textures", nullptr, show_textures_window)) {
				show_textures_window = !show_textures_window;
			}
			ImGui::EndMenu();
		}
		ImGui::EndMainMenuBar();
	}

	if (show_uniforms_window) {
		if (ImGui::Begin("Uniforms", &show_uniforms_window)) {
			if (ImGui::CollapsingHeader("Built-in uniform names")) {
				ImGui::InputText("Time", u_time_name, sizeof(u_time_name));
				ImGui::InputText("Resolution", u_resolution_name, sizeof(u_resolution_name));
				ImGui::InputText("View Matrix", u_view_mat_name, sizeof(u_view_mat_name));
			}
			ImGui::Separator();
	
			if (!compile_error_log) {
				ImGui::AlignFirstTextHeightToWidgets();
				ImGui::Text("Data"); ImGui::SameLine();
				if (ImGui::Button("Clear")) {
					if (uniform_data) {
						memset(uniform_data, 0, uniform_data_size);
					}
				} ImGui::SameLine();
				if (ImGui::Button("Save")) {
					writeUniformData();
				} ImGui::SameLine();
				if (ImGui::Button("Load")) {
					readUniformData();
				}
				for (int i = 0; i < uniform_count; i++) {
					// skip builtin uniforms
					if (!strcmp(u_time_name,       uniforms[i].name)) continue;
					if (!strcmp(u_resolution_name, uniforms[i].name)) continue;
					if (!strcmp(u_view_mat_name,   uniforms[i].name)) continue;
					uniforms[i].gui();
				}
			}
		}
		ImGui::End();
	}

	if (show_textures_window) {
		if (ImGui::Begin("Textures", &show_textures_window, ImGuiWindowFlags_NoResize|ImGuiWindowFlags_AlwaysAutoResize)) {
			ImGui::Columns(2);
			for (int tsi = 0; tsi < ARRAY_COUNT(texture_slots); tsi++) {
				TextureSlot *texture_slot = texture_slots+tsi;
				
				ImGui::BeginGroup();
				ImGui::PushID(tsi);
				ImGui::Text("%d:", tsi);
				ImGui::SameLine();
				ImGui::PushStyleColor(ImGuiCol_Button, ImColor::HSV(0.0f, 0.6f, 0.6f));
				ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImColor::HSV(0.0f, 0.7f, 0.7f));
				ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImColor::HSV(0.0f, 0.8f, 0.8f));
				if (ImGui::SmallButton("x")) texture_slot->clear();
				ImGui::PopStyleColor(3);
				if (ImGui::Button(" 2D ")) openImageDialog(texture_slot);
				if (ImGui::Button("Cube")) openImageDialog(texture_slot, /*load_cube_cross*/true);
				ImGui::PopID();
				ImGui::EndGroup();

				ImGui::SameLine();
				//ImTextureID im_tex_id = (ImTextureID)(intptr_t)texture_slot->texture;
				ImGui::Image((void*)texture_slot, ImVec2(64, 64));
				if (ImGui::IsItemHovered() && texture_slot->image_file_path) {
					ImGui::SetTooltip("%s\n%dx%d", texture_slot->image_file_path,
						texture_slot->image_width, texture_slot->image_height);
				}

				if ((tsi&1) && tsi+1 != ARRAY_COUNT(texture_slots)) {
					ImGui::Separator();
				} else {
					ImGui::SameLine(); ImGui::Spacing();
				}
				ImGui::NextColumn();
			}
			ImGui::Columns(1);
		}
		ImGui::End();
	}

	//ImGui::ShowTestWindow();

	// autoreload (every 60 frames)
	if (file_path && file_autoreload && (frame_count % 60) == 0) {
		struct stat attr;
		if (!stat(file_path, &attr)) { // file exists
			if (attr.st_mtime > file_mod_time) { // file has been modified
				file_mod_time = attr.st_mtime;
				loadShader(file_path, /*initial*/false);
			}
		}
	}

	// overlay messages
	int overlay_flags = ImGuiWindowFlags_NoTitleBar
		| ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize
		| ImGuiWindowFlags_NoBringToFrontOnFocus
		| ImGuiWindowFlags_NoMove
		| ImGuiWindowFlags_NoSavedSettings;
	if (!file_path) {
		ImGui::SetNextWindowPosCenter();
		ImGui::Begin("Overlay", nullptr, ImVec2(0, 0), 0.3f, overlay_flags);
		ImGui::AlignFirstTextHeightToWidgets(); // valign text to button
		ImGui::Text("No fragment shader");
		ImGui::SameLine();
		if (ImGui::Button("Open")) {
			openShaderDialog();
		}
		ImGui::End();
	} else if (compile_error_log) {
		ImGui::SetNextWindowPosCenter();
		ImGui::Begin("Overlay", nullptr, ImVec2(0, 0), 0.3f, overlay_flags);
		ImGui::TextUnformatted(compile_error_log);
		ImGui::End();
	}

	// update camera
	camera.euler_angles += 2.0f*delta_time
		* v3(movement_command.rotate.x, movement_command.rotate.y, 0.0f);
	mat3 rot_y = rotationMatrix(v3(0.0f, 0.0f, 1.0f), -camera.euler_angles.y);
	camera.location += rot_y * (8.0f*delta_time*movement_command.move);
	camera.updateViewMatrix();

	// bind textures
	for (int tsi = 0; tsi < ARRAY_COUNT(texture_slots); tsi++) {
		glActiveTexture(GL_TEXTURE0+tsi);
		glBindTexture(texture_slots[tsi].target, texture_slots[tsi].texture);
	}

	// draw two triangles
	glClearColor(0.2, 0.21, 0.22, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	{ BindShader bind_shader(shader);
		if (!compile_error_log) {
			for (int i = 0; i < uniform_count; i++) {
				uniforms[i].apply();
			}
		}

		// apply builtin uniforms
		u_time = (float)frame_count / 60.0f;
		glUniform1f(shader.getUniformLocation(u_time_name), u_time);
		vec2 u_resolution = v2(video.pixel_scale*video.width, video.pixel_scale*video.height);
		glUniform2fv(shader.getUniformLocation(u_resolution_name), 1, u_resolution.e);
		mat4 u_inv_view_mat = camera.makeInverseViewMatrix();
		int u_view_mat_loc = shader.getUniformLocation(u_view_mat_name);
		glUniformMatrix4fv(u_view_mat_loc, 1, GL_FALSE, u_inv_view_mat.e);

		{ BindArrayBuffer bind_array_buffer(two_triangles_vbo);
			glEnableVertexAttribArray(VAT_POSITION);
			glVertexAttribPointer(VAT_POSITION, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
			glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
			glDisableVertexAttribArray(VAT_POSITION);
		}
	}
}