Пример #1
0
bool ChOpenGLCloud::Initialize(const std::vector<glm::vec3>& data, ChOpenGLMaterial mat, ChOpenGLShader* _shader) {
    if (GLReturnedError("Background::Initialize - on entry"))
        return false;

    if (!super::Initialize()) {
        return false;
    }

    this->vertices = data;
    this->vertex_indices.resize(data.size());
    for (int i = 0; i < data.size(); i++) {
        this->vertex_indices[i] = i;
    }

    if (!this->PostGLInitialize((GLuint*)(&this->vertices[0]), this->vertices.size() * sizeof(vec3))) {
        return false;
    }

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vec3), 0);  // Position

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    if (GLReturnedError("Cloud::Initialize - on exit"))
        return false;

    this->AttachShader(_shader);
    color_handle = this->shader->GetUniformLocation("color");
    point_size_handle = this->shader->GetUniformLocation("point_size");
    color = glm::vec4(mat.diffuse_color, 1);
    return true;
}
Пример #2
0
bool ChOpenGLMesh::Initialize(std::vector<glm::vec3>& vertices,
                              std::vector<glm::vec3>& normals,
                              std::vector<glm::vec2>& texcoords,
                              std::vector<GLuint>& indices,
                              ChOpenGLMaterial mat) {
    if (GLReturnedError("Mesh::Initialize - on entry")) {
        return false;
    }

    if (!super::Initialize()) {
        return false;
    }

    this->vertex_indices = indices;
    ambient = mat.ambient_color;
    diffuse = mat.diffuse_color;
    specular = mat.specular_color;

    for (unsigned int i = 0; i < vertices.size(); i++) {
        this->data.push_back(ChOpenGLVertexAttributesPN(vertices[i], normals[i]));
    }

    PostInitialize();

    if (GLReturnedError("ChOpenGLMesh::Initialize - on exit")) {
        return false;
    }

    return true;
}
Пример #3
0
bool ChOpenGLMesh::PostInitialize() {
    if (GLReturnedError("ChOpenGLMesh::PostInitialize - on entry"))
        return false;
    // Generation complete bind everything!
    if (!this->PostGLInitialize((GLuint*)(&this->data[0]), this->data.size() * sizeof(ChOpenGLVertexAttributesPN))) {
        return false;
    }

    // mat.ambient_color, mat.diffuse_color, mat.specular_color,

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);
    glEnableVertexAttribArray(3);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(ChOpenGLVertexAttributesPN),
                          (GLvoid*)(sizeof(vec3) * 0));  // Position
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(ChOpenGLVertexAttributesPN),
                          (GLvoid*)(sizeof(vec3) * 1));  // Normal

    glGenBuffers(1, &vertex_ambient_handle);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_ambient_handle);
    // glBufferData(GL_ARRAY_BUFFER, sizeof(vec3), &this->ambient,
    // GL_STATIC_DRAW);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glVertexAttribDivisor(2, 1);

    glGenBuffers(1, &vertex_diffuse_handle);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_diffuse_handle);
    // glBufferData(GL_ARRAY_BUFFER, sizeof(vec3), &this->diffuse,
    // GL_STATIC_DRAW);
    glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glVertexAttribDivisor(3, 1);

    int model_loc = 4;
    glGenBuffers(1, &vertex_model_handle);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_model_handle);
    for (int i = 0; i < 4; i++) {
        // Set up the vertex attribute
        glVertexAttribPointer(model_loc + i,  // Location
                              4, GL_FLOAT,
                              GL_FALSE,                    // vec4
                              sizeof(mat4),                // Stride
                              (void*)(sizeof(vec4) * i));  // Start offset
        // Enable it
        glEnableVertexAttribArray(model_loc + i);
        // Make it instanced
        glVertexAttribDivisor(model_loc + i, 1);
    }

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    if (GLReturnedError("ChOpenGLMesh::PostInitialize - on exit"))
        return false;

    return true;
}
Пример #4
0
bool Object::PostGLInitialize()
{

	glGenVertexArrays(1, &this->vertex_array_handle);
	glBindVertexArray(this->vertex_array_handle);

	glGenBuffers(1, &this->vertex_data_handle);
	glBindBuffer(GL_ARRAY_BUFFER, this->vertex_data_handle);
	//glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(VertexData), &this->vertices[0], GL_DYNAMIC_DRAW);
	glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(VertexAttributesPCNT), &this->vertices[0], GL_STATIC_DRAW);

	glGenBuffers(1, &this->vertex_indices_handle);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->vertex_indices_handle);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->vertex_indices.size() * sizeof(GLuint), &this->vertex_indices[0], GL_STATIC_DRAW);

	glGenBuffers(1, & this->vertex_indices_wire_frame_handle);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->vertex_indices_wire_frame_handle);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->vertex_indices_wire_frame.size() * sizeof(GLuint), &this->vertex_indices_wire_frame[0], GL_STATIC_DRAW);

	/* TODO: Currently normal handles are explicitly initialized in each object, bring them back here */
	/*
	glGenVertexArrays(1, &this->normal_array_handle);
	glBindVertexArray(this->normal_array_handle);

	glGenBuffers(1, &this->normal_data_handle);
	glBindBuffer(GL_ARRAY_BUFFER, this->normal_data_handle);
	glBufferData(GL_ARRAY_BUFFER, this->normal_vertices.size() * sizeof(VertexAttributesP), &this->normal_vertices[0], GL_DYNAMIC_DRAW);

	glGenBuffers(1, &this->normal_indices_handle);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->normal_indices_handle);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->normal_indices.size() * sizeof(GLuint), &this->normal_indices[0], GL_DYNAMIC_DRAW);
	*/

	return !GLReturnedError("test_object::PostGLInitialize - on exit");
}
Пример #5
0
void ChOpenGLMesh::Draw(const mat4& projection, const mat4& view) {
    if (GLReturnedError("ChOpenGLMesh::Draw - on entry"))
        return;

    // Enable the shader
    shader->Use();
    GLReturnedError("ChOpenGLMesh::Draw - after use");
    // Send our common uniforms to the shader
    shader->CommonSetup(value_ptr(projection), value_ptr(view));

    GLReturnedError("ChOpenGLMesh::Draw - after common setup");
    // Bind and draw! (in this case we draw as triangles)
    glBindVertexArray(this->vertex_array_handle);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_data_handle);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_ambient_handle);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_diffuse_handle);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_model_handle);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->vertex_element_handle);

    GLReturnedError("ChOpenGLMesh::Draw - before draw");
    glDrawElementsInstanced(GL_TRIANGLES, (GLsizei)this->vertex_indices.size(), GL_UNSIGNED_INT, (void*)0, size);
    GLReturnedError("ChOpenGLMesh::Draw - after draw");
    // unbind everything and cleanup
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glUseProgram(0);

    if (GLReturnedError("ChOpenGLMesh::Draw - on exit"))
        return;
}
Пример #6
0
void ChOpenGLCloud::Draw(const mat4& projection, const mat4& view) {
    if (GLReturnedError("ChOpenGLCloud::Draw - on entry"))
        return;
    if (this->vertices.size() == 0)
        return;
    glEnable(GL_DEPTH_TEST);
    // compute the mvp matrix and normal matricies
    // mat4 mvp = projection * modelview;
    // mat3 nm = inverse(transpose(mat3(modelview)));

    // Enable the shader
    shader->Use();
    GLReturnedError("ChOpenGLCloud::Draw - after use");
    // Send our common uniforms to the shader
    shader->CommonSetup(value_ptr(projection), value_ptr(view));
    glUniform4fv(color_handle, 1, glm::value_ptr(color));
    glUniform1fv(point_size_handle, 1, &point_size);
    GLReturnedError("ChOpenGLCloud::Draw - after common setup");
    // Bind and draw! (in this case we draw as triangles)
    glBindVertexArray(this->vertex_array_handle);

    glBindBuffer(GL_ARRAY_BUFFER, vertex_data_handle);
    glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(vec3), &this->vertices[0], GL_DYNAMIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertex_element_handle);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertex_indices.size() * sizeof(GLuint), &vertex_indices[0], GL_DYNAMIC_DRAW);

    glDrawElements(GL_POINTS, (GLsizei)this->vertex_indices.size(), GL_UNSIGNED_INT, (void*)0);

    GLReturnedError("ChOpenGLCloud::Draw - after draw");
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glUseProgram(0);

    if (GLReturnedError("ChOpenGLCloud::Draw - on exit"))
        return;
}
Пример #7
0
bool World::init(int sphere_count)
{
	totalSpheres = sphere_count;
	if (GLReturnedError("World init - on entry")) return true;
	b2Vec2 gravity;
	gravity.Set(0.0f, 0.0f);
	bool doSleep = true;
	this->world = new b2World(gravity);
	world->SetAllowSleeping(doSleep);
	world->SetContactListener(&contactListener);
	//world->SetContinuousPhysics(true); //For testing only
	birdsEye.proj = glm::perspective(birdsEye.fov, window.aspect, 1.0f, 50000.0f); 
	birdsEye.modelview = glm::lookAt(glm::vec3(0.0f, 1000.0f, 0.0f), glm::vec3(-000.01f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
	
	for (int i = 0; i < sphere_count; i++)
	{
#ifndef BOX2D_DEBUG
		Sphere * new_s = new Sphere();

		new_s->initialize(SPHERE_RADIUS, 10, 10);

		glm::vec3 spherePosForCube = glm::vec3(-WALL_LENGTH + rand() % (int) (2 * WALL_LENGTH), 0.0f, -WALL_LENGTH + rand() % (int) (2 * WALL_LENGTH));

		new_s->setPos(spherePosForCube);
		new_s->initPhysics(world); //Since this uses the inital position, must call after setPos
		
		cube.init(50.0f,50.0f,50.0f);
		cube.setPos(glm::vec3(spherePosForCube.x, 70.0f, spherePosForCube.z));		
		
		this->spheres.push_back(new_s);
#endif
	}

	skydome.initialize(5000, 50, 50);
	
	Shader * skyShader = new Shader();
	skyShader->init(TEX_NO_LIGHTING);
	skydome.setShader(skyShader);

	//DEBUG
	/*
	for (int j = -50; j < 50; j++)
	{
		Sphere new_s;
		new_s.initialize(50, 10, 10);
		new_s.setPos(glm::vec3(110.0f * j, 0.0f, 110.0f * j));
		new_s.initPhysics(world); //Since this uses the inital position, must call after setPos
		this->spheres.push_back(new_s);
//		this->spheres.push_back(new DrawObject()); //TODO: But for serious need to get a good distribution
	}*/
	this->currCam = &birdsEye;

	player.init(SPHERE_RADIUS, 10, 10, 0.0f, 0.0f);
	player.initPhysics(world);
	player.update();

	l.amb = glm::vec3(0.5, 0.5, 0.5);
	l.diff = glm::vec3(0.5, 0.5, 0.5);
	l.spec = glm::vec3(0.5, 0.5, 0.5);
	l.position = glm::vec4(200.0, 500.0, 000.0, 1.0);
	m.kA = glm::vec3(0.9f, 0.9f, 0.9f);
	m.kD = glm::vec3(0.5f, 0.5f, 0.5f);
	m.kS = glm::vec3(0.6f, 0.6f, 0.6f);
	m.shininess = 2.5f;
	
	cursor.init(glm::vec3(0.0f, 0.0f, 0.0f));

	stadium.init();
	stadium.initPhysics(world);

#ifdef BOX2D_DEBUG

	circleDef.type = b2_dynamicBody;
	circleDef.position.Set(50.0f, 50.0f);
	circleDef.bullet = true;
	circleBody = world->CreateBody(&circleDef);
	circleShape.m_p.Set(50.0f, 50.0f);
	circleShape.m_radius = 5.0f;
	circleFixtureDef.shape = &circleShape;
	circleFixtureDef.density = 1.0f;
	circleFixtureDef.friction = 0.1001f;

	circleDef2.type = b2_dynamicBody;
	circleDef2.position.Set(150.0f, 150.0f);
	circleDef2.bullet = true;
	circleBody2 = world->CreateBody(&circleDef);
	circleShape2.m_p.Set(50.0f, 50.0f);
	circleShape2.m_radius = 5.0f;
	circleFixtureDef2.shape = &circleShape;
	circleFixtureDef2.density = 1.0f;
	circleFixtureDef2.friction = 0.1001f;
	
	circleBody->CreateFixture(&circleFixtureDef);
	circleBody2->CreateFixture(&circleFixtureDef2);

#endif

	sky_index = 0;
	initSkyboxes();
	switchSkydome();
	
	dummy_gooch_shader.init(NONE);

	//We used to init common shader here - that's now done in main

	if (GLReturnedError("World init - on exit")) return false;
	is_init = true;
	return true;
}
Пример #8
0
void World::draw(bool do_physics)
{
	if (GLReturnedError("World draw - on entry")) return;
	lightInfo * new_l = &l;
	materialInfo * new_m = &m;
	Shader * common_shader_back; //Used for Gooch
	if (currCam == &birdsEye)
	{
		birdsEye.proj = glm::perspective(birdsEye.fov, window.aspect, 1.0f, 50000.0f); 
	}
	if (render_target == RENDER_FULL) 
	{
		world->Step(0.016667f, 10, 10); //TODO: Match this w/ draw rate
	}
	//Precompute some matrices for shadow maps on first pass
	if (useShadows && (render_target == RENDER_SFBO))
	{
		light_matrix = glm::lookAt(glm::vec3(l.position), glm::vec3(0.00001, 0.0, 0.0), glm::vec3(0.0, 1.0, 0.0));
		bp_matrix = bias_matrix * currCam->proj;
	}
	if (render_target == RENDER_GOOCH)
	{
		common_shader_back = common_shader;
		common_shader = &dummy_gooch_shader;
	}

	for (sphereIt = spheres.begin(); sphereIt < spheres.end(); sphereIt++)
	{
		if (render_target == RENDER_FULL)
		{
			(*sphereIt)->updatePos(); //Update sphere's location with physics-based one
		}
		(*sphereIt)->draw(currCam->proj, currCam->modelview, glm::ivec2(1.0, 1.0), elapsed_time, new_l, new_m);
	}
	if (render_target == RENDER_FULL)
	{
		player.update();
	}
	
	cursor.draw(currCam->proj, currCam->modelview, glm::ivec2(1.0, 1.0), elapsed_time, new_l, new_m);

	
	stadium.draw(currCam->proj, currCam->modelview, glm::ivec2(1.0, 1.0), elapsed_time, new_l, new_m);


	skydome.draw(currCam->proj, currCam->modelview, glm::ivec2(1.0, 1.0), elapsed_time, new_l, new_m);

	player.draw(currCam->proj, currCam->modelview, glm::ivec2(1.0, 1.0), elapsed_time, new_l, new_m);
#ifdef BOX2D_DEBUG
		printf("x: %f, y: %f, q: %f\n", circleBody->GetPosition().x, circleBody->GetPosition().y, circleBody->GetAngle());
		printf("x: %f, y: %f, q: %f\n\n", circleBody2->GetPosition().x, circleBody2->GetPosition().y, circleBody->GetAngle());
#endif
	debug_counter++;
	if (debug_counter % 10000 == 0)
	{
		printf("Stop! 10000 passed.\n");
		world->Dump();
	}
	if (render_target == RENDER_GOOCH)
	{
		common_shader = common_shader_back;
	}
	if (GLReturnedError("World draw - on exit")) return;
}
Пример #9
0
bool ChOpenGLMesh::Initialize(chrono::ChTriangleMeshShape* tri_mesh, ChOpenGLMaterial mat) {
    if (GLReturnedError("Mesh::Initialize - on entry")) {
        return false;
    }

    if (!super::Initialize()) {
        return false;
    }
    int num_triangles = tri_mesh->GetMesh().getNumTriangles();

    for (unsigned int i = 0; i < (unsigned)num_triangles; i++) {
        chrono::geometry::ChTriangle tri = tri_mesh->GetMesh().getTriangle(i);
        ChVector<> norm = tri.GetNormal();
        ChVector<> v1 = tri.p1;
        ChVector<> v2 = tri.p2;
        ChVector<> v3 = tri.p3;
        this->vertex_indices.push_back(i * 3 + 0);
        this->vertex_indices.push_back(i * 3 + 1);
        this->vertex_indices.push_back(i * 3 + 2);
        glm::vec3 v, n;
        v = glm::vec3(v1.x(), v1.y(), v1.z());
        n = glm::vec3(norm.x(), norm.y(), norm.z());
        this->data.push_back(ChOpenGLVertexAttributesPN(v, n));
        v = glm::vec3(v2.x(), v2.y(), v2.z());
        this->data.push_back(ChOpenGLVertexAttributesPN(v, n));
        v = glm::vec3(v3.x(), v3.y(), v3.z());
        this->data.push_back(ChOpenGLVertexAttributesPN(v, n));
    }
    ambient = mat.ambient_color;
    diffuse = mat.diffuse_color;
    specular = mat.specular_color;
    //
    //   std::vector<ChVector<int> >& indices =
    //   tri_mesh->GetMesh().getIndicesVertexes();
    //   std::vector<ChVector<double> >& vertices =
    //   tri_mesh->GetMesh().getCoordsVertices();
    //   std::vector<ChVector<double> >& normals =
    //   tri_mesh->GetMesh().getCoordsNormals();
    //

    //   this->vertex_indices.resize(indices.size() * 3);
    //   for (unsigned int i = 0; i < indices.size(); i++) {
    //
    //      this->vertex_indices[i * 3 + 0] = indices[i].x;
    //      this->vertex_indices[i * 3 + 1] = indices[i].y;
    //      this->vertex_indices[i * 3 + 2] = indices[i].z;
    //   }
    //   for (unsigned int i = 0; i < vertices.size(); i++) {
    //      glm::vec3 n;
    //      glm::vec3 v(vertices[i].x, vertices[i].y, vertices[i].z);
    //      if (normals.size() > 0) {
    //         n = glm::vec3(normals[i].x, normals[i].y, normals[i].z);
    //      } else {
    //         n = glm::vec3(1, 0, 0);
    //      }
    //
    //      this->data.push_back(ChOpenGLVertexAttributesPN(v, n));
    //   }

    PostInitialize();

    if (GLReturnedError("ChOpenGLMesh::Initialize - on exit")) {
        return false;
    }

    return true;
}