首页 > 其他分享 >【libGDX】使用ShapeRenderer绘制几何图形

【libGDX】使用ShapeRenderer绘制几何图形

时间:2024-02-21 22:55:22浏览次数:29  
标签:mShapeRenderer 几何图形 libGDX float void ShapeRenderer com public

1 ShapeRenderer 简介

​ ShapeRenderer 是 libGDX 中用于绘制基本形状的工具之一。它可以绘制点、线、矩形、多边形、圆形、椭圆形、扇形、立方体、圆锥体等几何图形。这对于在游戏或图形应用程序中绘制简单的形状是很有用的。

​ ShapeRenderer 的主要方法如下:

1)开始和结束绘制

// 开始绘制, ShapeType取值有: Point(点)、Line(线)、Filled(内部填充)
// 例: shapeRenderer.begin(ShapeRenderer.ShapeType.Line)
public void begin(ShapeType type)
public void begin() // ShapeType默认取值Line
// 结束绘制
public void end()

​ 说明:绘制相关逻辑需要放在 begin 和 end 之间。

2) 设置颜色和形状类型

// 设置图形颜色
public void setColor(float r, float g, float b, float a)
public void setColor(Color color)
// 设置ShapeType, 取值有: Point(点)、Line(线)、Filled(内部填充)
public void set (ShapeType type)

3)模型变换矩阵

// transformMatrix矩阵
public void setTransformMatrix(Matrix4 matrix)
public void identity() // 重置transformMatrix
public void translate(float x, float y, float z) // 平移
public void rotate(float axisX, float axisY, float axisZ, float degrees) // 旋转
public void scale(float scaleX, float scaleY, float scaleZ) // 缩放

4)投影变换矩阵

// projectionMatrix矩阵, 通常设置为camera.combined
public void setProjectionMatrix(Matrix4 matrix)
public Matrix4 getProjectionMatrix()

5)绘制图形

// 绘制一个点
public void point(float x, float y, float z)

// 绘制一条线段
public final void line(float x, float y, float z, float x2, float y2, float z2)
public final void line(Vector3 v0, Vector3 v1)
public final void line(float x, float y, float x2, float y2)
public final void line(Vector2 v0, Vector2 v1)
public final void line(float x, float y, float x2, float y2, Color c1, Color c2)
public void line(float x, float y, float z, float x2, float y2, float z2, Color c1, Color c2)

// 绘制三角形
public void triangle(float x1, float y1, float x2, float y2, float x3, float y3)
public void triangle(float x1, float y1, float x2, float y2, float x3, float y3, Color col1, Color col2, Color col3)

// 绘制矩形
public void rect(float x, float y, float width, float height)
public void rect(float x, float y, float width, float height, Color col1, Color col2, Color col3, Color col4)
public void rect(float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float degrees)

// 绘制矩形线段
public void rectLine(float x1, float y1, float x2, float y2, float width)
public void rectLine(float x1, float y1, float x2, float y2, float width, Color c1, Color c2)
public void rectLine(Vector2 p1, Vector2 p2, float width)

// 绘制立方体
public void box(float x, float y, float z, float width, float height, float depth)

// 在指定位置绘制一个X形交叉线
public void x(float x, float y, float size)
public void x(Vector2 p, float size)

// 绘制扇形
public void arc(float x, float y, float radius, float start, float degrees)
public void arc(float x, float y, float radius, float start, float degrees, int segments)

// 绘制圆形
public void circle(float x, float y, float radius)
public void circle(float x, float y, float radius, int segments)

// 绘制椭圆形, x、y指椭圆左下角的位置坐标
public void ellipse(float x, float y, float width, float height)
public void ellipse(float x, float y, float width, float height, int segments)
public void ellipse(float x, float y, float width, float height, float rotation)
public void ellipse(float x, float y, float width, float height, float rotation, int segments)

// 绘制圆锥
public void cone(float x, float y, float z, float radius, float height)
public void cone(float x, float y, float z, float radius, float height, int segments)

// 绘制多边形(首尾相连, ShapeType只能取Line)
public void polygon(float[] vertices, int offset, int count)
public void polygon(float[] vertices)

// 绘制多边形(首尾不相连, ShapeType只能取Line)
public void polyline(float[] vertices, int offset, int count)
public void polyline(float[] vertices)

​ 说明:左下角为坐标原点,向右和向上分别为 x 轴和 y 轴正方向。

2 ShapeRenderer 应用

2.1 绘制三角形

​ MyGame.java

