GLuint compileShaders(char* pixelShaders, char* vertexShaders){ string vertexSourceStr = LoadFileToString(vertexShaders); string fragmentSourceStr = LoadFileToString(pixelShaders); const GLchar* vertexSource = vertexSourceStr.c_str(); const GLchar* fragmentSource = fragmentSourceStr.c_str(); GLint Result = GL_FALSE; int InfoLogLength; printf("Compiling shader : %s\n", vertexShaders); GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexSource, NULL); glCompileShader(vertexShader); // Check Vertex Shader glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &Result); glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &InfoLogLength); std::vector<char> VertexShaderErrorMessage(InfoLogLength); glGetShaderInfoLog(vertexShader, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]); printf("Compiling shader : %s\n", pixelShaders); GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentSource, NULL); glCompileShader(fragmentShader); // Check Fragment Shader glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &Result); glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &InfoLogLength); std::vector<char> FragmentShaderErrorMessage(InfoLogLength); glGetShaderInfoLog(fragmentShader, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]); fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]); printf("Linking program\n"); GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glBindFragDataLocation(shaderProgram, 0, "outColor"); glLinkProgram(shaderProgram); glUseProgram(shaderProgram); // Check the program glGetProgramiv(shaderProgram, GL_LINK_STATUS, &Result); glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &InfoLogLength); std::vector<char> ProgramErrorMessage(InfoLogLength + 1); glGetProgramInfoLog(shaderProgram, InfoLogLength, NULL, &ProgramErrorMessage[0]); fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); glDeleteShader(vertexShader); glDeleteShader(fragmentShader); return shaderProgram; }
bool CGraphmatFile :: LoadFileToGraphan (const string& CommandLine) { try { m_SourceFileName = CommandLine.c_str(); m_GraFileName = MakeFName (m_SourceFileName,"gra"); m_XmlMacSynName = MakeFName (m_SourceFileName,"xml"); m_SaveTxtName = MakeFName (m_SourceFileName,"tds"); if (IsHtmlFile(m_SourceFileName)) { HTML Convert(m_SourceFileName); string Text = Convert.getText(); if (!InitInputBuffer(Text)) { m_LastError = Format("Cannot init inpur buffer for %i bytes", Text.length()); return false; } if (m_bSaveHtmlFileToTdsFile) WriteVector(m_SaveTxtName, GetInputBuffer()); } else { if (access(m_SourceFileName.c_str(), 04) != 0) return false; string Text; LoadFileToString(m_SourceFileName, Text); if (!InitInputBuffer(Text)) { m_LastError = Format("Cannot init inpur buffer for %i bytes", Text.length()); return false; }; }; return GraphmatMain (); } catch (CExpc& C) { m_LastError = C.m_ErrorCode; return false; } catch (...) { m_LastError = "general exception"; return false; }; };
void CLemmatizer::ReadOptions(string FileName) { string Options; LoadFileToString(FileName, Options); StringTokenizer lines(Options.c_str(), "\r\n"); while (lines()) { string line = lines.val(); Trim(line); if (line.empty()) continue; if (line == "AllowRussianJo") m_bAllowRussianJo = true; }; };
// Attempts to open the specified file and read in formatted data. If an error occurs // while attempting to read from the file, will throw a FileReadException. If there is a syntax // error, will throw a SettingsFileSyntaxError with one of these types: // SettingsFile::SyntaxError::BAD_VARIABLE_NAME if there is a problem with the variable name; or // SettingsFile::SyntaxError::BAD_VALUE if there is a problem with a variable's value. SettingsFile::SettingsFile(const std::string& file_name) { std::ifstream file; // First, tell the file not to throw any exceptions. file.exceptions(std::ios_base::goodbit); // Now attempt to open the specified file name for reading. file.open(file_name.c_str(), std::ios_base::in); // Check to see if the file was successfully opened. If not, then throw a FileReadException. if(file.fail() == true) { file.close(); throw FileNotFoundException(file_name); } // At this point, the file should be ready for reading. Read the file into memory. StringVector lines; LoadFileToString(file, lines, file_name); // Close the file. file.close(); // Load the settings from the file. LoadSettings(lines, file_name); }
/** * Runs a test with the specified base filename. This will load * BaseFilename.hlsl from the test directory, compile it, and compare the output * with the contents of BaseFilename.out in the test directory. The test passes * the compilation output exactly matches the .out file. If the test fails, the * compilation output is written to BaseFilename.fail in the tests directory. * * true is returned if the test was run successfally, otherwise false is returned. */ bool RunTest(const char* BaseFilename) { char Filename[MAX_PATH]; EHlslShaderFrequency ShaderFrequency = HSF_InvalidFrequency; char* HlslSource = NULL; char* CompiledGlsl = NULL; char* ErrorLog = NULL; bool bPass = true; dprintf("Running %s...", BaseFilename); if (BaseFilename[0] == 'v' && BaseFilename[1] == 's' && BaseFilename[2] == '_') { ShaderFrequency = HSF_VertexShader; } else if (BaseFilename[0] == 'p' && BaseFilename[1] == 's' && BaseFilename[2] == '_') { ShaderFrequency = HSF_PixelShader; } else if (BaseFilename[0] == 'g' && BaseFilename[1] == 's' && BaseFilename[2] == '_') { ShaderFrequency = HSF_GeometryShader; } else if (BaseFilename[0] == 'h' && BaseFilename[1] == 's' && BaseFilename[2] == '_') { ShaderFrequency = HSF_HullShader; } else if (BaseFilename[0] == 'd' && BaseFilename[1] == 's' && BaseFilename[2] == '_') { ShaderFrequency = HSF_DomainShader; } else if (BaseFilename[0] == 'c' && BaseFilename[1] == 's' && BaseFilename[2] == '_') { ShaderFrequency = HSF_ComputeShader; } if (ShaderFrequency == HSF_InvalidFrequency) { dprintf("test must start with vs_, ps_, gs_, or cs\n"); return false; } _snprintf_s(Filename, MAX_PATH, "%s%s.hlsl", GTestDirectory, BaseFilename); HlslSource = LoadFileToString(Filename); if (HlslSource == NULL) { dprintf("can't open HLSL source '%s'\n", Filename); return false; } const int testConfigs = 2; struct TestConfig { EHlslCompileTarget target; const char* extension; const char* label; }; TestConfig targets[testConfigs] = { { HCT_FeatureLevelSM4, "", "GLSL 1.50" }, { HCT_FeatureLevelSM5, "_gl4", "GLSL 4.30" } }; dprintf( "\n"); for ( int i = 0; i < testConfigs; i++ ) { char OutFilename[MAX_PATH]; dprintf( " %s...", targets[i].label); unsigned int CCFlags = HLSLCC_PackUniforms | HLSLCC_DX11ClipSpace; FGlslCodeBackend GlslBackEnd(CCFlags); //@todo-rco: Fix me! FGlslLanguageSpec GlslLanguageSpec(false); HlslCrossCompile( Filename, HlslSource, "TestMain", ShaderFrequency, &GlslBackEnd, &GlslLanguageSpec, CCFlags, targets[i].target, &CompiledGlsl, &ErrorLog ); char* TestOutput = _asprintf( "----------------------------------------------------------------------\n" "%s\n----------------------------------------------------------------------\n%s", ErrorLog ? ErrorLog : "no errors", CompiledGlsl ? CompiledGlsl : "no compiler output" ); bool bTargetPass = false; bool bRebase = Rebase; if (TestOutput) { _snprintf_s(OutFilename, MAX_PATH, "%s%s%s.out", GTestDirectory, BaseFilename, targets[i].extension); char* ExpectedOutput = LoadFileToString(OutFilename); if (ExpectedOutput) { if (strcmp(TestOutput, ExpectedOutput) == 0) { dprintf("succeeded\n"); bTargetPass = true; } else { // check to see if it differs by just the version const char* TrimmedTestOutput; const char* TrimmedExpectedOutput; TrimmedTestOutput = strstr( TestOutput, "// Compiled by HLSLCC"); TrimmedExpectedOutput = strstr( ExpectedOutput, "// Compiled by HLSLCC"); TrimmedTestOutput = TrimmedTestOutput ? strstr( TrimmedTestOutput, "\n") : NULL; TrimmedExpectedOutput = TrimmedExpectedOutput ? strstr( TrimmedExpectedOutput, "\n") : NULL; if ( TrimmedTestOutput && TrimmedExpectedOutput && strcmp(TrimmedTestOutput, TrimmedExpectedOutput) == 0 ) { dprintf("conditional success, update version numbers\n"); bTargetPass = true; bRebase = bRebase & true; } else { dprintf("failed\n", Filename); } } free(ExpectedOutput); } else { dprintf("can't open expected output '%s'\n", OutFilename); // don't rebase files that don't have a former version bRebase = false; } if (!bTargetPass || bRebase) { FILE* fp = NULL; _snprintf_s(OutFilename, MAX_PATH, "%s%s%s.%s", GTestDirectory, BaseFilename, targets[i].extension, bRebase ? "out" : "fail"); fopen_s(&fp, OutFilename, "w"); if (fp) { fprintf(fp, "%s", TestOutput); fclose(fp); dprintf("\toutput written to '%s'\n", OutFilename); } else { dprintf("\toutput couldn't be written to '%s':\n%s\n", OutFilename, TestOutput); } } free(TestOutput); } if (CompiledGlsl) { free(CompiledGlsl); } if (ErrorLog) { free(ErrorLog); } //pass means all targets passed bPass &= bTargetPass; } return bPass; }
int main() { if (glfwInit() == false) { // did not succeed fprintf(stderr, "GLFW failed to initialize."); return -1; } // 4 AA glfwWindowHint(GLFW_SAMPLES, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); GLFWwindow* window; window = glfwCreateWindow(640, 480, "Mike's C++ OpenGL Demo", NULL, NULL); if (!window) { fprintf(stderr, "Window failed to create"); glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glewExperimental = true; if (glewInit() != GLEW_OK) { fprintf(stderr, "GLEW failed to initialize."); glfwTerminate(); return -1; } // generate VAO GLuint vaoID; glGenVertexArrays(1, &vaoID); glBindVertexArray(vaoID); std::string manRaw = LoadFileToString("man.raw"); std::vector<std::string> lines = split(manRaw, '\n'); std::vector<GLfloat> vertices; for (auto &line : lines) { if (line.empty()) continue; std::vector<std::string> coords = split(line, ' '); for (auto &coord : coords) { if (coord.empty()) continue; double d = atof(coord.c_str()); vertices.push_back(d); fprintf(stdout, "%s = %f\n", coord.c_str(), d); } fprintf(stdout, "EOL\n"); } // shaders GLuint vertShader = glCreateShader(GL_VERTEX_SHADER); GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER); const char* vertShaderSource = R"( varying vec3 vertex_color; void main(){ gl_Position = ftransform(); vertex_color = gl_Vertex.xyz; } )"; const char* fragShaderSource = R"( varying vec3 vertex_color; void main(){ gl_FragColor = vec4(vertex_color,1.0); } )"; glShaderSource(vertShader, 1, &vertShaderSource, NULL); glShaderSource(fragShader, 1, &fragShaderSource, NULL); glCompileShader(vertShader); glCompileShader(fragShader); GLuint program = glCreateProgram(); glAttachShader(program, vertShader); glAttachShader(program, fragShader); glLinkProgram(program); // generate vbo GLuint vboID; glGenBuffers(1, &vboID); glBindBuffer(GL_ARRAY_BUFFER, vboID); GLfloat* verts = vertices.data(); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(verts), verts, GL_STATIC_DRAW); fprintf(stdout, "size %d %d", vertices.size(), sizeof(verts)); int width, height; glfwGetWindowSize(window, &width, &height); glViewport(0, 0, width, height); glClearColor(0.2f, 0.2f, 0.2f, 1.0f); double x = 1.0f; double y = 1.0f; double rx = 1.0f; double ry = 1.0f; do { x = x + 0.01f; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // projection matrix as describing the attributes of your camera, such as field of view, focal length, fish eye lens, etc. glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(65.0, width / height, 1.0f, 64.0f); // ModelView matrix as where you stand with the camera and the direction you point it. glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); //gluLookAt(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f); glTranslated(x, y, 0.0f); //glRotated(180.0f*rx, 0.0f, 1.0f, 0.0f); //glRotated(180.0f*ry, 1.0f, 0.0f, 0.0f); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vboID); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); glUseProgram(program); glDrawArrays(GL_TRIANGLES, 0, vertices.size() / 3); glDisableVertexAttribArray(0); glPopMatrix(); glfwSwapBuffers(window); glfwPollEvents(); } while (glfwWindowShouldClose(window) == false); return 0; }