Exemple #1
0
/**
 * Sets the mesh's offsets and copies its associated vertices into
 * the mesh buffer (VBO).
 */
void PatchCache::setupMesh(Patch* newMesh, TextureVertex* vertices) {
    // This call ensures the VBO exists and that it is bound
    init(Caches::getInstance());

    // If we're running out of space, let's clear the entire cache
    uint32_t size = newMesh->getSize();
    if (mSize + size > mMaxSize) {
        clearCache();
        createVertexBuffer();
    }

    // Find a block where we can fit the mesh
    BufferBlock* previous = NULL;
    BufferBlock* block = mFreeBlocks;
    while (block) {
        // The mesh fits
        if (block->size >= size) {
            break;
        }
        previous = block;
        block = block->next;
    }

    // We have enough space left in the buffer, but it's
    // too fragmented, let's clear the cache
    if (!block) {
        clearCache();
        createVertexBuffer();
        previous = NULL;
        block = mFreeBlocks;
    }

    // Copy the 9patch mesh in the VBO
    newMesh->offset = (GLintptr) (block->offset);
    newMesh->textureOffset = newMesh->offset + gMeshTextureOffset;
    glBufferSubData(GL_ARRAY_BUFFER, newMesh->offset, size, vertices);

    // Remove the block since we've used it entirely
    if (block->size == size) {
        if (previous) {
            previous->next = block->next;
        } else {
            mFreeBlocks = block->next;
        }
        delete block;
    } else {
        // Resize the block now that it's occupied
        block->offset += size;
        block->size -= size;
    }

    mSize += size;
}
Exemple #2
0
int main(int argc, char* argv)
{
	glutInit(&argc, &argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowSize(1024, 768);
	glutInitWindowPosition(200, 200);
	glutCreateWindow("Perspective Projection");

	GLint res = glewInit();
	if (res != GLEW_OK)
	{
		fprintf(stderr, "Error: glewInit\n");
		exit(1);
	}


	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	InitializeCallback();

	createVertexBuffer();
	createIndexBuffer();

	compileShader();

	glutMainLoop();

	return 0;
}
//Init function, used to initialise the component
void CGeometryComponent::init()
{
	//Call create vertex buffer
	createVertexBuffer();
	//Call create index buffer
	createIndexBuffer();
}
geometry* createSphere(int divisions)
{
	geometry* geom = new geometry();
	glm::vec3 v[4] = 
	{
		glm::vec3(0.0f, 0.0f, 1.0f),
		glm::vec3(0.0f, 0.942806, -0.333333),
		glm::vec3(-0.816497, -0.471405, -0.333333),
		glm::vec3(0.816497, -0.471405, -0.333333)
	};

	glm::vec2 t[4] = 
	{
		glm::vec2(v[0].x, v[0].y),
		glm::vec2(v[1].x, v[1].y),
		glm::vec2(v[2].x, v[2].y),
		glm::vec2(v[3].x, v[3].y)
	};
	
	divideTriangle(geom, v[0], v[1], v[2], divisions);
	divideTriangle(geom, v[3], v[2], v[1], divisions);
	divideTriangle(geom, v[0], v[3], v[1], divisions);
	divideTriangle(geom, v[0], v[2], v[3], divisions);

	createVertexBuffer(geom);
	createNormalBuffer(geom);
	createTexBuffer(geom);
	glBindVertexArray(0);
	return geom;
}
//create Torus
geometry* createTorus(float radius, int stacks, int slices)
{
    geometry* geom = new geometry();
    float deltaStack = 2.0f * glm::pi<float>() / stacks;
    float deltaSlice = 2.0f * glm::pi<float>() / slices;
    for (int i = 0; i < stacks; ++i)
    {
        float a0 = i * deltaStack;
        float a1 = a0 + deltaStack;
        glm::vec3 vertices[4];
        for (int j = 0; j <= slices; ++j)
        {
            float c = cos(j * deltaSlice);
            float r = c + radius;
            vertices[0] = glm::vec3(sin(a0) * r, sin(j * deltaSlice), cos(a0) * r);
            vertices[1] = glm::vec3(sin(a1) * r, sin(j * deltaSlice), cos(a1) * r);
            c = cos((j + 1) * deltaSlice);
            r = c + radius;
            vertices[2] = glm::vec3(sin(a0) * r, sin((j + 1) * deltaSlice), cos(a0) * r);
            vertices[3] = glm::vec3(sin(a1) * r, sin((j + 1) * deltaSlice), cos(a1) * r);
            // Triangle 1
            geom->vertices.push_back(vertices[0]);
            geom->vertices.push_back(vertices[1]);
            geom->vertices.push_back(vertices[2]);
            // Triangle 2
            geom->vertices.push_back(vertices[1]);
            geom->vertices.push_back(vertices[3]);
            geom->vertices.push_back(vertices[2]);
        }
    }

    createVertexBuffer(geom);

    return geom;
}
Exemple #6
0
void op3d::Engine::initVulkan()
{
    instance.create();
    callback.setup(instance);
    surface.create(instance, window);
    physicalDevice.create(instance, surface);
    device.create(physicalDevice, surface, graphicsQueue, presentQueue);
    swapChain.create(device, surface, physicalDevice, window);
    swapChain.createImageViews(device, swapChainImageViews);
    createRenderPass();
    createDescriptorSetLayout();
    createGraphicsPipeline();
    commandBufferManager.createCommandPool(physicalDevice, surface);
    createDepthResources();
    createFramebuffers();
    createTextureImage();
    createTextureImageView();
    createTextureSampler();
    createVertexBuffer();
    createIndexBuffer();
    createUniformBuffer();
    descriptorPool.createPool();
    descriptorSet.createSet(uniformBuffer, textureImageView, textureSampler, descriptorSetLayout, descriptorPool, device);
    createCommandBuffers();
    createSemaphores();
}
	void BasicMaterial::createVertexBuffer(ID3D11Device * device, const Mesh & mesh, ID3D11Buffer ** vertexBuffer)const
	{
		const std::vector<XMFLOAT3>& sourceVerts = mesh.vertices();
		std::vector<BasicMaterialVertex> vertices;
		vertices.reserve(sourceVerts.size());

		if (mesh.vertexColors().size() > 0)
		{
			std::vector<XMFLOAT4> * vertexColors = mesh.vertexColors().at(0);
			assert(vertexColors->size() == sourceVerts.size());
			for (UINT i = 0; i < sourceVerts.size(); i++)
			{
				XMFLOAT3 position = sourceVerts.at(i);
				XMFLOAT4 color = vertexColors->at(i);

				vertices.push_back(BasicMaterialVertex(XMFLOAT4(position.x, position.y, position.z, 1.0f), color));
			}
		}
		else
		{

			XMFLOAT4 color = XMFLOAT4(reinterpret_cast<float*>(&Colors::WHITE));

			for (UINT i = 0; i < sourceVerts.size(); i++)
			{
				XMFLOAT3 position = sourceVerts.at(i);

				vertices.push_back(BasicMaterialVertex(XMFLOAT4(position.x, position.y, position.z, 1.0f), color));
			}
		}

		createVertexBuffer(device, &vertices[0],vertices.size(), vertexBuffer);

	}
//create Plane
geometry* createPlane(int width, int depth)
{
    geometry* geom = new geometry();
    glm::vec3 vertices[4];
    for (int x = 0; x < width; ++x)
    {
        for (int z = 0; z < depth; ++z)
        {
            vertices[0] = glm::vec3(-((float)width / 2.0f) + x, 0.0f, -((float)depth / 2.0f) + z);
            vertices[1] = glm::vec3(-((float)width / 2.0f) + (x + 1), 0.0f, -((float)depth / 2.0f) + z);
            vertices[2] = glm::vec3(-((float)width / 2.0f) + x, 0.0f, -((float)depth / 2.0f) + (z + 1));
            vertices[3] = glm::vec3(-((float)width / 2.0f) + (x+1), 0.0f, -((float)depth / 2.0f) + (z + 1));
            // Triangle 1
            geom->vertices.push_back(vertices[0]);
            geom->vertices.push_back(vertices[3]);
            geom->vertices.push_back(vertices[2]);
            // Triangle 2
            geom->vertices.push_back(vertices[0]);
            geom->vertices.push_back(vertices[1]);
            geom->vertices.push_back(vertices[3]);
        }
    }

    createVertexBuffer(geom);

    return geom;
}
geometry* createBox()
{
    geometry* geom = new geometry();
    for(int i = 0; i < 8; ++i)
    {
        geom->vertices.push_back(box_vertices[i]);
        geom->normals.push_back(box_normals[i]);
    }
    for (int i = 0; i < 36; ++i)
        geom->indices.push_back(box_indices[i]);

    createVertexBuffer(geom);

    glGenBuffers(1, &geom->normalBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, geom->normalBuffer);
    glBufferData(GL_ARRAY_BUFFER, geom->normals.size() * sizeof(glm::vec3), &geom->normals[0], GL_STATIC_DRAW);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(1);

    glBindVertexArray(0);

    createIndexBuffer(geom);

    return geom;
}
//Init function, used to initialise the component
void CGeometry::createBuffers()
{
	//Call create vertex buffer
	createVertexBuffer();
	//Call create index buffer
	createIndexBuffer();
}
Exemple #11
0
int main( int argc , char **argv )
{
	glutInit( &argc , argv );

	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );

	glutInitWindowSize( WINDOW_WIDTH , WINDOW_HEIGHT );
	glutInitWindowPosition( 100 , 100 );
	
	glutCreateWindow( "Perspective Projection" );

	glutDisplayFunc( render );
	glutIdleFunc( render );

	if( glewInit() != GLEW_OK )
		return 1;

	glClearColor( 0.0f , 0.0f , 0.0f , 0.0f );
	
	createVertexBuffer();
	createIndexBuffer();

	compileShader();

	glutMainLoop();

	return 0;
}
void SceneRenderer::createVAO(GeoModel3D model)
{
	GLuint vao_new_id;

	std::vector<GLModel3DData> modelData = model.retrieveMeshes();

	for (std::vector<GLModel3DData>::iterator mesh = modelData.begin(); mesh != modelData.end(); mesh++)
	{ 
		glGenVertexArrays(1, &vao_new_id); // Create our Vertex Array Object
		glBindVertexArray(vao_new_id); // Bind our Vertex Array Object so we can use it  

		glGenBuffers(1, &IBO);
		glGenBuffers(3, vbos);

		GLuint mesh_id = mesh->getMeshID();
		tinyobj::mesh_t mesh_data = mesh->getMeshData();
		std::pair<GLuint, GLuint> mapping(mesh_id, vao_new_id);
		object_vao_map.insert(mapping);
		createVertexBuffer(mesh_data.positions);
		/*createNormalsBuffer(mesh_data.normals);*/
		createTexCoordsBuffer(mesh_data.texcoords);
		createIndexBuffer(mesh_data.indices);

		glEnableVertexAttribArray(0); // Disable our Vertex Array Object
		glBindVertexArray(0); // Disable our Vertex Buffer Object
	}
}
Exemple #13
0
	VertexBuffer::VertexBuffer() :
		mVertexCount(6),
		mCurrentTexture(0, 0, 1, 1),
		mCurrentColour(0xFFFFFFFF),
		mRenderMode(RenderModeNormal)
	{
		createVertexBuffer();
	}
