#include #include #include #include #include typedef struct { float x, y; GLubyte r, g, b, a; } Vert; static const GLchar vert_shader_source[] = "attribute vec4 other_color;\n" "attribute vec4 pos;\n" "attribute vec4 color;\n" "\n" "void\n" "main ()\n" "{\n" " gl_Position = pos;\n" " gl_FrontColor = color + other_color;\n" "}\n"; static const GLchar frag_shader_source[] = "\n" "void\n" "main ()\n" "{\n" " gl_FragColor = gl_Color;\n" "}\n"; static void display_cb (void) { static const Vert verts[] = { { -0.5, -0.5, 255, 0, 0, 255 }, { 0.5, -0.5, 0, 255, 0, 255 }, { -0.5, 0.5, 255, 0, 0, 255 }, { 0.5, 0.5, 0, 255, 0, 255 } }; const GLchar *source; GLint status; GLuint shader; GLuint glsl_program; char buf[1024]; GLint pos_location; GLint color_location; GLint other_color_location; glClear (GL_COLOR_BUFFER_BIT); glsl_program = glCreateProgram (); shader = glCreateShader (GL_VERTEX_SHADER); source = vert_shader_source; glShaderSource (shader, 1, &source, 0); glCompileShader (shader); glGetShaderInfoLog (shader, sizeof (buf), NULL, buf); if (*buf) printf ("vertex shader info: %s\n", buf); glGetShaderiv (shader, GL_COMPILE_STATUS, &status); if (!status) printf ("vertex shader compile failed\n"); glAttachShader (glsl_program, shader); shader = glCreateShader (GL_FRAGMENT_SHADER); source = frag_shader_source; glShaderSource (shader, 1, &source, 0); glCompileShader (shader); glGetShaderInfoLog (shader, sizeof (buf), NULL, buf); if (*buf) printf ("fragment shader info: %s\n", buf); glGetShaderiv (shader, GL_COMPILE_STATUS, &status); if (!status) printf ("fragment shader compile failed\n"); glAttachShader (glsl_program, shader); glLinkProgram (glsl_program); glUseProgram (glsl_program); pos_location = glGetAttribLocation (glsl_program, "pos"); printf ("glGetAttribLocation(\"pos\") = %i\n", pos_location); color_location = glGetAttribLocation (glsl_program, "color"); printf ("glGetAttribLocation(\"color\") = %i\n", color_location); other_color_location = glGetAttribLocation (glsl_program, "other_color"); printf ("glGetAttribLocation(\"other_color\") = %i\n", other_color_location); glEnableVertexAttribArray (pos_location); glVertexAttribPointer (pos_location, 2, GL_FLOAT, GL_FALSE, sizeof (Vert), &verts[0].x); glEnableVertexAttribArray (color_location); glVertexAttribPointer (color_location, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof (Vert), &verts[0].r); glVertexAttrib4f (other_color_location, 0.0f, 0.0f, 1.0f, 0.0f); glDrawArrays (GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray (pos_location); glDisableVertexAttribArray (color_location); glDeleteShader (shader); glUseProgram (0); glDeleteProgram (glsl_program); glutSwapBuffers (); } int main (int argc, char **argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE); glutInitWindowSize (512, 512); glutCreateWindow ("Look! A triangle!"); glutDisplayFunc (display_cb); glewInit (); glutMainLoop (); return 0; }