package com.zhyan8.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class MyGame extends ApplicationAdapter {
	private ShapeRenderer mShapeRenderer;
	
	@Override
	public void create () {
		mShapeRenderer = new ShapeRenderer();
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
		Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
		mShapeRenderer.begin(ShapeRenderer.ShapeType.Line); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(1, 0, 0, 1); // 设置图形颜色
		mShapeRenderer.triangle(100, 100, 300, 100, 200, 300); // 绘制三角形
		mShapeRenderer.end();

		mShapeRenderer.begin(ShapeRenderer.ShapeType.Filled); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(0, 1, 0, 1); // 设置图形颜色
		mShapeRenderer.triangle(350, 100, 550, 100, 450, 300); // 绘制三角形
		mShapeRenderer.end();
	}

	@Override
	public void dispose () {
		mShapeRenderer.dispose();
	}
}

​ 运行效果如下。

img

2.2 绘制矩形

​ MyGame.java

package com.zhyan8.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class MyGame extends ApplicationAdapter {
	private ShapeRenderer mShapeRenderer;
	
	@Override
	public void create () {
		mShapeRenderer = new ShapeRenderer();
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
		Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
		mShapeRenderer.begin(ShapeRenderer.ShapeType.Line); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(1, 0, 0, 1); // 设置图形颜色
		mShapeRenderer.rect(100, 100, 200, 200); // 绘制矩形
		mShapeRenderer.end();

		mShapeRenderer.begin(ShapeRenderer.ShapeType.Filled); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(0, 1, 0, 1); // 设置图形颜色
		mShapeRenderer.rect(350, 100, 200, 300); // 绘制矩形
		mShapeRenderer.end();
	}

	@Override
	public void dispose () {
		mShapeRenderer.dispose();
	}
}

​ 运行效果如下。

img

2.3 绘制线框

​ MyGame.java

package com.zhyan8.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class MyGame extends ApplicationAdapter {
	private ShapeRenderer mShapeRenderer;

	@Override
	public void create () {
		mShapeRenderer = new ShapeRenderer();
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
		Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
		shapeRenderer.begin(ShapeRenderer.ShapeType.Line); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(1, 0, 0, 1); // 设置图形颜色
		mShapeRenderer.rectLine(100, 100, 300, 300, 20); // 绘制线框(不填充)
		mShapeRenderer.end();

		mShapeRenderer.begin(ShapeRenderer.ShapeType.Filled); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(0, 1, 0, 1); // 设置图形颜色
		mShapeRenderer.rectLine(350, 100, 550, 300, 20); // 绘制线段(填充)
		mShapeRenderer.end();
	}

	@Override
	public void dispose () {
		mShapeRenderer.dispose();
	}
}

​ 运行效果如下。

img

2.4 绘制多边形

​ MyGame.java

package com.zhyan8.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class MyGame extends ApplicationAdapter {
	private ShapeRenderer mShapeRenderer;

	@Override
	public void create () {
		mShapeRenderer = new ShapeRenderer();
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
		Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
		mShapeRenderer.begin(ShapeRenderer.ShapeType.Line); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(1, 0, 0, 1); // 设置图形颜色
		drawPolygon5(); // 绘制正五边形
		drawPolygon6(); // 绘制正六边形
		mShapeRenderer.end();
	}

	@Override
	public void dispose () {
		mShapeRenderer.dispose();
	}

	private void drawPolygon5() { // 绘制正五边形
		float[] starVertices = calculatePolygonVertices(200, 250, 100, 5);
		mShapeRenderer.polygon(starVertices);
	}

	private void drawPolygon6() { // 绘制正六边形
		float[] starVertices = calculatePolygonVertices(450, 250, 100, 6);
		mShapeRenderer.polygon(starVertices);
	}

	private float[] calculatePolygonVertices(float x, float y, float size, int sides) { // 计算正n边形的顶点坐标
		float[] vertices = new float[sides * 2];
		for (int i = 0; i < sides * 2; i += 2) {
			float angleRad = (i / 2) * 360f / sides * (float) Math.PI / 180f;
			vertices[i] = x + size * (float) Math.cos(angleRad);
			vertices[i + 1] = y + size * (float) Math.sin(angleRad);
		}
		return vertices;
	}
}

​ 运行效果如下。

img

2.5 绘制圆形

​ MyGame.java

package com.zhyan8.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class MyGame extends ApplicationAdapter {
	private ShapeRenderer mShapeRenderer;

	@Override
	public void create () {
		mShapeRenderer = new ShapeRenderer();
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
		Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
		shapeRenderer.begin(ShapeRenderer.ShapeType.Line); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(1, 0, 0, 1); // 设置图形颜色
		mShapeRenderer.circle(200, 250, 100); // 绘制圆形
		mShapeRenderer.end();

		mShapeRenderer.begin(ShapeRenderer.ShapeType.Filled); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(0, 1, 0, 1); // 设置图形颜色
		mShapeRenderer.circle(450, 250, 100); // 绘制圆形
		mShapeRenderer.end();
	}

	@Override
	public void dispose () {
		mShapeRenderer.dispose();
	}
}

