示例#1
0
文件: window.cpp 项目: Fenror/physics
Window::Window()
{
  InitializeGLFW();
}
示例#2
0
int main() 
{
    try {

    // Camera settings
	Camera camera;
	glm::vec3 vrp(0.0f, 0.0f, 10.0f);
	glm::vec3 vpn(0.0f, 0.0f, 1.0f);
	glm::vec3 vup(0.0f, 1.0f, 0.0f);
	glm::vec3 prp(0.0f, 0.0f, 100.0f);
	float F = 10.0f;
	float B = -80.0f;
	glm::vec2 lower_left(-20.0f, -20.0f);
	glm::vec2 upper_right(20.0f, 20.0f);
	camera = Camera(vrp, vpn, vup, prp, lower_left, upper_right, F, B);
	
	// Examples
	int curves = 4;    // Amount of examples
	BezierRow G[curves];
	G[0] = BezierRow(glm::vec3(-15.0f, -15.0f, 0.0f),
					 glm::vec3(-10.0f, 25.0f, 0.0f),
					 glm::vec3(10.0f, 25.0f, 0.0f),
					 glm::vec3(15.0f, -15.0f, 0.0f));
	G[1] = BezierRow(glm::vec3(-20.0f, 0.0f, 0.0f),
					 glm::vec3(-1.0f, 55.0f, 0.0f),
					 glm::vec3(1.0f, -55.0f, 0.0f),
					 glm::vec3(20.0f, 0.0f, 0.0f));
	G[2] = BezierRow(glm::vec3(-1.0f, -5.0f, 0.0f),
					 glm::vec3(-60.0f, 5.0f, 0.0f),
					 glm::vec3(60.0f,  5.0f, 0.0f),
					 glm::vec3(1.0f,  -5.0f, 0.0f));
	G[3] = BezierRow(glm::vec3(-10.0f, -5.0f, 0.0f),
					 glm::vec3(60.0f,   5.0f, 0.0f),
					 glm::vec3(-60.0f,  5.0f, 0.0f),
					 glm::vec3(10.0f,  -5.0f, 0.0f));

	int currentfigure = 3; // Set figure between 4 different examples

	// Decide whether to use sampling or subdivision
	int decider = 1;

	int Npoint = 0;
	std::vector<glm::vec3> points;

	// Sampling
	if(decider == 0){
	float t = 0.0f;
	float step = 12.0f;
	sampling(G[currentfigure], t, step, points);
	Npoint = step*2;
	}

	// Subdivision
	else if(decider == 1){
	DLB /= 8.0f;	
	DRB /= 8.0f;
	int n = 3; 				// Amount of curves (smoothness)
 	int npow = pow(2, n+1); // Amount of points
	subdivide(G[currentfigure], n, points);
	Npoint = npow;
	}
	else{
		printf("No method chosen\n"); return 1;
	}




	glm::mat4x4 CTM = camera.CurrentTransformationMatrix();
	std::cout << "CTM = " << std::endl << CTM << std::endl;


	// Initialize the graphics
	InitializeGLFW();
	GLFWwindow* Window = CreateWindow(WindowWidth, WindowHeight, WindowTitle.c_str());
	InitializeGLEW();
	InitializeOpenGL();
	glfwSwapBuffers(Window);

	// Read and Compile the vertex program vertextransform.vert
	GLuint vertexprogID = CreateGpuProgram("vertextransform.vert", GL_VERTEX_SHADER);

        // Read and Compile the fragment program linefragment.frag
	GLuint linefragmentprogID = CreateGpuProgram("linefragment.frag", GL_FRAGMENT_SHADER);

	// Create a lineshader program and Link it with the vertex and linefragment programs
	GLuint lineshaderID = CreateShaderProgram(vertexprogID, linefragmentprogID);
	
	// Now comes the OpenGL core part

	// This is where the curve is initialized
        // User data is in the global variable curveVertices, and the number of entries
	// is in Ncurvevertices

    // Make a VertexArrayObject - it is used by the VertexArrayBuffer, and it must be declared!
	GLuint CurveVertexArrayID;
	glGenVertexArrays(1, &CurveVertexArrayID);
	glBindVertexArray(CurveVertexArrayID);

	// Make a curvevertexbufferObject - it uses the previous VertexArrayBuffer!
	GLuint curvevertexbuffer;
	glGenBuffers(1, &curvevertexbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, curvevertexbuffer);




	// Give our vertices to OpenGL.
	glBufferData(GL_ARRAY_BUFFER, Npoint * 3 * sizeof(float), &points[0], GL_STATIC_DRAW);
	

    // Validate the shader program
	ValidateShader(lineshaderID, "Validating the lineshader");

	// Get locations of Uniforms
	GLuint curvevertextransform   = glGetUniformLocation(lineshaderID, "CTM");
	GLuint curvefragmentcolor     = glGetUniformLocation(lineshaderID, "Color");	




	// Initialize Attributes
	GLuint curvevertexattribute = glGetAttribLocation(lineshaderID, "VertexPosition");
	glVertexAttribPointer(curvevertexattribute, 3, GL_FLOAT, GL_FALSE, 0, 0);

	// The main loop
	while (!glfwWindowShouldClose(Window)) {
	    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	    glUseProgram(lineshaderID);
		glm::mat4x4 CTM = camera.CurrentTransformationMatrix();
		glUniformMatrix4fv(curvevertextransform, 1, GL_FALSE, &CTM[0][0]);
		glUniform3f(curvefragmentcolor, 0.2f, 0.2f, 0.2f);

		glEnableVertexAttribArray(curvevertexattribute);
		glBindVertexArray(CurveVertexArrayID);  // This is very important! There are two "binds"!
		glDrawArrays(GL_LINES, 0, Npoint);

		glDisableVertexAttribArray(curvevertexattribute);
	    glUseProgram(0);

	    glfwSwapBuffers(Window);
	    std::stringstream errormessage;
	    errormessage << "End of loop: " << "assignment5.cpp" << ": " << __LINE__ << ": ";
	    ErrorCheck(errormessage.str());

	    glfwPollEvents();
	}
    }
    catch (std::exception const& exception) {
	std::cerr << "Exception: " << exception.what() << std::endl;
    }

    glfwTerminate();

    return 0;
}