Ejemplo n.º 1
0
    void CreateComputePipeline()
    {
        // Create compute shader
        if (Supported(LLGL::ShadingLanguage::GLSL))
        {
            computeShader = LoadShaderProgram(
                {
                    { LLGL::ShaderType::Compute, "Example.comp" }
                }
            );
        }
        else if (Supported(LLGL::ShadingLanguage::SPIRV))
        {
            computeShader = LoadShaderProgram(
                {
                    { LLGL::ShaderType::Compute, "Example.comp.spv" }
                }
            );
        }
        else if (Supported(LLGL::ShadingLanguage::HLSL))
        {
            computeShader = LoadShaderProgram(
                {
                    { LLGL::ShaderType::Compute, "Example.hlsl", "CS", "cs_5_0" },
                }
            );
        }
        else if (Supported(LLGL::ShadingLanguage::Metal))
        {
            computeShader = LoadShaderProgram(
                {
                    { LLGL::ShaderType::Compute, "Example.metal", "CS", "1.1" },
                }
            );
        }
        else
            throw std::runtime_error("shaders not available for selected renderer in this example");

        // Create compute pipeline layout
        computeLayout = renderer->CreatePipelineLayout(
            LLGL::PipelineLayoutDesc("cbuffer(2):comp, rwbuffer(3):comp, rwbuffer(4):comp")
        );

        // Create compute pipeline
        LLGL::ComputePipelineDescriptor pipelineDesc;
        {
            pipelineDesc.shaderProgram  = computeShader;
            pipelineDesc.pipelineLayout = computeLayout;
        }
        computePipeline = renderer->CreateComputePipeline(pipelineDesc);

        // Create resource heap for compute pipeline
        LLGL::ResourceHeapDescriptor resourceHeapDesc;
        {
            resourceHeapDesc.pipelineLayout = computeLayout;
            resourceHeapDesc.resourceViews  = { inputBuffer, instanceBuffer, indirectArgBuffer };
        }
        computeResourceHeap = renderer->CreateResourceHeap(resourceHeapDesc);
    }
Ejemplo n.º 2
0
Shader::Shader(std::string vpFile, std::string fpFile)
{
	m_vp.assign(LoadShaderProgram(vpFile));
	m_gp.clear();
	m_fp.assign(LoadShaderProgram(fpFile));

	InitializeShader();
}
Ejemplo n.º 3
0
    void CreateGraphicsPipeline()
    {
        // Create graphics shader
        if (Supported(LLGL::ShadingLanguage::GLSL))
        {
            graphicsShader = LoadShaderProgram(
                {
                    { LLGL::ShaderType::Vertex,   "Example.vert" },
                    { LLGL::ShaderType::Fragment, "Example.frag" }
                },
                { vertexFormat[0], vertexFormat[1] }
            );
        }
        else if (Supported(LLGL::ShadingLanguage::SPIRV))
        {
            graphicsShader = LoadShaderProgram(
                {
                    { LLGL::ShaderType::Vertex,   "Example.vert.spv" },
                    { LLGL::ShaderType::Fragment, "Example.frag.spv" }
                },
                { vertexFormat[0], vertexFormat[1] }
            );
        }
        else if (Supported(LLGL::ShadingLanguage::HLSL))
        {
            graphicsShader = LoadShaderProgram(
                {
                    { LLGL::ShaderType::Vertex,   "Example.hlsl", "VS", "vs_5_0" },
                    { LLGL::ShaderType::Fragment, "Example.hlsl", "PS", "ps_5_0" }
                },
                { vertexFormat[0], vertexFormat[1] }
            );
        }
        else if (Supported(LLGL::ShadingLanguage::Metal))
        {
            graphicsShader = LoadShaderProgram(
                {
                    { LLGL::ShaderType::Vertex,   "Example.metal", "VS", "1.1" },
                    { LLGL::ShaderType::Fragment, "Example.metal", "PS", "1.1" }
                },
                { vertexFormat[0], vertexFormat[1] }
            );
        }
        else
            throw std::runtime_error("shaders not available for selected renderer in this example");

        // Create graphics pipeline
        LLGL::GraphicsPipelineDescriptor pipelineDesc;
        {
            pipelineDesc.shaderProgram              = graphicsShader;
            pipelineDesc.primitiveTopology          = LLGL::PrimitiveTopology::TriangleStrip;
            pipelineDesc.rasterizer.multiSampling   = GetMultiSampleDesc();
        }
        graphicsPipeline = renderer->CreateGraphicsPipeline(pipelineDesc);
    }