​ 运行效果如下。

img

2.6 绘制椭圆

​ MyGame.java

package com.zhyan8.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class MyGame extends ApplicationAdapter {
	private ShapeRenderer mShapeRenderer;

	@Override
	public void create () {
		mShapeRenderer = new ShapeRenderer();
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
		Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
		mShapeRenderer.begin(ShapeRenderer.ShapeType.Line); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(1, 0, 0, 1); // 设置图形颜色
		mShapeRenderer.ellipse(100, 200, 200, 100); // 绘制椭圆形
		mShapeRenderer.end();

		mShapeRenderer.begin(ShapeRenderer.ShapeType.Filled); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(0, 1, 0, 1); // 设置图形颜色
		mShapeRenderer.ellipse(400, 150, 100, 200); // 绘制椭圆形
		mShapeRenderer.end();
	}

	@Override
	public void dispose () {
		mShapeRenderer.dispose();
	}
}

​ 运行效果如下。

img

2.7 绘制弧形

​ MyGame.java

package com.zhyan8.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class MyGame extends ApplicationAdapter {
	private ShapeRenderer mShapeRenderer;

	@Override
	public void create () {
		mShapeRenderer = new ShapeRenderer();
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
		Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
		mShapeRenderer.begin(ShapeRenderer.ShapeType.Line); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(1, 0, 0, 1); // 设置图形颜色
		mShapeRenderer.arc(100, 200, 200, 0, 72); // 绘制扇形
		mShapeRenderer.end();

		mShapeRenderer.begin(ShapeRenderer.ShapeType.Filled); // ShapeType取值: Point、Line、Filled
		mShapeRenderer.setColor(0, 1, 0, 1); // 设置图形颜色
		mShapeRenderer.arc(450, 200, 100, 0, 300); // 绘制扇形
		mShapeRenderer.end();
	}

	@Override
	public void dispose () {
		mShapeRenderer.dispose();
	}
}

​ 运行效果如下。

img

2.8 绘制立方体

​ MyGame.java

package com.zhyan8.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class MyGame extends ApplicationAdapter {
	private ShapeRenderer mShapeRenderer;
	private PerspectiveCamera mCamera;
	private float mRotationAngle;

	@Override
	public void create() {
		mShapeRenderer = new ShapeRenderer();
		mCamera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		mCamera.position.set(5f, 5f, 5f);
		mCamera.lookAt(0, 0, 0);
		mCamera.near = 1f;
		mCamera.far = 100f;
		mCamera.update();
		mShapeRenderer.setProjectionMatrix(mCamera.combined);
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
		Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT);
		mShapeRenderer.begin(ShapeRenderer.ShapeType.Line);
		mShapeRenderer.setColor(1, 0, 0, 1);
		rotate();
		mShapeRenderer.box(-2, 0, 0, 2, 2, 2); // 绘制立方体
		mShapeRenderer.end();

		mShapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
		mShapeRenderer.setColor(0, 1, 0, 1);
		rotate();
		mShapeRenderer.box(2, 0, 0, 2, 2, 2); // 绘制立方体
		mShapeRenderer.end();
	}

	@Override
	public void resize(int width, int height) {
		mCamera.viewportWidth = width;
		mCamera.viewportHeight = height;
		mCamera.update();
		mShapeRenderer.setProjectionMatrix(mCamera.combined);
	}

	@Override
	public void dispose() {
		mShapeRenderer.dispose();
	}

	private void rotate() {
		mRotationAngle += Gdx.graphics.getDeltaTime() * 45f; // 每秒转45度
		mShapeRenderer.identity(); // 重置transformation矩阵
		mShapeRenderer.rotate(2, 2, 2, mRotationAngle);
	}
}

​ 运行效果如下。

img

2.9 绘制圆锥

​ MyGame.java

package com.zhyan8.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class MyGame extends ApplicationAdapter {
	private ShapeRenderer mShapeRenderer;
	private PerspectiveCamera mCamera;
	private float mRotationAngle;

	@Override
	public void create() {
		mShapeRenderer = new ShapeRenderer();
		mCamera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		mCamera.position.set(5f, 5f, 5f);
		mCamera.lookAt(0, 0, 0);
		mCamera.near = 1f;
		mCamera.far = 100f;
		mCamera.update();
		mShapeRenderer.setProjectionMatrix(mCamera.combined);
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0.455f, 0.725f, 1.0f, 1.0f);
		Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT);
		mShapeRenderer.begin(ShapeRenderer.ShapeType.Line);
		mShapeRenderer.setColor(1, 0, 0, 1);
		rotate();
		mShapeRenderer.cone(-2, 0, 0, 2, 2);
		mShapeRenderer.end();

		mShapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
		mShapeRenderer.setColor(0, 1, 0, 1);
		rotate();
		mShapeRenderer.cone(2, 0, 0, 2, 2);
		mShapeRenderer.end();
	}

	@Override
	public void resize(int width, int height) {
		mCamera.viewportWidth = width;
		mCamera.viewportHeight = height;
		mCamera.update();
		mShapeRenderer.setProjectionMatrix(mCamera.combined);
	}

	@Override
	public void dispose() {
		mShapeRenderer.dispose();
	}

	private void rotate() {
		mRotationAngle += Gdx.graphics.getDeltaTime() * 45f; // 每秒转45度
		mShapeRenderer.identity(); // 重置transformation矩阵
		mShapeRenderer.rotate(2, 2, 2, mRotationAngle);
	}
}

