1 /* Copyright (c) 2011 Dave Airlie
2 based on vbo-drawarrays.c, which should be MIT licensed */
4 /* Basic VBO testing ARB_vertex_type_2_10_10_10_rev */
11 #include <glad/glad.h>
12 #include "glut_wrap.h"
14 GLboolean bgra
= GL_FALSE
;
15 #define i32to10(x) ((x) >= 0 ? (x & 0x1ff) : 1024-(abs((x))& 0x1ff))
16 #define i32to2(x) ((x) >= 0 ? (x & 0x1) : 3-abs((x)))
18 static unsigned iconv(int x
, int y
, int z
, int w
)
23 val
|= i32to10(y
) << 10;
24 val
|= i32to10(z
) << 20;
25 val
|= i32to2(w
) << 30;
28 #define conv(x,y,z,w) (((x) & 0x3ff) | ((y) & 0x3ff) << 10 | ((z) & 0x3ff)<< 20 | ((w) & 0x3) << 30)
40 static void SetupVerts(void)
42 verts
[0].pos
= iconv(-XYVAL
, -XYVAL
, ZVAL
, 1);
43 verts
[0].color
= conv(COLVAL
, 0, 0, 0);
45 verts
[1].pos
= iconv(XYVAL
, -XYVAL
, ZVAL
, 1);
46 verts
[1].color
= conv(0, COLVAL
, 0, 0);
48 verts
[2].pos
= iconv(0, XYVAL
, ZVAL
, 1);
49 verts
[2].color
= conv(0, 0, COLVAL
, 0);
52 GLuint arrayObj
, elementObj
;
54 static void Init( void )
60 static const char *prog1
=
62 "PARAM mvp[4] = {state.matrix.mvp};\n"
63 "DP4 result.position.x, vertex.position, mvp[0]; \n"
64 "DP4 result.position.y, vertex.position, mvp[1]; \n"
65 "DP4 result.position.z, vertex.position, mvp[2]; \n"
66 "DP4 result.position.w, vertex.position, mvp[3]; \n"
67 "MOV result.color, vertex.color;\n"
70 #ifndef GL_ARB_vertex_type_2_10_10_10_rev
71 fprintf(stderr
,"built without ARB_vertex_type_2_10_10_10_rev\n");
75 if (!glutExtensionSupported("GL_ARB_vertex_type_2_10_10_10_rev")){
76 fprintf(stderr
,"requires ARB_vertex_type_2_10_10_10_rev\n");
80 glGenProgramsARB(1, &prognum
);
81 glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, prognum
);
82 glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
83 strlen(prog1
), (const GLubyte
*) prog1
);
85 assert(glIsProgramARB(prognum
));
86 errnum
= glGetError();
88 if (errnum
!= GL_NO_ERROR
)
92 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errorpos
);
93 printf("errorpos: %d\n", errorpos
);
94 printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB
));
98 glEnableClientState( GL_VERTEX_ARRAY
);
99 glEnableClientState( GL_COLOR_ARRAY
);
103 glGenBuffersARB(1, &arrayObj
);
104 glBindBufferARB(GL_ARRAY_BUFFER_ARB
, arrayObj
);
105 glBufferDataARB(GL_ARRAY_BUFFER_ARB
, sizeof(verts
), verts
, GL_STATIC_DRAW_ARB
);
108 color_size
= GL_BGRA
;
110 #ifdef GL_ARB_vertex_type_2_10_10_10_rev
111 glVertexPointer( 4, GL_INT_2_10_10_10_REV
, sizeof(verts
[0]), 0 );
112 glColorPointer( color_size
, GL_UNSIGNED_INT_2_10_10_10_REV
, sizeof(verts
[0]), (void *)(sizeof(unsigned int)) );
118 static void Display( void )
120 glClearColor(0.3, 0.3, 0.3, 1);
121 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
123 glEnable(GL_VERTEX_PROGRAM_ARB
);
125 glDrawArrays( GL_TRIANGLES
, 0, 3 );
131 static void Reshape( int width
, int height
)
133 glViewport( 0, 0, width
, height
);
134 glMatrixMode( GL_PROJECTION
);
136 glOrtho(-100.0, 100.0, -100.0, 100.0, -0.5, 1000.0);
137 glMatrixMode( GL_MODELVIEW
);
142 static void Key( unsigned char key
, int x
, int y
)
154 static GLenum
Args(int argc
, char **argv
)
158 for (i
= 1; i
< argc
; i
++) {
159 if (strcmp(argv
[i
], "-bgra") == 0) {
162 fprintf(stderr
, "%s (Bad option).\n", argv
[i
]);
169 int main( int argc
, char *argv
[] )
171 glutInit( &argc
, argv
);
173 if (Args(argc
, argv
) == GL_FALSE
) {
177 glutInitWindowPosition( 0, 0 );
178 glutInitWindowSize( 250, 250 );
179 glutInitDisplayMode( GLUT_RGB
| GLUT_SINGLE
| GLUT_DEPTH
);
180 glutCreateWindow(argv
[0]);
182 glutReshapeFunc( Reshape
);
183 glutKeyboardFunc( Key
);
184 glutDisplayFunc( Display
);