您当前的位置: 首页 >  游戏

天才小熊猫oo

暂无认证

  • 0浏览

    0关注

    37博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

11.游戏引擎源码 使用OpenGL绘制三维球体

天才小熊猫oo 发布时间:2022-09-18 01:16:56 ,浏览量:0

        绘制球体的难点主要在于 要在遍历循环中 根据经纬度反复的使用Cos、Sin函数算出球面上的XYZ三个顶点坐标,一直反复计算,最终三角面多的形成了一个球的形状。

        还是老样子,直接贴代码,感兴趣的先自己看看吧。原理后面有空了统一讲!

Ball.h

#pragma once

namespace U3D
{
	class CBall :public CElement
	{
	private:
		GLSphere *sphere=NULL;
		CCTexture *ballTexture;

	public:
		void draw(float deltaTime);
		GLAABB3D getBoundBox() { return GLAABB3D{Vector3D(-9999,-9999,-99999),Vector3D(9999,9999,9999)};}
		GLSphere *getBall();

		void setBall(Vector3D ¢er, float radius,char*name="res/其他/地球.png");
		CBall(Vector3D ¢er, float radius,char*name="res/其他/地球.png");
		CBall();
		~CBall();
	};
}

Ball.cpp

#include "Engine.h"


namespace U3D
{

	CBall::CBall()
	{
	}


	CBall::CBall(Vector3D ¢er,float radius,char *name)
	{
		ballTexture = CTextureManager::getInstance()->addTexture(name);
		setBall(center, radius,name);
	}

	void CBall::setBall(Vector3D ¢er, float radius,char*name)
	{
		ballTexture = CTextureManager::getInstance()->addTexture(name);
		if (sphere == NULL)
		{
			sphere = new GLSphere;
		}
		sphere->Set(center, radius);
	}


	void CBall::draw(float deltaTime)
	{
		Matrix3D scaleMatrix;
		Matrix3D rotateMatrix;
		Matrix3D transMatrix;

		//放缩图片
		scaleMatrix.Scale(scale.x, scale.y, scale.z);
		//水平翻转
		if (flip == true)
			scaleMatrix._11 *= -1;
		//旋转图片
		Matrix3D tempx, tempy, tempz;
		tempy.YRotate(angle.y);
		tempx.XRotate(angle.x);
		tempz.ZRotate(angle.z);
		rotateMatrix = tempx*tempy*tempz;

		// 平移图片到我们的指定位置
		transMatrix.Translate(pos.x, pos.y, pos.z);
		local_matrix = scaleMatrix*rotateMatrix*transMatrix;

		if (parent == NULL)
		{
			world_color = local_color;
			world_matrix = local_matrix;
		}
		else
		{
			sColor col = parent->getWorldColor();
			world_color.r = local_color.r*col.r;
			world_color.g = local_color.g*col.g;
			world_color.b = local_color.b*col.b;
			world_color.a = local_color.a*col.a;
			world_matrix = local_matrix*parent->getWorldMatrix();//行*列矩阵相乘__计算得到新矩阵
		}

		if (visible == false)
			return;

		Vector3D v;
		float radius = sphere->m_radius;
		Vector3D center = sphere->m_center;

		float dtor = PI / 180.0f;
		int dphi = 10; //纬度
		int dtheta = 10; //经度

		glPushMatrix();
		glColor4fv(world_color.color);
		glMultMatrixf(world_matrix.mat);

		glDisable(GL_CULL_FACE);
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, ballTexture->getTextureID());
		glBegin(GL_TRIANGLE_STRIP);
		for (int phi = 0; phi             
关注
打赏
1665545649
查看更多评论
0.0390s