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

OpenGL一维纹理映射练习

时间:2015-07-13 16:22:05      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:opengl

算是抛开书本自己敲了一个完整的渲染代码。

用漫射光强度在一维纹理中索引纹理颜色。所以从顶点转配之后传给片段着色器的是漫射光强度值即可。过这章之后可以考虑着手glsl方向。

可是导航网格的实现还没搞定,我该如何是好?总之,避免写UI,避免接入无休止的SDK。时至今日,终于明白昔日的老大为何说只在固定的时间发版。因为各种版本的发布耗时耗力缺不能提高技术,能少则免。


要么是在行业中制定SDK协议标准,所有按照该协议标准编写的SDK和被嵌入软件均可实现自动化或一键接入。这么理想化的工作只能在有行业号召力的公司实现,比如触控的anySDK,但是不开源导致接受度低啊。



//vp

#version 330

in vec4 vVertex;
in vec3 vNormal;

uniform mat4 mvMatrix;
uniform mat4 mvpMatrix;
uniform mat3 normalMatrix;
uniform vec3 vLightPosition;

smooth out float textureCoordinate;

void main(void)
{
//获得表面法线的视觉坐标
	vec3 vEyeNormal = normalMatrix * vNormal;
	
	//获得顶点的视觉坐标,即世界坐标
	vec4 vPosition4 = mvMatrix * vVertex;
	vec3 vPosition3 = vPosition4.xyz/vPosition4.w;
	
	vec3 vLightDir = normalize(vLightPosition - vPosition3);

	textureCoordinate = max(0.0f,dot(vLightDir,vEyeNormal));
	
	gl_Position = mvpMatrix * vVertex;
}


//fp

#version 330
smooth in float textureCoordinate;

uniform sampler1D colorTable;

out vec4 vFragColor;

void main(void)
{
	vFragColor = texture(colorTable,textureCoordinate);
	
}




//cpp

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


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

GLMatrixStack modelViewMatrix;
GLMatrixStack mvpMatrix;
GLMatrixStack projctionMatrix;

GLFrustum viewFrustum;
GLFrame viewFrame;

GLGeometryTransform transformPipeLine;
GLTriangleBatch torusBatch;

GLuint toonShader;
GLuint uiTexture;

GLint locMV;
GLint locMVP;
GLint locNM;
GLint locLP;
GLint locColorTable;

static  GLfloat vEyeLight[] = { -100.0f, 100.0f, 100.0f };

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

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

}

void SetupRC(void)
{
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	glEnable(GL_DEPTH_TEST);

	viewFrame.MoveForward(7.0f);

	gltMakeTorus(torusBatch, 0.8f, 0.25f, 52, 26);

	toonShader = gltLoadShaderPairWithAttributes("toonShader.vp", "toonShader.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal");

	locMV = glGetUniformLocation(toonShader, "mvMatrix");
	locMVP = glGetUniformLocation(toonShader, "mvpMatrix");
	locNM = glGetUniformLocation(toonShader, "normalMatrix");
	locLP = glGetUniformLocation(toonShader, "vLightPosition"); 
	locColorTable = glGetUniformLocation(toonShader, "colorTable");

	glGenTextures(1, &uiTexture);
	glBindTexture(GL_TEXTURE_1D, uiTexture);

	GLubyte textureData[4][3] = {
		32, 0, 0,
		64, 0, 0,
		128, 0, 0,
		255, 0, 0
	};

	glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 4, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData);
	//todo

	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

}

void RenderScene(void)
{
	static CStopWatch rotTimer;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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

		glUseProgram(toonShader);

		glUniformMatrix4fv(locMV, 1, GL_FALSE, transformPipeLine.GetModelViewMatrix());
		glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeLine.GetModelViewProjectionMatrix());
		glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeLine.GetNormalMatrix());
		glUniform3fv(locLP, 1, vEyeLight);

		glUniform1i(locColorTable, 0);

		torusBatch.Draw();
	}
	modelViewMatrix.PopMatrix();

	glutSwapBuffers();

	glutPostRedisplay();
}

void ShutdownRC(void)
{

}

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

	glutInit(&argc, argv);
	glutInitDisplayMode(GL_DOUBLE | GL_DEPTH | GL_STENCIL);
	glutInitWindowSize(800, 600);
	glutCreateWindow("ToonShader Jingz");
	glutReshapeFunc(ChangeSize);
	glutDisplayFunc(RenderScene);

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

	SetupRC();

	glutMainLoop();

	ShutdownRC();

	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

OpenGL一维纹理映射练习

标签:opengl

原文地址:http://blog.csdn.net/jingzhewangzi/article/details/46861575

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