码迷,mamicode.com
首页 > 其他好文 > 详细

OpenGL蓝宝书第七章:立体天空和纹理折射、双纹理(下)

时间:2018-03-18 10:26:21      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:transform   物理   书籍   parameter   gen   com   bind   errors   etc   

对照了蓝宝书,才知道红宝书的长处。

reflect函数的原理在红宝书中有说明,仅仅有对照了红宝书,才知道红宝书的定位:高级工具书。

蓝宝书作为入门级书籍,以较快的速度让读者敲到代码去思考,总遗留了须要探索的内容。


蓝宝书要看完。

红宝书要看完。

C++11 标准版英文版也看了小部分。

多线程入门都不算。

Socket仅仅能实现基础网络连接,离真正的数据通信都算,何况多人在线对战网络模型的实现

设计模式看了又忘了,忘了又看。如果没有机会去设计模块级别的代码实现,真的血了没用,难道改UI?加班赶工哪有魄力略微幅度大点调整代码。可惜当时没预算到需求的演化须要我去用策略模式或者装饰者模式去调整代码结构。

工厂模式:封装了类对象的生成过程。同意不同參数获取不用对象

单例模式:控制模块间的数据訪问出口,减低模块之间的耦合

策略模式:也能够封装不用数据表现出统一的行为,提供了相似于单例的使用方法,减低耦合。


当时无心写UI,大部分时间用来实现技能和AI,谁知UI的需求演化成为压死我的最后一根稻草——由于主管们一致觉得可展示的UI具有最高的优先权,或者说当时主程仅仅希望我能完整实现UI的大大小小需求,给一个看得到的东西给我,但是,新代码一次性让我接触到3大模块。我最不喜欢偏偏是UI。

加上内部一些异动,我没有适当地争取,主程又给我添加UI任务。对啊。不讨人喜欢的下场。一堆UI给你做,看你怎么进步?

至少我今天搞定了图形渲染的入门,明确了面对需求,对程序的修改是通过添加新代码进行的,而不是更改现有的代码。


要意识到变化性往往比沟通中被有意识忽略的多多了。

无意义的加班是不可能有这些进步的。

尽管我走了之后你们的奖金池多了。但是主程能让人我的前任和我走了,你们之后就能让其它优秀队友走。

我选的这条路尽管辛苦。看着轻浮。仅仅要熬过了之后我就能成为大神。


win32或其它操作系统文件头中的时间戳还没找到,怎么实现同文件副本的MD5正确校验?如果找到了,文件更新列表的加减能够由生成本地列表和获取更新列表对照差异得到,调用下载模块,覆盖或者填充在本地资源路径下。


导航网格内容中,至今没能理解三角形生成原理,人家的博士论文,想在1个月内看懂,可行性非常低啊


ORGE没碰、Unreal Engine 4.0 啃不下、Cocos 3D、Gameplay3D,怎样深化?


至于IK,还非常远,数学、物理基础知识都要补呢。


//vp

#version 330

in vec4 vVertex;
in vec3 vNormal;
in vec2 vTexCoords;

uniform mat4 mvpMatrix;
uniform mat4 mvMatrix;
uniform mat3 normalMatrix;
uniform mat4 mInverseCamera;

smooth out vec3 vVaryingTexCoord;
smooth out vec2 vTarnishCoords;


void main(void)
{
//在惯性系的坐标
	vec3 vEyeNormal = normalMatrix * vNormal;
	
	vec4 vPosition4 = mvMatrix * vVertex;
	vec3 vEyeVertex = normalize(vPosition4.xyz/vPosition4.w);
	
	vec4 cCoords = vec4(reflect(vEyeVertex,vEyeNormal),1.0f);//不清楚reflect的原理,导致代码思想断片,靠猜?开玩笑,还是开源比較好
	cCoords = mInverseCamera * cCoords;//全面反射场景???
	
	vVaryingTexCoord.xyz = normalize(cCoords.xyz); //获得是什么坐标?
	
	vTarnishCoords = vTexCoords.st;
	
	gl_Position = mvpMatrix * vVertex;
}


//fp

#version 330

smooth in vec3 vVaryingTexCoord;
smooth in vec2 vTarnishCoords;

