virtual void OnInit() {
FILE *fp = fopen("test.pts", "r");
if(!fp){
std::cout << "Unable to open file" << std::endl;
return;
}
char buffer[256];
char str[256];
static const int max = 100;
if(!fgets(buffer, sizeof(buffer), fp)) return;
str[0]='\0';
sscanf(buffer, "%s", str);
// Create system memory buffer
sscanf(buffer, "%d", &nPoints);
triangle_vertices = new GLfloat[nPoints*3];
triangle_colors = new GLfloat[nPoints];
size_t i_ind = 0, v_ind = 0;
float _xval, _yval, _zval;
float min_x, min_y;
float max_x, max_y;
while(i_ind < nPoints)
{
if(!fgets(buffer, sizeof(buffer), fp))
break;
std::cout << (i_ind/nPoints)*100 << "%\r";
sscanf(buffer, "%f%f%f", &_xval, &_yval, &_zval);
if(i_ind == 0)
{
min_x = max_x = _xval;
min_y = max_y = _yval;
}
else
{
if (_xval < min_x)
min_x = _xval;
if (_xval > max_x)
max_x = _xval;
if (_yval < min_y)
min_y = _yval;
if (_yval > max_y)
max_y = _yval;
}
triangle_vertices[v_ind++] = _xval;
triangle_vertices[v_ind++] = _yval;
triangle_vertices[v_ind++] = _zval;
triangle_colors[i_ind++] = _zval;
}
fclose(fp);
glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
shader = SM.loadfromFile("vertexshader.txt","fragmentshader.txt"); // load (and compile, link) from file
if (shader==0)
std::cout << "Error Loading, compiling or linking shader\n";
const char *uniAttr="texture_id";
attr_Utexture_id = shader->GetUniformLocation(uniAttr);
if(attr_Utexture_id == -1)
{
std::cout << "Could not bind texture sampler" << std::endl;
return;
}
/* lookup texture */
glGenTextures(1, &attr_Utexture_id); /* GL_TEXTURE0 */
glBindTexture(GL_TEXTURE_1D,attr_Utexture_id);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_1D);
unsigned char colors[3*32];
colors[0] = 255; colors[1] = 0; colors[2] = 0; /* red */
colors[3] = 0; colors[4] = 0; colors[5] = 255; /* green */
//colors[6] = 0; colors[7] = 0; colors[8] = 255; /* blue */
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 6, 0, GL_RGB, GL_UNSIGNED_BYTE, colors);
glGenBuffers(1, &vbo_triangle);
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle);
glBufferData(GL_ARRAY_BUFFER, nPoints*3*sizeof(GLfloat), triangle_vertices, GL_STATIC_DRAW);
glGenBuffers(1, &vbo_triangle_colors);
glBindBuffer(GL_ARRAY_BUFFER, vbo_triangle_colors);
glBufferData(GL_ARRAY_BUFFER, nPoints*sizeof(GLfloat), triangle_colors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
const char* attribute_name="coord2d";
attribute_coord2d = shader->GetAttribLocation(attribute_name);
if(attribute_coord2d == -1){
std::cout << "Could not bind attribute " << attribute_name << std::endl;
return;
}
attribute_name="v_color";
attrib_v_color = shader->GetAttribLocation(attribute_name);
if(attrib_v_color == -1){
std::cout << "Could not bind attribute " << attribute_name << std::endl;
return;
}
}