OpenGL Fragment Sahder 重写 1024x1024 渲染效果

TheBooksofShaders ShaderToy
OpenGL Tutorial
回复
头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

OpenGL Fragment Sahder 重写 1024x1024 渲染效果

帖子 523066680 »

shadertoy 链接:https://www.shadertoy.com/view/4s2BRh

图片

代码: 全选

#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
)
头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

Re: OpenGL Fragment Sahder 重写 1024x1024 渲染效果

帖子 523066680 »

https://www.shadertoy.com/view/4s2BRh
float RGB(float x, float y, float e)
{
return ( x * tan(x/y) ) * ( y * tan( x/y + iGlobalTime/2.0 * e ) ) / (iMouse.x*10.0+500.0);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 coord;
coord = (fragCoord - iResolution.xy/2.0)*5.0;

fragColor = vec4(
float(int(RGB(coord.x, coord.y, 0.33)) %255) / 255.0,
float(int(RGB(coord.x, coord.y, 0.66)) %255) / 255.0,
float(int(RGB(coord.x, coord.y, 0.99)) %255) / 255.0,
1.0);
}
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 0 访客