int CALLBACK WinMain(
	__in  HINSTANCE hInstance,
	__in  HINSTANCE hPrevInstance,
	__in  LPSTR lpCmdLine,
	__in  int nCmdShow
	)
#endif
{
	if (!glfwInit())
		return 0;

	GLFWwindow* window = glfwCreateWindow(640, 480, "Window Title", NULL, NULL);
	glfwMakeContextCurrent(window);
	glewExperimental = GL_TRUE;
	if (glewInit() != GLEW_OK)
		return -1;


	GLuint vertexBuffer;
	glGenBuffers(1, &vertexBuffer);

	Shader* vertexShader = new Shader("Content\\Shaders\\vertexShader.vs", GL_VERTEX_SHADER);
	Shader* fragmentShader = new Shader("Content\\Shaders\\fragmentShader.fs", GL_FRAGMENT_SHADER);
	ShaderProgram* program = new ShaderProgram();
	program->AttachShader(vertexShader);
	program->AttachShader(fragmentShader);

	program->BindFragDataLocation(0, "outColour");
	program->Link();
	program->Use();

	GLint posAttrib = program->GetAttribLocation("position");
	
	GLuint vao;
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	GLuint vbo;
	glGenBuffers(1, &vbo);

	float vertices[] = {
		0.0f, 0.5f,
		0.5f, -0.5f,
		-0.5f, -0.5f
	};

	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	glEnableVertexAttribArray(posAttrib);
	glVertexAttribPointer
		(posAttrib, // Ref input
		2, // Number of values for input  
		GL_FLOAT,  // Type of each component
		GL_FALSE, // Whether to normalise
		0,  // Stride
		0); // Offset

	

	

	

	while (!glfwWindowShouldClose(window))
	{
		glClearColor(0.f, 0.f, 0.f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		glDrawArrays(GL_TRIANGLES, 0, 3);

		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	glfwDestroyWindow(window);
	return 0;
}
Exemple #2
0
int main(int argc, char * argv[]) {

	char path[256];
	engine = new Engine(argc, argv);

	ShaderProgram* defergbufferShader = new ShaderProgram();
	FBO g_buffer(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT), 1);
	FBO shadow_buffer(2000, 2000, 1);

	GET_SHADER_VISBUFFER_PATH(path, 256, "defergbuffer.vert");
	if (!defergbufferShader->AddVertexShaderPath(path)) return 0;
	GET_SHADER_VISBUFFER_PATH(path, 256, "defergbuffer.frag");
	if (!defergbufferShader->AddFragmentShaderPath(path)) return 0;
	if (!defergbufferShader->Link()) return 0;

	defergbufferShader->SetAttribVertex("vertex_coord");
	defergbufferShader->SetAttribNormal("normal_coord");

	ShaderProgram* shadowMapShader = new ShaderProgram();
	GET_SHADER_VISBUFFER_PATH(path, 256, "shadow.vert");
	if (!shadowMapShader->AddVertexShaderPath(path)) return 0;
	GET_SHADER_VISBUFFER_PATH(path, 256, "shadow.frag");
	if (!shadowMapShader->AddFragmentShaderPath(path)) return 0;
	if (!shadowMapShader->Link()) return 0;
	shadowMapShader->SetAttribVertex("vertex");

	ShaderProgram* shadowRenderShader = new ShaderProgram();
	GET_SHADER_VISBUFFER_PATH(path, 256, "shadowRender.vert");
	if (!shadowRenderShader->AddVertexShaderPath(path)) return 0;
	GET_SHADER_VISBUFFER_PATH(path, 256, "shadowRender.frag");
	if (!shadowRenderShader->AddFragmentShaderPath(path)) return 0;
	if (!shadowRenderShader->Link()) return 0;
	shadowRenderShader->SetAttribVertex("vertex");
	shadowRenderShader->SetAttribTexture("texture_coordinate");
    
	// --------------SHADER LOADING--------------------------

	GET_MODEL_PATH(path, 256, "bunny_smooth.ply");
	Mesh bunny1, bunny2, bunny3;
	Mesh shadowBunny1, shadowBunny2, shadowBunny3;
    Mesh square1, square2, square3, square4, square5, square6;
    Mesh shadowSquare1, shadowSquare2, shadowSquare3, shadowSquare4, shadowSquare5, shadowSquare6;
	bunny1.Load(path);
	bunny2.Load(path);
	bunny3.Load(path);
	shadowBunny1.Load(path);
	shadowBunny2.Load(path);
	shadowBunny3.Load(path);
    
    
    square1.LoadSquare();
    square2.LoadSquare();
    square3.LoadSquare();
    square4.LoadSquare();
    square5.LoadSquare();
    square6.LoadSquare();
    shadowSquare1.LoadSquare();
    shadowSquare2.LoadSquare();
    shadowSquare3.LoadSquare();
    shadowSquare4.LoadSquare();
    shadowSquare5.LoadSquare();
    shadowSquare6.LoadSquare();

	GET_MODEL_PATH(path, 256, "sphere.ply");
	Mesh Light1, Light2, Light3;
	Light1.Load(path);
    Light2.Load(path);
    Light3.Load(path);

	Mesh AmbientFSQ;
	AmbientFSQ.LoadSquare();
    
    Mesh ShadowRenderFSQ;
    ShadowRenderFSQ.LoadSquare();
    
    Mesh DeferredBRDFFSQ;
    DeferredBRDFFSQ.LoadSquare();

	// ---------------MODEL LOADING--------------------------

	Scene bunnyScene;
	bunnyScene.addObject(&bunny1);
	bunnyScene.addObject(&bunny2);
	bunnyScene.addObject(&bunny3);
	bunnyScene.addObject(&Light1);
    bunnyScene.addObject(&Light2);
    bunnyScene.addObject(&Light3);
    bunnyScene.addObject(&square1);
    bunnyScene.addObject(&square2);
    bunnyScene.addObject(&square3);
    bunnyScene.addObject(&square4);
    bunnyScene.addObject(&square5);
    bunnyScene.addObject(&square6);


    Scene ambientScene;
	ambientScene.addObject(&AmbientFSQ);

	Scene shadowMapScene;
	shadowMapScene.addObject(&shadowBunny1);
	shadowMapScene.addObject(&shadowBunny2);
	shadowMapScene.addObject(&shadowBunny3);
    shadowMapScene.addObject(&shadowSquare1);
    shadowMapScene.addObject(&shadowSquare2);
    shadowMapScene.addObject(&shadowSquare3);
    shadowMapScene.addObject(&shadowSquare4);
    shadowMapScene.addObject(&shadowSquare5);
    shadowMapScene.addObject(&shadowSquare6);

    Scene shadowRenderScene;
    shadowRenderScene.addObject(&ShadowRenderFSQ);
    
    Scene deferredBRDFScene;
    deferredBRDFScene.addObject(&DeferredBRDFFSQ);

	// --------------SCENE LOADING --------------------------

	Pass gbufferPass(defergbufferShader, &bunnyScene);
	Pass shadowPass(shadowMapShader, &shadowMapScene);
	Pass shadowRenderPass(shadowRenderShader, &shadowRenderScene);

	gbufferPass.BindAttribNormal();
	gbufferPass.BindAttribVertex();
	shadowPass.BindAttribVertex();
	shadowRenderPass.BindAttribVertex();
	shadowRenderPass.BindAttribTexture();

	// --------------- BIND ATTRIBUTES ---------------------

	gbufferPass.BindUniformMatrix4("ViewMatrix", &ViewMatrixPtr);
	gbufferPass.BindUniformMatrix4("ProjectionMatrix", &ProjectionMatrixPtr);
	shadowPass.BindUniformMatrix4("ViewMatrix", &Light1ViewMatrixPtr);
	shadowPass.BindUniformMatrix4("ProjectionMatrix", &ProjectionMatrixPtr);
	shadowRenderPass.BindUniformMatrix4("shadowMatrix", &Light1ShadowMatrixPtr);
	shadowRenderPass.BindUniformVec3("lightPos", &Light1PosPtr);
	shadowRenderPass.BindUniformVec3("eyePos", &EyePosPtr);
	shadowRenderPass.BindUniformVec3("lightValue", &Light1DiffusePtr);

	// ------------- BIND PASS-WISE UNIFORMS---------------

	gbufferPass.MeshBindUniformMatrix4(&bunny1, "ModelMatrix", &Bunny1ModelMatrixPtr);
	gbufferPass.MeshBindUniformMatrix4(&bunny1, "NormalMatrix", &Bunny1NormalMatrixPtr);
	shadowRenderPass.MeshBindUniformVec3(&bunny1, "diffuse", &Bunny1DiffusePtr);
	shadowRenderPass.MeshBindUniformVec3(&bunny1, "specular", &Bunny1SpecularPtr);
	gbufferPass.MeshBindUniformMatrix4(&bunny2, "ModelMatrix", &Bunny2ModelMatrixPtr);
	gbufferPass.MeshBindUniformMatrix4(&bunny2, "NormalMatrix", &Bunny2NormalMatrixPtr);
	gbufferPass.MeshBindUniformVec3(&bunny2, "diffuse", &Bunny2DiffusePtr);
	gbufferPass.MeshBindUniformVec3(&bunny2, "specular", &Bunny2SpecularPtr);
	gbufferPass.MeshBindUniformMatrix4(&bunny3, "ModelMatrix", &Bunny3ModelMatrixPtr);
	gbufferPass.MeshBindUniformMatrix4(&bunny3, "NormalMatrix", &Bunny3NormalMatrixPtr);
	gbufferPass.MeshBindUniformVec3(&bunny3, "diffuse", &Bunny3DiffusePtr);
	gbufferPass.MeshBindUniformVec3(&bunny3, "specular", &Bunny3SpecularPtr);
	gbufferPass.MeshBindUniformMatrix4(&Light1, "ModelMatrix", &Light1ModelMatrixPtr);
	gbufferPass.MeshBindUniformMatrix4(&Light1, "NormalMatrix", &Light1NormalMatrixPtr);
	gbufferPass.MeshBindUniformVec3(&Light1, "diffuse", &Light1DiffusePtr);
	gbufferPass.MeshBindUniformVec3(&Light1, "specular", &Light1SpecularPtr);
    gbufferPass.MeshBindUniformMatrix4(&Light2, "ModelMatrix", &Light2ModelMatrixPtr);
    gbufferPass.MeshBindUniformMatrix4(&Light2, "NormalMatrix", &Light2NormalMatrixPtr);
    gbufferPass.MeshBindUniformVec3(&Light2, "diffuse", &Light2DiffusePtr);
    gbufferPass.MeshBindUniformVec3(&Light2, "specular", &Light2SpecularPtr);
    gbufferPass.MeshBindUniformMatrix4(&Light3, "ModelMatrix", &Light3ModelMatrixPtr);
    gbufferPass.MeshBindUniformMatrix4(&Light3, "NormalMatrix", &Light3NormalMatrixPtr);
    gbufferPass.MeshBindUniformVec3(&Light3, "diffuse", &Light3DiffusePtr);
    gbufferPass.MeshBindUniformVec3(&Light3, "specular", &Light3SpecularPtr);
    gbufferPass.MeshBindUniformMatrix4(&square1, "ModelMatrix", &Square1ModelMatrixPtr);
    gbufferPass.MeshBindUniformMatrix4(&square1, "NormalMatrix", &Square1NormalMatrixPtr);
    gbufferPass.MeshBindUniformVec3(&square1, "diffuse", &Square1DiffusePtr);
    gbufferPass.MeshBindUniformVec3(&square1, "specular", &Square1SpecularPtr);
    gbufferPass.MeshBindUniformMatrix4(&square2, "ModelMatrix", &Square2ModelMatrixPtr);
    gbufferPass.MeshBindUniformMatrix4(&square2, "NormalMatrix", &Square2NormalMatrixPtr);
    gbufferPass.MeshBindUniformVec3(&square2, "diffuse", &Square2DiffusePtr);
    gbufferPass.MeshBindUniformVec3(&square2, "specular", &Square2SpecularPtr);
    gbufferPass.MeshBindUniformMatrix4(&square3, "ModelMatrix", &Square3ModelMatrixPtr);
    gbufferPass.MeshBindUniformMatrix4(&square3, "NormalMatrix", &Square3NormalMatrixPtr);
    gbufferPass.MeshBindUniformVec3(&square3, "diffuse", &Square3DiffusePtr);
    gbufferPass.MeshBindUniformVec3(&square3, "specular", &Square3SpecularPtr);
    gbufferPass.MeshBindUniformMatrix4(&square4, "ModelMatrix", &Square4ModelMatrixPtr);
    gbufferPass.MeshBindUniformMatrix4(&square4, "NormalMatrix", &Square4NormalMatrixPtr);
    gbufferPass.MeshBindUniformVec3(&square4, "diffuse", &Square4DiffusePtr);
    gbufferPass.MeshBindUniformVec3(&square4, "specular", &Square4SpecularPtr);
    gbufferPass.MeshBindUniformMatrix4(&square5, "ModelMatrix", &Square5ModelMatrixPtr);
    gbufferPass.MeshBindUniformMatrix4(&square5, "NormalMatrix", &Square5NormalMatrixPtr);
    gbufferPass.MeshBindUniformVec3(&square5, "diffuse", &Square5DiffusePtr);
    gbufferPass.MeshBindUniformVec3(&square5, "specular", &Square5SpecularPtr);
    gbufferPass.MeshBindUniformMatrix4(&square6, "ModelMatrix", &Square6ModelMatrixPtr);
    gbufferPass.MeshBindUniformMatrix4(&square6, "NormalMatrix", &Square6NormalMatrixPtr);
    gbufferPass.MeshBindUniformVec3(&square6, "diffuse", &Square6DiffusePtr);
    gbufferPass.MeshBindUniformVec3(&square6, "specular", &Square6SpecularPtr);

	shadowPass.MeshBindUniformMatrix4(&shadowBunny1, "ModelMatrix", &Bunny1ModelMatrixPtr);
	shadowPass.MeshBindUniformMatrix4(&shadowBunny2, "ModelMatrix", &Bunny2ModelMatrixPtr);
	shadowPass.MeshBindUniformMatrix4(&shadowBunny3, "ModelMatrix", &Bunny3ModelMatrixPtr);
    shadowPass.MeshBindUniformMatrix4(&shadowSquare1, "ModelMatrix", &Square1ModelMatrixPtr);
    shadowPass.MeshBindUniformMatrix4(&shadowSquare2, "ModelMatrix", &Square2ModelMatrixPtr);
    shadowPass.MeshBindUniformMatrix4(&shadowSquare3, "ModelMatrix", &Square3ModelMatrixPtr);
    shadowPass.MeshBindUniformMatrix4(&shadowSquare4, "ModelMatrix", &Square4ModelMatrixPtr);
    shadowPass.MeshBindUniformMatrix4(&shadowSquare5, "ModelMatrix", &Square5ModelMatrixPtr);
    shadowPass.MeshBindUniformMatrix4(&shadowSquare6, "ModelMatrix", &Square6ModelMatrixPtr);


	// ------------BIND MESH-WISE UNIFORMS----------------

	gbufferPass.SetTarget(&g_buffer);
	shadowPass.SetTarget(&shadow_buffer);

	Texture* visibilityTex = g_buffer.GetTexture(0);
	Texture* shadowTex = shadow_buffer.GetTexture(0);

	shadowRenderPass.BindTexture("visibilityTexture", visibilityTex);
	shadowRenderPass.BindTexture("shadowTexture", shadowTex);


	gbufferPass.SetBlend(false);
	gbufferPass.SetDepthTest(true);
    gbufferPass.SetClear(true);
	shadowPass.SetBlend(false);
	shadowPass.SetDepthTest(true);
    shadowPass.SetClear(true);
	shadowRenderPass.SetBlend(true);
	shadowRenderPass.SetDepthTest(false);
    shadowRenderPass.SetClear(false);

	// ---------------PASS CONFIG --------------------------
	engine->addPass(&gbufferPass);
	engine->addPass(&shadowPass);
	engine->addPass(&shadowRenderPass);



	// ----------------ENGINE------------------------------

#ifdef _WIN32
	glutMouseWheelFunc(mouseWheel);
#endif

	glutDisplayFunc(Draw);
	glutMouseFunc(mouseClick);
	glutMotionFunc(mouseMove);

	glutKeyboardUpFunc(keyboardPress);
	glutMainLoop();

	return 0;
}
Exemple #3
0
void Renderer::Init()
{
	// initialize OpenGL ES and EGL

	/*
	 * Here specify the attributes of the desired configuration.
	 * Below, we select an EGLConfig with at least 8 bits per color
	 * component compatible with on-screen windows
	 */
	const EGLint attribs[] =
	{
			EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
			EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
			EGL_BLUE_SIZE, 8,
			EGL_GREEN_SIZE, 8,
			EGL_RED_SIZE, 8,
			EGL_NONE
	};

	EGLint format;
	EGLint numConfigs;
	EGLConfig config;

	m_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

	eglInitialize(m_display, NULL, NULL);

	/* Here, the application chooses the configuration it desires. In this
	 * sample, we have a very simplified selection process, where we pick
	 * the first EGLConfig that matches our criteria */
	eglChooseConfig(m_display, attribs, &config, 1, &numConfigs);

	/* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
	 * guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
	 * As soon as we picked a EGLConfig, we can safely reconfigure the
	 * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
	eglGetConfigAttrib(m_display, config, EGL_NATIVE_VISUAL_ID, &format);

	ANativeWindow_setBuffersGeometry(m_pState->window, 0, 0, format);

	m_surface = eglCreateWindowSurface(m_display, config, m_pState->window, NULL);

	EGLint contextAttribs[] =
	{
			EGL_CONTEXT_CLIENT_VERSION, 2,
			EGL_NONE
	};
	m_context = eglCreateContext(m_display, config, NULL, contextAttribs);

	eglMakeCurrent(m_display, m_surface, m_surface, m_context);

	eglQuerySurface(m_display, m_surface, EGL_WIDTH, &m_width);
	eglQuerySurface(m_display, m_surface, EGL_HEIGHT, &m_height);

	for (TextureVectorIterator iter = m_textures.begin(); iter != m_textures.end(); ++iter)
	{
		Texture* pCurrent = *iter;
		pCurrent->Init();
	}

	for (ShaderVectorIterator iter = m_shaders.begin(); iter != m_shaders.end(); ++iter)
	{
		ShaderProgram* pCurrent = *iter;
		pCurrent->Link();
	}

	m_initialized = true;

	DLOG() << "initialized" << m_initialized;
}