GLSL透视矩阵(C语言)

perspective.vert

#version 330

layout(location =0) in vec4 vertexPosition;
layout(location =1) in vec4 vertexColor;

smooth out vec4 theColor;

uniform vec2 offset;
uniform mat4 perspectiveMatrix;

void main()
{
	vec4 cameraPosition =vertexPosition +vec4(offset.x, offset.y, 0.0, 0.0);

	gl_Position =perspectiveMatrix *cameraPosition;
	theColor =vertexColor;
}

perspective.frag

#version 330

smooth in vec4 theColor;

out vec4 outputColor;

void main()
{
	outputColor =theColor;
}

perspective.c

////////////////////////////////////////////////////////////////////
///透视版本二:矩阵实现。
//调用库vmath
//作者:青丝成霜
/////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include "readtext.h"
#include "vmath.h"

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

#pragma comment(lib, "readtext.lib")
#pragma comment(lib, "vmath.lib")

int i;
GLuint vShader, fShader, programHandle;

GLint  vaoHandle, vboHandles[2], positionBufferHandle, colorBufferHandle;

Matrix44f M;   //投影矩阵

GLint offsetUniformLocation;
GLint perspectiveMatrixUniformLocation;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
int infoLogLength =0;
int charWritten =0;
char *infoLog;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
float positionData[] ={ 
	0.25f,  0.25f, -1.25f, 1.0f,
	 0.25f, -0.25f, -1.25f, 1.0f,
	-0.25f,  0.25f, -1.25f, 1.0f,

	 0.25f, -0.25f, -1.25f, 1.0f,
	-0.25f, -0.25f, -1.25f, 1.0f,
	-0.25f,  0.25f, -1.25f, 1.0f,

	 0.25f,  0.25f, -2.75f, 1.0f,
	-0.25f,  0.25f, -2.75f, 1.0f,
	 0.25f, -0.25f, -2.75f, 1.0f,

	 0.25f, -0.25f, -2.75f, 1.0f,
	-0.25f,  0.25f, -2.75f, 1.0f,
	-0.25f, -0.25f, -2.75f, 1.0f,

	-0.25f,  0.25f, -1.25f, 1.0f,
	-0.25f, -0.25f, -1.25f, 1.0f,
	-0.25f, -0.25f, -2.75f, 1.0f,

	-0.25f,  0.25f, -1.25f, 1.0f,
	-0.25f, -0.25f, -2.75f, 1.0f,
	-0.25f,  0.25f, -2.75f, 1.0f,

	 0.25f,  0.25f, -1.25f, 1.0f,
	 0.25f, -0.25f, -2.75f, 1.0f,
	 0.25f, -0.25f, -1.25f, 1.0f,

	 0.25f,  0.25f, -1.25f, 1.0f,
	 0.25f,  0.25f, -2.75f, 1.0f,
	 0.25f, -0.25f, -2.75f, 1.0f,

	 0.25f,  0.25f, -2.75f, 1.0f,
	 0.25f,  0.25f, -1.25f, 1.0f,
	-0.25f,  0.25f, -1.25f, 1.0f,

	 0.25f,  0.25f, -2.75f, 1.0f,
	-0.25f,  0.25f, -1.25f, 1.0f,
	-0.25f,  0.25f, -2.75f, 1.0f,

	 0.25f, -0.25f, -2.75f, 1.0f,
	-0.25f, -0.25f, -1.25f, 1.0f,
	 0.25f, -0.25f, -1.25f, 1.0f,

	 0.25f, -0.25f, -2.75f, 1.0f,
	-0.25f, -0.25f, -2.75f, 1.0f,
	-0.25f, -0.25f, -1.25f, 1.0f};

