本帖最后由 523066680 于 2017-7-8 19:29 编辑
昨天把自己那个1024特效用shader完成了,用WebGL是为了方便学习和实践,本质上我还是在用纯C作为主程序,希望能一直用下去。
实时动态渲染
 | #include <GL/glew.h> | | #include <GL/freeglut.h> | | #include <stdio.h> | | #include <unistd.h> | | #include <math.h> | | #include <time.h> | | | | #define SIZE_X 500 | | #define SIZE_Y 500 | | | | int winID; | | int program; | | | | GLuint vTest; | | GLfloat test = 200.0; | | | | GLuint vAng; | | GLfloat ang = 0.0; | | | | const GLchar* readShader( const char *filename ); | | | | void display(void) | | { | | static const GLfloat black[] = { 0.0f, 0.0f, 0.0f, 1.0f }; | | glClearBufferfv(GL_COLOR, 0, black ); | | | | glDrawArrays(GL_POINTS, 0, 1); | | glutSwapBuffers(); | | } | | | | void idle(void) | | { | | usleep(10000); | | ang += 0.05; | | glVertexAttrib1f( vAng, ang ); | | | | glutPostRedisplay(); | | } | | | | void reshape(int Width,int Height) | | { | | glViewport(0, 0, Width, Height); | | } | | | | void keypress(unsigned char key, int mousex, int mousey) | | { | | switch (key) | | { | | case 'q': | | glutDestroyWindow(winID); | | exit(0); | | break; | | case 'Q': | | glutDestroyWindow(winID); | | exit(0); | | break; | | case ']': | | test += test*0.1; | | glVertexAttrib1f( vTest, test ); | | glutPostRedisplay(); | | break; | | case '[': | | test -= test*0.1; | | glVertexAttrib1f( vTest, test ); | | glutPostRedisplay(); | | break; | | } | | } | | | | void init(void) | | { | | glClearColor(0.0, 0.0, 0.0, 0.0); | | | | glEnable(GL_PROGRAM_POINT_SIZE); | | | | srand(time(NULL)); | | } | | | | int loadShader(void) | | { | | | | | | glewInit(); | | const GLchar * vs_src; | | const GLchar * fs_src; | | | | vs_src = readShader( "pointTest.vert" ); | | fs_src = readShader( "pointTest.frag" ); | | | | GLuint vs = glCreateShader(GL_VERTEX_SHADER); | | glShaderSource(vs, 1, &vs_src, NULL); | | glCompileShader(vs); | | | | GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); | | glShaderSource(fs, 1, &fs_src, NULL); | | glCompileShader(fs); | | | | program = glCreateProgram(); | | | | glAttachShader(program, vs); | | glAttachShader(program, fs); | | | | glLinkProgram(program); | | | | glUseProgram(program); | | | | vTest = glGetAttribLocation( program, "vTest" ); | | glVertexAttrib1f( vTest, test ); | | printf("%d\n", vTest); | | | | vAng = glGetAttribLocation( program, "vAng" ); | | glVertexAttrib1f( vAng, ang ); | | | | } | | | | int main(int argc, char *argv[]) | | { | | | | glutInit(&argc, argv); | | | | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); | | glutInitWindowSize(SIZE_X, SIZE_Y); | | glutInitWindowPosition(10, 10); | | winID = glutCreateWindow("Simple"); | | | | init(); | | loadShader(); | | | | glutDisplayFunc(display); | | glutKeyboardFunc(keypress); | | glutReshapeFunc(reshape); | | glutIdleFunc(idle); | | glutMainLoop(); | | return 0; | | } | | | | | | const GLchar* readShader( const char *filename ) | | { | | FILE *FILE = fopen(filename, "rb"); | | | | GLchar *source; | | int size; | | | | fseek( FILE, 0, SEEK_END ); | | size = ftell(FILE) + 1; | | fseek( FILE, 0, SEEK_SET ); | | | | source = (GLchar *) malloc( sizeof(GLchar) * size ); | | fread( source, 1, size, FILE); | | | | return (const GLchar*)(source); | | }COPY |
Vertex Shader | #version 420 core | | | | attribute float vTest; | | attribute float vAng; | | | | out vec4 coord; | | out float fTest; | | out float fAng; | | | | void main(void) | | { | | gl_Position = vec4(0.0, 0.0, 0.0, 0.0); | | gl_PointSize = 480.0; | | coord = gl_Position; | | fTest = vTest; | | fAng = vAng; | | } | | | | | | | | | | | | | | COPY |
Fragment Sahder | #version 420 core | | | | in float fTest; | | in float fAng; | | | | float RGB(float x, float y, float e) | | { | | return ( x * tan(x/y) ) * ( y * tan( x/y + fAng*e ) ) / fTest; | | } | | | | void main(void) | | { | | vec4 coord = (gl_FragCoord - 250.0)*2.0; | | | | gl_FragColor = | | vec4( | | int(RGB(coord.x, coord.y, 0.33))%255/255.0, | | int(RGB(coord.x, coord.y, 0.66))%255/255.0, | | int(RGB(coord.x, coord.y, 0.99))%255/255.0, | | 0.0 | | ); | | }COPY |
编译脚本(实在记不住 makefile 语法,用批处理) | @echo off | | set path=D:\tdm-gcc-64\bin | | | | :Compile | | gcc -std=c11 "%1" -o "%~n1.exe" ^ | | -ID:\Lib\freeglut-MinGW-3.0.0-1.mp\include ^ | | -LD:\Lib\freeglut-MinGW-3.0.0-1.mp\lib\x64 ^ | | -ID:\Lib\glew-1.13.0\include ^ | | -LD:\Lib\glew-1.13.0\lib ^ | | -lfreeglut -lglew32 -lopengl32 -lglu32 | | | | :Run | | if %errorlevel% == 0 ( | | %~n1.exe | | ) else ( | | echo Compile Error | | pause | | )COPY |
|