Example #1
0
/**
 * Sets an uniform matrix.
 * Matrices are always of type float.
 * \warning uses glGetError();
 * \remark only available if GLSL_ALLOW_IMPLICIT_CASTS is defined.
 * \param name - name of the parameter
 * \param m - an int array containing up to 16 ints for the matrix. Ints are converted to float before uploading.
 * \param bTranspose - if true, the matrix will be transposed before uploading.
 * \return void
 * \author <a href="mailto:[email protected]">Jens Schneider</a>
 * \date Mar.2005
 */
void GLSLProgram::SetUniformMatrix(const char *name,const bool *m, bool bTranspose) const {
  assert(m_bEnabled);
  CheckGLError();

  GLenum iType;
  GLint iLocation;

  try {
    iLocation = get_uniform_vector(name, m_hProgram, &iType);
  } catch(tuvok::GLError gl) {
    T_ERROR("Error (%d) obtaining uniform %s in '%s' or '%s'.", gl.errno(),
            name, m_sVS.c_str(), m_sFS.c_str());
    return;
  }

  float M[16];
  switch (iType) {
    case GL_FLOAT_MAT2:
      for (unsigned int ui=0; ui<4; ui++) M[ui]=(m[ui] ? 1.0f : 0.0f);
      glUniformMatrix2fv(iLocation,1,bTranspose,M);
      break;
    case GL_FLOAT_MAT3:
      for (unsigned int ui=0; ui<9; ui++) M[ui]=(m[ui] ? 1.0f : 0.0f);
      glUniformMatrix3fv(iLocation,1,bTranspose,M);
      break;
    case GL_FLOAT_MAT4:
      for (unsigned int ui=0; ui<16; ui++) M[ui]=(m[ui] ? 1.0f : 0.0f);
      glUniformMatrix4fv(iLocation,1,bTranspose,M);
      break;
    default:
      T_ERROR("Unknown type (%d) for %s.", iType, name);
      break;
  }
#ifdef GLSL_DEBUG
  CheckGLError("SetUniformMatrix(%s,int*,bool)",name);
#endif
}
Example #2
0
	void Renderer::SetupUi(Ui* pUiPtr)
	{
		UnloadAssets();

		_uiPtr = pUiPtr;
		for (UiElement* elem : _uiPtr->GetElements())
		{
			ProcessElement(elem);
		}

		if(_menuPtr != nullptr)
			ProcessElement(_menuPtr);

		CheckGLError();
	}
Example #3
0
vsSurface::~vsSurface()
{
	for ( int i = 0; i < m_textureCount; i++ )
	{
		if ( m_isRenderbuffer )
		{
			glDeleteRenderbuffers(1, &m_texture[i]);
			CheckGLError("vsSurface");
		}
		else
		{
			// really shouldn't delete this;  we have a texture that's using this and will delete it itself.
			//glDeleteTextures(1, &m_texture[i]);
		}
	}
	if ( m_depth )
	{
		glDeleteRenderbuffers(1, &m_depth);
		CheckGLError("vsSurface");
	}
	glDeleteFramebuffers(1, &m_fbo);
	CheckGLError("vsSurface");
	vsDeleteArray(m_texture);
}
Example #4
0
LabyrinthGLContext::LabyrinthGLContext(wxGLCanvas *canvas)
             : wxGLContext(canvas)
{
    SetCurrent(*canvas);

    wxRect rect = canvas->GetRect();
    m_width = rect.GetWidth();
    m_height = rect.GetHeight();

    MyInit();

    DrawScene();

    CheckGLError();
}
/**
 * Class constructor, loads shaders & gets locations of variables in them
 */
