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

glsl著色器(c語言)

編輯:關於C語言

basic.vert
#version 400

layout(location =0) in vec3 VertexPosition;
layout(location =1) in vec3 VertexColor;

out vec3 Color;

uniform mat4 Matrix;

void main()
{
    Color= VertexColor;
	
    gl_Position= Matrix *vec4(VertexPosition,1.0);
}
basic.frag
#version 400  
  
in vec3 Color;  
  
out vec4 FragColor;  
  
void main()  
{  
    FragColor = vec4(Color,1.0);  
}  



主程序main.c
#include 
#include 
#include 

#include "readtext.h"

#ifdef __APPLE__
#include 
#else
#define FREEGLUT_STATIC
#include 
#endif


#pragma  comment(lib,"../readtext.lib")
#pragma  comment(lib,"../glew32.lib")



GLuint vShader, fShader,programHandle;

int infologLength = 0;  
int charsWritten  = 0;  
char *infoLog;  

//頂點位置數組  
float positionData[] = {  
    -1.0f, -1.0f, 0.0f, 
    1.0f, -1.0f, 0.0f,
    0.0f,  1.0f, 0.0f};  
//顏色數組  
float colorData[] = {  
        0.2f, 0.8f, 0.0f,  
        0.4f, 0.6f, 0.0f,  
        0.1f, 0.9f, 1.0f };  


float matrix[16];


matrix[16] ={0.5 , 0.0, -0.866, 0.0,
	          0.0,  1.0, 0.0, 0.0,
			  0.866 , 0.0, 0.5, 0.0,
			  0.0, 0.0, 0.0, 1.0 };

	/////////////////變換矩陣
/////////////////////////////////////////////////////
// cos(angle), 0, -sin(angle), //列向量
//0, 1, 0,
//sin(angle), 0,cos(angle)
//


GLint matrixLocation;
GLuint vaoHandle;//vertex array object 


void initShader()
{
	 char *renderer, *vender, *version, *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("%s \n", renderer);
	 printf("%s \n", vender);
	 printf("%s \n", version);
	 printf("%s \n", glslVersion);
     

	 //著色器處理
     vShader= glCreateShader(GL_VERTEX_SHADER);
	 vs=readText("../basic.vert");
	 printf("%s\n", vs);

	 if(vShader ==0)
	 {
		 printf("fail to creat vertex shader");
		 exit(1);
	 }
	 //將源代碼與著色器相關聯
	 vv =vs;
	 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, &charsWritten, infoLog);  
         printf("%s\n",infoLog);  
         free(infoLog);  
     }  
	 //////////////////////////////////////////////////////////////////////////////////////
	 /////////////////////////////////////////////////////////////////////
	 ////////////////////////////////////////////////////////
	 fShader = glCreateShader(GL_FRAGMENT_SHADER); 
	 if(fShader ==0)
	 {
		 printf("fail to creat shader\n");
		 exit(1);
	 }

	 fs= readText("../basic.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, &charsWritten, infoLog);  
         printf("%s\n",infoLog);  
         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, &charsWritten, infoLog);  
         printf("%s\n",infoLog);  
         free(infoLog);  
     }  
     glUseProgram(programHandle);  
}



void initVBO()  
{  
    // Create and populate the buffer objects  
    GLuint vboHandles[2];
	GLuint positionBufferHandle, colorBufferHandle;

    glGenBuffers(2, vboHandles);  
    positionBufferHandle = vboHandles[0];  
    colorBufferHandle = vboHandles[1];  
  
    //綁定VBO以供使用  
    glBindBuffer(GL_ARRAY_BUFFER,positionBufferHandle);  
    //加載數據到VBO  
    glBufferData(GL_ARRAY_BUFFER,12 * sizeof(float),  
        positionData,GL_STATIC_DRAW);  
  
    //綁定VBO以供使用  
    glBindBuffer(GL_ARRAY_BUFFER,colorBufferHandle);  
    //加載數據到VBO  
    glBufferData(GL_ARRAY_BUFFER,9 * sizeof(float),  
        colorData,GL_STATIC_DRAW);  
  
    glGenVertexArrays(1,&vaoHandle);  
    glBindVertexArray(vaoHandle);  
  
    glEnableVertexAttribArray(0);//頂點坐標  
    glEnableVertexAttribArray(1);//頂點顏色  
  
    //調用glVertexAttribPointer之前需要進行綁定操作  
    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 init()  
{  
    //初始化glew擴展庫  
    glewInit();  
    
  
    initShader();
  
    initVBO();  
  
    glClearColor(0.0,0.0,0.0,0.0);  
    //glShadeModel(GL_SMOOTH);  
  
}  
  

void display()  
{  
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_TEST);  
  
    ////繪制一個三角形(使用普通方法)  
    //glBegin(GL_TRIANGLES);  
    //glColor3f(0.0f,1.0f,0.0f);  
    //glVertex3f(0.0f,1.0f,0.0f);  
  
    //glColor3f(0.0f,1.0f,0.0f);  
    //glVertex3f(-1.0f,-1.0f,0.0f);  
  
    //glColor3f(0.0f,0.0f,1.0f);  
    //glVertex3f(1.0f,-1.0f,0.0f);  
    //glEnd();  

	
  
    //使用VAO、VBO繪制  
    glBindVertexArray(vaoHandle);  
    glDrawArrays(GL_TRIANGLES,0,3);  
    glBindVertexArray(0);  

	//傳遞矩陣。
	matrixLocation= glGetUniformLocation(programHandle, "Matrix");
	glUniformMatrix4fv(matrixLocation, 1, GL_TRUE, matrix);
  
    glutSwapBuffers();  
	glutPostRedisplay();
}  
  


int main(int argc,char** argv)  
{  
    glutInit(&argc,argv);  
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);  
    glutInitWindowSize(600,600);  
    glutInitWindowPosition(100,100);  
    glutCreateWindow("GLSL Test : Draw a triangle");  
    init();  
    glutDisplayFunc(display);  
      
    glutMainLoop();  
    return 0;  
}  
使用的庫可至我的資源頁下載!

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