예제 #1
0
Program::Program(int files, char** fileNames, char *options) {
    program = -1;
    vertexShader = -1;
    fragmentShader = -1;

    const char **contents = (const char**) malloc((files + 2) * sizeof(char*));

    int i;
    for (i = 0; i < files; ++i) {
        contents[i + 2] = readFile(fileNames[i]);
    }

    

    if (program == -1) {
        program = glCreateProgram();
        vertexShader = glCreateShader(GL_VERTEX_SHADER);
        fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        glAttachShader(program, vertexShader);
        glAttachShader(program, fragmentShader);
        assert(glGetError() == 0);
    }

    contents[0] = "#define _VERTEXSHADER_\n";
    contents[1] = options == NULL ? "" : options;
    glShaderSource(vertexShader, files + 2, contents, NULL);
    glCompileShader(vertexShader);
    checkShader(vertexShader);
    assert(glGetError() == 0);

    contents[0] = "#define _FRAGMENTSHADER_\n";
    glShaderSource(fragmentShader, files + 2, contents, NULL);
    glCompileShader(fragmentShader);
    checkShader(fragmentShader);
    assert(glGetError() == 0);

    for (i = 0; i < files; ++i) {
        free((void*) contents[i + 2]);
    }

    glBindAttribLocation(program, 1, "normal");
    glBindAttribLocation(program, 2, "color");

    glLinkProgram(program);
    GLint logLength;
    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
    if (logLength > 0) {
        char *log = new char[logLength];
        glGetProgramInfoLog(program, logLength, &logLength, log);
        printf("%s", log);
    }

    if (checkProgram(program)) {
        assert(glGetError() == 0);
    } else {
        printShaderLog(vertexShader);
        printShaderLog(fragmentShader);
     //   exit(-1);
    }
}
예제 #2
0
GLuint setShaders(char * vert, char * frag) 
{
	GLuint v, f, pro;
	char *vs, *fs;

	v = glCreateShader(GL_VERTEX_SHADER);
	f = glCreateShader(GL_FRAGMENT_SHADER);

	vs = textFileRead(vert);
	fs = textFileRead(frag);

	const char * vv = vs;
	const char * ff = fs;

	glShaderSource(v, 1, &vv, NULL);
	glShaderSource(f, 1, &ff, NULL);

	free(vs); free(fs);

	glCompileShader(v);
	glCompileShader(f);

	printShaderLog(v);
	printShaderLog(f);

	pro = glCreateProgram();
	glAttachShader(pro, v);
	glAttachShader(pro, f);

	glLinkProgram(pro);
	printProgramLog(pro);

	return(pro);
}
예제 #3
0
/**
 * Compiles all GLSL shaders
 */
void Resources::compileShaders() {
  GLuint vert, frag; // Vertex and fragment shader handles
  for(int i = 0; i < NUM_SHADERS; i++) {
    // Create handles for program and shaders
    programs[i] = glCreateProgram();
    vert = glCreateShader(GL_VERTEX_SHADER);
    frag = glCreateShader(GL_FRAGMENT_SHADER);
    // Read shaders from files
    char *vs_source = readShader(vertex_shaders[i]);
    char *fs_source = readShader(fragment_shaders[i]);
    // Loader shader sources
    glShaderSource(vert, 1, (const char**)&vs_source, NULL);
    glShaderSource(frag, 1, (const char**)&fs_source, NULL);
    // Compile shaders
    glCompileShader(vert);
    printShaderLog(vert);
    glCompileShader(frag);
    printShaderLog(frag);
    // Attach shaders to program
    glAttachShader(programs[i], vert);
    glAttachShader(programs[i], frag);
    // Link program
    glLinkProgram(programs[i]);
    printProgramLog(programs[i]);

    // Free memory from shader files
    delete[] vs_source;
    delete[] fs_source;
  }
}
예제 #4
0
void setupShaders()
{
   v = glCreateShader(GL_VERTEX_SHADER);
   f = glCreateShader(GL_FRAGMENT_SHADER);

   vs = textFileRead("shader.vert");
   fs = textFileRead("shader.frag");

   const char * ff = fs;
   const char * vv = vs;

   glShaderSource(v, 1, &vv, NULL);
   glShaderSource(f, 1, &ff, NULL);

   free(vs); free(fs);

   glCompileShader(v);
   glCompileShader(f);

   printShaderLog(v);
   printShaderLog(f);

   p = glCreateProgram();
   glAttachShader(p, f);
   glAttachShader(p, v);

   glLinkProgram(p);
   printProgramLog(p);
}
예제 #5
0
//--------------------------------------------------------------
// compile OpenGL21 shader  
GLuint mgWinGL21Support::compileShader(
  const char* source,
  GLenum shaderType)
{
  // Create shader objects
  GLuint shader = glCreateShader(shaderType);
  const GLchar* strings[1];
  strings[0] = (GLchar *) source;
  glShaderSource(shader, 1, strings, NULL);

  glCompileShader(shader);
  mgDebug("%s shader log:", (shaderType == GL_VERTEX_SHADER) ? "vertex" : "fragment");
  printShaderLog(shader);

  // Check for errors
  GLint testVal;
  glGetShaderiv(shader, GL_COMPILE_STATUS, &testVal);
  if (testVal == GL_FALSE)
  {
    mgDebug("%s shader compilation failed.", (shaderType == GL_VERTEX_SHADER) ? "vertex" : "fragment");
    return 0;
  }

  return shader;
}
예제 #6
0
/**
 * Compile the shader from file 'filename', with error handling
 */