AssimpLoader::AssimpLoader() {
    importerPtr = new Assimp::Importer;
    scene = NULL;
    isObjectLoaded = false;

    // shader related setup -- loading, attribute and uniform locations
    std::string vertexShader    = "shaders/modelTextured.vsh";
    std::string fragmentShader  = "shaders/modelTextured.fsh";
    shaderProgramID         = LoadShaders(vertexShader, fragmentShader);
    vertexAttribute         = GetAttributeLocation(shaderProgramID, "vertexPosition");
    vertexUVAttribute       = GetAttributeLocation(shaderProgramID, "vertexUV");
    mvpLocation             = GetUniformLocation(shaderProgramID, "mvpMat");
    textureSamplerLocation  = GetUniformLocation(shaderProgramID, "textureSampler");

    CheckGLError("AssimpLoader::AssimpLoader");
}
Example #6
0
	/**
	*	Draw a frame
	*	@param delta Delta time between the last frame and now
	*/
	void Renderer::DrawFrame(float pDelta, bool pDisplayMenu)
	{
		UpdateDynamicElements();

		glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);

		glViewport(0, 0, _width, _height);

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		DrawUi(pDelta);

		if (pDisplayMenu && _menuPtr != nullptr)
			DrawMenu(pDelta);

		CheckGLError();
	}
