X-Git-Url: https://git.cworth.org/git?p=apitrace-tests;a=blobdiff_plain;f=apps%2Fgl%2Ftri_glsl.c;fp=apps%2Fgl%2Ftri_glsl.c;h=741aeceefee4c98f75231e3d2b58ef2bc5b561bb;hp=0000000000000000000000000000000000000000;hb=dc6ab389f14bf38b2e6df500aacd52a5ab999cc5;hpb=a79d84589687bf0d651067a8e75370dd6a8ec7b7 diff --git a/apps/gl/tri_glsl.c b/apps/gl/tri_glsl.c new file mode 100644 index 0000000..741aece --- /dev/null +++ b/apps/gl/tri_glsl.c @@ -0,0 +1,154 @@ +/* Test fragment shader + * + * Brian Pau + */ + +#ifdef _WIN32 +#include +#endif + +#include +#include +#include +#include +#include + +#include +#ifdef __APPLE__ +# include +#else +# include +#endif + + +static GLuint fragShader; +static GLuint vertShader; +static GLuint program; +static GLint win = 0; +static GLfloat xpos = 0, ypos = 0; + + +static void +LoadAndCompileShader(GLuint shader, const char *text) +{ + const GLint length = -1; + GLint stat; + + glShaderSource(shader, 1, (const GLchar **) &text, &length); + + glCompileShader(shader); + + glGetShaderiv(shader, GL_COMPILE_STATUS, &stat); + if (!stat) { + GLchar log[1000]; + GLsizei len; + glGetShaderInfoLog(shader, 1000, &len, log); + fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log); + exit(1); + } +} + + +static void +CheckLink(GLuint prog) +{ + GLint stat; + glGetProgramiv(prog, GL_LINK_STATUS, &stat); + if (!stat) { + GLchar log[1000]; + GLsizei len; + glGetProgramInfoLog(prog, 1000, &len, log); + fprintf(stderr, "Linker error:\n%s\n", log); + } +} + + +static void +Init(void) +{ + static const char *fragShaderText = + "void main() {\n" + " gl_FragColor = gl_Color; \n" + "}\n"; + static const char *vertShaderText = + "void main() {\n" + " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" + " gl_FrontColor = gl_Color;\n" + "}\n"; + + if (!GLEW_VERSION_2_0) { + printf("This program requires OpenGL 2.x\n"); + exit(1); + } + + fragShader = glCreateShader(GL_FRAGMENT_SHADER); + LoadAndCompileShader(fragShader, fragShaderText); + + vertShader = glCreateShader(GL_VERTEX_SHADER); + LoadAndCompileShader(vertShader, vertShaderText); + + program = glCreateProgram(); + glAttachShader(program, fragShader); + glAttachShader(program, vertShader); + glLinkProgram(program); + CheckLink(program); + glUseProgram(program); + + assert(glGetError() == 0); + + glClearColor(0.3f, 0.1f, 0.3f, 1.0f); +} + + +static void +Reshape(int width, int height) +{ + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); + glMatrixMode(GL_MODELVIEW); +} + + +static void +Draw(void) +{ + glClear(GL_COLOR_BUFFER_BIT); + + glBegin(GL_TRIANGLES); + glColor3f(.8, 0, 0); + glVertex3f(-0.9, -0.9, -30.0); + glColor3f(0, .9, 0); + glVertex3f( 0.9, -0.9, -30.0); + glColor3f(0, 0, .7); + glVertex3f( 0.0, 0.9, -30.0); + glEnd(); + + glFlush(); + + glutSwapBuffers(); + + glDeleteShader(fragShader); + glDeleteShader(vertShader); + glDeleteProgram(program); + glutDestroyWindow(win); + + exit(0); +} + + +int +main(int argc, char **argv) +{ + glutInit(&argc, argv); + glutInitWindowSize(250, 250); + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); + win = glutCreateWindow(argv[0]); + glewInit(); + glutReshapeFunc(Reshape); + glutDisplayFunc(Draw); + Init(); + glutMainLoop(); + return 0; +}