int Shader::loadShader(string fileName){
	GLint link_ok = GL_FALSE;
	GLuint vs, fs;

	string vsName = fileName+".v.glsl";
	string fsName = fileName+".f.glsl";

	if ((vs = createShader(vsName.c_str(), GL_VERTEX_SHADER))   == 0) return 0;
	if ((fs = createShader(fsName.c_str(), GL_FRAGMENT_SHADER)) == 0) return 0;
	program = glCreateProgram();
	glAttachShader(program, vs);
	glAttachShader(program, fs);
	glLinkProgram(program);

	glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
	if (!link_ok) {
		logWar("glLinkProgram:");
		printShaderLog(program);
        glDeleteShader(vs);
        glDeleteShader(fs);
		return -1;
	}
    glDeleteShader(vs);
    glDeleteShader(fs);
	loadVars();
    return 0;
}
예제 #7
0
GLuint AntSim::shaderLoadFromFile(std::string path, GLenum shaderType)
{
    std::string stringShader;
    std::ifstream fileShader(path.c_str());

    if (fileShader)
    {
        stringShader.assign((std::istreambuf_iterator<char>(fileShader)), std::istreambuf_iterator<char>());

        GLuint shader = glCreateShader(shaderType);

        const GLchar* pntStringSource = stringShader.c_str();

        glShaderSource(shader, 1, static_cast<const GLchar**>(&pntStringSource), 0);
        glCompileShader(shader);

        GLint shaderCompiled = GL_FALSE;
        glGetShaderiv(shader, GL_COMPILE_STATUS, &shaderCompiled);

        if (shaderCompiled != GL_TRUE)
        {
            std::cout << "Unable to compile shader " << shader << " (path: " << path << ")" << std::endl;
            printShaderLog(shader);
            glDeleteShader(shader);
            return 0;
        }
        return shader;
    }
    else
    {
        std::cout << "Could not open file " << path.c_str() << std::endl;
        return 0;
    }
}
예제 #8
0
/**
 * Compile the shader from file 'filename', with error handling
 */