Example #7
0
vsRenderTarget::vsRenderTarget( Type t, const vsSurface::Settings &settings_in ):
	m_texture(NULL),
	m_renderBufferSurface(NULL),
	m_textureSurface(NULL),
	m_type(t)
{
	CheckGLError("RenderTarget");
	vsSurface::Settings settings = settings_in;

	if ( m_type == Type_Window )
	{
		m_textureSurface = new vsSurface( settings.width, settings.height );

		m_texWidth = 1.f;
		m_texHeight = 1.f;
	}
	else
	{
		//settings.width = vsNextPowerOfTwo(settings.width);
		//settings.height = vsNextPowerOfTwo(settings.height);
		if ( m_type == Type_Multisample )
		{
			vsSurface::Settings rbs = settings_in;
			rbs.mipMaps = false;
			m_renderBufferSurface = new vsSurface(rbs, false, true);
		}
		m_textureSurface = new vsSurface(settings, (t == Type_Depth), false);
		m_texWidth = 1.0;//settings.width / (float)vsNextPowerOfTwo(settings.width);
		m_texHeight = 1.0;//settings.height / (float)vsNextPowerOfTwo(settings.height);
	}

	m_viewportWidth = settings.width;
	m_viewportHeight = settings.height;

	m_bufferCount = settings.buffers;
	m_texture = new vsTexture*[m_bufferCount];
	for ( int i = 0; i < settings.buffers; i++ )
	{
		vsString name = vsFormatString("RenderTarget%d", s_renderTargetCount++);
		vsTextureInternal *ti = new vsTextureInternal(name, m_textureSurface, i, (t == Type_Depth));
		vsTextureManager::Instance()->Add(ti);
		m_texture[i] = new vsTexture(name);
	}

	Clear();
}
Example #8
0
TestGLContext::TestGLContext(wxGLCanvas *canvas)
             : wxGLContext(canvas)
{
    SetCurrent(*canvas);

    // set up the parameters we want to use
    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_TEXTURE_2D);

    // add slightly more light, the default lighting is rather dark
    GLfloat ambient[] = { 0.5, 0.5, 0.5, 0.5 };
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);

    // set viewing projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-0.5f, 0.5f, -0.5f, 0.5f, 1.0f, 3.0f);

    // create the textures to use for cube sides: they will be reused by all
    // canvases (which is probably not critical in the case of simple textures
    // we use here but could be really important for a real application where
    // each texture could take many megabytes)
    glGenTextures(WXSIZEOF(m_textures), m_textures);

    for ( unsigned i = 0; i < WXSIZEOF(m_textures); i++ )
    {
        glBindTexture(GL_TEXTURE_2D, m_textures[i]);

        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

        const wxImage img(DrawDice(256, i + 1));

        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.GetWidth(), img.GetHeight(),
                     0, GL_RGB, GL_UNSIGNED_BYTE, img.GetData());
    }

    CheckGLError();
}
Example #9
0
void Scene::DrawScene()
{
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

    	camera.SetUpCamera();
		for( Object *obj : sceneObjs )
		obj->Draw();

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	CheckGLError();
}
AppGLContext::AppGLContext(wxGLCanvas	*canvas)
	: wxGLContext(canvas),
	mainFrame(NULL),
	seabed(NULL),
	dolphin(NULL),
	aquaSurf(NULL),
	sky(NULL)
{
	SetCurrent(*canvas);

	GLenum err = glewInit();
	if( GLEW_OK != err )
	{
		wxLogError("Can't initialize GLEW! Error: %s", glewGetErrorString(err));
	}

	glsl120fallback = false;
	fpsCount = 0;
	
	initShaders();

	seabed = new CMesh();
	seabed->Create("seabed.obj");
	seabed->setGLSLProgram(&prog);

	dolphin = new CMesh();
	dolphin->Create("Dolphin.obj");
	dolphin->setGLSLProgram(&prog);
	dolphinWorld = mat4(1.0f);

	aquaSurf = new AquaSurface("aqua.jpg", "aqua_nrm.png", 4.0f, 80);

	sky = new SkyBox();
	sky->loadCubeMap("texture/pos_x.jpg",
					 "texture/neg_x.jpg",
					 "texture/pos_y.jpg",
					 "texture/neg_y.jpg",
					 "texture/pos_z.jpg",
					 "texture/neg_z.jpg");
	sky->buildEffect("cubemap");

	//Initialize light data
	lightPos = glm::vec4(0.0f, 1.0f, 0.0f, 1.0f);

	CheckGLError();
}
Example #11
0
void DaydreamRenderer::DrawFrame(JNIEnv &env) {
    // use the scratch list to get the recommended viewports
    scratch_viewport_list_->SetToRecommendedBufferViewports();

    // construct FBO backed viewports
    gvr::BufferViewport fbo_viewport = gvr_api_->CreateBufferViewport();

    scratch_viewport_list_->GetBufferViewport(0, &scratch_viewport_);
    fbo_viewport.SetSourceBufferIndex(0);
    fbo_viewport.SetSourceFov(scratch_viewport_.GetSourceFov());
    fbo_viewport.SetReprojection(scratch_viewport_.GetReprojection());
    fbo_viewport.SetSourceUv(kDefaultUV);
    fbo_viewport.SetTargetEye(GVR_LEFT_EYE);
    viewport_list_->SetBufferViewport(0, fbo_viewport);

    scratch_viewport_list_->GetBufferViewport(1, &scratch_viewport_);
    fbo_viewport.SetSourceBufferIndex(1);
    fbo_viewport.SetSourceFov(scratch_viewport_.GetSourceFov());
    fbo_viewport.SetReprojection(scratch_viewport_.GetReprojection());
    fbo_viewport.SetSourceUv(kDefaultUV);
    fbo_viewport.SetTargetEye(GVR_RIGHT_EYE);
    viewport_list_->SetBufferViewport(1, fbo_viewport);

    gvr::Frame frame = swapchain_->AcquireFrame();

    gvr::ClockTimePoint target_time = gvr::GvrApi::GetTimePointNow();
    target_time.monotonic_system_time_nanos += kPredictionTimeWithoutVsyncNanos;

    head_view_ = gvr_api_->GetHeadSpaceFromStartSpaceRotation(target_time);

    cameraRig_->getHeadTransform()->setModelMatrix(MatrixToGLMMatrix(head_view_));

    // Render the eye images.
    for (int eye = 0; eye < 2; eye++) {
        frame.BindBuffer(eye);
        viewport_list_->GetBufferViewport(eye, &scratch_viewport_);
        SetViewport(scratch_viewport_);
        env.CallVoidMethod(rendererObject_, onDrawEyeMethodId_, eye);
        frame.Unbind();
    }

    // Submit frame.
    frame.Submit(*viewport_list_, head_view_);

    CheckGLError("onDrawFrame");
}
/**
 * Renders the 3D model by rendering every mesh in the object
 */
