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

天才小熊猫oo

暂无认证

  • 0浏览

    0关注

    37博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

10.游戏引擎 在3D空间中渲染出三维几何体

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

        文章写得比较丑,主要是想把代码先给贴出来。等后面有空了文章里面的原理再慢慢的讲解。 

        这个类里面会使用第9章里的三维体数据来调用OpenGL的接口绘制出来对应的形状。几何体绘制类主要是调试使用的,比如想要更直观的看到一个对象身上的碰撞框。绘制的形状非常多,大家直接看代码吧。

Gizmo.h

#pragma once

#define GL_PI 3.1415926




class Gizmo
{
private:

public:
//2D部分
static void Init();
static void drawPoint(Vector2 point, sColor color);//点
static void drawLine(Vector2 originPoint, Vector2 endPoint, sColor color);//线段
static void drawLine(GLLine &line, sColor color);// 用对象设置的线段
static void drawArrowLine(Vector2 originPoint, Vector2 endPoint, sColor color);//箭头线
static void drawArrowLine(GLLine &line, sColor color); //用对象设置的箭头线
static void drawCirle(Vector2 position, float radius, sColor color);//有圆心点和半径的园
static void drawCirle(GLCircle &circle, sColor color);//用对象设置的园
static void drawGLPolygon(GLPolygon &poly, sColor color);// 用对象设置的多段线
static void drawRay(GLRay &ray, sColor color);//画射线
static void drawAABB(GLAABB &aabb, sColor color);//画AABB盒子
static void drawRectangle(Vector2 ¢erPoint,float wid,float hei,sColor color);//画矩形盒子
static void drawRect(RECT rect,sColor color);

/3D部分//
static void drawLine3D(Vector3D &originPoint, Vector3D &endPoint, sColor color);//3D线段
static void drawLine3D(GLLine3D &line, sColor color);//3D线段(对象来画)
static void drawArrowLine3D(Vector3D &originPoint, Vector3D &endPoint, sColor color);//3D箭头线
static void drawArrowLine3D(GLLine3D &line, sColor color); //用对象设置3D箭头线
static void drawFrustum(Vector3D vec[], sColor color);
static void drawSphere(GLSphere &sphere, sColor color);//
static void drawAABB3D(GLAABB3D &aabb3D, sColor color);
static void drawAABB3D(float len, float wid, float hei, Vector3D &point,sColor color);
static void drawAABB3D(Vector3D eightPoint[8], sColor color);
static void drawAabbLine(GLAABB3D aabb, sColor color);
static void drawAABB3D(GLAABB3D *Aabb, sColor color);
static void drawRay(GLRay3D &ray,sColor color);
static void	drawTrianglePlane(GLTrianglePlane &TrianglePlane,sColor color);
static void drawPoint(Vector3D &point, sColor color);
	Gizmo();
	~Gizmo();
};

Gizmo.cpp

#include "Engine.h"

static GLUquadricObj *Quadratic;	// 二次几何体

Gizmo::Gizmo()
{
}

void Gizmo::Init()
{
	Quadratic= gluNewQuadric();
	gluQuadricNormals(Quadratic, GLU_SMOOTH);	// 使用平滑法线
	gluQuadricTexture(Quadratic, GL_TRUE);		// 使用纹理
}

void Gizmo::drawPoint(Vector2 point, sColor color)
{
	glPushMatrix();
	glPointSize(6.0f);
	glColor3fv(color.color);
	glBegin(GL_POINTS);
	glVertex2f(point.x, point.y);
	glEnd();
	glPopMatrix();
}

void Gizmo::drawLine(Vector2 originPoint, Vector2 endPoint, sColor color)
{
	glPushMatrix();
	glLineWidth(2.0f);
	glColor3fv(color.color);
	glBegin(GL_LINES);
	glVertex2f(originPoint.x, originPoint.y);
	glVertex2f(endPoint.x, endPoint.y);
	glEnd();
	glPopMatrix();
}

void Gizmo::drawLine(GLLine &line, sColor color)
{
	glPushMatrix();
	glLineWidth(2.0f);
	glColor3fv(color.color);
	glBegin(GL_LINES);
	glVertex2f(line.m_vcOrig.x, line.m_vcOrig.y);
	glVertex2f(line.m_vcOrig.x + line.m_vcDir.x*line.m_len,
		line.m_vcOrig.y + line.m_vcDir.y*line.m_len);
	glEnd();
	glPopMatrix();
}


