コード例 #1
0
 void Cal3DAppearance::draw() {
     glPushMatrix();
     glEnable(GL_DEPTH_TEST);
     glRotatef(_direction, 0, 1, 0);
     ModelRenderer().draw(*_model);
     glDisable(GL_DEPTH_TEST);
     glPopMatrix();
 }
コード例 #2
0
void ModelRenderer::init(int x, int y, int z, CubeModel * parentModel) {
	if (parentModel == NULL) {
		*this = ModelRenderer();
		return;
	}

	this->parentModel = parentModel;

	Shader vertexShader;
	vertexShader.loadShader("shaders/colorShader.vert", GL_VERTEX_SHADER);

	Shader fragmentShader;
	fragmentShader.loadShader("shaders/colorShader.frag", GL_FRAGMENT_SHADER);

	shaderProgram.createProgram();
	shaderProgram.addShader(&vertexShader);
	shaderProgram.addShader(&fragmentShader);
	shaderProgram.linkProgram();

	vertexShader.deleteShader();
	fragmentShader.deleteShader();

	ModelPosition = glm::vec3(x, y, z);
	modelMatNeedsUpdate = true;

	int modelWidth = parentModel->getWidth();
	int modelHeight = parentModel->getHeight();
	int modelDepth = parentModel->getDepth();

	int numCubes = modelWidth * modelHeight * modelDepth;

	int numVerticesPerModel = numCubes * 24 * 3;
	int numColorsPerModel = numCubes * 24 * 3;
	int numNormalsPerModel = numCubes * 24 * 3;
	int numIndicesPerModel = numCubes * 36;

	initBuffersWithSize((numVerticesPerModel + numColorsPerModel + numNormalsPerModel) * sizeof(float), numIndicesPerModel * sizeof(unsigned int));

	float * vertexArray = new float[numVerticesPerModel + numColorsPerModel + numNormalsPerModel];

	/*   Cube Diagram
	 *
	 * 		  p7__________p6
	 * 		   /|		 /|
	 * 		p3/_|_____p2/ |
	 * 		 |  |	   |  |
	 * 		 |	|p4____|__|p5
	 * 		 | /	   | /
	 * 	   p0|/________|/p1
	 */

	float cubeSize = parentModel->getScale();

	float cubeVertexData[] = {
			//Front
			0, 0, cubeSize,
			cubeSize, 0, cubeSize,
			cubeSize, cubeSize, cubeSize,
			0, cubeSize, cubeSize,

			//Back
			cubeSize, 0, 0,
			0, 0, 0,
			0, cubeSize, 0,
			cubeSize, cubeSize, 0,

			//Left
			0, 0, 0,
			0, 0, cubeSize,
			0, cubeSize, cubeSize,
			0, cubeSize, 0,

			//Right
			cubeSize, 0, cubeSize,
			cubeSize, 0, 0,
			cubeSize, cubeSize, 0,
			cubeSize, cubeSize, cubeSize,

			//Top
			0, cubeSize, cubeSize,
			cubeSize, cubeSize, cubeSize,
			cubeSize, cubeSize, 0,
			0, cubeSize, 0,

			//Bottom
			0, 0, 0,
			cubeSize, 0, 0,
			cubeSize, 0, cubeSize,
			0, 0, cubeSize
	};

	float cubeNormalData[] = {
			0, 0, 1,
			0, 0, 1,
			0, 0, 1,
			0, 0, 1,

			0, 0, -1,
			0, 0, -1,
			0, 0, -1,
			0, 0, -1,

			-1, 0, 0,
			-1, 0, 0,
			-1, 0, 0,
			-1, 0, 0,

			1, 0, 0,
			1, 0, 0,
			1, 0, 0,
			1, 0, 0,

			0, 1, 0,
			0, 1, 0,
			0, 1, 0,
			0, 1, 0,

			0, -1, 0,
			0, -1, 0,
			0, -1, 0,
			0, -1, 0
	};

	Block ** blockArray = parentModel->storage->getBlockArray();

	for (int x = 0; x < modelWidth; ++x) {
		for (int y = 0; y < modelHeight; ++y) {
			for (int z = 0; z < modelDepth; ++z) {
				int blockIndex = x * modelHeight * modelDepth + y * modelDepth + z;

				int numVerticesPerCube = 24;

				int vertexIndex = blockIndex * numVerticesPerCube * 3;
				int colorIndex = blockIndex * numVerticesPerCube * 3;
				int normalIndex = blockIndex * numVerticesPerCube * 3;

				Block * block = blockArray[blockIndex];

				if (block != NULL && block->isDrawn()) {
					for (int i = 0; i < numVerticesPerCube; ++i) {
						vertexArray[vertexIndex + (i * 3)] = cubeVertexData[(i * 3)] + x;
						vertexArray[vertexIndex + (i * 3) + 1] = cubeVertexData[(i * 3) + 1] + y;
						vertexArray[vertexIndex + (i * 3) + 2] = cubeVertexData[(i * 3) + 2] + z;

						vertexArray[numVerticesPerModel + colorIndex + (i * 3)] = block->getColor().r;
						vertexArray[numVerticesPerModel + colorIndex + (i * 3) + 1] = block->getColor().g;
						vertexArray[numVerticesPerModel + colorIndex + (i * 3) + 2] = block->getColor().b;

						vertexArray[numVerticesPerModel + numColorsPerModel + normalIndex + (i * 3)] = cubeNormalData[(i * 3)];
						vertexArray[numVerticesPerModel + numColorsPerModel + normalIndex + (i * 3) + 1] = cubeNormalData[(i * 3) + 1];
						vertexArray[numVerticesPerModel + numColorsPerModel + normalIndex + (i * 3) + 2] = cubeNormalData[(i * 3) + 2];
					}
				}
			}
		}
	}

	substituteDataToVertexBuffer((numVerticesPerModel + numColorsPerModel + numNormalsPerModel) * sizeof(float), 0, vertexArray);

	remakeIndexBuffer();

	delete[] vertexArray;
}