void AssimpLoader::Render3DModel(glm::mat4 *mvpMat) {

    if (!isObjectLoaded) {
        return;
    }

    glUseProgram(shaderProgramID);
    glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, (const GLfloat *) mvpMat);

    glActiveTexture(GL_TEXTURE0);
    glUniform1i(textureSamplerLocation, 0);

    unsigned int numberOfLoadedMeshes = modelMeshes.size();

    // render all meshes
    for (unsigned int n = 0; n < numberOfLoadedMeshes; ++n) {

        // Texture
        if (modelMeshes[n].textureIndex) {
            glBindTexture( GL_TEXTURE_2D, modelMeshes[n].textureIndex);
        }

        // Faces
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelMeshes[n].faceBuffer);

        // Vertices
        glBindBuffer(GL_ARRAY_BUFFER, modelMeshes[n].vertexBuffer);
        glEnableVertexAttribArray(vertexAttribute);
        glVertexAttribPointer(vertexAttribute, 3, GL_FLOAT, 0, 0, 0);

        // Texture coords
        glBindBuffer(GL_ARRAY_BUFFER, modelMeshes[n].textureCoordBuffer);
        glEnableVertexAttribArray(vertexUVAttribute);
        glVertexAttribPointer(vertexUVAttribute, 2, GL_FLOAT, 0, 0, 0);

        glDrawElements(GL_TRIANGLES, modelMeshes[n].numberOfFaces * 3, GL_UNSIGNED_INT, 0);

        // unbind buffers
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    }

    CheckGLError("AssimpLoader::renderObject() ");

}
Example #13
0
void GLMainContext::DrawScene()
{
    glClear(GL_COLOR_BUFFER_BIT);

    // Draw the grass
    int xPos=0, yPos=0;
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 6; j++)
        {
            xPos = i * 256/2 - 128;
            if (i%2)
                yPos = (j * 128) - 128/2;
            else
                yPos = (j * 128);

            m_pGrassImg->BlitImage(xPos, yPos);
        }
    }

    // Draw some trees
    m_pTreesImg[0]->BlitImage(15,25);
    m_pTreesImg[1]->BlitImage(695,55);
    m_pTreesImg[2]->BlitImage(15,25);
    m_pTreesImg[3]->BlitImage(300,400);
    m_pTreesImg[4]->BlitImage(125,75);
    m_pTreesImg[5]->BlitImage(350,250);
    m_pTreesImg[6]->BlitImage(400,350);
    m_pTreesImg[7]->BlitImage(350,105);
    m_pTreesImg[8]->BlitImage(530,76);
    m_pTreesImg[9]->BlitImage(125,450);
    m_pTreesImg[10]->BlitImage(425,390);
    m_pTreesImg[11]->BlitImage(25,125);
    m_pTreesImg[12]->BlitImage(550,365);
    m_pTreesImg[13]->BlitImage(680,250);
    m_pTreesImg[14]->BlitImage(245,325);
    m_pTreesImg[15]->BlitImage(300,245);

    // Draw the knight
    m_pKnightSprite->DrawSprite();
    // Move to the next frame of the animation
    m_pKnightSprite->NextFrame();

    glFlush();
    CheckGLError();
}
/**
 * Perform inits and load the triangle's vertices/colors to GLES
 */