void Gizmo::drawArrowLine(Vector2 originPoint, Vector2 endPoint, sColor color)
{
	
	
		Vector2 ds = endPoint - originPoint;
		float da = 0;
		if (ds.x == 0)
		{
			if (ds.y < 0)  da = GL_PI /2;
			else if (ds.y > 0)  da = GL_PI / 2 * 3;
			else   da = 0;
		}

		else if (ds.x > 0)
		{
			if (ds.y  0)
		da = GL_PI / 2 * 3;
	else
		da = 0;
}
else if (ds.x > 0)
{
	if (ds.y m_vcMax.y - Aabb->m_vcMin.y;

	point[0] = Aabb->m_vcMax - Vector3D(width, 0, 0);
	point[1] = Aabb->m_vcMax;
	point[2] = Aabb->m_vcMax - Vector3D(0, heigth, 0);
	point[3] = Aabb->m_vcMax - Vector3D(width, heigth, 0);

	point[4] = Aabb->m_vcMin + Vector3D(0, heigth, 0);
	point[5] = Aabb->m_vcMin + Vector3D(width, heigth, 0);
	point[6] = Aabb->m_vcMin + Vector3D(width, 0, 0);
	point[7] = Aabb->m_vcMin;
	glPushMatrix();
	glColor4fv(color.color);
	glFrontFace(GL_CW);//设置顺时针方向为正面
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
	//前
	glBegin(GL_QUADS);
	glVertex3f(point[0].x, point[0].y, point[0].z);
	glVertex3f(point[1].x, point[1].y, point[1].z);
	glVertex3f(point[2].x, point[2].y, point[2].z);
	glVertex3f(point[3].x, point[3].y, point[3].z);
	//后
	glVertex3f(point[4].x, point[4].y, point[4].z);
	glVertex3f(point[5].x, point[5].y, point[5].z);
	glVertex3f(point[6].x, point[6].y, point[6].z);
	glVertex3f(point[7].x, point[7].y, point[7].z);
	//上
	glVertex3f(point[4].x, point[4].y, point[4].z);
	glVertex3f(point[5].x, point[5].y, point[5].z);
	glVertex3f(point[1].x, point[1].y, point[1].z);
	glVertex3f(point[0].x, point[0].y, point[0].z);
	//下
	glVertex3f(point[7].x, point[7].y, point[7].z);
	glVertex3f(point[6].x, point[6].y, point[6].z);
	glVertex3f(point[2].x, point[2].y, point[2].z);
	glVertex3f(point[3].x, point[3].y, point[3].z);
	//左	
	glVertex3f(point[0].x, point[0].y, point[0].z);
	glVertex3f(point[4].x, point[4].y, point[4].z);
	glVertex3f(point[7].x, point[7].y, point[7].z);
	glVertex3f(point[3].x, point[3].y, point[3].z);
	//右	
	glVertex3f(point[5].x, point[5].y, point[5].z);
	glVertex3f(point[6].x, point[6].y, point[6].z);
	glVertex3f(point[2].x, point[2].y, point[2].z);
	glVertex3f(point[1].x, point[1].y, point[1].z);
	glEnd();
	glFrontFace(GL_CCW);//设置逆时针方向为正面
	drawLine3D(point[0], point[1], sColor(1, 1, 1, 1));
	drawLine3D(point[1], point[2], sColor(1, 1, 1, 1));
	drawLine3D(point[2], point[3], sColor(1, 1, 1, 1));
	drawLine3D(point[3], point[0], sColor(1, 1, 1, 1));
	drawLine3D(point[5], point[6], sColor(1, 1, 1, 1));
	drawLine3D(point[6], point[7], sColor(1, 1, 1, 1));
	drawLine3D(point[7], point[4], sColor(1, 1, 1, 1));
	drawLine3D(point[4], point[5], sColor(1, 1, 1, 1));
	drawLine3D(point[0], point[4], sColor(1, 1, 1, 1));
	drawLine3D(point[1], point[5], sColor(1, 1, 1, 1));
	drawLine3D(point[3], point[7], sColor(1, 1, 1, 1));
	drawLine3D(point[2], point[6], sColor(1, 1, 1, 1));
	glColor4f(1,1,1,1);
	glDisable(GL_BLEND);
	glPopMatrix();
}

void Gizmo::drawAabbLine(GLAABB3D aabb, sColor color)
{
	glLineWidth(3.0f);
	glPushMatrix();
	glColor3fv(color.color);
	glBegin(GL_LINES);
	glDisable(GL_LIGHTING);
	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMax.y, aabb.m_vcMax.z);
	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMax.y, aabb.m_vcMax.z);

	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMax.y, aabb.m_vcMax.z);
	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMax.y, aabb.m_vcMin.z);

	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMax.y, aabb.m_vcMin.z);
	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMax.y, aabb.m_vcMin.z);

	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMax.y, aabb.m_vcMin.z);
	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMax.y, aabb.m_vcMax.z);

	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMax.y, aabb.m_vcMax.z);
	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMin.y, aabb.m_vcMax.z);

	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMax.y, aabb.m_vcMax.z);
	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMin.y, aabb.m_vcMax.z);

	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMax.y, aabb.m_vcMin.z);
	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMin.y, aabb.m_vcMin.z);

	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMax.y, aabb.m_vcMin.z);
	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMin.y, aabb.m_vcMin.z);

	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMin.y, aabb.m_vcMax.z);
	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMin.y, aabb.m_vcMin.z);

	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMin.y, aabb.m_vcMin.z);
	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMin.y, aabb.m_vcMin.z);

	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMin.y, aabb.m_vcMin.z);
	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMin.y, aabb.m_vcMax.z);

	glVertex3f(aabb.m_vcMax.x, aabb.m_vcMin.y, aabb.m_vcMax.z);
	glVertex3f(aabb.m_vcMin.x, aabb.m_vcMin.y, aabb.m_vcMax.z);

	glColor4f(1, 1, 1, 1);
	glEnd();
	glPopMatrix();
}