uniform samplerCube cubeMap;
uniform sampler2D tarnishMap;

out vec4 vFragColor;

void main(void)
{
	vFragColor = texture(cubeMap, vVaryingTexCoord.stp);
	//两段纹理。仅仅须要颜色相乘就能够了。后面的纹理为最外层色彩
	vFragColor *= texture(tarnishMap, vTarnishCoords.st);
}



//cpp

#include <GLTools.h>
#include <GLMatrixStack.h>
#include <GLFrame.h>
#include <GLFrustum.h>
#include <GLGeometryTransform.h>

#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif

GLFrame viewFrame;
GLFrustum viewFrustum;
GLMatrixStack projctionMatrix;
GLMatrixStack mvpMatrix;
GLMatrixStack modelViewMatrix;
GLGeometryTransform transformPipeLine;

GLTriangleBatch sphereBatch;
GLBatch cubeBatch;

GLuint tarnishTexture;
GLuint cubeTexture;
GLuint reflectionShader;
GLuint skyBoxShader;

GLint locMVPReflect;
GLint locMVReflect;
GLint locNormalReflect;
GLint locInvertedCamera;

GLint locCubeMap, locTarnishMap;

GLint locMVPSkyBox;

// Six sides of a cube map
const char *szCubeFaces[6] = { "pos_x.tga", "neg_x.tga", "pos_y.tga", "neg_y.tga", "pos_z.tga", "neg_z.tga" };

GLenum cube[6] = {
	GL_TEXTURE_CUBE_MAP_POSITIVE_X,
	GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
	GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
	GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
	GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
	GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
};

void ChangeSize(int w, int h)
{
	if (h <= 0)
	{
		h = 1;
	}

	glViewport(0, 0, w, h);
	viewFrustum.SetPerspective(80.0f, float(w) / float(h), 1.0f, 120.0f);
	projctionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
	transformPipeLine.SetMatrixStacks(modelViewMatrix, projctionMatrix);
}