void Triangle::PerformGLInits() {

    MyGLInits();

    //specify 3 vertices of the triangle
    static const GLfloat    vertexBufferData[] = {
            -1.0f, -1.0f, 0.0f,
            1.0f, -1.0f, 0.0f,
            0.0f,  1.0f, 0.0f
    };

    // Generate a vertex buffer and load the vertices into it
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBufferData), vertexBufferData, GL_DYNAMIC_DRAW);

    //specify colors of the 3 vertices
    static const GLfloat colorBufferData[] = {
            0.0f, 1.0f, 0.0f, //green
            0.0f, 1.0f, 0.0f, //green
            0.0f, 0.0f, 1.0f  //blue
    };

    // Generate a vertex buffer and load the colors into it
    glGenBuffers(1, &colorBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colorBufferData), colorBufferData, GL_STATIC_DRAW);

    // shader related setup
    std::string vertexShader    = "shaders/colorTriangle.vsh";
    std::string fragmentShader  = "shaders/colorTriangle.fsh";

    // compile the vertex and fragment shaders, and link them together
    shaderProgramID = LoadShaders(vertexShader, fragmentShader);
    // fetch the locations of "vertexPosition" and "vertexColor" from the shader
    vertexAttribute = GetAttributeLocation(shaderProgramID, "vertexPosition");
    colorAttribute  = GetAttributeLocation(shaderProgramID, "vertexColor");

    // set parameters that control animation of the triangle
    triangleVertexDelta = 0;
    triangleSwapRate = 0.01; // increase this to speedup the animation

    CheckGLError("Triangle::PerformGLInits");
    initsDone = true;
}
Example #15
0
void Image::Load(const std::wstring& fn)
{
    texture = std::make_shared<Texture>();

    std::string fns(fn.begin(), fn.end());
    unsigned char* data = stbi_load(fns.c_str(), &w, &h, &channels, STBI_default);
    Bind bind(*this);
    if (channels==4)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    else if (channels == 3)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    else
        assert(false && "Unsupported image format");
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    //glTexParameteri(GL_TEXTURE_MAX_LEVEL, 0);
    CheckGLError();
    stbi_image_free(data);
}
 MultisampleBuffer(GLsizei bufferWidth, GLsizei bufferHeight) {
     maxSize = 0;
     // Query the maximum size
     glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &maxSize);
     printf("Maximum reported renderbuffer size is: %d\n", maxSize);
     
     // Gen buffers
     glGenFramebuffers(1, &fbo);
     glGenTextures(1, &colorBuffer);
     
     glGenFramebuffers(1, &multisampleFBO);
     glGenRenderbuffers(1, &multisampleColorBuffer);
     glGenRenderbuffers(1, &multisampleDepthBuffer);
     
     // Setup Standard FBO
     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
     
     glBindTexture(GL_TEXTURE_2D, colorBuffer);
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     glBindTexture(GL_TEXTURE_2D, 0);
     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorBuffer, 0);
     
     // Setup Multisample FBO
     glBindFramebuffer(GL_FRAMEBUFFER, multisampleFBO);
     
     glBindRenderbuffer(GL_RENDERBUFFER, multisampleColorBuffer);
     glRenderbufferStorageMultisample(GL_RENDERBUFFER, sampleCount, GL_RGBA, bufferWidth, bufferHeight);
     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, multisampleColorBuffer);
     
     glBindRenderbuffer(GL_RENDERBUFFER, multisampleDepthBuffer);
     glRenderbufferStorageMultisample(GL_RENDERBUFFER, sampleCount, GL_DEPTH_COMPONENT24, bufferWidth, bufferHeight);
     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, multisampleDepthBuffer);
     
     glBindFramebuffer(GL_FRAMEBUFFER, 0);
     
     width = bufferWidth;
     height = bufferHeight;
     
     CheckGLError("MultisampleBuffer created");
 }