void Gizmo::drawRay(GLRay3D& ray, sColor color)
{
	glPushMatrix();
	glLineWidth(2.0);
	glColor3fv(color.color);
	glBegin(GL_LINES);

	glVertex3f(ray.m_vcOrig.x, ray.m_vcOrig.y, ray.m_vcOrig.z);
	glVertex3f(ray.m_vcOrig.x + ray.m_vcDir.x * 10000,
		ray.m_vcOrig.y + ray.m_vcDir.y * 10000,
		ray.m_vcOrig.z+ray.m_vcDir.z*10000);

	glColor3f(1.0f, 1.0f, 1.0f);
	glEnd();
	glPopMatrix();

}


void Gizmo::drawTrianglePlane(GLTrianglePlane &trianglePlane,sColor color)
{
	glPushMatrix();
	
	glColor4fv(color.color);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glBegin(GL_TRIANGLES);
	glVertex3f(trianglePlane.m_v0.x, trianglePlane.m_v0.y, trianglePlane.m_v0.z);
	glVertex3f(trianglePlane.m_v1.x, trianglePlane.m_v1.y, trianglePlane.m_v1.z);
	glVertex3f(trianglePlane.m_v2.x, trianglePlane.m_v2.y, trianglePlane.m_v2.z);
	glColor3f(1.0f, 1.0f, 1.0f);
	glEnd();
	glDisable(GL_BLEND);


	glBegin(GL_LINE_LOOP);
	glLineWidth(2.0);
	glVertex3f(trianglePlane.m_v0.x, trianglePlane.m_v0.y, trianglePlane.m_v0.z);
	glVertex3f(trianglePlane.m_v1.x, trianglePlane.m_v1.y, trianglePlane.m_v1.z);
	glVertex3f(trianglePlane.m_v2.x, trianglePlane.m_v2.y, trianglePlane.m_v2.z);
	glEnd();

	glPopMatrix();

}

void Gizmo::drawPoint(Vector3D &point, sColor color)
{
	glPushMatrix();
	glPointSize(6.0f);
	glColor3fv(color.color);
	glBegin(GL_POINTS);
	glVertex3f(point.x, point.y,point.z);
	glEnd();
	glColor3f(1, 1, 1);
	glPopMatrix();

}


Gizmo::~Gizmo()
{
}

关注
打赏
1665545649
查看更多评论
立即登录/注册

微信扫码登录

0.0428s