Ejemplo n.º 1
0
void Background :: initgl() {
	// The standard gl object stuff.
	vert = createShader(GL_VERTEX_SHADER,   VS_BKGD);
	frag = createShader(GL_FRAGMENT_SHADER, FS_BKGD);

	prog = glCreateProgram();
	glAttachShader(prog, vert);
	glAttachShader(prog, frag);
	glBindFragDataLocation(prog, 0, "out_color");

	glLinkProgram(prog);
	glUseProgram(prog);
	printShaderError(vert, VS_BKGD);
	printShaderError(frag, FS_BKGD);

	//vao, vbo, 
	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);

	// Uploaded to the GPU
	float rectArray [] {
		-1.0f,  1.0f, 0.0f, 0.0f, // TL
		 1.0f,  1.0f, 1.0f, 0.0f, // TR
		 1.0f, -1.0f, 1.0f, 1.0f, // BR

		-1.0f,  1.0f, 0.0f, 0.0f, // TL
		-1.0f, -1.0f, 0.0f, 1.0f, // BL
		 1.0f, -1.0f, 1.0f, 1.0f  // BR
	};

	glBufferData(GL_ARRAY_BUFFER, sizeof(rectArray), rectArray, GL_STATIC_DRAW);

	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	GLuint pos = glGetAttribLocation(prog, "pos");
	GLuint uv  = glGetAttribLocation(prog, "uv");

	glVertexAttribPointer(pos, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4, nullptr);
	glVertexAttribPointer(uv , 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4, (void *) (sizeof(GLfloat) * 2));

	glEnableVertexAttribArray(pos);
	glEnableVertexAttribArray(uv);

	glUniform1i(glGetUniformLocation(prog, "BkgdTex"), BSPOT_BKGD);
	// ren_dim / img_dim = num of repeat
	glUniform2f(glGetUniformLocation(prog, "rep_num"),
		RENDER_WIDTH  / (float) ih->getSheetWidth(),
		RENDER_HEIGHT / (float) ih->getSheetHeight());

	// Because the vectors must be horizontal (in arrays), the matrix must be transposed.
	glUniform2f(glGetUniformLocation(prog, "img_dim"), ih->getSheetWidth(), ih->getSheetHeight());

	unbind();
}
Ejemplo n.º 2
0
GLuint initShaders(const char *vertFile, const char *fragFile) {
	GLuint p, f, v;

	char *vs,*fs;

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

	// load shaders & get length of each
	GLint vlen;
	GLint flen;
	vs = loadFile(vertFile,vlen);
	fs = loadFile(fragFile,flen);
	
	const char * vv = vs;
	const char * ff = fs;

	glShaderSource(v, 1, &vv,&vlen);
	glShaderSource(f, 1, &ff,&flen);
	
	GLint compiled;

	glCompileShader(v);
	glGetShaderiv(v, GL_COMPILE_STATUS, &compiled);
	if (!compiled) {
		cerr << "Vertex shader not compiled." << endl;
		printShaderError(v);
	}

	glCompileShader(f);
	glGetShaderiv(f, GL_COMPILE_STATUS, &compiled);
	if (!compiled) {
		cerr << "Fragment shader not compiled." << endl;
		printShaderError(f);
	}
	
	p = glCreateProgram();
		
	glAttachShader(p,v);
	glAttachShader(p,f);

	glBindAttribLocation(p,VERTEX,"in_Position");
	glBindAttribLocation(p,COLOUR,"in_Color");
	glBindAttribLocation(p,NORMAL,"in_Normal");
	glBindAttribLocation(p,TEXCOORD,"in_TexCoord");

	glLinkProgram(p);
	glUseProgram(p);

	delete [] vs; // dont forget to free allocated memory
	delete [] fs; // we allocated this in the loadFile function...

	return p;
}
Ejemplo n.º 3
0
//-----------------------------------------------------------------------------
//	Shader - Shader From a Buffer
//-----------------------------------------------------------------------------
bool shader::loadBuffer( const char* buffer, int length, int shaderType ) {
    GLint shaderStatus(0);
    GLuint shaderId( 0 );
    
    if ( !programId ) {
        programId = glCreateProgram();
        
        if ( !programId ) {
            std::cerr << "ERROR: Unable to create a shader program." << std::endl;
            return false;
        }
    }
    

    shaderId = glCreateShader( shaderType ); // Fragment shader or Vertex Shader
    glShaderSource( shaderId, 1, (const GLchar**)&buffer, (const GLint*)&length );

    glCompileShader( shaderId );
    glGetShaderiv( shaderId, GL_COMPILE_STATUS, &shaderStatus );

    if ( shaderStatus != GL_TRUE ) {
        printShaderError( 0, shaderId );
        return false;
    }
    
    glAttachShader( programId, shaderId );
    shaderIds.push_back( shaderId );

    printGlError("General shader loading error");
    return true;
}
Ejemplo n.º 4
0
//-----------------------------------------------------------------------------
//	Shader - Compilation
//-----------------------------------------------------------------------------
bool shader::compile() {
    if (shaderIds.empty()) {
        std::cerr << "WARNING: Attempted to compile a shader program without input" << std::endl;
        return false;
    }
    GLint shaderStatus(0);
    
    glLinkProgram(programId);
    glGetProgramiv(programId, GL_LINK_STATUS, &shaderStatus);
    if (shaderStatus != GL_TRUE) {
        std::cerr << "\nWARNING: A GLSL Shader Error has occurred\n";
        for ( int i = 0; i < shaderIds.size(); ++i )
            printShaderError(programId, shaderIds[ i ]);
        return false;
    }

    printGlError("Error compiling shader");
    return true;
}
Ejemplo n.º 5
0
void Grid :: initDraw() {
// STEP 1: Create Shader Programs
	// Vertex/Fragment Shaders
	vShader = createShader(GL_VERTEX_SHADER,   VS_GRID);
	fShader = createShader(GL_FRAGMENT_SHADER, FS_GRID);

	program = glCreateProgram();
	glAttachShader(program, vShader);
	glAttachShader(program, fShader);

	glBindFragDataLocation(program, 0, "out_color");
	glLinkProgram(program);

	// Create mouse shader program.
	fVShader = createShader(GL_VERTEX_SHADER, VS_MOUSE);
	fMShader = createShader(GL_FRAGMENT_SHADER, FS_MOUSE);

	mprogram = glCreateProgram();
	glAttachShader(mprogram, fVShader);
	glAttachShader(mprogram, fMShader);

	glBindFragDataLocation(mprogram, 0, "out_color");
	glLinkProgram(mprogram);

	// For debugging.
	printShaderError(vShader,  VS_GRID);
	printShaderError(fShader,  FS_GRID);
	printShaderError(fMShader, FS_MOUSE);

	glUseProgram (program);

// STEP 2: Create Vertex Buffer Object
	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * LINE_GRID_SIZE, lineGrid, GL_STATIC_DRAW);

