程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> GLSL透視矩陣(C語言)

GLSL透視矩陣(C語言)

編輯:關於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 
#include 
#include 
#include "readtext.h"
#include "vmath.h"

#ifdef _APPLE_
#include 
#else
#define FREEGLUT_STATIC
#include 
#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可至我的資源下載

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved