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

GLSL變換顏色的三角形(C語言)

編輯:關於C
 
#include 
#include 
#include 
#include readtext.h

#ifdef _APPLE_
#include 
#else
#define FREEGLUT_STATIC
#include 
#endif

#pragma  comment(lib, readtext.lib)

GLuint vShader, fShader, programHandle;

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

GLuint elapseTimeUniformLocation, loopDurationLocation, fragLoopDurationLocation;
GLint positionLocation, colorLocation;

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

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
float positionData[]={  0.8f, 0.0f, 0.0f, 
                       -0.8f, 0.0f, 0.0f, 
					    0.0f, 0.8f, 0.0f};
//////////////////////////////////////////
//float colorData[]= { 1.0, 0.0, 0.0,
//                     0.0, 1.0, 0.0,
//					   0.0, 0.0, 1.0};
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SetupShader()
{
	char *renderer;
	char *vender;
	char *version;
	char *glslVersion;

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

	printf(graphic card:	%s 
, renderer);
	printf(graphic company:	%s 
, vender);
	printf(openGL version:	%s 
, version);
	printf(GLSL version:	 %s 
, glslVersion);
	printf(*****************************************************
);
	//////////////////////////////////////////////////////////////
    //txt =readText(2.txt);
	//printf(%s
,txt);
	//putchar(10);
	vShader =glCreateShader(GL_VERTEX_SHADER);

	vs =readText(shader/triangle.vert);
	//printf(%s
,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
, infoLog);
		printf(**********************************************************
);
		free(infoLog);
	}

	/////////////////////////////////////////////////////////////////////////////////
	fShader =glCreateShader(GL_FRAGMENT_SHADER);
	if(fShader ==0)
	{
		printf(can't create fragment shader! 
);
		exit(1);
	}
    
	fs =readText(shader/triangle.frag);
    //printf(%s 
,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 
,infoLog);
		printf(***********************************************************
);
		free(infoLog);
	}
	////////////////////////////////////
	//////////////////////////////////////
	programHandle =glCreateProgram();

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

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

	glUseProgram(programHandle);
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

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, 3, GL_FLOAT, GL_FALSE, 0, NULL);

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

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

	SetupShader();

	InitVBO();

	glClearColor(1.0, 0.0, 0.0, 1.0);
}
/////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
void RenderScene()
{
	glClear(GL_COLOR_BUFFER_BIT );

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

	elapseTimeUniformLocation =glGetUniformLocation(programHandle, time);
	fragLoopDurationLocation =glGetUniformLocation(programHandle,fragLoopDuration);
    loopDurationLocation =glGetUniformLocation(programHandle, loopDuration);
	//printf(%d	%d	,elapseTimeUniformLocation, loopDurationLocation);

	glUniform1f(loopDurationLocation, 5.0f);
	glUniform1f(fragLoopDurationLocation, 10.0f);
	glUniform1f(elapseTimeUniformLocation, glutGet(GLUT_ELAPSED_TIME)/1000.0f );

	glutSwapBuffers();
	glutPostRedisplay();
}

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

	glutInitWindowSize(800, 600);
	glutInitWindowPosition(0,0);
	glutCreateWindow(second triangle:active!);

	SetupRC();
	glutDisplayFunc(RenderScene);
	
	glutMainLoop();
	return 0;
}


triangle.vert

 

#version 400


layout(location =0) in vec3 vertexPosition;



uniform float loopDuration;

uniform float time;



void main()

{

	float timeScale = 3.14159f * 2.0f / loopDuration;

	float currTime = mod(time, loopDuration);


	vec4 totalOffset = vec4(cos(currTime * timeScale) * 0.5f,
				
sin(currTime * timeScale) * 0.5f, 
0.0f,
0.0f);


	gl_Position = vec4(vertexPosition,1.0) + totalOffset;

}

triangle.frag

 

 

#version 400


out vec4 fragColor;

uniform float fragLoopDuration;
uniform float time;

const vec4 firstColor =vec4(1.0f, 0.0f, 0.0f, 1.0f);
const vec4 secondColor =vec4(0.0f, 1.0f, 0.0f, 1.0f);

void main(){
	float currentTime =mod(time, fragLoopDuration);
	float currentLerp =currentTime/fragLoopDuration;


	fragColor =mix(firstColor, secondColor, currentLerp);

 }


 

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