[C/C++] [OpenGL ES 3.0] 简单的类(包括顶点数组,索引数组,纹理数组等)

标题

要绘制的图形增多了以后管理起来会比较麻烦,所以干脆封装成一个类(结构…好吧反正没区别…)

_ESSHAPES里的用起来有些麻烦(毕竟函数名字太长了)

esShape.hpp

#ifndef ESSHAPES_HPP_
#define ESSHAPES_HPP_
#include <GLES3/gl3.h>
#include "esUtil.hpp"
namespace _ESSHAPES
{
	class Shape
	{
		private:
		ESMatrix _modelView;
		GLfloat * _vertices;
		GLfloat * _normals;
		GLfloat * _texCoords;
		GLuint * _indices;
		float _angle;
		float _scale[3];
		float _translate[3];
		float _rotate[3];
		
		public:
		Shape()
		{
			_vertices = NULL;
			_normals = NULL;
			_texCoords = NULL;
			_indices = NULL;
		}
		Shape(GLfloat * vertices,GLfloat * normals,GLfloat * texCoords,GLuint * indices)
		{
			_vertices = vertices;
			_normals = normals;
			_texCoords = texCoords;
			_indices = indices;
		}
		
		public:
		GLfloat* getVertices()
		{
			return _vertices;
		}
		GLfloat* getNormals()
		{
			return _normals;
		}
		GLfloat* getTexCoords()
		{
			return _texCoords;
		}
		GLuint* getIndices()
		{
			return _indices;
		}
		GLfloat** getVerticesP()
		{
			return &_vertices;
		}
		GLfloat** getNormalsP()
		{
			return &_normals;
		}
		GLfloat** getTexCoordsP()
		{
			return &_texCoords;
		}
		GLuint** getIndicesP()
		{
			return &_indices;
		}
		ESMatrix& getModelView()
		{
			esScale(&_modelView,_scale[0],_scale[1],_scale[2]);
			esTranslate(&_modelView,_translate[0],_translate[1],_translate[2]);
			esRotate(&_modelView,_angle,_rotate[0],_rotate[1],_rotate[2]);
			return _modelView;
		}
		GLfloat getAngle()
		{
			return _angle;
		}
		GLfloat* getScale()
		{
			return _scale;
		}
		GLfloat* getTranslate()
		{
			return _translate;
		}
		GLfloat* getRotate()
		{
			return _rotate;
		}
		
		public:
		void setScale(GLfloat sx,GLfloat sy,GLfloat sz)
		{
			_scale[0] = sx;
			_scale[1] = sy;
			_scale[2] = sz;
		}
		void setTranslate(GLfloat tx,GLfloat ty,GLfloat tz)
		{
			_translate[0] = tx;
			_translate[1] = ty;
			_translate[2] = tz;
		}
		void setRotate(float angle,GLfloat x,GLfloat y,GLfloat z)
		{
			_angle = angle;
			_rotate[0] = x;
			_rotate[1] = y;
			_rotate[2] = z;
		}
		
	};
	
	class Cube : public Shape
	{
		private:
		float _scale;
		int _numIndices;
		
		public:
		Cube() : Shape()
		{}
		Cube(float scale)
		{
			_scale = scale;
			_numIndices = esGenCube
			              (
			                  _scale,
			                  this->getVerticesP(),
			                  this->getNormalsP(),
			                  this->getTexCoordsP(),
			                  this->getIndicesP()
			              );
		}
		
		public:
		int getIndicesNumber()
		{
			return _numIndices;
		}
	};
	
	class ShaderProgram
	{
		private:
		GLuint _shaderProgram;
		public:
		ShaderProgram()
		{
			_shaderProgram = 0;
		}
		GLuint getShaderProgram()
		{
			return _shaderProgram;
		}
		bool setShaderProgram(GLuint program)
		{
			if(program == 0)
			{
				return false;
			}
			_shaderProgram = program;
			return true;
		}
		bool setShaderProgram(std::string vStr,std::string fStr)
		{
			_shaderProgram = esLoadProgram(vStr.data(),fStr.data());
			if(_shaderProgram == 0)
			{
				return false;
			}
			return true;
		}
		void deleteShaderProgram()
		{
			if(_shaderProgram != 0)
			{
				glDeleteProgram(_shaderProgram);
			}
		}
		void useShaderProgram()
		{
			if(_shaderProgram != 0)
			{
				glUseProgram(_shaderProgram);
			}
		}
		GLint getUniformLocation(std::string name)
		{
			if(_shaderProgram == 0)
			{
				return -1;
			}
			GLint location;
			glGetUniformLocation(_shaderProgram,name.data());
		}
		void setUniformMatrix(std::string name,ESMatrix * mat,GLboolean transpose = GL_FALSE)
		{
			GLint location = this->getUniformLocation(name);
			if(location == -1)
			{
				return;
			}
			glUniformMatrix4fv(location,1,transpose,(GLfloat*)&mat->m[0][0]);
		}
	};
	