​ 运行效果如下。

img

​ 声明:本文转自【libGDX】使用ShapeRenderer绘制几何图形

标签:mShapeRenderer,几何图形,libGDX,float,void,ShapeRenderer,com,public
From: https://www.cnblogs.com/zhyan8/p/18024333

相关文章

  • 【libGDX】初识libGDX
    1前言​libGDX是一个开源且跨平台的Java游戏开发框架,于2010年3月11日推出0.1版本,它通过OpenGLES2.0/3.0渲染图像,支持Windows、Linux、macOS、Android、iOS、Web等平台,提供了统一的API,用户只需要写一套代码就可以在多个平台上运行,官方介绍见→Features。......
  • 【libGDX】ApplicationAdapter生命周期
    1前言​libGDX中,用户自定义的渲染窗口需要继承ApplicationAdapter类,ApplicationAdapter实现了ApplicationListener接口,但实现的方法都是空方法,方法释义如下。publicinterfaceApplicationListener{ //应用首次创建时调用一次 publicvoidcreate(); //窗口尺......
  • WPF性能优化:形状(Shape)、几何图形(Geometry)和图画(Drawing)的使用
    在用户界面技术中,绘图是一个绕不开的话题。WPF提供了多种可根据应用程序要求进行优化的2D图形和图像的处理功能,包括画刷(Brush)、形状(Shape)、几何图形(Geometry)、图画(Drawing)和变换(Transform)等。其中形状(Shape)、几何图形(Geometry)和图画(Drawing)承担了基础的绘图功能,形......
  • libgdx摄像头的移动
    要知道,做一个游戏,摄像头是必不可少的。接下来,我将讲解libgdx里面摄像头的移动2d摄像头OrthographicCamera也叫做正交相机结果展示:按上下左右是可以移动的OrthographicCameracamera的使用:完整代码:packagecom.brentaureli.mariobros.cam;importcom.badlogic.gdx.Appli......
  • libgdx的基础讲解
    今天讲解libgdx的示例代码知识SpriteBatch用于绘制2D图像Texture资源的实体类music长音乐如背景音乐sound短音乐,如一个物体触发的短时间音乐代码:packagecom.mygdx.game;importcom.badlogic.gdx.ApplicationAdapter;importcom.badlogic.gdx.graphics.Texture;......
  • 如何使用libgdx做游戏01---libgdx的安装
    一般来说使用这个工具做游戏的都是java开发者,这种技术在国外勉强算是必学的,而在国内却很少有这方面的知识。接下来,我将讲解如何安装libgdx,也算是简单的libgdx入门工具:idea、jdk17、gradle7.5.1及其以上第一步,下载gdx-setup.jar包下载地址:https://libgdx-nightlies.s3.amazonaw......
  • 常见立体几何图形的体积
    文章目录abstract祖暅原理推论棱锥和圆锥的体积用积分的方法推导棱台和圆台的体积圆台体积公式球体的体积球体的表面积abstract锥体和球体的体积公式主要通过积分的方法推导这类公式的推导中学一般不要求,只要会应用公式在高等数学中由合适和方便的工具来推导这些公式而相关......
  • 2.几何图形
    2.1绘制直线......
  • 【matplotlib基础】--几何图形
    除了绘制各类分析图形(比如柱状图,折线图,饼图等等)以外,matplotlib也可以在画布上任意绘制各类几何图形。这对于计算机图形学、几何算法和计算机辅助设计等领域非常重要。matplitlib中的patches类提供了丰富的几何对象,本篇抛砖引玉,介绍其中几种常用的几何图形绘制方法。其实matp......
  • 在LaTeX的几何图形内嵌入图片
    本文简单介绍在LaTeX中如何在几何图形内嵌入图片。1、环境操作系统:Ubuntu22.04编译方式:XeLaTeX2、完整示例代码%博客园,繁星间漫步,陆巍的博客\documentclass{ctexart}\usepackage{graphics}%图形支持\usepackage{tikz}\usetikzlibrary{shapes.geometric,positionin......