From 9110dbd9b2b598183815ed113dd690051d42e5f0 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 24 Jul 2007 10:00:29 -0600 Subject: simple fragment shader test --- progs/trivial/fs-tri.c | 207 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 progs/trivial/fs-tri.c (limited to 'progs/trivial/fs-tri.c') diff --git a/progs/trivial/fs-tri.c b/progs/trivial/fs-tri.c new file mode 100644 index 0000000000..ecb13fd56d --- /dev/null +++ b/progs/trivial/fs-tri.c @@ -0,0 +1,207 @@ +/* Test fragment shader */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "extfuncs.h" + + +static GLuint fragShader; +static GLuint vertShader; +static GLuint program; +static GLint win = 0; +static GLfloat xpos = 0, ypos = 0; + + +static void +Redisplay(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glPushMatrix(); + glTranslatef(xpos, ypos, 0); + + glBegin(GL_TRIANGLES); + glVertex2f(-0.9, -0.9); + glVertex2f( 0.9, -0.9); + glVertex2f( 0, 0.9); + glEnd(); + + glPopMatrix(); + + glutSwapBuffers(); +} + + +static void +Reshape(int width, int height) +{ + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-1, 1, -1, 1, -1, 1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + + +static void +CleanUp(void) +{ + glDeleteShader_func(fragShader); + glDeleteShader_func(vertShader); + glDeleteProgram_func(program); + glutDestroyWindow(win); +} + + +static void +Key(unsigned char key, int x, int y) +{ + (void) x; + (void) y; + + switch(key) { + case 27: + CleanUp(); + exit(0); + break; + } + glutPostRedisplay(); +} + + +static void +SpecialKey(int key, int x, int y) +{ + const GLfloat step = 0.1; + + (void) x; + (void) y; + + switch(key) { + case GLUT_KEY_UP: + ypos += step; + break; + case GLUT_KEY_DOWN: + ypos -= step; + break; + case GLUT_KEY_LEFT: + xpos -= step; + break; + case GLUT_KEY_RIGHT: + xpos += step; + break; + } + glutPostRedisplay(); +} + + +static void +LoadAndCompileShader(GLuint shader, const char *text) +{ + GLint stat; + + glShaderSource_func(shader, 1, (const GLchar **) &text, NULL); + + glCompileShader_func(shader); + + glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat); + if (!stat) { + GLchar log[1000]; + GLsizei len; + glGetShaderInfoLog_func(shader, 1000, &len, log); + fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log); + exit(1); + } +} + + +static void +CheckLink(GLuint prog) +{ + GLint stat; + glGetProgramiv_func(prog, GL_LINK_STATUS, &stat); + if (!stat) { + GLchar log[1000]; + GLsizei len; + glGetProgramInfoLog_func(prog, 1000, &len, log); + fprintf(stderr, "Linker error:\n%s\n", log); + } +} + + +static void +Init(void) +{ + /* fragment color is a function of fragment position: */ + static const char *fragShaderText = + "void main() {\n" + " gl_FragColor = gl_FragCoord * vec4(0.005); \n" + "}\n"; +#if 0 + static const char *vertShaderText = + "varying vec3 normal;\n" + "void main() {\n" + " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" + " normal = gl_NormalMatrix * gl_Normal;\n" + "}\n"; +#endif + const char *version; + + version = (const char *) glGetString(GL_VERSION); + if (version[0] != '2' || version[1] != '.') { + printf("This program requires OpenGL 2.x, found %s\n", version); + exit(1); + } + + GetExtensionFuncs(); + + fragShader = glCreateShader_func(GL_FRAGMENT_SHADER); + LoadAndCompileShader(fragShader, fragShaderText); + +#if 0 + vertShader = glCreateShader_func(GL_VERTEX_SHADER); + LoadAndCompileShader(vertShader, vertShaderText); +#endif + + program = glCreateProgram_func(); + glAttachShader_func(program, fragShader); +#if 0 + glAttachShader_func(program, vertShader); +#endif + glLinkProgram_func(program); + CheckLink(program); + glUseProgram_func(program); + + assert(glGetError() == 0); + + glClearColor(0.3f, 0.3f, 0.3f, 0.0f); + + printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); +} + + +int +main(int argc, char *argv[]) +{ + glutInit(&argc, argv); + glutInitWindowPosition( 0, 0); + glutInitWindowSize(200, 200); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); + win = glutCreateWindow(argv[0]); + glutReshapeFunc(Reshape); + glutKeyboardFunc(Key); + glutSpecialFunc(SpecialKey); + glutDisplayFunc(Redisplay); + Init(); + glutMainLoop(); + return 0; +} + + -- cgit v1.2.3 From 3af7876521e23152bc82f42f00e1a1d51bd37812 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 24 Jul 2007 12:28:01 -0600 Subject: added code to test per-vertex colors --- progs/trivial/fs-tri.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'progs/trivial/fs-tri.c') diff --git a/progs/trivial/fs-tri.c b/progs/trivial/fs-tri.c index ecb13fd56d..3be4d42e54 100644 --- a/progs/trivial/fs-tri.c +++ b/progs/trivial/fs-tri.c @@ -27,8 +27,11 @@ Redisplay(void) glTranslatef(xpos, ypos, 0); glBegin(GL_TRIANGLES); + glColor3f(1, 0, 0); glVertex2f(-0.9, -0.9); + glColor3f(0, 1, 0); glVertex2f( 0.9, -0.9); + glColor3f(0, 0, 1); glVertex2f( 0, 0.9); glEnd(); @@ -143,6 +146,8 @@ Init(void) static const char *fragShaderText = "void main() {\n" " gl_FragColor = gl_FragCoord * vec4(0.005); \n" + " //gl_FragColor = gl_Color; \n" + " //gl_FragColor = vec4(1, 0, 0.5, 0); \n" "}\n"; #if 0 static const char *vertShaderText = -- cgit v1.2.3 From 9aa73cfae84c7710df97ce182d32bea8d3423ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Fonseca?= Date: Sun, 1 Feb 2009 12:00:07 +0000 Subject: progs: Get more samples building on windows. --- progs/trivial/fs-tri.c | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) (limited to 'progs/trivial/fs-tri.c') diff --git a/progs/trivial/fs-tri.c b/progs/trivial/fs-tri.c index 3be4d42e54..6e86df1dcf 100644 --- a/progs/trivial/fs-tri.c +++ b/progs/trivial/fs-tri.c @@ -5,10 +5,8 @@ #include #include #include -#include +#include #include -#include -#include "extfuncs.h" static GLuint fragShader; @@ -56,9 +54,9 @@ Reshape(int width, int height) static void CleanUp(void) { - glDeleteShader_func(fragShader); - glDeleteShader_func(vertShader); - glDeleteProgram_func(program); + glDeleteShader(fragShader); + glDeleteShader(vertShader); + glDeleteProgram(program); glutDestroyWindow(win); } @@ -110,15 +108,15 @@ LoadAndCompileShader(GLuint shader, const char *text) { GLint stat; - glShaderSource_func(shader, 1, (const GLchar **) &text, NULL); + glShaderSource(shader, 1, (const GLchar **) &text, NULL); - glCompileShader_func(shader); + glCompileShader(shader); - glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat); + glGetShaderiv(shader, GL_COMPILE_STATUS, &stat); if (!stat) { GLchar log[1000]; GLsizei len; - glGetShaderInfoLog_func(shader, 1000, &len, log); + glGetShaderInfoLog(shader, 1000, &len, log); fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log); exit(1); } @@ -129,11 +127,11 @@ static void CheckLink(GLuint prog) { GLint stat; - glGetProgramiv_func(prog, GL_LINK_STATUS, &stat); + glGetProgramiv(prog, GL_LINK_STATUS, &stat); if (!stat) { GLchar log[1000]; GLsizei len; - glGetProgramInfoLog_func(prog, 1000, &len, log); + glGetProgramInfoLog(prog, 1000, &len, log); fprintf(stderr, "Linker error:\n%s\n", log); } } @@ -165,24 +163,22 @@ Init(void) exit(1); } - GetExtensionFuncs(); - - fragShader = glCreateShader_func(GL_FRAGMENT_SHADER); + fragShader = glCreateShader(GL_FRAGMENT_SHADER); LoadAndCompileShader(fragShader, fragShaderText); #if 0 - vertShader = glCreateShader_func(GL_VERTEX_SHADER); + vertShader = glCreateShader(GL_VERTEX_SHADER); LoadAndCompileShader(vertShader, vertShaderText); #endif - program = glCreateProgram_func(); - glAttachShader_func(program, fragShader); + program = glCreateProgram(); + glAttachShader(program, fragShader); #if 0 - glAttachShader_func(program, vertShader); + glAttachShader(program, vertShader); #endif - glLinkProgram_func(program); + glLinkProgram(program); CheckLink(program); - glUseProgram_func(program); + glUseProgram(program); assert(glGetError() == 0); @@ -200,6 +196,7 @@ main(int argc, char *argv[]) glutInitWindowSize(200, 200); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); win = glutCreateWindow(argv[0]); + glewInit(); glutReshapeFunc(Reshape); glutKeyboardFunc(Key); glutSpecialFunc(SpecialKey); -- cgit v1.2.3