GLuint Shader::createShader(const char* filename, GLenum type)
{
	if(FileSystem::getInstance()->openFile(filename) == -1){
		logErr("Error opening %s: ", filename);
		return 0;
	}
	const char* source = FileSystem::getInstance()->getFileData(filename);
	if (source == NULL) {
		logErr("Error data %s: ", filename);
		return 0;
	}

	logInf("filename shader: %s",filename);
	GLuint res = glCreateShader(type);
	glShaderSource(res, 1, &source, NULL);

    FileSystem::getInstance()->destroyFileData(filename);
	glCompileShader(res);
	GLint compile_ok = GL_FALSE;
	glGetShaderiv(res, GL_COMPILE_STATUS, &compile_ok);
	if (compile_ok == GL_FALSE) {
		logErr("%s:", filename);
		printShaderLog(res);
		glDeleteShader(res);
		return 0;
	}

    
	return res;
}
예제 #9
0
void checkShader(GLuint s) {
    GLint compiled;
    glGetShaderiv(s, GL_COMPILE_STATUS, &compiled);
    if (!compiled) {
        printShaderLog(s);
    //    exit(-1);
    }
}
예제 #10
0
//loades the shaders into a program, retruning the programs id
GLuint glslLoadShaderProgram( std::string vertex, std::string fragment ){
	//a quick check
	if( vertex.empty() || fragment.empty() ){
		printf( "ERROR: Unable to load programs, program defined as NULL" );
		return 0;
	}

	printf( "INFO: Loading Shaders %s %s\n", vertex.c_str(), fragment.c_str());

	//temp ids
	GLuint vsID, fsID, program;

	//make the vertex shader
	const char* vertex_data = ReadShaderFile( vertex );
	vsID = glCreateShader( GL_VERTEX_SHADER );
	glShaderSource( vsID, 1, &vertex_data, NULL );
	glCompileShader( vsID );
	printShaderLog( vsID );

	delete [] vertex_data;

	//make the fragment shader
	const char * fragment_data = ReadShaderFile( fragment );
	fsID = glCreateShader( GL_FRAGMENT_SHADER );
	glShaderSource( fsID, 1, &fragment_data, NULL );
	glCompileShader( fsID );
	printShaderLog( fsID );

	delete [] fragment_data;

	//make and link the program
	program = glCreateProgram();
	glAttachShader( program, vsID );
	glAttachShader( program, fsID );

	glBindFragDataLocationEXT( program, 0, "pixelColor" );

	glLinkProgram( program );
	printProgramLog( program );

	//and return, best of luck
	return program;
}
예제 #11
0
파일: main.cpp 프로젝트: jhk2/glsandbox
void loadShaderFromSource(GLenum type, std::string sourcefile)
{
	std::stringstream ss;
	std::ifstream file(sourcefile.c_str());
	std::string line;
	if (file.is_open() && file.good())
	{
		getline(file, line);
		// skip past whitespace at the beginning
		while (file.good() && (strcmp(line.c_str(), "\n")==0 || strcmp(line.c_str(), "")==0))
		{
			getline(file, line);
		}
		// #define the shader type
		switch (type)
		{
			case GL_FRAGMENT_SHADER:
				ss << "#define _FRAGMENT_" << std::endl;
				break;
			case GL_VERTEX_SHADER:
				ss << "#define _VERTEX_" << std::endl;
				break;
			case GL_GEOMETRY_SHADER:
				ss << "#define _GEOMETRY_" << std::endl;
				break;
			case GL_TESS_CONTROL_SHADER:
				ss << "#define _TESSCONTROL_" << std::endl;
				break;
			case GL_TESS_EVALUATION_SHADER:
				ss << "#define _TESSEVAL_" << std::endl;
				break;
			default:
				break;
		}
		ss << line << std::endl;
		while (file.good())
		{
			getline(file, line);
			ss << line << std::endl;
		}
		file.close();
		// load into gl
		std::string str = ss.str();
		//printf("shader source is:\n%s\n", str);
		const int length = str.length();
		const char *text = str.c_str();
		GLuint id = glCreateShader(type);
		glShaderSource(id, 1, (const char **)&text, &length);
		glCompileShader(id);
		printShaderLog(id);
		glAttachShader(g_shader.id, id);
		glDeleteShader(id);
		g_shader.progs.push_back(id);
	}
}
예제 #12
0
GLuint Shader::compileShader(GLenum shader_type, const char* file_path)
{
    std::string s = readFile(file_path);
    const char* shader_src = s.c_str();
    GLuint shader = glCreateShader(shader_type);
    GLint len = s.length();
    glShaderSource(shader, 1, &shader_src, &len);
    glCompileShader(shader);
    printShaderLog(shader, file_path);
    return shader;
}
예제 #13
0
bool shaderProgram::loadProgramFromSource(const char *vertexSource, const char *fragmentSource) {
    assert(vertexSource != nullptr);
    assert(fragmentSource != nullptr);

    programID = glCreateProgram();
    GLint compiled = 0;

    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexSource, nullptr);
    glCompileShader(vertexShader);

    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compiled);
    if (!compiled) {
        printShaderLog(vertexShader, "vertex");
        return false;
    }

    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentSource, nullptr);
    glCompileShader(fragmentShader);

    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compiled);
    if (!compiled) {
        printShaderLog(fragmentShader, "fragment");
        return false;
    }

    glBindFragDataLocation(programID, 0, "color");

    glAttachShader(programID, vertexShader);
    glAttachShader(programID, fragmentShader);

    glLinkProgram(programID);
    bindAddresses();
    return true;
}
예제 #14
0
파일: GLSLShader.cpp 프로젝트: hoijui/CG1
GLuint createShaderFromSource(GLuint stype, const std::string& src)
{
	GLuint S = glCreateShader(stype);
	const GLchar* src_cstr = src.c_str();

	glShaderSource(S, 1, &src_cstr, 0);
	glCompileShader(S);

	if(!check(S))
	{
		printShaderLog(S);
		glDeleteShader(S);
		return 0;
	}
	return S;
}
예제 #15
0
파일: GLSLShader.cpp 프로젝트: hoijui/CG1
GLuint createShader(GLuint stype, const std::string& file)
{
	string str = readShaderSource(file);
	assert(!str.empty());

	if(!str.empty())
	{
		GLuint shader = createShaderFromSource(stype, str);
		if(!check(shader))
		{
			cerr << file << endl;
			printShaderLog(shader);
			glDeleteShader(shader);
			return 0;
		}
		return shader;
	}

	return 0;
}
예제 #16
0
GLuint ShaderManager::loadShaderFromFile(std::string path, GLenum shaderType)
{
	// Open file
	GLuint shaderID = 0;
	std::string shaderString;
	std::ifstream sourceFile(path.c_str());

	// Source file loaded
	if (sourceFile)
	{
		// Get shader source
		shaderString.assign((std::istreambuf_iterator< char >(sourceFile)), std::istreambuf_iterator< char >());

		// Create shader ID
		shaderID = glCreateShader(shaderType);

		// Set shader source
		const GLchar* shaderSource = shaderString.c_str();
		glShaderSource(shaderID, 1, (const GLchar**)&shaderSource, NULL);

		// Compile shader source
		glCompileShader(shaderID);

		// Check shader for errors
		GLint shaderCompiled = GL_FALSE;
		glGetShaderiv(shaderID, GL_COMPILE_STATUS, &shaderCompiled);
		if (shaderCompiled != GL_TRUE)
		{
			printf("Unable to compile shader %d!\n\nSource:\n%s\n", shaderID, shaderSource);
			printShaderLog(shaderID);
			glDeleteShader(shaderID);
			shaderID = 0;
		}
	}
	else
	{
		printf("Unable to open file %s\n", path.c_str());
	}

	return shaderID;
}
예제 #17
0
GLuint ShaderManager::loadShaderFromFile( const string& filename, const GLenum& shaderType )
{
	Log::InfoFmt(getClassName(), "Load from file \"%s\"", filename.c_str());
	GLuint shader = 0;
	string shaderString;
	std::ifstream file(filename);

	if (file)
	{
		shaderString.assign( (std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>() );
		shader = glCreateShader(shaderType);

		const GLchar* shaderSource = shaderString.c_str();

		glShaderSource(shader, 1, (const GLchar**)&shaderSource, NULL);

		glCompileShader(shader);

		GLint shaderCompiled = GL_FALSE;
		glGetShaderiv(shader, GL_COMPILE_STATUS, &shaderCompiled);
		if (shaderCompiled != GL_TRUE)
		{
			Log::ErrorFmt(getClassName(), "Failed to compile shader %d \"%s", shader, filename.c_str());
			printShaderLog(shader);
			glDeleteShader(shader);
			shader = 0;
		}
	}
	else
	{
		Log::ErrorFmt(getClassName(), "Unable to open file \"%s\"", filename.c_str());
		return 0;
	}

	Log::InfoFmt(getClassName(), "Shader \"%s\" Loaded", filename.c_str());

	return shader;
}
예제 #18
0
void Shader::addProgram(std::string text, int type)
{
	int shader = glCreateShader(type); //actually a gluint

	if (shader == 0)
	{
		//error
	}

	//Set source
	const char *cstring = text.c_str();
	glShaderSource(shader, 1, &cstring, NULL);
	glCompileShader(shader);

	GLint fShaderCompiled = GL_FALSE;
	glGetShaderiv(shader, GL_COMPILE_STATUS, &fShaderCompiled);
	if (fShaderCompiled != GL_TRUE)
	{
		printf("Unable to compile fragment shader %d!\n", shader);
		printShaderLog(shader);
		//success = false;
	}


	/*
	//GLint status = 0;
	//glGetShaderiv(shader, GL_COMPILE_STATUS, &status);

	if (status == 0)
	{
		//glGetShaderInfoLog(shader, )
	}
	*/

	glAttachShader(gProgramID, shader);
}
예제 #19
0
파일: MD5.cpp 프로젝트: zeebok/MD5loader
bool MD5::loadShader(const char* vshader, const char* fshader)
{
    GLuint vertShader;
    GLuint fragShader;
    GLint vsSize;
    GLint fsSize;
    char* vsBuffer;
    char* fsBuffer;

    // Load Vertex Shader
    FILE* vs = fopen(vshader, "r");
    if(NULL == vs)
    {
        fprintf(stderr, "Cannot open vertex shader\n");
        return false;
    }
    fseek(vs, 0, SEEK_END);
    vsSize = ftell(vs);
    rewind(vs);
    vsBuffer = new char[vsSize];
    if(0 == vsBuffer)
        return false;
    if(vsSize != fread(vsBuffer, 1, vsSize, vs))
        return false;
    fclose(vs);

    // Load Fragment Shader
    FILE* fs = fopen(fshader, "r");
    if(NULL == fs)
    {
        fprintf(stderr, "Cannot open fragment shader\n");
        return false;
    }
    fseek(fs, 0, SEEK_END);
    fsSize = ftell(fs);
    rewind(fs);
    fsBuffer = new char[fsSize];
    if(0 == fsBuffer)
        return false;
    if(fsSize != fread(fsBuffer, 1, fsSize, fs))
        return false;

    // Create Vertex and Fragment Shaders
    vertShader = glCreateShader(GL_VERTEX_SHADER);
    fragShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(vertShader, 1, (const GLchar**)&vsBuffer, &vsSize);
    glShaderSource(fragShader, 1, (const GLchar**)&fsBuffer, &fsSize);

    //Compile Shaders
    GLint compiled;
    glCompileShader(vertShader);
    glGetShaderiv(vertShader, GL_COMPILE_STATUS, &compiled);
    if(compiled == false)
    {
        fprintf(stderr, "Vert Shader failed to compile\n");
        printShaderLog(vertShader);
        glDeleteShader(vertShader);
        vertShader = 0;
        glDeleteShader(fragShader);
        fragShader = 0;

        return false;
    }
    glCompileShader(fragShader);
    glGetShaderiv(fragShader, GL_COMPILE_STATUS, &compiled);
    if(compiled == false)
    {
        fprintf(stderr, "Frag Shader failed to compile\n");
        printShaderLog(fragShader);
        glDeleteShader(vertShader);
        vertShader = 0;
        glDeleteShader(fragShader);
        fragShader = 0;

        return false;
    }

    // Create and link GLSL program
    shaderProgram = glCreateProgram();

    glAttachShader(shaderProgram, vertShader);
    glAttachShader(shaderProgram, fragShader);

    GLint linked;
    glLinkProgram(shaderProgram);
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linked);
    if(linked == false)
    {
        fprintf(stderr, "Program linking failed\n");
        glDetachShader(shaderProgram, vertShader);
        glDetachShader(shaderProgram, fragShader);
        glDeleteShader(vertShader);
        glDeleteShader(fragShader);
        vertShader = 0;
        fragShader = 0;
        shaderProgram = 0;

        return false;
    }

    if(!GLEW_ARB_uniform_buffer_object)
    {
        jpLoc = glGetUniformLocation(shaderProgram, "jPos");
        joLoc = glGetUniformLocation(shaderProgram, "jOrt");
    }

    mvpLoc = glGetUniformLocation(shaderProgram, "mvpMatrix");

    // Clean up
    glDeleteShader(vertShader);
    glDeleteShader(fragShader);
    delete vsBuffer;
    delete fsBuffer;

    return true;
}
예제 #20
0
bool InitializeShader(Shader_T *s, const char *vsfile, const char *fsfile)
{
	bool success = true;
	GLuint vertexShader;
	GLuint fragmentShader;
	GLint vShaderCompiled = GL_FALSE;
	GLint fShaderCompiled = GL_FALSE;
	GLint programSuccess = GL_TRUE;
	GLchar *txt;
	unsigned long len;	
	//Generate program
	s->id = glCreateProgram();
	s->vertex_attrib = -1;
	s->uv_attrib = -1;
	//Create vertex shader
	vertexShader = glCreateShader( GL_VERTEX_SHADER );
	//Set vertex source
	LoadShaderSource(vsfile, &txt, &len);
	glShaderSource( vertexShader, 1, &txt, NULL );
	//Compile vertex source
	glCompileShader( vertexShader );
	ValidateShader(vertexShader, vsfile);
	UnloadShaderSource(&txt);
	//Check vertex shader for errors
	glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &vShaderCompiled );
	if( vShaderCompiled != GL_TRUE )
	{
		printf( "Unable to compile vertex shader %d!\n", vertexShader );
		printShaderLog( vertexShader );
        success = false;
	}
	else
	{
		//Attach vertex shader to program
		glAttachShader( s->id, vertexShader );
		//Create fragment shader
		LoadShaderSource(fsfile, &txt, &len);
		fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
		//Set fragment source
		glShaderSource( fragmentShader, 1, &txt, NULL );
		//Compile fragment source
		glCompileShader( fragmentShader );
		ValidateShader(fragmentShader, fsfile);
		UnloadShaderSource(&txt);
		//Check fragment shader for errors
		glGetShaderiv( fragmentShader, GL_COMPILE_STATUS, &fShaderCompiled );
		if( fShaderCompiled != GL_TRUE )
		{
			printf( "Unable to compile fragment shader %d!\n", fragmentShader );
			printShaderLog( fragmentShader );
			success = false;
		}
		else
		{
			//Attach fragment shader to program
			glAttachShader( s->id, fragmentShader );
			//Link program
			glLinkProgram( s->id );
			//Check for errors
			glGetProgramiv( s->id, GL_LINK_STATUS, &programSuccess );
			if( programSuccess != GL_TRUE )
			{
				printf( "Error linking program %d!\n", s->id );
				printProgramLog( s->id );
				success = false;
			}
			else
			{
				//Get vertex attribute location
				s->uv_attrib = glGetAttribLocation( s->id, "vertexUV" );
				s->vertex_attrib = glGetAttribLocation( s->id, "vertexPosition" );
				if( s->vertex_attrib == -1 )
				{
					printf( "vertexPosition is not a valid glsl program variable!\n" );
					success = false;
				}
				if(s->uv_attrib == -1 )
				{
					printf( "vertexTex is not a valid glsl program variable!\n" );
					success = false;
				}
			}
		}
	}
	return success;
}
예제 #21
0
void Shader::TestRemove()
{
	//Success flag
	bool success = true;
	


	//Generate program
	gProgramID = glCreateProgram();

	//Create vertex shader
	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);

	//Get vertex source
	const GLchar* vertexShaderSource[] =
	{
	"#version 140\nin vec2 LVertexPos2D; void main() { gl_Position = vec4( LVertexPos2D.x, LVertexPos2D.y, 0, 1 ); }"
	};

	//Set vertex source
	glShaderSource(vertexShader, 1, vertexShaderSource, NULL);

	//Compile vertex source
	glCompileShader(vertexShader);

	//Check vertex shader for errors
	GLint vShaderCompiled = GL_FALSE;
	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vShaderCompiled);
	if (vShaderCompiled != GL_TRUE)
	{
	printf("Unable to compile vertex shader %d!\n", vertexShader);
	printShaderLog(vertexShader);
	success = false;
	}

	//Attach vertex shader to program
	glAttachShader(gProgramID, vertexShader);


	//Create fragment shader
	GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

	//Get fragment source
	const GLchar* fragmentShaderSource[] =
	{
	"#version 140\nout vec4 LFragment; void main() { LFragment = vec4( 1.0, 0.0, 1.0, 1.0 ); }"
	};

	//Set fragment source
	glShaderSource(fragmentShader, 1, fragmentShaderSource, NULL);

	//Compile fragment source
	glCompileShader(fragmentShader);

	//Check fragment shader for errors
	GLint fShaderCompiled = GL_FALSE;
	glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fShaderCompiled);
	if (fShaderCompiled != GL_TRUE)
	{
	printf("Unable to compile fragment shader %d!\n", fragmentShader);
	printShaderLog(fragmentShader);
	success = false;
	}

	//Attach fragment shader to program
	glAttachShader(gProgramID, fragmentShader);


	//Link program
	glLinkProgram(gProgramID);

	//Check for errors
	GLint programSuccess = GL_TRUE;
	glGetProgramiv(gProgramID, GL_LINK_STATUS, &programSuccess);
	if (programSuccess != GL_TRUE)
	{
	printf("Error linking program %d!\n", gProgramID);
	printProgramLog(gProgramID);
	success = false;
	}

	//Initialize clear color
	glClearColor(1.f, 0.f, 0.f, 1.f);
	

	
}
bool initGL()
{
	bool success = true;

	//Generate program
	gProgramID = glCreateProgram();

	//Create vertex shader
	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);

	//Get vertex source
	const GLchar* vertexShaderSource[] =
	{
		"#version 140\nin vec2 LVertexPos2D; void main(){gl_Position = vec4( LVertexPos2D.x, LVertexPos2D.y, 0, 1 ); }"
	};

	//Set vertex shource
	glShaderSource(vertexShader, 1, vertexShaderSource, NULL);

	//Compile vertex source
	glCompileShader(vertexShader);

	//Check vertex shader for errors
	GLint vShaderCompiled = GL_FALSE;
	glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vShaderCompiled);
	if (vShaderCompiled != GL_TRUE)
	{
		printf("Unable to compile vertext shader %d!\n", vertexShader);
		printShaderLog(vertexShader);
		success = false;
	}
	else
	{
		//Attach vertext shader to program
		glAttachShader(gProgramID, vertexShader);

		//Create fragment shader
		GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

		//Get fragment source
		const GLchar* fragmentShaderSource[] = 
		{
			"#version 140\nout vec4 LFragment; void main(){LFragment = vec4( 1.0, 1.0, 1.0, 1.0);}"
		};

		//Set fragment source
		glShaderSource(fragmentShader, 1, fragmentShaderSource, NULL);

		//Compile fragment source
		glCompileShader(fragmentShader);

		//Check fragment shader for errors
		GLint fShaderCompiled = GL_FALSE;
		glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fShaderCompiled);
		if (fShaderCompiled != GL_TRUE)
		{
			printf("Unable to compile fragment shader %d!\n", fragmentShader);
			printShaderLog(fragmentShader);
			success = false;
		}
		else
		{
			//Attach fragment shader to program
			glAttachShader(gProgramID, fragmentShader);

			//Link program
			glLinkProgram(gProgramID);

			//Check for errors
			GLint programSuccess = GL_TRUE;
			glGetProgramiv(gProgramID, GL_LINK_STATUS, &programSuccess);
			if (programSuccess != GL_TRUE)
			{
				printf("Error linking program %d\n", gProgramID);
				printProgramLog(gProgramID);
				success = false;
			}
			else
			{
				//Get vertext attribute location
				gVertexPos2DLocation = glGetAttribLocation(gProgramID, "LVertexPos2D");
				if (gVertexPos2DLocation == -1)
				{
					printf("LVertexPos2D is not a valid glsl program variable!\n");
					success = false;
				}
				else
				{
					//Initalize clear color
					glClearColor(0.f, 0.f, 0.f, 1.f);

					//VBO data
					GLfloat vertexData[] = 
					{
						-0.5f, -0.5f,
						0.5f, -0.5f,
						0.5f,  0.5f,
						-0.5f,  0.5f
					};

					//IBO data
					GLuint indexData[] = { 0,1,2,3 };

					//Create VBO
					glGenBuffers(1, &gVBO);
					glBindBuffer(GL_ARRAY_BUFFER, gVBO);
					glBufferData(GL_ARRAY_BUFFER, 2 * 4 * sizeof(GLfloat), vertexData, GL_STATIC_DRAW);

					//Create IBO
					glGenBuffers(1, &gIBO);
					glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gIBO);
					glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLuint), indexData, GL_STATIC_DRAW);
				}//end gVertexPos2DLocation
			}//end program success
		}//end if fShaderCopiled
	}//end vShader compiled

	return success;
}
예제 #23
0
Text::Text(FT_Face face, int height){
    c = new glyphData[256];
	color = glm::vec4(0.0f,0.0f,0.0f,1.0f);
	cIndex = 0;
	GLuint vs, fs;
	GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
	if(FileSystem::getInstance()->openFile("text.v.glsl") == -1){
		logErr("Error opening text.v.glsl");
		return;
	}
	if(FileSystem::getInstance()->openFile("text.f.glsl") == -1){
		logErr("Error opening text.f.glsl");
		return;
	}
	const char* source = FileSystem::getInstance()->getFileData("text.v.glsl");
	if (source == NULL){
		logErr("Error data text.v.glsl");
		return;
	}

	vs = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vs, 1, &source, NULL);

	glCompileShader(vs);
	compile_ok = GL_FALSE;
	glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
	if(compile_ok == GL_FALSE){
		logErr("text.v.glsl");
		printShaderLog(vs);
		glDeleteShader(vs);
		return;
	}

	source = FileSystem::getInstance()->getFileData("text.f.glsl");
	if(source == NULL){
		logErr("Error data text.f.glsl");
		return;
	}

	fs = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fs, 1, &source, NULL);

	glCompileShader(fs);
	compile_ok = GL_FALSE;
	glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
	if(compile_ok == GL_FALSE){
		logErr("text.v.glsl");
		printShaderLog(fs);
		glDeleteShader(fs);
		return;
	}

	programText = glCreateProgram();
	glAttachShader(programText, vs);
	glAttachShader(programText, fs);
	glLinkProgram(programText);

    glDeleteShader(vs);//these are marked to delete but until they are attached to the program, they will remain
    glDeleteShader(fs);

	glGetProgramiv(programText, GL_LINK_STATUS, &link_ok);
	if(!link_ok){
		logWar("glLinkProgram:");
		return;
	}

    std::string attributeName = "coord";
	attribute_coord = glGetAttribLocation(programText, attributeName.c_str());
	if(attribute_coord == -1){
		logErr("Could not bind attribute %s\n", attributeName.c_str());
		return;
	}

	attributeName = "tex";
	uniform_tex = glGetUniformLocation(programText, attributeName.c_str());
	if(uniform_tex == -1){
		logErr("Could not bind uniform %s\n", attributeName.c_str());
		return;
	}
	attributeName = "color";
	uniform_color = glGetUniformLocation(programText, attributeName.c_str());
	if(uniform_color == -1){
		logErr("Could not bind uniform %s\n", attributeName.c_str());
		return;
	}

	attributeName = "pvmMatrix";
	uniform_pvmMatrix = glGetUniformLocation(programText, attributeName.c_str());
	if(uniform_pvmMatrix == -1){
		logErr("Could not bind uniform %s\n", attributeName.c_str());
		return;
	}

	vbo = 0;

	FT_Set_Pixel_Sizes(face, 0, height);
	FT_GlyphSlot g = face->glyph;
	int roww = 0;
	int rowh = 0;
	w = 0;
	h = 0;
	memset(c, 0, sizeof(glyphData) * 256);

	/* Find minimum size for a texture holding all visible ASCII characters */
	for(int i = 32; i < 256; i++){
		if(FT_Load_Char(face, i, FT_LOAD_RENDER)){
			logErr("Loading character %c failed!\n", i);
			continue;
		}
		if(roww + g->bitmap.width + 1 >= MAXWIDTH){
			w = std::max(w, roww);
			h += rowh;
			roww = 0;
			rowh = 0;
		}
		roww += g->bitmap.width + 1;
		rowh = std::max(rowh, g->bitmap.rows);
	}
	maxHeight = rowh;

	w = std::max(w, roww);
	h += rowh;
	/* Create a texture that will be used to hold all ASCII glyphs */
	glActiveTexture(GL_TEXTURE0);
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 0);

	/* We require 1 byte alignment when uploading texture data */
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	/* Clamping to edges is important to prevent artifacts when scaling */
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	/* Linear filtering usually looks best for text */
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


	/* Paste all glyph bitmaps into the texture, remembering the offset */
	int ox = 0;
	int oy = 0;
	rowh = 0;

	for(int i = 32; i < 256; i++){
		if(FT_Load_Char(face, i, FT_LOAD_RENDER)){
			logErr("Loading character %c failed!\n", i);
			continue;
		}
		if(ox + g->bitmap.width + 1 >= MAXWIDTH){
			oy += rowh+1;
			rowh = 0;
			ox = 0;
		}
		glTexSubImage2D(GL_TEXTURE_2D, 0, ox, oy, g->bitmap.width, g->bitmap.rows, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
		c[i].ax = g->advance.x >> 6;
		c[i].ay = g->advance.y >> 6;
		c[i].bw = g->bitmap.width;
		c[i].bh = g->bitmap.rows;
		c[i].bl = g->bitmap_left;
		c[i].bt = g->bitmap_top;
		c[i].tx = ox / (float)w;
		c[i].ty = oy / (float)h;
		rowh = std::max(rowh, g->bitmap.rows);
		ox += g->bitmap.width + 1;
	}
	logErr("Generated a %d x %d (%d kb) texture atlas\n", w, h, w * h / 1024);
	atlasWidth = w;
	atlasHeight = h;
}
예제 #24
0
파일: Shaders.cpp 프로젝트: ndommanget/demo
// Loads files [files] in string table, builds program object, compile shaders in shaders objects, links and return the id program object (with the executable code)
unsigned int Shaders::loadProgram(const std::vector<std::string> &files) const
{
    // Creates a program object which id is returned
    unsigned int programId=glCreateProgram();

    glBindAttribLocation(programId, 0, "vertexPosition");
    glBindAttribLocation(programId, 1, "vertexNormal");
    glBindAttribLocation(programId, 2, "vertexUvs");
    glBindAttribLocation(programId, 3, "vertexColor");

    // Creates a vertex shader object which id is returned
    unsigned int vertexShaderId=glCreateShader(GL_VERTEX_SHADER);
    // Creates a fragment shader object which id is returned
    unsigned int fragmentShaderId=glCreateShader(GL_FRAGMENT_SHADER);
    glAttachShader(programId, vertexShaderId);
    glAttachShader(programId, fragmentShaderId);

    unsigned int n=files.size();
    std::string ** strs=new std::string*[n];
    const char ** lines=new const char*[n+2];
    //std::cout<<"Loading program "<<files[n - 1]<<"..."<<std::endl;
    
    bool geo=false;
    // For every file :
    for (unsigned int i=0 ; i<n ; ++i)
    {
        // Gets std::string of file
        std::string* s=loadFile(files[i]);
        // Stores it [std::string] in strs[i]
        strs[i]=s;
        // Stores it [char] in lines[i+2]
        lines[i+2]=s->c_str();
        
        // If _GEOMETRY_ is in file, geometrey shader : geo=true
        // strstr(a, b)-> finds firt occurence of b in a
        if (strstr(lines[i+2], "_GEOMETRY_")!=NULL) 
            geo=true;
    }

    lines[0]="#version 150\n";
    lines[1]="#define _VERTEX_\n";
    // Loads shader source chars in shader object
    glShaderSource(vertexShaderId, n+2, lines, NULL);
    // Compiles the loaded shader source code
    glCompileShader(vertexShaderId);
    // Prints compilation potential problems
    printShaderLog(vertexShaderId);

    if (geo) 
    {
        // Creates a geometry shader object which id is returned
        unsigned int geometryShaderId=glCreateShader(GL_GEOMETRY_SHADER_EXT);
        glAttachShader(programId, geometryShaderId);
        // Adds this text before the source text
        lines[1]="#define _GEOMETRY_\n";
        glShaderSource(geometryShaderId, n+2, lines, NULL);
        glCompileShader(geometryShaderId);
        printShaderLog(geometryShaderId);
        // Specifies type of primitives accessible in geometry shader
        glProgramParameteriEXT(programId, GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES);
    }

    lines[1]="#define _FRAGMENT_\n";
    glShaderSource(fragmentShaderId, n+2, lines, NULL);
    glCompileShader(fragmentShaderId);
    printShaderLog(fragmentShaderId);

    // Links the program object to build the executable code
    glLinkProgram(programId);

    for (unsigned int i=0 ; i<n ; ++i) 
        delete strs[i];

    delete[] strs;
    delete[] lines;

    // Returns the ID of the program object
    return programId;
}