glsl着色器(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 <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>

#include "readtext.h"

#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#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;  
}  
使用的库可至我的资源页下载!

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。