summaryrefslogtreecommitdiff
path: root/src/trivial/vbo-drawarrays-2101010.c
blob: e4cf50d9cda5d97282bf8087bd570b61cecfcbdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Copyright (c) 2011 Dave Airlie
   based on vbo-drawarrays.c, which should be MIT licensed */

/* Basic VBO testing ARB_vertex_type_2_10_10_10_rev */

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <glad/glad.h>
#include "glut_wrap.h"

GLboolean bgra = GL_FALSE;
#define i32to10(x) ((x) >= 0 ? (x & 0x1ff) : 1024-(abs((x))& 0x1ff))
#define i32to2(x) ((x) >= 0 ? (x & 0x1) : 3-abs((x)))

static unsigned iconv(int x, int y, int z, int w)
{
	unsigned val;

	val = i32to10(x);
	val |= i32to10(y) << 10;
	val |= i32to10(z) << 20;
	val |= i32to2(w) << 30;
	return val;
}
#define conv(x,y,z,w) (((x) & 0x3ff) | ((y) & 0x3ff) << 10 | ((z) & 0x3ff)<< 20 | ((w) & 0x3) << 30)

struct {
   GLuint pos;
   GLuint color;
} verts[3];

#define XYVAL 90
#define ZVAL -30

#define COLVAL 820

static void SetupVerts(void)
{
  verts[0].pos = iconv(-XYVAL, -XYVAL, ZVAL, 1);
  verts[0].color = conv(COLVAL, 0, 0, 0);

  verts[1].pos = iconv(XYVAL, -XYVAL, ZVAL, 1);
  verts[1].color = conv(0, COLVAL, 0, 0);

  verts[2].pos = iconv(0, XYVAL, ZVAL, 1);
  verts[2].color = conv(0, 0, COLVAL, 0);
}

GLuint arrayObj, elementObj;

static void Init( void )
{
   GLint errnum;
   GLuint prognum;
   int color_size = 4;

   static const char *prog1 =
      "!!ARBvp1.0\n"
      "PARAM mvp[4] = {state.matrix.mvp};\n"
      "DP4 result.position.x, vertex.position, mvp[0]; \n"
      "DP4 result.position.y, vertex.position, mvp[1]; \n"
      "DP4 result.position.z, vertex.position, mvp[2]; \n"
      "DP4 result.position.w, vertex.position, mvp[3]; \n"
      "MOV  result.color, vertex.color;\n"
      "END\n";

#ifndef GL_ARB_vertex_type_2_10_10_10_rev
   fprintf(stderr,"built without ARB_vertex_type_2_10_10_10_rev\n");
   exit(1);
#endif

  if (!glutExtensionSupported("GL_ARB_vertex_type_2_10_10_10_rev")){
     fprintf(stderr,"requires ARB_vertex_type_2_10_10_10_rev\n");
     exit(1);
   }

   glGenProgramsARB(1, &prognum);
   glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
   glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
		      strlen(prog1), (const GLubyte *) prog1);

   assert(glIsProgramARB(prognum));
   errnum = glGetError();

   if (errnum != GL_NO_ERROR)
   {
      GLint errorpos;

      glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
      printf("errorpos: %d\n", errorpos);
      printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
   }


   glEnableClientState( GL_VERTEX_ARRAY );
   glEnableClientState( GL_COLOR_ARRAY );

   SetupVerts();

   glGenBuffersARB(1, &arrayObj);
   glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrayObj);
   glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts, GL_STATIC_DRAW_ARB);

   if (bgra)
     color_size = GL_BGRA;

#ifdef GL_ARB_vertex_type_2_10_10_10_rev
   glVertexPointer( 4, GL_INT_2_10_10_10_REV, sizeof(verts[0]), 0 );
   glColorPointer( color_size, GL_UNSIGNED_INT_2_10_10_10_REV, sizeof(verts[0]), (void *)(sizeof(unsigned int)) );
#endif
}



static void Display( void )
{
   glClearColor(0.3, 0.3, 0.3, 1);
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

   glEnable(GL_VERTEX_PROGRAM_ARB);

   glDrawArrays( GL_TRIANGLES, 0, 3 );

   glFlush();
}


static void Reshape( int width, int height )
{
   glViewport( 0, 0, width, height );
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   glOrtho(-100.0, 100.0, -100.0, 100.0, -0.5, 1000.0);
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
}


static void Key( unsigned char key, int x, int y )
{
   (void) x;
   (void) y;
   switch (key) {
      case 27:
         exit(0);
         break;
   }
   glutPostRedisplay();
}

static GLenum Args(int argc, char **argv)
{
   GLint i;

   for (i = 1; i < argc; i++) {
      if (strcmp(argv[i], "-bgra") == 0) {
 	 bgra = GL_TRUE;
      } else {
         fprintf(stderr, "%s (Bad option).\n", argv[i]);
         return GL_FALSE;
      }
   }
   return GL_TRUE;
}

int main( int argc, char *argv[] )
{
   glutInit( &argc, argv );

   if (Args(argc, argv) == GL_FALSE) {
      exit(1);
   }

   glutInitWindowPosition( 0, 0 );
   glutInitWindowSize( 250, 250 );
   glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH );
   glutCreateWindow(argv[0]);
   gladLoadGL();
   glutReshapeFunc( Reshape );
   glutKeyboardFunc( Key );
   glutDisplayFunc( Display );

   gladLoadGL();
   Init();
   glutMainLoop();
   return 0;
}