Example #17
0
void VideoEngine::MakeScreenshot(const std::string &filename)
{
    private_video::ImageMemory buffer;

    // Retrieve the width and height of the viewport.
    GLint viewport_dimensions[4]; // viewport_dimensions[2] is the width, [3] is the height
    glGetIntegerv(GL_VIEWPORT, viewport_dimensions);

    // Buffer to store the image before it is flipped
    buffer.width = viewport_dimensions[2];
    buffer.height = viewport_dimensions[3];
    buffer.pixels = malloc(buffer.width * buffer.height * 3);
    buffer.rgb_format = true;

    // Read the viewport pixel data
    glReadPixels(viewport_dimensions[0], viewport_dimensions[1],
                 buffer.width, buffer.height, GL_RGB, GL_UNSIGNED_BYTE, buffer.pixels);

    if(CheckGLError() == true) {
        IF_PRINT_WARNING(VIDEO_DEBUG) << "an OpenGL error occured: " << CreateGLErrorString() << std::endl;

        free(buffer.pixels);
        buffer.pixels = NULL;
        return;
    }

    // Vertically flip the image, then swap the flipped and original images
    void *buffer_temp = malloc(buffer.width * buffer.height * 3);
    for(uint32 i = 0; i < buffer.height; ++i) {
        memcpy((uint8 *)buffer_temp + i * buffer.width * 3,
               (uint8 *)buffer.pixels + (buffer.height - i - 1) * buffer.width * 3, buffer.width * 3);
    }
    void *temp = buffer.pixels;
    buffer.pixels = buffer_temp;
    buffer_temp = temp;

    buffer.SaveImage(filename);

    free(buffer_temp);
    free(buffer.pixels);
    buffer.pixels = NULL;
}
void AppGLContext::render()
{
	//calc fps & update status bar
	fpsTimer.AdvanceTime();
	fpsCount++;	
	if(fpsTimer.GetElapsedTime()>1)
	{	
		if( mainFrame )
		{
			char tempBuf[512];		
			sprintf(tempBuf,"FPS: %d",fpsCount);
			mainFrame->getStatusBar()->SetStatusText(tempBuf);
		}
		fpsTimer.Reset();
		fpsCount=0;
	}

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	if( seabed )
	{
		seabed->Render();
	}

	if( dolphin )
	{
		dolphin->Render();
	}

	if( aquaSurf )
	{
		aquaSurf->render();
	}

	if( sky )
	{
		sky->render();
	}

    CheckGLError();
}
Example #19
0
		//--------------------------------------------------------------
		void Lighting::setup()
		{
			ENTROPY_SCENE_EXIT_LISTENER;
			ENTROPY_SCENE_RESIZE_LISTENER;
			ENTROPY_SCENE_UPDATE_LISTENER;
			ENTROPY_SCENE_DRAW_WORLD_LISTENER;
			ENTROPY_SCENE_GUI_LISTENER;

			// Load shaders.
			this->shader.load("shaders/main");
			this->shader.printActiveUniforms();
			this->shader.printActiveUniformBlocks();
			CheckGLError();

			this->skyboxShader.load("shaders/skybox");
			glGenVertexArrays(1, &this->defaultVao);
			CheckGLError();

			// Set up view UBO.
			const int viewUboBinding = 1;
			this->viewUbo.setup(viewUboBinding);
			this->viewUbo.configureShader(this->shader);
			this->viewUbo.configureShader(this->skyboxShader);
			CheckGLError();

			// Set up lighting.
			this->lightingSystem.setup(this->getCamera());
			this->lightingSystem.configureShader(this->shader);
			this->lightingSystem.setAmbientIntensity(0.5f);
			CheckGLError();

			// Set up PBR.
			this->material.setBaseColor(ofFloatColor(1.0f, 1.0f, 1.0f, 1.0f));
			this->material.setMetallic(0.0f);
			this->material.setRoughness(0.0f);
			this->material.setEmissiveColor(ofFloatColor(1.0f, 0.4f, 0.0f, 1.0f));
			this->material.setEmissiveIntensity(0.0f);
			CheckGLError();

			this->skyboxMap.loadDDSTexture("textures/output_skybox.dds");
			this->irradianceMap.loadDDSTexture("textures/output_iem.dds");
			this->radianceMap.loadDDSTexture("textures/output_pmrem.dds");
			CheckGLError();

			glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);

			this->sphere = ofSpherePrimitive(1.0f, 24);
		}
void Scene::Draw()
{
	// Clear colour and depth buffers
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	// Update based on window details (also sets initial projection properties)
	Reshape(windowWidth, windowHeight);
	// Reset MODELVIEW matrix
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	// Setup viewing properties
	camera.SetupCamera();
	// Display all objects in the Scene
	for (DisplayableObject* obj : objects)
		obj->Display();

	// Zealous reset of MODELVIEW matrix
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	// Dump silent GL errors into console output
	CheckGLError();
}
Example #21
0
 void Texture2D::initWithData(const void* data,ssize_t dataLen,Texture2D::PixelFormat format,int width,int height){
     assert(dataLen > 0 && width > 0 && height > 0);
     
     glGenTextures(1,&_textureId);
     glBindTexture(GL_TEXTURE_2D,_textureId);
     
     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
     
     if (format == PixelFormat::I8) {
         glTexImage2D(GL_TEXTURE_2D,0,GL_LUMINANCE,width,height,0,GL_UNSIGNED_BYTE,GL_LUMINANCE,data);
     }else if (format == PixelFormat::RGBA8888){
         glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,0,GL_UNSIGNED_BYTE,GL_RGBA,data);
     }
     CheckGLError();
     
     _width = width;
     _height = height;
     _pixelFormat = format;
 }
Example #22
0
void GLTextureObject::resize(int w, int h) {
  if (fboMode == false) {

    // cant do resizing in this function as its only for resizing for fbo
    // drawing
    return;
  }

  else {
    width = w;
    height = h;

    bind();
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(textureType, 0, GL_RGBA32F, w, h, 0, GL_RGB, GL_FLOAT, NULL);
    CheckGLError("resize");
  }
}
/**
 * Basic initializations for GL.
 */