Exemple #14
0
void MapEditor::loadStart()
{
	lua_getglobal(L, "mapEditUI");

	error = lua_pcall(L, 0, LUA_MULTRET, 0);

	getUIFromLua(false);
	createVertexBuffer();
}
geometry* createSierpinski(int divisions)
{
	glm::vec3 vertices[4];
	std::memcpy(vertices, tetrahedron_vertices, sizeof(glm::vec3) * 4);
	geometry* geom = new geometry();
	divide_sierpinski(geom, vertices, divisions);
	createVertexBuffer(geom);
	createIndexBuffer(geom);
	return geom;
}
//create cylinder
geometry* createCylinder(int stacks, int slices)
{
    geometry* geom = new geometry();
    //Create top
    glm::vec3 centre(0.0f, 1.0f, 0.0f);
    glm::vec3 vertex_prev(1.0f, 1.0, 0.0f);
    glm::vec3 vertex_current;
    float deltaAngle = (2 * glm::pi<float>()) / slices;
    for (int i = 1; i <= slices; ++i)
    {
        vertex_current = glm::vec3(cos(i * deltaAngle), 1.0f, sin(i * deltaAngle));
        geom->vertices.push_back(centre);
        geom->vertices.push_back(vertex_prev);
        geom->vertices.push_back(vertex_current);
        vertex_prev = vertex_current;
    }

    //create Bottom
    centre = glm::vec3(0.0f, -1.0f, 0.0f);
    vertex_prev = glm::vec3(1.0f, -1.0f, 0.0f);
    for (int i = 1; i <= slices; ++i)
    {
        vertex_current = glm::vec3(cos(i * deltaAngle), -1.0f, sin(i * deltaAngle));
        geom->vertices.push_back(centre);
        geom->vertices.push_back(vertex_prev);
        geom->vertices.push_back(vertex_current);
        vertex_prev = vertex_current;
    }

    //Create Stack
    glm::vec3 vertices[4];
    float deltaHeight = 2.0f / stacks;
    for (int i = 0; i < stacks; ++i)
    {
        for (int j = 0; j < slices; ++j)
        {
            vertices[0] = glm::vec3(cos(j * deltaAngle), 1.0f - (deltaHeight * i), sin(j * deltaAngle));
            vertices[1] = glm::vec3(cos((j + 1) * deltaAngle), 1.0f - (deltaHeight * i), sin((j + 1) * deltaAngle));
            vertices[2] = glm::vec3(cos(j * deltaAngle), 1.0f - (deltaHeight * (i + 1)), sin(j * deltaAngle));
            vertices[3] = glm::vec3(cos((j + 1) * deltaAngle), 1.0f - (deltaHeight *(i + 1)), sin((j + 1) * deltaAngle));
            //triangle 1
            geom->vertices.push_back(vertices[0]);
            geom->vertices.push_back(vertices[3]);
            geom->vertices.push_back(vertices[2]);
            //triangle 2
            geom->vertices.push_back(vertices[0]);
            geom->vertices.push_back(vertices[1]);
            geom->vertices.push_back(vertices[3]);
        }
    }

    createVertexBuffer(geom);

    return geom;
}
Exemple #17
0
HRESULT MeshGroup::createBuffers(ID3D11Device* device)
	{
		HRESULT hr = S_OK;
		hr = createIndexBuffer(device);

		if (FAILED(hr)) return hr;

	    hr = createVertexBuffer(device);

		return hr;
	}
