// Use this shader when rendering
void ShaderProgram::use(){
    if (glIsProgram(shaderID) != GL_TRUE) {
        std::cout << "GL ERROR: THERE IS NO PROGRAM " << shaderID << std::endl;
    }
    glCheckErrors("ShaderProgram::use()1");
    glUseProgram(shaderID);
    glCheckErrors("ShaderProgram::use()2");
}
Example #2
0
kinc_g4_constant_location_t kinc_g4_pipeline_get_constant_location(kinc_g4_pipeline_t *state, const char *name) {
	kinc_g4_constant_location_t location;
	location.impl.location = glGetUniformLocation(state->impl.programId, name);
	location.impl.type = GL_FLOAT;
	GLint count = 0;
	glGetProgramiv(state->impl.programId, GL_ACTIVE_UNIFORMS, &count);
	char arrayName[1024];
	strcpy(arrayName, name);
	strcat(arrayName, "[0]");
	for (GLint i = 0; i < count; ++i) {
		GLenum type;
		char uniformName[1024];
		GLsizei length;
		GLint size;
		glGetActiveUniform(state->impl.programId, i, 1024 - 1, &length, &size, &type, uniformName);
		if (strcmp(uniformName, name) == 0 || strcmp(uniformName, arrayName) == 0) {
			location.impl.type = type;
			break;
		}
	}
	glCheckErrors();
	if (location.impl.location < 0) {
		kinc_log(KINC_LOG_LEVEL_WARNING, "Uniform %s not found.", name);
	}
	return location;
}
Example #3
0
int mainLoop( int argc, char *argv[] )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
    glutInitWindowSize( winW, winH );
    glutInitWindowPosition( 0, 0 );
    if ( !glutCreateWindow( "tpldemo" ) )
    {
        printf( "Couldn't open window.\n" );
        return 1;
    }
    glutDisplayFunc( display );
    glutIdleFunc( idlefunc );
    glutMouseFunc( mouse );
    glutMotionFunc( mouseMotion );
    glutPassiveMotionFunc( mouseMotion );
    glutKeyboardFunc( keyboard );
    glutSpecialFunc( keyboard_special );
    glutReshapeFunc( reshape );
    glutSetCursor( GLUT_CURSOR_CROSSHAIR );

    glEnable(GL_LIGHTING);
    GLfloat lightAmbient[] = {0.5f, 0.5f, 0.5f, 1.0f} ;
    glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lightAmbient);

    glEnable(GL_LIGHT0);
    GLfloat light0Position[] = {0.7f, 0.5f, 0.9f, 0.0f} ;
    GLfloat light0Ambient[]    = {0.0f, 0.5f, 1.0f, 0.8f} ;
    GLfloat light0Diffuse[]    = {0.0f, 0.5f, 1.0f, 0.8f} ;
    GLfloat light0Specular[] = {0.0f, 0.5f, 1.0f, 0.8f} ;
    glLightfv(GL_LIGHT0, GL_POSITION,light0Position) ;
    glLightfv(GL_LIGHT0, GL_AMBIENT,light0Ambient) ;
    glLightfv(GL_LIGHT0, GL_DIFFUSE,light0Diffuse) ;
    glLightfv(GL_LIGHT0, GL_SPECULAR,light0Specular) ;

    glEnable(GL_LIGHT1);
    GLfloat light1Position[] = {0.5f, 0.7f, 0.2f, 0.0f} ;
    GLfloat light1Ambient[]    = {1.0f, 0.5f, 0.0f, 0.8f} ;
    GLfloat light1Diffuse[]    = {1.0f, 0.5f, 0.0f, 0.8f} ;
    GLfloat light1Specular[] = {1.0f, 0.5f, 0.0f, 0.8f} ;
    glLightfv(GL_LIGHT1, GL_POSITION,light1Position) ;
    glLightfv(GL_LIGHT1, GL_AMBIENT,light1Ambient) ;
    glLightfv(GL_LIGHT1, GL_DIFFUSE,light1Diffuse) ;
    glLightfv(GL_LIGHT1, GL_SPECULAR,light1Specular) ;

    glCheckErrors();
    clear_grid();

    create_menu() ;
    glutMainLoop();

    return 0;
}
Example #4
0
kinc_g4_texture_unit_t kinc_g4_pipeline_get_texture_unit(kinc_g4_pipeline_t *state, const char *name) {
	int index = findTexture(state, name);
	if (index < 0) {
		int location = glGetUniformLocation(state->impl.programId, name);
		glCheckErrors();
		index = state->impl.textureCount;
		state->impl.textureValues[index] = location;
		strcpy(state->impl.textures[index], name);
		++state->impl.textureCount;
	}
	kinc_g4_texture_unit_t unit;
	unit.impl.unit = index;
	return unit;
}
Example #5
0
void kinc_g4_pipeline_init(kinc_g4_pipeline_t *state) {
	memset(state, 0, sizeof(kinc_g4_pipeline_t));
	
	kinc_g4_internal_pipeline_set_defaults(state);

	state->impl.textureCount = 0;
	// TODO: Get rid of allocations
	state->impl.textures = (char**)malloc(sizeof(char*) * 16);
	for (int i = 0; i < 16; ++i) {
		state->impl.textures[i] = (char*)malloc(sizeof(char) * 128);
		state->impl.textures[i][0] = 0;
	}
	state->impl.textureValues = (int*)malloc(sizeof(int) * 16);

	state->impl.programId = glCreateProgram();
	glCheckErrors();
}
Example #6
0
static void compileShader(unsigned *id, const char *source, size_t length, kinc_g4_shader_type_t type) {
	*id = glCreateShader(toGlShader(type));
	glCheckErrors();
	glShaderSource(*id, 1, (const GLchar**)&source, 0);
	glCompileShader(*id);

	int result;
	glGetShaderiv(*id, GL_COMPILE_STATUS, &result);
	if (result != GL_TRUE) {
		int length;
		glGetShaderiv(*id, GL_INFO_LOG_LENGTH, &length);
		char* errormessage = (char*)malloc(length);
		glGetShaderInfoLog(*id, length, NULL, errormessage);
		printf("GLSL compiler error: %s\n", errormessage);
		free(errormessage);
	}
}
void ShaderProgram::init(const char *vsFile, const char *fsFile){
    // Create the shaders
    std::cout << "using vertex shader: " << vsFile << std::endl;
    std::cout << "using fragment shader: " << fsFile << std::endl;
    
    shaderVP = glCreateShader(GL_VERTEX_SHADER);
    shaderFP = glCreateShader(GL_FRAGMENT_SHADER);
    
    const char *vp = readSource(vsFile);
    const char *fp = readSource(fsFile);
    
    if (vp == NULL || fp == NULL) {
        std::cerr << "ShaderProgram::Init ERROR: ONE OR BOTH FILES NOT FOUND!" << std::endl;
        exit(EXIT_FAILURE);
    }
    
    // Set the source codes
    glShaderSource(shaderVP, 1, &vp, 0);
    glShaderSource(shaderFP, 1, &fp, 0);
    
    delete [] vp;
    delete [] fp;
    
    // Compile the shader source
    glCompileShader(shaderVP);
    
    // Check for errors
    GLint isCompiled = 0;
    glGetShaderiv(shaderVP, GL_COMPILE_STATUS, &isCompiled);
    if(isCompiled == GL_FALSE)
    {
        GLint maxLength = 0;
        glGetShaderiv(shaderVP, GL_INFO_LOG_LENGTH, &maxLength);
        
        // The maxLength includes the NULL character
        std::vector<GLchar> errorLog(maxLength);
        glGetShaderInfoLog(shaderVP, maxLength, &maxLength, &errorLog[0]);
        
        // Provide the infolog in whatever manor you deem best.
        std::cout << "Vertex Shader Compilation Error:" << std::endl;
        for (std::vector<GLchar>::iterator it = errorLog.begin(); it != errorLog.end(); it++) {
            std::cout << *it;
        }
        
        std::cout << std::endl;
        // Exit with failure.
        glDeleteShader(shaderVP); // Don't leak the shader.
        exit(EXIT_FAILURE);
    }
    
    glCompileShader(shaderFP);
    
    isCompiled = 0;
    glGetShaderiv(shaderFP, GL_COMPILE_STATUS, &isCompiled);
    if(isCompiled == GL_FALSE)
    {
        GLint maxLength = 0;
        glGetShaderiv(shaderFP, GL_INFO_LOG_LENGTH, &maxLength);
        
        // The maxLength includes the NULL character
        std::vector<GLchar> errorLog(maxLength);
        glGetShaderInfoLog(shaderFP, maxLength, &maxLength, &errorLog[0]);
        
        // Provide the infolog in whatever manor you deem best.
        std::cout << "Fragment Shader Compilation Error:" << std::endl;
        for (std::vector<GLchar>::iterator it = errorLog.begin(); it != errorLog.end(); it++) {
            std::cout << *it;
        }
        
        std::cout << std::endl;
        // Exit with failure.
        glDeleteShader(shaderVP);
        glDeleteShader(shaderFP); // Don't leak the shader.
        exit(EXIT_FAILURE);
    }
    
    // Create the shader program
    shaderID = glCreateProgram();
   
    // Attach the shaders to the program
    glAttachShader(shaderID, shaderVP);
    glAttachShader(shaderID, shaderFP);
    
    glLinkProgram(shaderID);
    
    GLint isLinked;
    glGetProgramiv(shaderID, GL_LINK_STATUS, &isLinked);
    if (isLinked == GL_FALSE) {
        GLint maxLength;
        glGetProgramiv(shaderID, GL_INFO_LOG_LENGTH, &maxLength);
        
        //The maxLength includes the NULL character
        std::vector<GLchar> infoLog(maxLength);
        glGetProgramInfoLog(shaderID, maxLength, &maxLength, &infoLog[0]);
        
        //The program is useless now. So delete it.
        glDeleteProgram(shaderID);
        
        //Provide the infolog in whatever manner you deem best.
        for (std::vector<GLchar>::iterator it = infoLog.begin(); it != infoLog.end(); it++) {
            std::cout << *it;
        }
        std::cout << std::endl;
        //Exit with failure.
        glDeleteShader(shaderVP);
        glDeleteShader(shaderFP);
        
        exit(EXIT_FAILURE);
    }
    
    glDetachShader(shaderID, shaderVP);
    glDetachShader(shaderID, shaderFP);
    
    glDeleteShader(shaderVP);
    glDeleteShader(shaderFP);
    
    glCheckErrors("After Linking");
    glBindAttribLocation(shaderID, 0, "vert_position");
    glCheckErrors("Bind Attrib");
    std::cout << "Shader Program " << shaderID << ": " << vsFile << " + " << fsFile << std::endl;
}
Example #8
0
void Kinc_G4_Internal_SetPipeline(kinc_g4_pipeline_t *pipeline) {
#ifndef KORE_OPENGL_ES
	Kinc_Internal_ProgramUsesTessellation = pipeline->tessellation_control_shader != NULL;
#endif
	glUseProgram(pipeline->impl.programId);
	glCheckErrors();
	for (int index = 0; index < pipeline->impl.textureCount; ++index) {
		glUniform1i(pipeline->impl.textureValues[index], index);
		glCheckErrors();
	}

	if (pipeline->stencil_mode == KINC_G4_COMPARE_ALWAYS && pipeline->stencil_both_pass == KINC_G4_STENCIL_KEEP &&
	    pipeline->stencil_depth_fail == KINC_G4_STENCIL_KEEP && pipeline->stencil_fail == KINC_G4_STENCIL_KEEP) {
		glDisable(GL_STENCIL_TEST);
	}
	else {
		glEnable(GL_STENCIL_TEST);
		int stencilFunc = Kinc_G4_Internal_StencilFunc(pipeline->stencil_mode);
		glStencilMask(pipeline->stencil_write_mask);
		glStencilOp(convertStencilAction(pipeline->stencil_fail), convertStencilAction(pipeline->stencil_depth_fail), convertStencilAction(pipeline->stencil_both_pass));
		glStencilFunc(stencilFunc, pipeline->stencil_reference_value, pipeline->stencil_read_mask);
	}

	#ifdef KORE_OPENGL_ES
	glColorMask(pipeline->colorWriteMaskRed[0], pipeline->colorWriteMaskGreen[0], pipeline->colorWriteMaskBlue[0], pipeline->colorWriteMaskAlpha[0]);
	#else
	for (int i = 0; i < 8; ++i) glColorMaski(i, pipeline->color_write_mask_red[i], pipeline->color_write_mask_green[i], pipeline->color_write_mask_blue[i], pipeline->color_write_mask_alpha[i]);
	#endif

	if (Kinc_Internal_SupportsConservativeRaster) {
		if (pipeline->conservative_rasterization) {
			glEnable(0x9346); // GL_CONSERVATIVE_RASTERIZATION_NV
		}
		else {
			glDisable(0x9346);
		}
	}

	glCheckErrors();

	/*switch (state) {
	case Normalize:
	device->SetRenderState(D3DRS_NORMALIZENORMALS, on ? TRUE : FALSE);
	break;
	case BackfaceCulling:
	if (on) device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
	else device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	break;
	case FogState:
	device->SetRenderState(D3DRS_FOGENABLE, on ? TRUE : FALSE);
	break;
	case ScissorTestState:
	device->SetRenderState(D3DRS_SCISSORTESTENABLE, on ? TRUE : FALSE);
	break;
	case AlphaTestState:
	device->SetRenderState(D3DRS_ALPHATESTENABLE, on ? TRUE : FALSE);
	device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
	break;
	default:
	throw Exception();
	}*/

	if (pipeline->depth_write) {
		glDepthMask(GL_TRUE);
	}
	else {
		glDepthMask(GL_FALSE);
	}

	if (pipeline->depth_mode != KINC_G4_COMPARE_ALWAYS) {
		glEnable(GL_DEPTH_TEST);
	}
	else {
		glDisable(GL_DEPTH_TEST);
	}

	GLenum func = GL_ALWAYS;
	switch (pipeline->depth_mode) {
	default:
	case KINC_G4_COMPARE_ALWAYS:
		func = GL_ALWAYS;
		break;
	case KINC_G4_COMPARE_NEVER:
		func = GL_NEVER;
		break;
	case KINC_G4_COMPARE_EQUAL:
		func = GL_EQUAL;
		break;
	case KINC_G4_COMPARE_NOT_EQUAL:
		func = GL_NOTEQUAL;
		break;
	case KINC_G4_COMPARE_LESS:
		func = GL_LESS;
		break;
	case KINC_G4_COMPARE_LESS_EQUAL:
		func = GL_LEQUAL;
		break;
	case KINC_G4_COMPARE_GREATER:
		func = GL_GREATER;
		break;
	case KINC_G4_COMPARE_GREATER_EQUAL:
		func = GL_GEQUAL;
		break;
	}
	glDepthFunc(func);
	glCheckErrors();

	switch (pipeline->cull_mode) {
	case KINC_G4_CULL_CLOCKWISE:
		glEnable(GL_CULL_FACE);
		glCullFace(GL_BACK);
		glCheckErrors();
		break;
	case KINC_G4_CULL_COUNTER_CLOCKWISE:
		glEnable(GL_CULL_FACE);
		glCullFace(GL_FRONT);
		glCheckErrors();
		break;
	case KINC_G4_CULL_NOTHING:
		glDisable(GL_CULL_FACE);
		glCheckErrors();
		break;
	default:
		break;
	}

	/*switch (state) {
	case DepthTestCompare:
	switch (v) {
	// TODO: Cmp-Konstanten systemabhaengig abgleichen
	default:
	case ZCmp_Always      : v = D3DCMP_ALWAYS; break;
	case ZCmp_Never       : v = D3DCMP_NEVER; break;
	case ZCmp_Equal       : v = D3DCMP_EQUAL; break;
	case ZCmp_NotEqual    : v = D3DCMP_NOTEQUAL; break;
	case ZCmp_Less        : v = D3DCMP_LESS; break;
	case ZCmp_LessEqual   : v = D3DCMP_LESSEQUAL; break;
	case ZCmp_Greater     : v = D3DCMP_GREATER; break;
	case ZCmp_GreaterEqual: v = D3DCMP_GREATEREQUAL; break;
	}
	device->SetRenderState(D3DRS_ZFUNC, v);
	break;
	case FogTypeState:
	switch (v) {
	case LinearFog:
	device->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);
	}
	break;
	case AlphaReferenceState:
	device->SetRenderState(D3DRS_ALPHAREF, (DWORD)v);
	break;
	default:
	throw Exception();
	}*/

	if (pipeline->blend_source != KINC_G4_BLEND_ONE || pipeline->blend_destination != KINC_G4_BLEND_ZERO || pipeline->alpha_blend_source != KINC_G4_BLEND_ONE ||
	    pipeline->alpha_blend_destination != KINC_G4_BLEND_ZERO) {
		glEnable(GL_BLEND);
	}
	else {
		glDisable(GL_BLEND);
	}

	// glBlendFunc(convert(pipeline->blendSource), convert(pipeline->blendDestination));
	glBlendFuncSeparate(convertBlendingOperation(pipeline->blend_source), convertBlendingOperation(pipeline->blend_destination),
	                    convertBlendingOperation(pipeline->alpha_blend_source), convertBlendingOperation(pipeline->alpha_blend_destination));
}
Example #9
0
void kinc_g4_pipeline_compile(kinc_g4_pipeline_t *state) {
	compileShader(&state->vertex_shader->impl._glid, state->vertex_shader->impl.source, state->vertex_shader->impl.length, KINC_SHADER_TYPE_VERTEX);
	compileShader(&state->fragment_shader->impl._glid, state->fragment_shader->impl.source, state->fragment_shader->impl.length, KINC_SHADER_TYPE_FRAGMENT);
#ifndef OPENGLES
	if (state->geometry_shader != NULL) {
		compileShader(&state->geometry_shader->impl._glid, state->geometry_shader->impl.source, state->geometry_shader->impl.length, KINC_SHADER_TYPE_GEOMETRY);
	}
	if (state->tessellation_control_shader != NULL) {
		compileShader(&state->tessellation_control_shader->impl._glid, state->tessellation_control_shader->impl.source,
		              state->tessellation_control_shader->impl.length, KINC_SHADER_TYPE_TESSELLATION_CONTROL);
	}
	if (state->tessellation_evaluation_shader != NULL) {
		compileShader(&state->tessellation_evaluation_shader->impl._glid, state->tessellation_evaluation_shader->impl.source,
		              state->tessellation_evaluation_shader->impl.length, KINC_SHADER_TYPE_TESSELLATION_EVALUATION);
	}
#endif
	glAttachShader(state->impl.programId, state->vertex_shader->impl._glid);
	glAttachShader(state->impl.programId, state->fragment_shader->impl._glid);
#ifndef OPENGLES
	if (state->geometry_shader != NULL) {
		glAttachShader(state->impl.programId, state->geometry_shader->impl._glid);
	}
	if (state->tessellation_control_shader != NULL) {
		glAttachShader(state->impl.programId, state->tessellation_control_shader->impl._glid);
	}
	if (state->tessellation_evaluation_shader != NULL) {
		glAttachShader(state->impl.programId, state->tessellation_evaluation_shader->impl._glid);
	}
#endif
	glCheckErrors();

	int index = 0;
	for (int i1 = 0; state->input_layout[i1] != NULL; ++i1) {
		for (int i2 = 0; i2 < state->input_layout[i1]->size; ++i2) {
			kinc_g4_vertex_element_t element = state->input_layout[i1]->elements[i2];
			glBindAttribLocation(state->impl.programId, index, element.name);
			glCheckErrors();
			if (element.data == KINC_G4_VERTEX_DATA_FLOAT4X4) {
				index += 4;
			}
			else {
				++index;
			}
		}
	}

	glLinkProgram(state->impl.programId);

	int result;
	glGetProgramiv(state->impl.programId, GL_LINK_STATUS, &result);
	if (result != GL_TRUE) {
		int length;
		glGetProgramiv(state->impl.programId, GL_INFO_LOG_LENGTH, &length);
		char* errormessage = (char*)malloc(length);
		glGetProgramInfoLog(state->impl.programId, length, NULL, errormessage);
		printf("GLSL linker error: %s\n", errormessage);
		free(errormessage);
	}

#ifndef KORE_OPENGL_ES
#ifndef KORE_LINUX
	if (state->tessellation_control_shader != NULL) {
		glPatchParameteri(GL_PATCH_VERTICES, 3);
		glCheckErrors();
	}
#endif
#endif
}