	class Object : public ShaderProgram , public Shape
	{
		private:
		GLuint _VAO;
		public:
		Object() : ShaderProgram() , Shape()
		{
			_VAO == 0;
		}
		bool setVAO(GLuint VAO)
		{
			if(VAO == 0)
			{
				return false;
			}
			_VAO = VAO;
			return true;
		}
		void draw(GLenum mode,GLsizei count,GLenum type,const void* ind)
		{
			this->useShaderProgram();
			if(_VAO == 0)
			{
				return;
			}
			glBindVertexArray(_VAO);
			glDrawElements(mode,count,type,ind);
		}
	};
	
	class CubeObject : public Cube , public Object
	{
		public:
		CubeObject() : Cube() , Object()
		{}
		CubeObject(float scale) : Cube(scale) , Object()
		{}
	};
};

namespace ESShapes
{
	struct Shape
	{
		public:
		ESMatrix modelView;
		GLfloat * vertices;
		GLfloat * normals;
		GLfloat * texCoords;
		GLuint * indices;
		
		public:
		Shape()
		{
			vertices = NULL;
			normals = NULL;
			texCoords = NULL;
			indices = NULL;
		}
		Shape(GLfloat * ver,GLfloat * nor,GLfloat * texCoo,GLuint * ind)
		{
			vertices = ver;
			normals = nor;
			texCoords = texCoo;
			indices = ind;
		}
		Shape& setScale(float sx,float sy,float sz)
		{
			esScale(&modelView,sx,sy,sz);
			return *this;
		}
		Shape& setTranslate(float tx,float ty,float tz)
		{
			esTranslate(&modelView,tx,ty,tz);
			return *this;
		}
		Shape& setRotate(float angle,float x,float y,float z)
		{
			esRotate(&modelView,angle,x,y,z);
			return *this;
		}
	};
	struct Shader
	{
		public:
		GLuint shaderProgram;
		
		public:
		Shader()
		{
			shaderProgram = 0;
		}
		Shader(GLuint program)
		{
			this->setProgram(program);
		}
		Shader(std::string vStr,std::string fStr)
		{
			this->setProgram(vStr,fStr);
		}
		bool setProgram(GLuint program)
		{
			if(program == 0)
			{
				return false;
			}
			shaderProgram = program;
			return true;
		}
		bool setProgram(std::string vStr,std::string fStr)
		{
			shaderProgram = esLoadProgram(vStr.data(),fStr.data());
			if(shaderProgram == 0)
			{
				return false;
			}
			return true;
		}
		void deleteProgram()
		{
			if(shaderProgram != 0)
			{
				glDeleteProgram(shaderProgram);
			}
		}
		void useProgram()
		{
			if(shaderProgram != 0)
			{
				glUseProgram(shaderProgram);
			}
		}
		GLint getUniformLocation(std::string name)
		{
			if(shaderProgram == 0)
			{
				return -1;
			}
			GLint location;
			glGetUniformLocation(shaderProgram,name.data());
			return location;
		}
		void setUniformMatrix(std::string name,ESMatrix * mat,GLboolean transpose = GL_FALSE)
		{
			GLint location = this->getUniformLocation(name);
			if(location == -1)
			{
				return;
			}
			glUniformMatrix4fv(location,1,transpose,(GLfloat*)&mat->m[0][0]);
		}
	};
	struct Object : public Shape , public Shader
	{
		public:
		//一堆构造函数...
		Object() : Shape() , Shader() {}
		Object(GLfloat * ver,GLfloat * nor,GLfloat * texCoo,GLuint * ind) :
		Shape(ver,nor,texCoo,ind) , Shader() {}
		Object(GLuint program) : Shape() , Shader(program) {}
		Object(std::string vStr,std::string fStr) : Shape() , Shader(vStr,fStr) {}
		Object(GLuint program,GLfloat * ver,GLfloat * nor,GLfloat * texCoo,GLuint * ind) :
		Shape(ver,nor,texCoo,ind) , Shader(program) {}
		Object(std::string vStr,std::string fStr,GLfloat * ver,GLfloat * nor,GLfloat * texCoo,GLuint * ind) :
		Shape(ver,nor,texCoo,ind) , Shader(vStr,fStr) {}
		Object(GLfloat * ver,GLuint * ind) : Shape(ver,NULL,NULL,ind) , Shader() {}
		Object(GLuint program,GLfloat * ver,GLuint * ind) : Shape(ver,NULL,NULL,ind) , Shader(program) {}
		Object(std::string vStr,std::string fStr,GLfloat * ver,GLuint * ind) : Shape(ver,NULL,NULL,ind) , Shader(vStr,fStr) {}
	};
};

#endif

P.S.

忘记在ESShape::Shape里增加

GLuint VAO;
GLuint VBO;
GLuint EBO;
virtual void draw(GLenum mode,GLsizei count,GLenum type,const void* ind); //绘制函数,和glDrawElements()一样
ESMatrix* multiply(ESMatrix * perspectiveMatrix); //将Shpae::modelView和perspectiveMatrix相乘并返回结果

了…

又是一个P.S.

虽说在c4droid里通过了编译,但是没实际用过,也不知道会出什bug(但愿没有)…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值