1.首先,必须要有一个能够读取文件的函数用来读取Shader,将Shader中的语句读到一个字符指针中,并返回这个指针,比如如下这个函数:
1 char* readFileFuc(char* filename){ 2 FILE* fp = fopen(filename, "r"); 3 if (fp == NULL){ return NULL; }; 4 fseek(fp, 0L, SEEK_END); 5 long size = ftell(fp); 6 char*buf = (char *)malloc(size * sizeof(char)); 7 memset(buf, 0, size); 8 fseek(fp, 0L, SEEK_SET); 9 fread(buf, sizeof(char), size, fp); 10 //buf[size] = ‘ ‘; 11 fclose(fp); 12 cout << buf; 13 return buf; 14 }
以上函数能将文件的原文读到buf中,但我在网上找过其他的方法,将结果cout出来末尾总会多几个屯屯屯,只有这个方法最好,故暂时用这个来读取文件。
##更新##
代码变成:
char* readFileFuc(char* filename){ FILE* fp = fopen(filename, "r"); if (fp == NULL){ return NULL; }; fseek(fp, 0L, SEEK_END); long size = ftell(fp); char*buf = (char *)malloc((size+1) * sizeof(char)); memset(buf, 0, size); fseek(fp, 0L, SEEK_SET); fread(buf, sizeof(char), size, fp); buf[size] = ‘\0‘; //buf[size] = ‘ ‘; fclose(fp); cout << buf; return buf; }
多分配一个字节,并且让最后一个字节是‘\0‘!!!!,这很重要,否则读出的内容最后的部分会指向某个不知道的地方!
2,有了这个函数之后便可以创建相应的Shader程序了,通常Shader有两个,一个Vertex Shader,一个Fragment Shader,
GLuint p;//use to return the handle of the program GLuint& set_shaders() { GLuint v = glCreateShader(GL_VERTEX_SHADER); GLuint f = glCreateShader(GL_FRAGMENT_SHADER); char *vs = readFileFuc("vShader.glsl"); char *fs = readFileFuc("fShader.glsl"); const char *vv=vs; const char *ff=fs; glShaderSource(v, 1, &vv, NULL); glShaderSource(f, 1, &ff, NULL); int success; glCompileShader(v); glGetShaderiv(v, GL_COMPILE_STATUS, &success); if (!success) { char info_log[5000]; glGetShaderInfoLog(v, 5000, NULL, info_log); printf("Error in vertex shader compilation!\n"); printf("Info Log: %s\n", info_log); } glCompileShader(f); glGetShaderiv(f, GL_COMPILE_STATUS, &success); if (!success) { char info_log[5000]; glGetShaderInfoLog(f, 5000, NULL, info_log); printf("Error in fragment shader compilation!\n"); printf("Info Log: %s\n", info_log); } p = glCreateProgram(); glAttachShader(p, v); glAttachShader(p, f); glLinkProgram(p); glUseProgram(p); free(vs); free(fs); return p; }
GLuint p是我定义的一个全局变量,用来返回shader program的。发现其实就几步:
glCreateShader->readFileFuc->glShaderSource->glCompileShader-> glGetShaderiv->glCreateProgram()->glAttachShader->glLinkProgram->glUseProgram
3,在初始化函数init()中,(init()是随便定义的),获取set_shaders的program,便可以使用了,贴上初始化函数代码:
void init() { point3 points[NumPoints]; //为三角形指定顶点 point3 vertices[4] = { point3(-1.0, -1.0, -1.0), point3(1.0, -1.0, -1.0), point3(0.0, 1.0, -1.0), point3(0.0, 0.0, 1.0) }; //在三角形内部选择一个任意的初始点 points[0] = point3(0.0, 0.0, 0.0); for (int i = 1; i < NumPoints; ++i){ int j = rand() % 4; points[i] = (points[i - 1] + vertices[j]) / 2.0; } GLuint program = set_shaders(); glUseProgram(program);//这句加不加无所谓 GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); //创建并初始化一个缓冲区对象 GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(points)/*+sizeof(colors)*/, points, GL_STATIC_DRAW); //初始化顶点着色器中的顶点位置属性 GLuint loc = glGetAttribLocation(program, "vPosition"); glEnableVertexAttribArray(loc); glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); glClearColor(1.0, 1.0, 1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(75, 1.0, 2.0, -10); glMatrixMode(GL_MODELVIEW); glPointSize(2.0); }
有关
glGenVertexArrays(1, &vao);glBindVertexArray(vao);..这几个函数,等我弄明白了再贴
时间: 2024-10-08 10:17:12