Exemplo n.º 1
0
void Render_GL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
    programobj.Use();

    Matrix<GLfloat, 3, 1, ColMajor > direction(0.0f, 5.0f - (-5.0f), 0.0f - 5.0f), camera_pos(0.0f, -5.0f, 5.0f), up(0.0f, 0.0f, 1.0f);

    Matrix<GLfloat, 4, 4, ColMajor > pers_view_mat, world_mat, rmat, smat, tmat, perspective_mat, camera_mat;

    //  scale_matrix(0.5f, smat.data());
    //  rotation_matrix(0.0f, 0.0f, 1.0f, view_rotx, rmat.data());
    scale_matrix(smat, 0.3f);
//    scale_matrix(smat, 0.3f, 0.3f, 0.3f);
    rotation_matrix(rmat, 0.0f, 0.0f, 1.0f, view_rotx);
//    rotation_matrix(rmat, 1.0f, 1.0f, 1.0f, view_rotx);
    translation_matrix(tmat, 0.0f, 5.0f, 0.0f);
    perspective_matrix(perspective_mat, (GLfloat) 3.14f * (GLfloat) 0.25f, (GLfloat) screen_width / (GLfloat) screen_height, (GLfloat) 0.01f, (GLfloat) 1000.0f);
    camera_matrix(camera_mat, direction, camera_pos, up);
    world_mat = tmat * smat * rmat;
    pers_view_mat = perspective_mat * camera_mat * world_mat;
    glUniformMatrix4fv(u_matrix, 1, GL_FALSE, pers_view_mat.data());
    glUniformMatrix4fv(u_world_mat, 1, GL_FALSE, world_mat.data());


