#include #include #include #include #define countof(x) (sizeof(x) / sizeof(*(x))) const char *vshader[] = { "attribute vec2 aCoord;\n", "void main(void)\n", "{\n", " gl_Position = vec4(aCoord.x / 128.0 - 1.0, aCoord.y / 128.0 - 1.0, 0.0, 1.0);\n", "}\n", }; const char *fshader[] = { "void main(void)\n", "{\n", " gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n", "}\n", }; int g_shader; struct Vertex { unsigned char x, y; //Add some dummy bytes to make the stride non-zero. unsigned char dummy[2]; }; struct Vertex star[] = { {250, 125}, {23, 198}, {163, 6}, {163, 243}, {23, 51}, {250, 125}, }; void Init() { int i; //Fill the dummy bytes with random data to make obvious if they are read for (i = 0; i < countof(star); ++i) { star[i].dummy[0] = rand() & 0xFF; star[i].dummy[1] = rand() & 0xFF; } glewInit(); int vsh = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vsh, countof(vshader), vshader, NULL); glCompileShader(vsh); int fsh = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fsh, countof(fshader), fshader, NULL); glCompileShader(fsh); g_shader = glCreateProgram(); glAttachShader(g_shader, vsh); glAttachShader(g_shader, fsh); glLinkProgram(g_shader); glBindAttribLocation(g_shader, 0, "aCoord"); } void OnDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); glUseProgram(g_shader); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(struct Vertex), star); glDrawArrays(GL_LINE_STRIP, 0, countof(star)); glDisableVertexAttribArray(0); glUseProgram(0); GLuint error; while ((error = glGetError()) != 0) printf("GLError 0x%04X\n", error); glutSwapBuffers(); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutCreateWindow("Test"); Init(); glutDisplayFunc(OnDisplay); glutMainLoop(); return 0; }