float colorData[] ={
	0.0f, 0.0f, 1.0f, 1.0f,
	0.0f, 0.0f, 1.0f, 1.0f,
	0.0f, 0.0f, 1.0f, 1.0f,

	0.0f, 0.0f, 1.0f, 1.0f,
	0.0f, 0.0f, 1.0f, 1.0f,
	0.0f, 0.0f, 1.0f, 1.0f,

	0.8f, 0.8f, 0.8f, 1.0f,
	0.8f, 0.8f, 0.8f, 1.0f,
	0.8f, 0.8f, 0.8f, 1.0f,

	0.8f, 0.8f, 0.8f, 1.0f,
	0.8f, 0.8f, 0.8f, 1.0f,
	0.8f, 0.8f, 0.8f, 1.0f,

	0.0f, 1.0f, 0.0f, 1.0f,
	0.0f, 1.0f, 0.0f, 1.0f,
	0.0f, 1.0f, 0.0f, 1.0f,

	0.0f, 1.0f, 0.0f, 1.0f,
	0.0f, 1.0f, 0.0f, 1.0f,
	0.0f, 1.0f, 0.0f, 1.0f,

	0.5f, 0.5f, 0.0f, 1.0f,
	0.5f, 0.5f, 0.0f, 1.0f,
	0.5f, 0.5f, 0.0f, 1.0f,

	0.5f, 0.5f, 0.0f, 1.0f,
	0.5f, 0.5f, 0.0f, 1.0f,
	0.5f, 0.5f, 0.0f, 1.0f,

	1.0f, 0.0f, 0.0f, 1.0f,
	1.0f, 0.0f, 0.0f, 1.0f,
	1.0f, 0.0f, 0.0f, 1.0f,

	1.0f, 0.0f, 0.0f, 1.0f,
	1.0f, 0.0f, 0.0f, 1.0f,
	1.0f, 0.0f, 0.0f, 1.0f,

	0.0f, 1.0f, 1.0f, 1.0f,
	0.0f, 1.0f, 1.0f, 1.0f,
	0.0f, 1.0f, 1.0f, 1.0f,

	0.0f, 1.0f, 1.0f, 1.0f,
	0.0f, 1.0f, 1.0f, 1.0f,
	0.0f, 1.0f, 1.0f, 1.0f};

////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
void SetupShader()
{
	char *renderer;
	char *vender;
	char *version;
	char *glslVersion;
	//////////////////////
	char *vs, *fs;
	char *vv, *ff;
	//////////////////////
	renderer =glGetString(GL_RENDERER);
	vender =glGetString(GL_VENDOR);
	version =glGetString(GL_VERSION);
	glslVersion =glGetString(GL_SHADING_LANGUAGE_VERSION);

	printf("*****************************************************\n");
	printf("graphic card:\t%s \n", renderer);
	printf("graphic company:\t%s \n", vender);
	printf("openGL version:\t%s \n", version);
	printf("GLSL version:\t %s \n", glslVersion);
	printf("*****************************************************\n");
	///////////////////////////////////////////////////////////////////////
	vShader =glCreateShader(GL_VERTEX_SHADER);

	vs =readText("shader/perspective.vert");
	//printf("%s\n",vs);
	vv =vs;

	if(vShader ==0)
	{
		printf("Error: fail to create shader!");
		exit(1);
	}

	glShaderSource(vShader, 1, &vv, NULL);
	glCompileShader(vShader);
	free(vs);

	glGetShaderiv(vShader, GL_INFO_LOG_LENGTH, &infoLogLength);
	if(infoLogLength >0)
	{
		infoLog =(char *)malloc(infoLogLength);
		glGetShaderInfoLog(vShader, infoLogLength, &charWritten, infoLog);
		printf("%s\n",infoLog);
		printf("********************************************************************\n");
		free(infoLog);
	}
	///////////////////////////////////////////////////////////////////////////
	fShader =glCreateShader(GL_FRAGMENT_SHADER);
	if(fShader ==0)
	{
		printf("can‘t create fragment shader! \n");
		exit(1);
	}
    
	fs =readText("shader/perspective.frag");
    //printf("%s \n",fs);
	ff= fs;
	glShaderSource(fShader, 1, &ff, NULL);
	glCompileShader(fShader);
	free(fs);

	glGetShaderiv(fShader, GL_INFO_LOG_LENGTH, &infoLogLength);

	if(infoLogLength >0)
	{
		infoLog =(char *)malloc(infoLogLength);
		glGetShaderInfoLog(fShader, infoLogLength, &charWritten, infoLog);
		////////charWritten is 
		printf("%s \n",infoLog);
		printf("*********************************************************\n");
		free(infoLog);
	}
	///////////////////////////////////////////////
	////////////////////////////////////////
	programHandle =glCreateProgram();

	glAttachShader(programHandle, vShader);
	glAttachShader(programHandle, fShader);

	glLinkProgram(programHandle);
	glUseProgram(programHandle);
	
	glGetProgramiv(programHandle, GL_INFO_LOG_LENGTH, &infoLogLength);
	if(infoLogLength >0)
	{
		infoLog =(char *)malloc(infoLogLength);
		glGetProgramInfoLog(programHandle, infoLogLength, &charWritten, infoLog);
		printf("%s \n", infoLog);
		printf("*********************************************************\n");
		free(infoLog);
	}	

}


