本帖最后由 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)
- {
-
- // Very Important !
- 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);
- //显示模式 双缓冲 RGBA
- 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 ); //turn back
-
- source = (GLchar *) malloc( sizeof(GLchar) * size );
- fread( source, 1, size, FILE);
-
- return (const GLchar*)(source); //强制转换
- }
复制代码 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;
- }
-
- /*
- 备注:attribute 只能在 顶点着色器中使用
-
- 实际测试 如果 main 中没有用到 传入的变量
- 主程序中就无法通过 GetAttribLocation 获取该变量索引。
- */
复制代码 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
- );
- }
复制代码 编译脚本(实在记不住 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
- )
复制代码
|