void MyGLInits() {

    // White background
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    // Enable depth test
    glEnable(GL_DEPTH_TEST);
    // Accept fragment if it closer to the camera than the former one
    glDepthFunc(GL_LEQUAL);

    MyLOGI("OpenGL %s, GLSL %s", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION));

    // check if the device supports GLES 3 or GLES 2
    const char* versionStr = (const char*)glGetString(GL_VERSION);
    if (strstr(versionStr, "OpenGL ES 3.") && gl3stubInit()) {
        MyLOGD("Device supports GLES 3");
    } else {
        MyLOGD("Device supports GLES 2");
    }

    CheckGLError("MyGLInits");
}
Example #24
0
	void ShaderProgram::FetchAttribs()
	{
		GLint num_active_attribs = 0;
		glGetProgramiv(m_program_id, GL_ACTIVE_ATTRIBUTES, &num_active_attribs);
		
		// goes thru each attrib defined in the vertex shader
		// attributes should only be float1, float2, float3, float4
		// attributes should be of the form: "attrib_0", "attrib_1"
		
		for ( int i = 0; i < num_active_attribs; ++i )
		{
			char buffer[64];
			
			GLsizei		attrib_length = 0;
			GLint		attrib_size = 0;
			GLenum		attrib_type = 0;
			
			glGetActiveAttrib( m_program_id, (GLuint)i, (GLsizei)sizeof(buffer), &attrib_length, &attrib_size, &attrib_type, buffer );
			
			// assure data correct
			ASSERT( attrib_length );
			
			// attrib types can only be float1-float4
			ASSERT( attrib_type == GL_FLOAT || attrib_type == GL_FLOAT_VEC2 || attrib_type == GL_FLOAT_VEC3 || attrib_type == GL_FLOAT_VEC4 );
			
			// grab number
			const char * attrib_title = "attrib_";
			const char * index_str = strstr( buffer, attrib_title);
			ASSERT( index_str );
			index_str += strlen(attrib_title);
			int attrib_index = atoi(index_str);
			
			// bind the attribute
			glBindAttribLocation( m_program_id, attrib_index, buffer );
		}
		
		CheckGLError();
	}
Example #25
0
void GLTextureObject::resizeWith3dData(int w, int h, int d, void *data,
                                       GLint internalFormat,
                                       GLenum incomingFormat) {

  setRenderTargetMode(false);
  textureType = GL_TEXTURE_3D;
  width = w;
  height = h;
  depth = d;
  this->bind();
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER,
                  GL_LINEAR); // GL_NEAREST
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // GL_LINEAR ccccc
  glTexImage3D(textureType, 0, internalFormat, w, h, d, 0, incomingFormat,
               GL_FLOAT, data);
  CheckGLError("resize");
}
// Copies the given multisample buffer to a texture and renders that texture to the screen
void DrawBuffer(MultisampleBuffer* buffer) {
    // Copies the content of the multisampled buffer into a renderable texture
    buffer->Commit();
    
    glViewport(0, 0, winWidth, winHeight);
    
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glColor3f(1.0f, 1.0f, 1.0f);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, buffer->colorBuffer);
	glBegin(GL_POLYGON);
        glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, -1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, 1.0f);
        glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, 1.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, -1.0f);
	glEnd();
    glDisable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);
    
    CheckGLError("Render to Screen");
}
Example #27
0
    void asdf_multiplat_t::render_debug() {
        
        LOG_IF(CheckGLError(), "Error before drawing spritebatch debug information");

        auto passthrough = Content.shaders["passthrough"];
        passthrough->use_program();
        passthrough->world_matrix = glm::mat4();
        passthrough->view_matrix = glm::mat4();
        passthrough->projection_matrix = spritebatch->spritebatch_shader->projection_matrix;

        passthrough->update_wvp_uniform();
        glUniform4f(passthrough->uniforms["Color"], 0.0f, 0.2f, 1.0f, 1.0f);

        glDisable(GL_CULL_FACE);
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        glLineWidth(1.2f);

        //glBegin(GL_POLYGON);
        //glColor4f(0.5f, 0.3f, 1.0f, 1.0f);
        //glVertex2f(-1, 1);
        //glVertex2f(1, 1);
        //glVertex2f(1, -1);
        //glVertex2f(-1, -1);
        //glEnd();

        //glBegin(GL_POLYGON);
        //glColor4f(0.3f, 0.1f, 0.3f, 1.0f);
        //glVertex2f(-0.5f, 0.5f);
        //glVertex2f(0.5f, 0.5f);
        //glVertex2f(0.5f, -0.5f);
        //glVertex2f(-0.5f, -0.5f);
        //glEnd();

        main_view->render_debug();

        glUseProgram(0);
    }
