LitTexture.cpp

  一晃又好久没发博文了.这个东西就是照亮一个贴了纹理的球球.上次也学到这了,上次是学到HDR的编码部分停下来的.

  最近工作累得不可开交,自己总是有事情做,解决了一个又一个问题,弄得我每天很烦.最近又涉及到了多线程共享资源互斥的问题.用处就是在加载资源的第一步是从文件中加载出待解密的资源,对其进行解密.这一步...做项目各种事情,这周写了个基于引擎的组织资源的工具,可以提高组织效率的东西.上午做人物动画部分,做着做着发觉跟另一位程序员的耦合太大.索性委托他去做了.在一个团队里,要想说话有分量,别人听,还是要自己牛逼啊,牛逼别人也愿意听你的.

  最近不开心的时候很多,似乎还是喜欢单身的生活,我也苛求女朋友太多了.似乎交女朋友让我有点力不从心,确实每天很累.

//	LitTexture.cpp -- 2013/08/22-21:19
#include "stdafx.h"
#include <GLTools.h>
#include <GLMatrixStack.h>
#include <GLFrame.h>
#include <GLFrustum.h>
#include <GLGeometryTransform.h>
#include <StopWatch.h>
#include <math.h>
#define FREEGLUT_STATIC
#include <GL/glut.h>

GLFrame             viewFrame;
GLFrustum           viewFrustum;
GLTriangleBatch     sphereBatch;
GLMatrixStack       modelViewMatrix;
GLMatrixStack       projectionMatrix;
GLGeometryTransform transformPipeline;
GLShaderManager     shaderManager;

GLuint	ADSTextureShader;	
GLint	locAmbient;			
GLint   locDiffuse;			
GLint   locSpecular;		
GLint	locLight;			
GLint	locMVP;				
GLint	locMV;				
GLint	locNM;				
GLint   locTexture;
GLuint  texture;

bool LoadTGATexture(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode)
{
	GLbyte *pBits;
	int nWidth, nHeight, nComponents;
	GLenum eFormat;

	// Read the texture bits
	pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat);
	if(pBits == NULL) 
		return false;

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0,
		eFormat, GL_UNSIGNED_BYTE, pBits);

	free(pBits);

	if(minFilter == GL_LINEAR_MIPMAP_LINEAR || 
		minFilter == GL_LINEAR_MIPMAP_NEAREST ||
		minFilter == GL_NEAREST_MIPMAP_LINEAR ||
		minFilter == GL_NEAREST_MIPMAP_NEAREST)
		glGenerateMipmap(GL_TEXTURE_2D);

	return true;
}

void SetupRC(void)
{
	glClearColor(0, 0, 0, 1) ;

	glEnable(GL_DEPTH_TEST) ;
	glEnable(GL_CULL_FACE) ;

	shaderManager.InitializeStockShaders();
	viewFrame.MoveForward(4.0f) ;

	gltMakeSphere(sphereBatch, 1.0, 26, 13) ;

	ADSTextureShader = gltLoadShaderPairWithAttributes("ADSTexture.vp", "ADSTexture.fp", 3, GLT_ATTRIBUTE_VERTEX, "vVertex",
		GLT_ATTRIBUTE_NORMAL, "vNormal", GLT_ATTRIBUTE_TEXTURE0, "vTexture0");

	locAmbient = glGetUniformLocation(ADSTextureShader, "ambientColor");
	locDiffuse = glGetUniformLocation(ADSTextureShader, "diffuseColor");
	locSpecular = glGetUniformLocation(ADSTextureShader, "specularColor");
	locLight = glGetUniformLocation(ADSTextureShader, "vLightPosition");
	locMVP = glGetUniformLocation(ADSTextureShader, "mvpMatrix");
	locMV  = glGetUniformLocation(ADSTextureShader, "mvMatrix");
	locNM  = glGetUniformLocation(ADSTextureShader, "normalMatrix");
	locTexture = glGetUniformLocation(ADSTextureShader, "colorMap");

	glGenTextures(1, &texture) ;
	glBindTexture(GL_TEXTURE_2D, texture) ;
	LoadTGATexture("CoolTexture.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE) ;
}

void ShutdownRC(void)
{

	glDeleteTextures(1, &texture) ;
}

void RenderScene(void)
{
	static CStopWatch rotTimer ;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	modelViewMatrix.PushMatrix(viewFrame);
	modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() * 10.0f, 0.0f, 1.0f, 0.0f);

	GLfloat vEyeLight[] = { -100.0f, 100.0f, 100.0f };
	GLfloat vAmbientColor[] = { 0.2f, 0.2f, 0.2f, 1.0f };
	GLfloat vDiffuseColor[] = { 1.0f, 1.0f, 1.0f, 1.0f};
	GLfloat vSpecularColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };

	glBindTexture(GL_TEXTURE_2D, texture);
	glUseProgram(ADSTextureShader);
	glUniform4fv(locAmbient, 1, vAmbientColor);
	glUniform4fv(locDiffuse, 1, vDiffuseColor);
	glUniform4fv(locSpecular, 1, vSpecularColor);
	glUniform3fv(locLight, 1, vEyeLight);
	glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()) ;
	glUniformMatrix4fv(locMV, 1, GL_FALSE, transformPipeline.GetModelViewMatrix()) ;
	glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeline.GetNormalMatrix()) ;
	glUniform1i(locTexture, 0) ;

	sphereBatch.Draw() ;

	modelViewMatrix.PopMatrix() ;

	glutSwapBuffers() ;
	glutPostRedisplay() ;
}

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

	glViewport(0, 0, w, h) ;

	viewFrustum.SetPerspective(35.0f, float(w)/h, 1, 100.0f) ;

	projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()) ;
	transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix) ;
}

int main(int argc, char* argv[])
{
	gltSetWorkingDirectory(argv[0]);

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Lit Texture");
	glutReshapeFunc(ChangeSize);
	glutDisplayFunc(RenderScene);

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

	SetupRC();    
	glutMainLoop();
	ShutdownRC();
	return 0;
}


 

你可能感兴趣的:(LitTexture.cpp)