void SetupRC(void)
{
	GLbyte * pBytes;
	int nWidth, nHeight, nComponent;
	GLenum eFormat;

	glCullFace(GL_BACK);
	glEnable(GL_DEPTH_TEST);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	// Load the tarnish texture
	glGenTextures(1, &tarnishTexture);
	glBindTexture(GL_TEXTURE_2D, tarnishTexture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	pBytes = gltReadTGABits("tarnish.tga", &nWidth, &nHeight, &nComponent, &eFormat);
	glTexImage2D(GL_TEXTURE_2D, 0, nComponent, nWidth, nHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
	free(pBytes);
	glGenerateMipmap(GL_TEXTURE_2D);

	glGenTextures(1, &cubeTexture);
	glBindTexture(GL_TEXTURE_CUBE_MAP, cubeTexture);

	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

	

	for (size_t i = 0; i < 6; ++i)
	{
		pBytes = gltReadTGABits(szCubeFaces[i], &nWidth, &nHeight, &nComponent, &eFormat);
		// border 为0,是指边框的宽度
		//eFormat 不须要 和internalFormat取值必须同样。都是指定纹理中的颜色组件
		glTexImage2D(cube[i], 0, nComponent, nWidth, nHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
		free(pBytes);
	}
	glGenerateMipmap(GL_TEXTURE_CUBE_MAP);

	viewFrame.MoveForward(-4.0f);
	gltMakeSphere(sphereBatch, 1.0f, 52, 26);
	gltMakeCube(cubeBatch, 20.0f);

	reflectionShader = gltLoadShaderPairWithAttributes("Reflection.vp", "Reflection.fp", 3, 
		GLT_ATTRIBUTE_VERTEX, "vVertex", 
		GLT_ATTRIBUTE_NORMAL, "vNormal", 
		GLT_ATTRIBUTE_TEXTURE0, "vTexCoords");
	//uniform值绑定
	locMVPReflect = glGetUniformLocation(reflectionShader, "mvpMatrix");
	locMVReflect = glGetUniformLocation(reflectionShader, "mvMatrix");
	locNormalReflect = glGetUniformLocation(reflectionShader, "normalMatrix");
	locInvertedCamera = glGetUniformLocation(reflectionShader, "mInverseCamera");

	locCubeMap = glGetUniformLocation(reflectionShader, "cubeMap");
	locTarnishMap = glGetUniformLocation(reflectionShader, "tarnishMap");

	skyBoxShader = gltLoadShaderPairWithAttributes("skyBox.vp", "skyBox.fp", 1, GLT_ATTRIBUTE_VERTEX, "vVertex");
	//uniform值绑定

	locMVPSkyBox = glGetUniformLocation(skyBoxShader, "mvpMatrix");


	//启用两段纹理 与顶点着色器输入输出的纹理数组相应
	// glUniform1i(locCubeMap, 0);
	// glUniform1i(locTarnishMap, 1);
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, tarnishTexture);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_CUBE_MAP, cubeTexture);
}

void RenderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	M3DMatrix44f mCamera;
	M3DMatrix44f mCameraRotOnly;
	M3DMatrix44f mInverseCamera;

	viewFrame.GetCameraMatrix(mCamera, false);
	viewFrame.GetCameraMatrix(mCameraRotOnly, true);
	m3dInvertMatrix44(mInverseCamera, mCameraRotOnly);

	modelViewMatrix.PushMatrix();
	{
		
		modelViewMatrix.MultMatrix(mCamera);//向后移动 4.0f,惯性坐标系的向后则是z变小,模型变小
		glUseProgram(reflectionShader);

		glUniformMatrix4fv(locMVReflect, 1, GL_FALSE, transformPipeLine.GetModelViewMatrix());
		glUniformMatrix4fv(locMVPReflect, 1, GL_FALSE, transformPipeLine.GetModelViewProjectionMatrix());
		glUniformMatrix3fv(locNormalReflect, 1, GL_FALSE, transformPipeLine.GetNormalMatrix());
		glUniformMatrix4fv(locInvertedCamera, 1, GL_FALSE, mInverseCamera);

		glUniform1i(locCubeMap, 0);
		glUniform1i(locTarnishMap, 1);

		glEnable(GL_CULL_FACE);
		sphereBatch.Draw();
		glDisable(GL_CULL_FACE);
	}
	modelViewMatrix.PopMatrix();

	modelViewMatrix.PushMatrix();
	{
		modelViewMatrix.MultMatrix(mCameraRotOnly);
		glUseProgram(skyBoxShader);
		glUniformMatrix4fv(locMVPSkyBox, 1, GL_FALSE, transformPipeLine.GetModelViewProjectionMatrix());
		cubeBatch.Draw();
	}
	modelViewMatrix.PopMatrix();

	glutSwapBuffers();
}

void SpecialKeys(int key, int x, int y)
{
	float linear = 1.0f;
	float angular = float(m3dDegToRad(5.0f));

	if (key == GLUT_KEY_UP)
		viewFrame.MoveForward(linear);

	if (key == GLUT_KEY_DOWN)
		viewFrame.MoveForward(-linear);

	if (key == GLUT_KEY_LEFT)
		viewFrame.RotateWorld(angular, 0.0f, 1.0f, 0.0f);

	if (key == GLUT_KEY_RIGHT)
		viewFrame.RotateWorld(-angular, 0.0f, 1.0f, 0.0f);

	glutPostRedisplay();
}

void ShutdownRC(void)
{
	glDeleteTextures(1, &tarnishTexture);
	glDeleteTextures(1, &cubeTexture);
	glDeleteProgram(reflectionShader);
	glDeleteProgram(skyBoxShader);
}

int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(800, 600);
	glutCreateWindow("OpenGL Cube Maps");
	glutReshapeFunc(ChangeSize);
	glutDisplayFunc(RenderScene);
	glutSpecialFunc(SpecialKeys);

	GLenum err = glewInit();
	if (GLEW_OK != err) {
		fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
		return 1;
	}


	SetupRC();

	glutMainLoop();

	ShutdownRC();

	return 0;
}


OpenGL蓝宝书第七章:立体天空和纹理折射、双纹理(下)

标签:transform   物理   书籍   parameter   gen   com   bind   errors   etc   

原文地址:https://www.cnblogs.com/zhchoutai/p/8594158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!