#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <locale>
+#include <sstream>
+#include <string>
#include <Eigen/Core>
#include "fp16.h"
{
const string full_pathname = *movit_data_directory + "/" + filename;
- static char buf[131072];
FILE *fp = fopen(full_pathname.c_str(), "r");
if (fp == NULL) {
perror(full_pathname.c_str());
exit(1);
}
- int len = fread(buf, 1, sizeof(buf), fp);
+ int ret = fseek(fp, 0, SEEK_END);
+ if (ret == -1) {
+ perror("fseek(SEEK_END)");
+ exit(1);
+ }
+
+ int size = ftell(fp);
+
+ ret = fseek(fp, 0, SEEK_SET);
+ if (ret == -1) {
+ perror("fseek(SEEK_SET)");
+ exit(1);
+ }
+
+ string str;
+ str.resize(size);
+ ret = fread(&str[0], size, 1, fp);
+ if (ret == -1) {
+ perror("fread");
+ exit(1);
+ }
+ if (ret == 0) {
+ fprintf(stderr, "Short read when trying to read %d bytes from %s\n",
+ size, full_pathname.c_str());
+ exit(1);
+ }
fclose(fp);
- return string(buf, len);
+ return str;
}
string read_version_dependent_file(const string &base, const string &extension)
GLint status;
glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
+ fprintf(stderr, "Failed to compile shader: %s\n", shader_src.c_str());
exit(1);
}
string output_glsl_mat3(const string &name, const Eigen::Matrix3d &m)
{
- char buf[1024];
- sprintf(buf,
- "const mat3 %s = mat3(\n"
- " %.8f, %.8f, %.8f,\n"
- " %.8f, %.8f, %.8f,\n"
- " %.8f, %.8f, %.8f);\n\n",
- name.c_str(),
- m(0,0), m(1,0), m(2,0),
- m(0,1), m(1,1), m(2,1),
- m(0,2), m(1,2), m(2,2));
- return buf;
+ // Use stringstream to be independent of the current locale in a thread-safe manner.
+ stringstream ss;
+ ss.imbue(locale("C"));
+ ss.precision(8);
+ ss << scientific;
+ ss << "const mat3 " << name << " = mat3(\n";
+ ss << " " << m(0,0) << ", " << m(1,0) << ", " << m(2,0) << ",\n";
+ ss << " " << m(0,1) << ", " << m(1,1) << ", " << m(2,1) << ",\n";
+ ss << " " << m(0,2) << ", " << m(1,2) << ", " << m(2,2) << ");\n\n";
+ return ss.str();
+}
+
+string output_glsl_float(const string &name, float x)
+{
+ // Use stringstream to be independent of the current locale in a thread-safe manner.
+ stringstream ss;
+ ss.imbue(locale("C"));
+ ss.precision(8);
+ ss << scientific;
+ ss << "const float " << name << " = " << x << ";\n";
+ return ss.str();
+}
+
+string output_glsl_vec2(const string &name, float x, float y)
+{
+ // Use stringstream to be independent of the current locale in a thread-safe manner.
+ stringstream ss;
+ ss.imbue(locale("C"));
+ ss.precision(8);
+ ss << scientific;
+ ss << "const vec2 " << name << " = vec2(" << x << ", " << y << ");\n";
+ return ss.str();
+}
+
+string output_glsl_vec3(const string &name, float x, float y, float z)
+{
+ // Use stringstream to be independent of the current locale in a thread-safe manner.
+ stringstream ss;
+ ss.imbue(locale("C"));
+ ss.precision(8);
+ ss << scientific;
+ ss << "const vec3 " << name << " = vec3(" << x << ", " << y << ", " << z << ");\n";
+ return ss.str();
}
template<class DestFloat>