Ejemplo n.º 4
0
void FontShader::LoadShader()
{
    if (FontShader::_program == 0)
    {
        FontShader::_program = LoadShaderProgram(vertexShader, fragmentShader);

        glUseProgram(FontShader::_program);

        FontShader::_u_projection = glGetUniformLocation(FontShader::_program, "u_projection");
        FontShader::_u_view = glGetUniformLocation(FontShader::_program, "u_view");
        FontShader::_u_global_color = glGetUniformLocation(FontShader::_program, "u_global_color");
    }
}
Ejemplo n.º 5
0
// Setup before we run
void init(){
	// Load in cubemap
	loadCubemap();

	glClearColor(0, 0, 0, 1);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(40.0, 1.0, 1.0, 100.0);
	glMatrixMode(GL_MODELVIEW);

	gluLookAt(cameraPosition[0], cameraPosition[1], cameraPosition[2],  // Set eye position, target position, and up direction.
		0.0, 0.0, 0.0,
		0.0, 1.0, 0.);


	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_FRONT /* or GL_BACK or even GL_FRONT_AND_BACK */);
	glFrontFace(GL_CW /* or GL_CCW */);


	// Prepare the shader program.
	GLuint shaderProgram = LoadShaderProgram();
	glUseProgram(shaderProgram);

	// generate "pointers" (names) for each buffer
	glGenBuffers(1, &vboIdVertex);
	glGenBuffers(1, &vboIdNormal);
	glGenBuffers(1, &vboIdColor);
	glGenBuffers(1, &vboIdTranslation);
	glGenBuffers(1, &vboIdRotation);
	glGenBuffers(1, &vboIdScale);


	// put data in buffers - glBindBuffer sets the active buffer, glBufferData pours data in the active buffer
	glBindBuffer(GL_ARRAY_BUFFER, vboIdVertex);
	glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * instanceVertexCount * 3, instanceVertices, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, vboIdNormal);
	glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * instanceVertexCount * 3, instanceNormals, GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, vboIdColor);
	glBufferData(GL_ARRAY_BUFFER, sizeof(particleColors), particleColors, GL_DYNAMIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, vboIdTranslation);
	glBufferData(GL_ARRAY_BUFFER, sizeof(particleTranslations), particleTranslations, GL_DYNAMIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, vboIdRotation);
	glBufferData(GL_ARRAY_BUFFER, sizeof(particleRotations), particleRotations, GL_DYNAMIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, vboIdScale);
	glBufferData(GL_ARRAY_BUFFER, sizeof(particleScales), particleScales, GL_DYNAMIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, 0); // or null, whatever we feel like

	attributeIdVertex = glGetAttribLocation(shaderProgram, attributeNameVertex);
	if (attributeIdVertex == -1) {
		fprintf(stderr, "Could not bind attribute %s\n", attributeNameVertex);
	}
	else
	{
		printf("%s bound to %d\n", attributeNameVertex, attributeIdVertex);
	}

	uniform_campos = glGetUniformLocation(shaderProgram, uniform_name_campos);
	if (uniform_campos == -1) {
		fprintf(stderr, "Could not bind uniform %s\n", uniform_name_campos);
	}
	else
	{
		printf("%s bound to %d\n", uniform_name_campos, uniform_campos);
	}

	uniformIdReflecRate = glGetUniformLocation(shaderProgram, uniformNameReflecRate);
	if (uniformIdReflecRate == -1) {
		fprintf(stderr, "Could not bind uniform %s\n", uniformNameReflecRate);
	}
	else
	{
		printf("%s bound to %d\n", uniformNameReflecRate, uniformIdReflecRate);
	}

	attributeIdNormal = glGetAttribLocation(shaderProgram, attributeNameNormal);
	if (attributeIdNormal == -1) {
		fprintf(stderr, "Could not bind attribute %s\n", attributeNameNormal);
	}
	else
	{
		printf("%s bound to %d\n", attributeNameNormal, attributeIdNormal);
	}

	attributeIdColor = glGetAttribLocation(shaderProgram, attributeNameColor);
	if (attributeIdColor == -1) {
		fprintf(stderr, "Could not bind attribute %s\n", attributeNameColor);
	}
	else
	{
		printf("%s bound to %d\n", attributeNameColor, attributeIdColor);
	}

	attributeIdTranslation = glGetAttribLocation(shaderProgram, attributeNameTranslation);
	if (attributeIdTranslation == -1) {
		fprintf(stderr, "Could not bind attribute %s\n", attributeNameTranslation);
	}
	else
	{
		printf("%s bound to %d\n", attributeNameTranslation, attributeIdTranslation);
	}

	attributeIdRotation = glGetAttribLocation(shaderProgram, attributeNameRotation);
	if (attributeIdRotation == -1) {
		fprintf(stderr, "Could not bind attribute %s\n", attributeNameRotation);
	}
	else
	{
		printf("%s bound to %d\n", attributeNameRotation, attributeIdRotation);
	}

	uniformIdScale = glGetUniformLocation(shaderProgram, uniformNameScale);
	if (uniformIdScale == -1) {
		fprintf(stderr, "Could not bind attribute %s\n", uniformNameScale);
	}
	else
	{
		printf("%s bound to %d\n", uniformNameScale, uniformIdScale);
	}

}
void InitApp()
{
    // Setup fonts.

    if (!g_font.create("Arial", 10, GLFont::BOLD))
        throw std::runtime_error("Failed to create font.");

    // Setup textures.

    if (!(g_nullTexture = CreateNullTexture(2, 2)))
        throw std::runtime_error("failed to create null texture.");

    for (int i = 0; i < TERRAIN_REGIONS_COUNT; ++i)
    {
        if (!(g_regions[i].texture = LoadTexture(g_regions[i].filename.c_str())))
            throw std::runtime_error("Failed to load texture: " + g_regions[i].filename);
    }

    // Setup shaders.

    std::string infoLog;

    if (!(g_terrainShader = LoadShaderProgram("content/shaders/terrain.glsl", infoLog)))
        throw std::runtime_error("Failed to load shader: terrain.glsl.\n" + infoLog);

    // Setup terrain.

    if (!g_terrain.create(HEIGHTMAP_SIZE, HEIGHTMAP_GRID_SPACING, HEIGHTMAP_SCALE))
        throw std::runtime_error("Failed to create terrain.");

    GenerateTerrain();

    // Setup camera.

    Vector3 pos;

    pos.x = HEIGHTMAP_SIZE * HEIGHTMAP_GRID_SPACING * 0.5f;
    pos.z = HEIGHTMAP_SIZE * HEIGHTMAP_GRID_SPACING * 0.5f;
    pos.y = g_terrain.getHeightMap().heightAt(pos.x, pos.z) + CAMERA_Y_OFFSET;

    g_camera.setBehavior(Camera::CAMERA_BEHAVIOR_FIRST_PERSON);
    g_camera.setPosition(pos);
    g_camera.setAcceleration(CAMERA_ACCELERATION);
    g_camera.setVelocity(CAMERA_VELOCITY);

    g_camera.perspective(CAMERA_FOVX,
                         static_cast<float>(g_windowWidth) / static_cast<float>(g_windowHeight),
                         CAMERA_ZNEAR, CAMERA_ZFAR);

    float upperBounds = (HEIGHTMAP_SIZE * HEIGHTMAP_GRID_SPACING - (2.0f * HEIGHTMAP_GRID_SPACING));
    float lowerBounds = static_cast<float>(HEIGHTMAP_GRID_SPACING);

    g_cameraBoundsMax.x = upperBounds;
    g_cameraBoundsMax.y = CAMERA_ZFAR;
    g_cameraBoundsMax.z = upperBounds;

    g_cameraBoundsMin.x = lowerBounds;
    g_cameraBoundsMin.y = 0.0f;
    g_cameraBoundsMin.z = lowerBounds;

    // Setup input.

    Mouse::instance().hideCursor(true);
    Mouse::instance().setPosition(g_windowWidth / 2, g_windowHeight / 2);
}
Ejemplo n.º 7
0
void SetupDefaultLights()
{
  GlLight *light;
  GlLight *light2;
  float fogcolor[4] = {0.75,.5,.4,1};
  ShaderProg = LoadShaderProgram("shaders/lighting1.vert","shaders/lighting1.frag");
  

   light=NewLight();
      light->ambientLight[0] =0.50f;
      light->ambientLight[1] =0.50f;
      light->ambientLight[2] =0.50f;
      light->ambientLight[3] =1.0f;
  
      light->diffuseLight[0] =1.0f;
      light->diffuseLight[1] =1.0f;
      light->diffuseLight[2] =1.0f;
      light->diffuseLight[3] =1.0f;
  
      light->specLight[0] =0.50f;
      light->specLight[1] =0.50f;
      light->specLight[2] =0.50f;
      light->specLight[3] =1.0f;
  
      light->direction[0] =1.0f;
      light->direction[1] =0.0f;
      light->direction[2] =0.0f;
      
      light->pos[0] =32.0f;
      light->pos[1] =15.0f;
      light->pos[2] =-32.0f;
      light->pos[3] =1.0f;
      light->spotEdge = 90.0f;
      light->spotAngle = 180.0f;
      light->useRot = 1;
      light->rot.x = 0;
      light->rot.y = 0;
      light->rot.z = 0;
      
      light2=NewLight();
      light2->ambientLight[0] =0.4f;
      light2->ambientLight[1] =0.4f;
      light2->ambientLight[2] =0.4f;
      light2->ambientLight[3] =1.0f;
  
      light2->diffuseLight[0] =1.0f;
      light2->diffuseLight[1] =1.0f;
      light2->diffuseLight[2] =1.0f;
      light2->diffuseLight[3] =1.0f;
  
      light2->specLight[0] =1.0f;
      light2->specLight[1] =1.0f;
      light2->specLight[2] =1.0f;
      light2->specLight[3] =1.0f;
  
      light2->direction[0] =1.0f;
      light2->direction[1] =0.0f;
      light2->direction[2] =0.0f;
      
      light2->pos[0] =32.0f;
      light2->pos[1] =590.0f;
      light2->pos[2] =-32.0f;
      light2->pos[3] =1.0f;
      light2->spotAngle =90.0f;
      light2->spotEdge = 180.0f;
      light2->useRot = 1;
      light2->rot.x = 0;
      light2->rot.y = 0;
      light2->rot.z = 180;


      
      glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION, 0.0f);
      glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION, 0.02f);
      glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION, 0.000002f);
      glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION, 0.001f);
      glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION, 0.02f);
      glLightf(GL_LIGHT1,GL_QUADRATIC_ATTENUATION, 0.000002f);
      glEnable(GL_LIGHT0);
      glEnable(GL_LIGHT1);
      glEnable(GL_FOG);
      glFogfv(GL_FOG_COLOR,fogcolor);
      glFogi(GL_FOG_MODE, GL_EXP);
      glFogf(GL_FOG_DENSITY, 0.001f);
      glFogf(GL_FOG_START, 1.0f);
      glFogf(GL_FOG_END, 2000.0f);
      /*
  light=NewLight();
  light->ambientLight[0] =0.7f;
  light->ambientLight[1] =0.7f;
  light->ambientLight[2] =0.7f;
  light->ambientLight[3] =1.0f;
  
  light->diffuseLight[0] =1.0f;
  light->diffuseLight[1] =1.0f;
  light->diffuseLight[2] =1.0f;
  light->diffuseLight[3] =1.0f;
  
  light->specLight[0] =1.0f;
  light->specLight[1] =1.0f;
  light->specLight[2] =1.0f;
  light->specLight[3] =1.0f;
  
  light->direction[0] =0.0f;
  light->direction[1] =0.0f;
  light->direction[2] =1.0f;
  
  light->pos[0] =32.0f;
  light->pos[1] =15.0f;
  light->pos[2] =32.0f;
  light->pos[3] =1.0f;
  light->spotEdge = 0.0f;
  light->spotAngle = 180.0f;
  light->useRot = 1;
  light->rot.x = 0;
  light->rot.y = 0;
  light->rot.z = 180;
  
  light2=NewLight();
  light2->ambientLight[0] =0.9f;
  light2->ambientLight[1] =0.9f;
  light2->ambientLight[2] =0.9f;
  light2->ambientLight[3] =1.0f;
  
  light2->diffuseLight[0] =1.0f;
  light2->diffuseLight[1] =1.0f;
  light2->diffuseLight[2] =1.0f;
  light2->diffuseLight[3] =1.0f;
  
  light2->specLight[0] =1.0f;
  light2->specLight[1] =1.0f;
  light2->specLight[2] =1.0f;
  light2->specLight[3] =1.0f;
  
  light2->direction[0] =0.0f;
  light2->direction[1] =0.0f;
  light2->direction[2] =1.0f;
  
  light2->pos[0] =0.0f;
  light2->pos[1] =0.0f;
  light2->pos[2] =-32.0f;
  light2->pos[3] =1.0f;
  light2->spotAngle =0.0f;
  light2->spotEdge = 90.0f;
  light2->useRot = 1;
  light2->rot.x = 0;
  light2->rot.y = 0;
  light2->rot.z = 0;*/
  /*
  light=NewLight();
  light->ambientLight[0] =1.0f;
  light->ambientLight[1] =1.0f;
  light->ambientLight[2] =1.0f;
  light->ambientLight[3] =1.0f;
  
  light->diffuseLight[0] =1.0f;
  light->diffuseLight[1] =1.0f;
  light->diffuseLight[2] =1.0f;
  light->diffuseLight[3] =1.0f;
  
  light->specLight[0] =1.0f;
  light->specLight[1] =1.0f;
  light->specLight[2] =1.0f;
  light->specLight[3] =1.0f;
  
  light->direction[0] =0.0f;
  light->direction[1] =0.0f;
  light->direction[2] =1.0f;
  
  light->pos[0] =0.0f;
  light->pos[1] =0.0f;
  light->pos[2] =-32.0f;
  light->pos[3] =0.0f;
  light->spotEdge = 1.0f;
  light->spotAngle = 45.0f;
  light->useRot = 1;
  light->rot.x = 0;
  light->rot.y = 0;
  light->rot.z = 0;
  
  */
/*  glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION, 0.0f);
  glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION, 0.02f);
  glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION, 0.000002f);
  glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION, 0.001f);
  glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION, 0.02f);
  glLightf(GL_LIGHT1,GL_QUADRATIC_ATTENUATION, 0.000002f);
  glLightf(GL_LIGHT2,GL_CONSTANT_ATTENUATION, 0.001f);
  glLightf(GL_LIGHT2,GL_LINEAR_ATTENUATION, 0.02f);
  glLightf(GL_LIGHT2,GL_QUADRATIC_ATTENUATION, 0.000002f);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
//  glEnable(GL_LIGHT2);
  glEnable(GL_FOG);
  glFogfv(GL_FOG_COLOR,fogcolor);
  glFogi(GL_FOG_MODE, GL_EXP);
  glFogf(GL_FOG_DENSITY, 0.001f);
  glFogf(GL_FOG_START, 0.8f);
  glFogf(GL_FOG_END, 2000.0f);*/

}