OpenGL学习笔记三(旋转带纹理的花托,球体,地板,点光源)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define FREEGLUT_STATIC
#include 

#define SPHERE_NUM 30

GLFrame cameraFrame;
GLMatrixStack modelViewStack;
GLMatrixStack projectionStack;
GLGeometryTransform transformPipeLine;
GLShaderManager shaderManager;
GLFrustum viewFrustum;
GLFrame spheres[SPHERE_NUM];

GLBatch batch_floor;
GLTriangleBatch batch_torus;
GLTriangleBatch batch_sphere;

GLfloat vWhite[] = {1.0f, 1.0f, 1.0f, 0.75f};
const char *texFileName[] = {"Marble.tga", "Marslike.tga", "MoonLike.tga"};
GLuint texIdArray[3] = {0};

//加载纹理并设置相关参数
bool LoadTgaTexture(const char *fileName, GLenum filterParam, GLenum wrapParam)
{
	GLbyte *pbits = NULL;
	GLint width, height, components;
	GLenum eFormat;
	//从tga文件获取纹理图像的地址和相关参数
	pbits = gltReadTGABits(fileName, &width, &height, &components, &eFormat);
	if (NULL == pbits)
	{
		return false;
	}
	//设置缩放算法
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterParam);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterParam);
	//设置纹理边界外的填充方法
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
	//根据获取的相关参数创建2D纹理
	glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, eFormat, GL_UNSIGNED_BYTE, pbits);
	free(pbits);
	return true;
}

bool SetupRC()
{
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	shaderManager.InitializeStockShaders();
	//开启深度测试
	glEnable(GL_DEPTH_TEST);
	//剔除背面渲染
	glEnable(GL_CULL_FACE);
	//生成3个纹理ID
	glGenTextures(3, texIdArray);

	glBindTexture(GL_TEXTURE_2D, texIdArray[0]);
	if (!LoadTgaTexture(texFileName[0], GL_LINEAR, GL_REPEAT))
	{
		return false;
	}
	glBindTexture(GL_TEXTURE_2D, texIdArray[1]);
	if (!LoadTgaTexture(texFileName[1], GL_LINEAR, GL_CLAMP_TO_EDGE))
	{
		return false;
	}
	glBindTexture(GL_TEXTURE_2D, texIdArray[2]);
	if (!LoadTgaTexture(texFileName[2], GL_LINEAR, GL_CLAMP_TO_EDGE))
	{
		return false;
	}
	//纹理坐标和顶点坐标的加载顺序不能反过来
	batch_floor.Begin(GL_TRIANGLE_FAN, 4, 1);
	batch_floor.MultiTexCoord2f(0, 0.0f, 0.0f);
	batch_floor.Vertex3f(-20.0f, -0.4f, 20.0f);

	batch_floor.MultiTexCoord2f(0, 10.0f, 0.0f);
	batch_floor.Vertex3f(20.0f, -0.4f, 20.0f);

	batch_floor.MultiTexCoord2f(0, 10.0f, 10.0f);
	batch_floor.Vertex3f(20.0f, -0.4f, -20.0f);
	
	batch_floor.MultiTexCoord2f(0, 0.0f, 10.0f);
	batch_floor.Vertex3f(-20.0f, -0.4f, -20.0f);
	batch_floor.End();

	//初始化花托的顶点批次
	gltMakeTorus(batch_torus, 0.4f, 0.15f, 40, 20);
	//初始化球体的顶点批次
	gltMakeSphere(batch_sphere, 0.1f, 26, 13);
	//初始化X-Z平面零散球体的坐标
	for (int n=0; n

匹配的纹理打包文件可以在此处下载https://download.csdn.net/download/gk_2014/10643475
这个示例基于《OpenGLchao超级宝典(第5版)》这本书

你可能感兴趣的:(OpenGL)