示例#1
0
文件: game.cpp 项目: lightbits/akari
void render_game(float dt)
{
	glBindFramebuffer(GL_FRAMEBUFFER, rt_frame.fbo);
	glViewport(0, 0, rt_frame.width, rt_frame.height);
	glBindTexture(GL_TEXTURE_2D, tex_dither);

	depth_test(true, GL_LEQUAL);
	depth_write(true);
	clear(0x300224ff, 1.0f);

	use_shader(shader_default);
	uniform("projection", mat_projection);
	uniform("view", mat_view);
	uniform("model", scale(0.07f));
	uniform("lightPos", vec3(0.8, 4.0, 0.8));
	uniform("palette", vec3(143.0, 199.0, 132.0) / 255.0f);
	uniform("texDither", 0);
	//mesh_teapot.draw();
	glBindBuffer(GL_ARRAY_BUFFER, mesh_teapot.vertex_buffer);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh_teapot.index_buffer);
	attribfv("position", 3, 8, 0);
	attribfv("normal", 3, 8, 5);
	glDrawElements(GL_TRIANGLES, mesh_teapot.num_indices, GL_UNSIGNED_INT, 0);
	
	glBindTexture(GL_TEXTURE_2D, rt_frame.color);
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	glViewport(0, 0, window_width, window_height);
	clear(0x00000000, 1.0f);
	use_shader(shader_dither);
	uniform("texScene", 0);
	mesh_quad.draw();
}
示例#2
0
void display() {
	// Erase the window and the depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Enable Z-buffering in OpenGL
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);

	// Set Shader Mode
	use_shader(v_shader_mode);

	Project();

	// Switch to manipulating the model matrix
	glMatrixMode(GL_MODELVIEW);

	// Undo previous transformations
	glLoadIdentity();

	// Check if we need to move //
	if (v_move_forward)
		Move(forward);
	else if (v_move_backward)
		Move(backward);
	if (v_move_left)
		Move(left);
	else if (v_move_right)
		Move(right);
	glRotatef(90, 1, 0, 0);
	   //  Perspective - set eye position
   	if (v_use_perspective)
   	{
      		double Ex = -2*v_dim*Sin(v_y_persp_rot)*Cos(v_x_persp_rot);
      		double Ey = +2*v_dim        *Sin(v_x_persp_rot);
      		double Ez = +2*v_dim*Cos(v_y_persp_rot)*Cos(v_x_persp_rot);
      		gluLookAt(Ex,Ey,Ez , 0,0,0 , 0,Cos(v_x_persp_rot),0);
   	}
  	 //  Orthogonal - set world orientation
	else
	{
		glTranslatef(0, 0, 2);
		glRotatef(v_x_persp_rot,1,0,0);
      		glRotatef(v_y_persp_rot,0,1,0);
   	}

	// Scene

	glCallList(v_models[0]);

	// Log
	glColor3f(1, 1, 1);
	glWindowPos2i(5, 5);
	Print("Homework 01");

	glFlush();
	glutSwapBuffers();
	glutPostRedisplay();
}
示例#3
0
文件: game.cpp 项目: lightbits/akari
void gen_heightmap()
{
	use_shader(shader_generate);
	uniform("time", get_elapsed_time());
	blend_mode(false);
	depth_test(false);
	depth_write(false);
	glBindFramebuffer(GL_FRAMEBUFFER, rt_heightmap.fbo);
	glViewport(0, 0, rt_heightmap.width, rt_heightmap.height);
	clearc(0x00000000);
	mesh_quad.draw();
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	glViewport(0, 0, window_width, window_height);
}
示例#4
0
void display() {
	// Erase the window and the depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Enable Z-buffering in OpenGL
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);

	// Set Shader Mode
	use_shader(v_shader_mode);

	Project();

	// Switch to manipulating the model matrix
	glMatrixMode(GL_MODELVIEW);

	// Undo previous transformations
	glLoadIdentity();

	// Check if we need to move //
	if (v_move_forward)
		Move(forward);
	else if (v_move_backward)
		Move(backward);
	if (v_move_left)
		Move(left);
	else if (v_move_right)
		Move(right);

	// Viewport
	vector3 look_pos = get_look_position();
	gluLookAt(v_camera_location.x, v_camera_location.y, v_camera_location.z,
			look_pos.x, look_pos.y, look_pos.z, 0, 1, 0);

	// Scene
	glColor3f(.4, .4, .4);
	draw_structure();
	glTranslatef(0, -1, 0);

	// Log
	glColor3f(1, 1, 1);
	glWindowPos2i(5, 5);
	Print("P (Shader Mode): %s -- K/L (Ambient Light): %2.0f%%", ShaderName(),
			100 * v_ambient_intensity);

	glFlush();
	glutSwapBuffers();
	glutPostRedisplay();
}
示例#5
0
文件: window.cpp 项目: wjh/TGL
bool tgl::window::init()
{
	m_window = glfwCreateWindow(m_config.width,
	                            m_config.height,
	                            m_config.title.c_str(),
	                            nullptr,
	                            nullptr);

	if (!m_window)
	{
		glfwTerminate();
		return false;
	}

	input::init(m_window);
	sound_manager::init();
	socket::init();

	glfwMakeContextCurrent(m_window);

	glewExperimental = GL_TRUE;
	GLenum err = glewInit();
	if (err != GLEW_OK)
	{
		std::cout << glewGetErrorString(err) << std::endl;
		throw std::runtime_error("GLEW init failed.");
	}

	glClearColor(0.f, 0.f, 0.f, 1.f);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	m_projection = util::matrix::ortho(
	    m_config.width, m_config.height, m_config.near, m_config.far);

	m_shader.load();
	use_shader(m_shader);

	return true;
}
示例#6
0
文件: game.cpp 项目: lightbits/akari
void render_game(float dt)
{
	static vec3 light_pos = vec3(0.0f, 2.0f, 2.0f);
	static vec3 light_color = vec3(1.0f, 0.8f, 0.5f);
	static vec3 ambient = vec3(67.0f, 66.0f, 63.0f) / 255.0f;
	static vec3 background = vec3(0.0f);
	//static vec3 background = vec3(0.55, 0.45, 0.45);
	static vec2 view_rot = vec2(-0.3f, 2.76f);
	static float view_radius = 2.0f;
	static float fog_density = 0.206f;
	static vec3 fog_color = background * background;
	static bool wireframe = false;
	mat4 mat_view = translate(0.0f, 0.0f, -view_radius) * rotateX(view_rot.x) * rotateY(view_rot.y);
	depth_test(true, GL_LEQUAL);
	depth_write(true);
	clear(vec4(background, 1.0f), 1.0f);

	if (wireframe)
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	use_shader(shader_terrain);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, rt_heightmap.color);
	uniform("time", get_elapsed_time());
	//uniform("heightmap", 0);
	uniform("projection", mat_projection);
	uniform("view", mat_view);
	uniform("lightPos", light_pos);
	uniform("lightColor", light_color);
	uniform("fogColor", fog_color);
	uniform("fogDensity", fog_density);
	uniform("ambient", ambient);
	uniform("sampleRes", vec2(terrain_samples_x, terrain_samples_y));
	mesh_terrain.draw();

	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	ImGui::NewFrame();
	ImGui::Begin("Terrain shader");
	ImGui::SliderFloat3("lightPos", &light_pos[0], -2.0f, 2.0f);
	ImGui::ColorEdit3("lightColor", &light_color[0]);
	ImGui::ColorEdit3("fogColor", &fog_color[0]);
	ImGui::SliderFloat("fogDensity", &fog_density, 0.0f, 1.0f);
	ImGui::ColorEdit4("background", &background[0]);
	ImGui::SliderAngle("viewRotX", &view_rot[0]);
	ImGui::SliderAngle("viewRotY", &view_rot[1]);
	ImGui::SliderFloat("terrainScale", &terrain_scale, 1.0f, 16.0f);
	ImGui::SliderFloat("terrainFrequency", &terrain_frequency, 0.5f, 3.0f * terrain_scale);
	ImGui::SliderFloat("terrainHeight", &terrain_height, 0.0f, 2.0f);
	ImGui::SliderInt("terrainSamplesX", &terrain_samples_x, 1, 128);
	ImGui::SliderInt("terrainSamplesY", &terrain_samples_y, 1, 128);
	ImGui::SliderFloat("viewRadius", &view_radius, 0.0f, 4.0f);
	ImGui::Checkbox("wireframe", &wireframe);
	if (ImGui::Button("Reload"))
	{
		shader_generate.dispose();
		shader_terrain.dispose();
		while (!shader_generate.load_from_file("./generate.vs", "./generate.fs") ||
			   !shader_terrain.load_from_file("./terrain.vs", "./terrain.fs"))
		{
			APP_LOG << "Failed to load shaders. [Press any key to continue]";
			std::cin.get();
		}
		mesh_terrain.dispose();
		mesh_terrain = gen_terrain_mesh(terrain_samples_x, terrain_samples_y);
		gen_heightmap();
	}
	ImGui::End();
	ImGui::Render();
}
void SkGpuDevice::drawTextureProducerImpl(GrTextureProducer* producer,
                                          const SkRect& clippedSrcRect,
                                          const SkRect& clippedDstRect,
                                          SkCanvas::SrcRectConstraint constraint,
                                          const SkMatrix& viewMatrix,
                                          const SkMatrix& srcToDstMatrix,
                                          const GrClip& clip,
                                          const SkPaint& paint) {
    // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp
    // combining by not baking anything about the srcRect, dstRect, or viewMatrix, into the texture
    // FP. In the future this should be an opaque optimization enabled by the combination of
    // GrDrawOp/GP and FP.
    const SkMaskFilter* mf = paint.getMaskFilter();
    // The shader expects proper local coords, so we can't replace local coords with texture coords
    // if the shader will be used. If we have a mask filter we will change the underlying geometry
    // that is rendered.
    bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf;

    bool doBicubic;
    GrSamplerParams::FilterMode fm =
        GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, srcToDstMatrix,
                                        &doBicubic);
    const GrSamplerParams::FilterMode* filterMode = doBicubic ? nullptr : &fm;

    GrTextureProducer::FilterConstraint constraintMode;
    if (SkCanvas::kFast_SrcRectConstraint == constraint) {
        constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
    } else {
        constraintMode = GrTextureAdjuster::kYes_FilterConstraint;
    }

    // If we have to outset for AA then we will generate texture coords outside the src rect. The
    // same happens for any mask filter that extends the bounds rendered in the dst.
    // This is conservative as a mask filter does not have to expand the bounds rendered.
    bool coordsAllInsideSrcRect = !paint.isAntiAlias() && !mf;

    // Check for optimization to drop the src rect constraint when on bilerp.
    if (filterMode && GrSamplerParams::kBilerp_FilterMode == *filterMode &&
        GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect) {
        SkMatrix combinedMatrix;
        combinedMatrix.setConcat(viewMatrix, srcToDstMatrix);
        if (can_ignore_bilerp_constraint(*producer, clippedSrcRect, combinedMatrix,
                                         fRenderTargetContext->isUnifiedMultisampled())) {
            constraintMode = GrTextureAdjuster::kNo_FilterConstraint;
        }
    }

    const SkMatrix* textureMatrix;
    SkMatrix tempMatrix;
    if (canUseTextureCoordsAsLocalCoords) {
        textureMatrix = &SkMatrix::I();
    } else {
        if (!srcToDstMatrix.invert(&tempMatrix)) {
            return;
        }
        textureMatrix = &tempMatrix;
    }
    sk_sp<GrFragmentProcessor> fp(producer->createFragmentProcessor(
        *textureMatrix, clippedSrcRect, constraintMode, coordsAllInsideSrcRect, filterMode,
        fRenderTargetContext->getColorSpace()));
    if (!fp) {
        return;
    }

    GrPaint grPaint;
    if (!SkPaintToGrPaintWithTexture(fContext.get(), fRenderTargetContext.get(), paint, viewMatrix,
                                     fp, producer->isAlphaOnly(), &grPaint)) {
        return;
    }
    GrAA aa = GrBoolToAA(paint.isAntiAlias());
    if (canUseTextureCoordsAsLocalCoords) {
        fRenderTargetContext->fillRectToRect(clip, std::move(grPaint), aa, viewMatrix,
                                             clippedDstRect, clippedSrcRect);
        return;
    }

    if (!mf) {
        fRenderTargetContext->drawRect(clip, std::move(grPaint), aa, viewMatrix, clippedDstRect);
        return;
    }

    // First see if we can do the draw + mask filter direct to the dst.
    if (viewMatrix.isScaleTranslate()) {
        SkRect devClippedDstRect;
        viewMatrix.mapRectScaleTranslate(&devClippedDstRect, clippedDstRect);

        SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
        if (mf->directFilterRRectMaskGPU(fContext.get(),
                                         fRenderTargetContext.get(),
                                         std::move(grPaint),
                                         clip,
                                         viewMatrix,
                                         rec,
                                         SkRRect::MakeRect(clippedDstRect),
                                         SkRRect::MakeRect(devClippedDstRect))) {
            return;
        }
    }

    SkPath rectPath;
    rectPath.addRect(clippedDstRect);
    rectPath.setIsVolatile(true);
    GrBlurUtils::drawPathWithMaskFilter(this->context(), fRenderTargetContext.get(), this->clip(),
                                        rectPath, std::move(grPaint), aa, viewMatrix, mf,
                                        GrStyle::SimpleFill(), true);
}