geometry* createTetrahedron()
{
    geometry* geom = new geometry();
    for (int i = 0; i < 4; ++i)
        geom->vertices.push_back(tetrahedron_vertices[i]);
    for (int i = 0; i < 12; ++i)
        geom->indices.push_back(tetrahedron_indices[i]);

    createVertexBuffer(geom);
    createIndexBuffer(geom);

    return geom;
}
geometry* createPyramid()
{
    geometry* geom = new geometry();
    for (int i = 0; i < 5; ++i)
        geom->vertices.push_back(pyramid_vertices[i]);
    for (int i = 0; i < 18; ++i)
        geom->indices.push_back(pyramid_indices[i]);

    createVertexBuffer(geom);
    createIndexBuffer(geom);

    return geom;
}
geometry* createBox()
{
	geometry* geom = new geometry();
	for(int i = 0; i < 8; ++i)
		geom->vertices.push_back(box_vertices[i]);
	for (int i = 0; i < 36; ++i)
		geom->indices.push_back(box_indices[i]);

	createVertexBuffer(geom);
	createIndexBuffer(geom);
	
	return geom;
}
Exemple #21
0
MenuState::MenuState(Game* game)
    : GameState(game)
    , m_TextureFileName("Textures/menuScreen.png")
{
    m_Game->getShaderManager().useShader("Texture");

    createTexture();
    createVertexBuffer();
    setupVertexAttrib();

    passTextureToShader();
    passMatrixToShader();
}
void initialiseBuffers(geometry* geom)
{
	if (geom->vertices.size() > 0) //if we have vertices, then add to the vertex array object
		createVertexBuffer(geom);

	if (geom->normals.size() > 0)//if we have normals, then add to the vertex array object
		createNormalBuffer(geom);

	if (geom->texcoords.size() > 0)//if we have texcoords, then add to the vertex array object
		createTexBuffer(geom);

	glBindVertexArray(0);
}
Exemple #23
0
void PatchCache::init(Caches& caches) {
    bool created = false;
    if (!mMeshBuffer) {
        glGenBuffers(1, &mMeshBuffer);
        created = true;
    }

    caches.bindMeshBuffer(mMeshBuffer);
    caches.resetVertexPointers();

    if (created) {
        createVertexBuffer();
    }
}
Exemple #24
0
void Renderer::initialize() {
    context = new QOpenGLContext(this);

    QSurfaceFormat format;
    context->setFormat(format);

    context->create();

    context->makeCurrent(this);

    initializeOpenGLFunctions();

    createVertexBuffer();

    context->doneCurrent();
}
void RayTracerMaterial::createVertexBuffer(ID3D11Device * device, const Mesh& mesh, ID3D11Buffer** vertexBuffers)const
{
	const std::vector<XMFLOAT3>& sourceVertices = mesh.vertices();
	std::vector<XMFLOAT3>* textureCoordinates = mesh.textureCoords().at(0);
	assert(textureCoordinates->size() == sourceVertices.size());

	std::vector<VertexPositionTexture> vertices;
	vertices.reserve(sourceVertices.size());
	for (UINT i = 0; i < sourceVertices.size(); i++)
	{
		XMFLOAT3 position = sourceVertices.at(i);
		XMFLOAT3 uv = textureCoordinates->at(i);
		vertices.push_back(VertexPositionTexture(XMFLOAT4(position.x, position.y, position.z, 1.0f), XMFLOAT2(uv.x, uv.y)));
	}

	createVertexBuffer(device, &vertices[0], vertices.size(), vertexBuffers);
}
//Create sphere
geometry* createSphere(int stacks, int slices)
{
	geometry* geom = new geometry();
	//create required values
	float deltaRho = glm::pi<float>() / stacks;
	float deltaTheta = 2.0f * glm::pi<float>() / slices;
	for (int i = 0; i < stacks; ++i)
	{
		float rho = i * deltaRho;
		glm::vec3 vertices[4];
		for (int j = 0; j < slices; ++j)
		{
			// Vertex 0 
			float theta = j * deltaTheta; 
			vertices[0] = glm::vec3(-sin(theta) * sin(rho), cos(theta) * sin(rho), cos(rho));
			// Vertex 1
			vertices[1] = glm::vec3(-sin(theta) * sin(rho + deltaRho), cos(theta) * sin(rho + deltaRho), cos(rho + deltaRho));
			// Vertex 2
			theta = ((j + 1) == slices) ? 0.0f : (j + 1) * deltaTheta;
			vertices[2] = glm::vec3(-sin(theta) * sin(rho), cos(theta) * sin(rho), cos(rho));
			// Vertex 3
			vertices[3] = glm::vec3(-sin(theta) * sin(rho + deltaRho), cos(theta) * sin(rho + deltaRho), cos(rho + deltaRho));

			// Triangle 1
			geom->vertices.push_back(vertices[0]);
			geom->normals.push_back(glm::normalize(vertices[0]));
			geom->vertices.push_back(vertices[1]);
			geom->normals.push_back(glm::normalize(vertices[1]));
			geom->vertices.push_back(vertices[2]);
			geom->normals.push_back(glm::normalize(vertices[2]));
			// Triangle 2
			geom->vertices.push_back(vertices[1]);
			geom->normals.push_back(glm::normalize(vertices[1]));
			geom->vertices.push_back(vertices[3]);
			geom->normals.push_back(glm::normalize(vertices[3]));
			geom->vertices.push_back(vertices[2]);
			geom->normals.push_back(glm::normalize(vertices[2]));
		}
	}

	createVertexBuffer(geom);
	createNormalBuffer(geom);
	glBindVertexArray(0);
	return geom;
}
	void SkyboxMaterial::createVertexBuffer(ID3D11Device * device, const Mesh & mesh, ID3D11Buffer ** vertexBuffer)const
	{
		const std::vector<XMFLOAT3>& sourceVerts = mesh.vertices();
		std::vector<XMFLOAT4> vertices;
		vertices.reserve(sourceVerts.size());


		for (UINT i = 0; i < sourceVerts.size(); i++)
		{
			XMFLOAT3 position = sourceVerts.at(i);

			vertices.push_back(XMFLOAT4(position.x, position.y, position.z, 1.0f));
		}


		createVertexBuffer(device, &vertices[0], vertices.size(), vertexBuffer);

	}