// STEP 3: Create Vertex Array Object
	GLuint pos = glGetAttribLocation(program, "pos");

	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	glVertexAttribPointer(pos, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, nullptr);
	glEnableVertexAttribArray(pos);

// STEP 4: Projection Matrix
	float projMatrix[] = {
		2.0f / SCREEN_WIDTH, 0.0f, 0.0f, -1.0f,
		0.0f, 2.0f / SCREEN_HEIGHT, 0.0f, -1.0f,
		0.0f, 0.0f, 1.0f, 0.0f,
		0.0f, 0.0f, 0.0f, 1.0f
	};

	// Because the vectors must be horizontal (in arrays), the matrix must be transposed.
	glUniformMatrix4fv(glGetUniformLocation(program, "proj"), 1, GL_TRUE, projMatrix);

// STEP 5: Texture Uniform && Screen Uniform
	// The framebuffer texture is on GL_TEXTURE1
	glUniform1i(glGetUniformLocation(program, "texFrame"), 1);
	glUniform2f(glGetUniformLocation(program, "screen"), SCREEN_WIDTH, SCREEN_HEIGHT);



	// Create the mouse vao without interfering.
	glUseProgram(mprogram);
		GLuint mouseShape = glGetAttribLocation(mprogram, "pos");

		glGenVertexArrays(1, &mvao);
		glBindVertexArray(mvao);

		glVertexAttribPointer(mouseShape, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, nullptr);
		glEnableVertexAttribArray(mouseShape);

		glUniformMatrix4fv(glGetUniformLocation(mprogram, "proj"), 1, GL_TRUE, projMatrix);

	printGLError();
	unbind();
}