//    texobj.Bind();
//    meshobj.Render();
    objobj.Render();
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
void DrawScene()
{
	static ShaderProgram sp("anag.vs","anag.fs");



	cam.SetupPosition();				//домножение текущей матрицы преобразований в соответствии с положением и ориентацией камеры

	
	glLightfv(GL_LIGHT0,GL_POSITION, &light.x);

	
	glDisable(GL_LIGHTING);
	glColor4d(0,0,0,1);
	glBegin(GL_LINES);
	glLineWidth(3);
	for(int i=0;i<section.size();i++)
	{
		//glColor3d(0,0,0);
		glVertex3fv(&section[i].v[0].x);
		//glColor3d(1,1,1);
		glVertex3fv(&section[i].v[1].x);
	}
	for(int i=0;i<section2d.size();i++)
	{
		//glColor3d(0,0,0);
		glVertex2fv(&section2d[i].v[0].x);
		//glColor3d(1,1,1);
		glVertex2fv(&section2d[i].v[1].x);
	}
/*
	float grid_size=2;
	glColor4d(0.1,0.5,0.1,0.7);
	glVertex2f(-grid_size,0);
	glVertex2f(grid_size,0);
	glVertex2f(0,-grid_size);
	glVertex2f(0,grid_size);

	glColor4d(0.1,0.5,0.1,0.3);

	float grid_step=0.1;
	for(float i=-grid_size;i<=grid_size;i+=grid_step)
	{
		glVertex2f(-grid_size,i);
		glVertex2f(grid_size,i);
		glVertex2f(i,-grid_size);
		glVertex2f(i,grid_size);
	}
*/
	glEnd();

	//glColor4d(0.1,0.5,0.1,0.5);	for(int i=1;i<200;i++)		DrawCircle(0.1*i,vec2(0),45);

	psd[cur_psd].Draw();
	
/*
	glPointSize(12);
	glColor4d(0.1,0.5,0.1,0.5);
	
	glBegin(GL_POINTS);
	for(int i=0;i<section2d.size();i++)
	{
		glVertex2fv(&section2d[i].v[0].x);
		glVertex2fv(&section2d[i].v[1].x);
	}
	glEnd();
	glPointSize(1);
	//DrawRectangle(vec2(-PI,-PI*0.5),vec2(PI,PI*0.5));
*/
	glLineWidth(1);
	glEnable(GL_LIGHTING);

	
	if(shaders_is) sp.Use();
	SetAnag(&sp,anag,anag_left);
	sp.SetVar("LightDir",vec3(light));
	sp.SetVar("pos",cam.GetPosition());
	sp.SetVar("params",vec4(0));

	//glEnable(GL_DEPTH_TEST);
for(int tr=0;tr<2;tr++)
	for(int i=0;i<neuron.size();i++)
//	for(int i=neuron.size()-1;i>=0;i--)
if((bool)tr==transparency[i])
	for(int j=0;j<neuron[i].size();j++)
		if(neuron[i][j].color.w)
		{
	//		if(i!=1 || j==cur_psd)
		if(!transparency[i])
	{
		sp.SetVar("params",vec4(0,0,0,0));
		
		glColor4fv(&neuron[i][j].color.x);
		if(smoothing)
			neuron[i][j].Draw2();
		else
			neuron[i][j].Draw();

		
		
		
	}
		
		else
	{
	
		sp.SetVar("params",vec4(1,1,0,0));
		glCullFace(GL_BACK);	
		glColor4fv(&neuron[i][j].color.x);
		if(smoothing)			neuron[i][j].Draw2();		else			neuron[i][j].Draw();
		sp.SetVar("params",vec4(1,0,0,0));
		glCullFace(GL_FRONT);	
		if(smoothing)			neuron[i][j].Draw2();		else			neuron[i][j].Draw();
		

	}
		if(draw_boxes)
		{
			if(shaders_is) sp.UnUse();
			neuron[i][j].DrawBoxes();
			if(shaders_is) sp.Use();
		}

		}
	if(shaders_is) sp.UnUse();

	glColor4d(0,0,0,0.2f);
	DrawSphere(cam.GetCenter(),sect_radius);
glCullFace(GL_BACK);	
	DrawSphere(cam.GetCenter(),sect_radius);
glCullFace(GL_FRONT);

	for(int i=0;i<flat_section.size();i++)
		flat_section[i].Draw();

	/*
	if(mouse.btn&1)
	{
		glDisable(GL_LIGHTING);
		glDisable(GL_DEPTH_TEST);
		glViewport(0,0,width,height);
		Begin2D(width,height);
		glColor4f(1,1,1,0.6f);
		DrawRectangle(vec2(old_mouse_pos.x,old_mouse_pos.y),vec2(mouse.oldx,mouse.oldy));
		End2D();
		glEnable(GL_DEPTH_TEST);
	}*/

}
    Application() : BaseApplication (900, 300, "Phong + Ward BRDF + Ward BRDF with map : Press space to play/stop. Press 1 or 2 to change object"), program("vertex.vert", "fragmetPhong.frag"),
        programBRDF("vertex.vert", "fragmetWardBRDF.frag"), programBRDFMap("vertex.vert", "fragmetWardBRDFMap.frag"), make_torus(0.8, 0.5, 40, 40), make_sphere(1.0, 40.0, 40.0), lightPosition(2.3, 1.3, -7.0, 1.0), rotation(true)
    {
        using namespace oglplus;
        
        program.Use();
        
        int width = 0;
        int height = 0;
        GetWidnowSize(width, height);
        
        oglplus::Mat4f projection_matrix = oglplus::CamMatrixf::PerspectiveX(
                                                     Degrees(60),
                                                     double(width / 3)/height,
                                                     1, 10.0
                                                     );
        oglplus::Mat4f camera_matrix = oglplus::CamMatrixf::LookingAt (oglplus::Vec3f(0.0, 0.0, -2.5), oglplus::Vec3f());
        
        CreateSphere();
        CreateTorus();
        
        pGLContext->ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        pGLContext->ClearDepth(1.0f);
        pGLContext->Enable(Capability::DepthTest);

        oglplus::Vec3f ambient(0.329412,	0.223529,	0.027451);
        oglplus::Vec3f diffusion(0.780392,	0.568627,	0.113725);
        oglplus::Vec3f specular(0.992157,	0.941176,	0.807843);
        
        oglplus::Mat4f cameraMatrix = camera_matrix;
        oglplus::Mat4f sphereModelMatrix;
        
        // Create shapes.
        
        // Phong shading
        {
            RenderObject sphereObject(sphere, program, shpereIndexes);
            sphereObject.Init(PrimitiveType::TriangleStrip, nShpereVertexCount);
            
            SetupCommonParams(sphereObject, ambient,diffusion,specular, cameraMatrix, sphereModelMatrix, projection_matrix, lightPosition);
            sphereObject.AddFloatParam("shininess", 25.0);
            
            spheres.push_back(sphereObject);
        }
        
        // Ward's BRDF Model: https://en.wikibooks.org/wiki/GLSL_Programming/Unity/Brushed_Metal
        {
            RenderObject sphereObject(sphere, programBRDF, shpereIndexes);
            sphereObject.Init(PrimitiveType::TriangleStrip, nShpereVertexCount);
            oglplus::Mat4f modelMatrix;
            
            SetupCommonParams(sphereObject, ambient,diffusion,specular, cameraMatrix, sphereModelMatrix, projection_matrix, lightPosition);
            
            spheres.push_back(sphereObject);
        }
        
        // Ward's SBRDF Model + map
        {
            RenderObject sphereObject(sphere, programBRDFMap, shpereIndexes);
            sphereObject.Init(PrimitiveType::TriangleStrip, nShpereVertexCount);
            oglplus::Mat4f modelMatrix;
            
            SetupCommonParams(sphereObject, ambient,diffusion,specular, cameraMatrix, sphereModelMatrix, projection_matrix, lightPosition);
            
            spheres.push_back(sphereObject);
        }
        
        // Create torus
        oglplus::Mat4f torusModelMatrix = oglplus::ModelMatrixf::RotationX(oglplus::Degrees(90));
        
        // Phong shading
        {
            RenderObject torusObject(torus, program, torusIndexes);
            torusObject.Init(PrimitiveType::TriangleStrip, nTorusVertexCount);
            
            SetupCommonParams(torusObject, ambient, diffusion, specular, cameraMatrix, torusModelMatrix, projection_matrix, lightPosition);
            torusObject.AddFloatParam("shininess", 25.0);
            
            toruses.push_back(torusObject);
        }
        
        // Ward's BRDF Model: https://en.wikibooks.org/wiki/GLSL_Programming/Unity/Brushed_Metal
        {
            RenderObject torusObject(torus, programBRDF, torusIndexes);
            torusObject.Init(PrimitiveType::TriangleStrip, nTorusVertexCount);
            
            SetupCommonParams(torusObject, ambient, diffusion, specular, cameraMatrix, torusModelMatrix, projection_matrix, lightPosition);
            
            toruses.push_back(torusObject);
        }
        
        // Ward's SBRDF Model + map
        {
            RenderObject torusObject(torus, programBRDFMap, torusIndexes);
            torusObject.Init(PrimitiveType::TriangleStrip, nTorusVertexCount);
            
            SetupCommonParams(torusObject, ambient, diffusion, specular, cameraMatrix, torusModelMatrix, projection_matrix, lightPosition);
            
            toruses.push_back(torusObject);
        }
        
        //Render spheres by default.
        activeObjects = &spheres;
        
        LoadBRDFMap(Resource::getResoucesDir() + "LoadBRDFMap.png");
        
        programBRDFMap.Use();
        UniformSampler(programBRDFMap.GetProgram(), "MapTexture").Set(0);
        
        prevTime = GetMsFromEpoch();
    }