geometry* createPyramid()
{
	geometry* geom = new geometry();
	for (int i = 0; i < 5; ++i)
	{
		geom->vertices.push_back(pyramid_vertices[i]);
		geom->normals.push_back(pyramid_normals[i]);
	}
	for (int i = 0; i < 18; ++i)
		geom->indices.push_back(pyramid_indices[i]);

	createVertexBuffer(geom);
	createNormalBuffer(geom);
	glBindVertexArray(0);
	createIndexBuffer(geom);

	return geom;
}
geometry* createBox()
{
	geometry* geom = new geometry();
	for(int i = 0; i < 8; ++i)
	{
		geom->vertices.push_back(box_vertices[i]);
		geom->normals.push_back(box_normals[i]);
	}
	for (int i = 0; i < 36; ++i)
		geom->indices.push_back(box_indices[i]);

	createVertexBuffer(geom);
	createNormalBuffer(geom);
	glBindVertexArray(0);
	createIndexBuffer(geom);
	
	return geom;
}
geometry* createTetrahedron()
{
	geometry* geom = new geometry();
	for (int i = 0; i < 4; ++i)
	{
		geom->vertices.push_back(tetrahedron_vertices[i]);
		geom->normals.push_back(tetrahedron_normals[i]);
	}
	for (int i = 0; i < 12; ++i)
		geom->indices.push_back(tetrahedron_indices[i]);
	
	createVertexBuffer(geom);
	createNormalBuffer(geom);
	glBindVertexArray(0);
	createIndexBuffer(geom);

	return geom;
}