void InitVBO()
{
	glGenBuffers(2, vboHandles);
	positionBufferHandle =vboHandles[0];
	colorBufferHandle =vboHandles[1];
	///////////////////////////////////////////
	glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
	glBufferData(GL_ARRAY_BUFFER, sizeof(positionData), positionData, GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
	glBufferData(GL_ARRAY_BUFFER, sizeof(colorData), colorData, GL_STATIC_DRAW);
	/////////////////////////////////////////
	glGenVertexArrays(1, &vaoHandle);
	glBindVertexArray(vaoHandle);
	//////////////////////////////////////
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);

	glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
	glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);

	glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
	glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, NULL);
}

////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void SetupRC()
{
	glewInit();

	SetupShader();

	InitVBO();

	glClearColor(1.0, 0.0, 0.0, 1.0);

	////////////////////////////////////////
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
	glFrontFace(GL_CW);
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//glGet**Location()系列函数需在glLinkProgram之后使用,切记!
//浮点值在shaderzhong 不会自己转换,需注意写法。
//
////////////////////////////////////////////

void RenderScene()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glUseProgram(programHandle);

	offsetUniformLocation =glGetUniformLocation(programHandle, "offset");
	perspectiveMatrixUniformLocation =glGetUniformLocation(programHandle, "perspectiveMatrix");

	perspectiveFrustum( M, -1.0, 1.0, 1.0, -1.0, 1.0, 3.0);  //计算矩阵M
	
	
	glUniform2f(offsetUniformLocation, 0.5, 0.5);  //此处改为0.0, 0.0则仅显示一正方形,看不到透视效果。
	glUniformMatrix4fv(perspectiveMatrixUniformLocation, 1, GL_TRUE, M);

	glBindVertexArray(vaoHandle);
	glDrawArrays(GL_TRIANGLES, 0, 36);
	glBindVertexArray(0);

	glutSwapBuffers();
	glutPostRedisplay();
}



void keyBoard(unsigned char key, int x, int y)
{
	if(key ==GLUT_KEY_F1)
		exit(0);
}




int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE |GLUT_RGBA |GLUT_DEPTH);

	glutInitWindowSize(800, 600);
	glutInitWindowPosition(0, 0);
	glutCreateWindow("fourth :perspective ");

	SetupRC();
	glutDisplayFunc(RenderScene);

	glutSpecialFunc(keyBoard);
	glutMainLoop();
	return 0;
}














vmath可至我的资源下载

GLSL透视矩阵(C语言)

上一篇:05. WebApp2.0时代启程:倒立者赢,从过去到现在的变化,看将来的发展(一)


下一篇:python,flask,login,login_request