Example #28
0
	void ShaderProgram::LinkShaders()
	{
		ASSERT( m_vp != nullptr && m_vp->Id() );
		ASSERT( m_fp != nullptr && m_fp->Id() );
		
		// attach
		glAttachShader( m_program_id, m_vp->Id() );
		glAttachShader( m_program_id, m_fp->Id() );

		// link
		GLint status;
		glLinkProgram(m_program_id);
		//glValidateProgram(m_program_id);
		glGetProgramiv(m_program_id, GL_LINK_STATUS, &status);
		ASSERT( status == GL_TRUE );		
		
		FetchUniforms();
		FetchAttribs();
		
		// TODO: Why do I have to link twice?
		glLinkProgram(m_program_id);
		
		CheckGLError();
	}
void DrawQuad(MultisampleBuffer* buffer) {
    ts += 1.0f;
    
    GLfloat r = (sinf(ts/10.0f) + 1.0f) * 0.5f;
    GLfloat g = (cosf(ts/100.0f) + 1.0f) * 0.5f;
    GLfloat b = (sinf(ts/1000.0f) + 1.0f) * 0.5f;
    
    glBindFramebuffer(GL_FRAMEBUFFER, buffer->multisampleFBO);
    glViewport(0, 0, buffer->width, buffer->height);
    
    glClearColor(0.0, 0.0, 0.7, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(r, g, b);
	glBegin(GL_POLYGON);
        glVertex2f(-0.5, -0.5);
        glVertex2f(-0.5, 0.5);
        glVertex2f(0.5, 0.5);
        glVertex2f(0.5, -0.5);
	glEnd();
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    
    CheckGLError("Render to Framebuffer");
}
Example #30
0
    virtual void initialize(glowwindow::Window & window) override {

        window.addTimer(0, 0);

        glow::debugmessageoutput::enable();

        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        CheckGLError();

        glow::Shader* vertexShader = glowutils::createShaderFromFile(GL_VERTEX_SHADER, "data/transparency/transparency.vert");

        m_algos.push_back(new glowutils::GlBlendAlgorithm);
        m_algos.push_back(new glowutils::ABufferAlgorithm);
        m_algos.push_back(new glowutils::WeightedAverageAlgorithm);
        m_algos.push_back(new glowutils::HybridAlgorithm);
        for (auto& algo : m_algos) {
            algo->initialize("data/transparency/", vertexShader, nullptr);
        }

        m_cube = new glowutils::UnitCube;

        m_camera = new glowutils::Camera(glm::vec3(0.0f, 0.0f, -15.0f), glm::vec3(0.0f, 0.0f, -8.0f), glm::vec3(0.0f, 1.0f, 0.0f));

        // Setup the screen aligned quad stuff
        glow::Program* quadProgram = new glow::Program();
        quadProgram->attach(glowutils::createShaderFromFile(GL_FRAGMENT_SHADER, "data/transparency/quad.frag"));
        quadProgram->attach(glowutils::createShaderFromFile(GL_VERTEX_SHADER, "data/transparency/quad.vert"));
        m_quad = new glowutils::ScreenAlignedQuad(quadProgram);

        m_aabb.extend(glm::vec3(-1.f, -0.5f, -10.5f));
        m_aabb.extend(glm::vec3(0.f, 0.5f, -5.5));

        m_nav.setCamera(m_camera);
        m_nav.setCoordinateProvider(this);
        m_nav.setBoundaryHint(m_aabb);
    }