コード例 #1
0
ファイル: 05_Tunnel.cpp プロジェクト: zijuan0810/supberbile5
/**
 * This function does any needed initialization on the rendering context.
 * This is the first opportunity to do any OpenGL related tasks.
 */
void SetupRC()
{
	// Blue background
	glClearColor(0.0f, 0.0f, 1.0f, 1.0f );

	shaderManager.init();

	// Load texure
	glGenTextures(TEXTURE_COUNT, arrTextures);
	for (int i = 0; i < TEXTURE_COUNT; i++) {
		GLint iWidth, iHeight, iComponents;
		GLenum eFormat;
		// Bind to next texture object
		glBindTexture(GL_TEXTURE_2D, arrTextures[i]);
		// Load texture
		GLbyte* pBytes = gltReadTGABits(szTextureFiles[i], &iWidth, &iHeight, &iComponents, &eFormat);
		// set filter and wrap modes
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE,
					pBytes);
		glGenerateMipmap(GL_TEXTURE_2D);
		// Don't need original texure data any more
		free(pBytes);
	}

	// Build Geometry
	floorBatch.begin(GL_TRIANGLE_STRIP, 28, 1);
	for(GLfloat i = 60.0f; i >= 0.0f; i -= 10.0f) {
		floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
		floorBatch.Vertex3f(-10.0f, -10.0f, i);
		floorBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
		floorBatch.Vertex3f(10.0f, -10.0f, i);

		floorBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
		floorBatch.Vertex3f(-10.0f, -10.0f, i - 10.0f);

		floorBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
		floorBatch.Vertex3f(10.0f, -10.0f, i - 10.0f);
	}
	floorBatch.end();
	ceilingBatch.begin(GL_TRIANGLE_STRIP, 28, 1);
	for ( GLfloat i = 60.0f; i >= 0.0f; i -=10.0f) {
		ceilingBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
		ceilingBatch.Vertex3f(-10.0f, 10.0f, i - 10.0f);
コード例 #2
0
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Create a matrix that maps geometry to the screen. 1 unit in the x direction equals one pixel 
// of width, same with the y direction.
// It also depends on the size of the texture being displayed
void GenerateOrtho2DMat(GLuint windowWidth, GLuint windowHeight, GLuint imageWidth, GLuint imageHeight)
{
	float right = (float)windowWidth;
	float quadWidth = right;
	float left = 0.0f;
	float top = (float)windowHeight;
	float quadHeight = top;
	float bottom = 0.0f;
	float screenAspect = (float)windowWidth / windowHeight;
	float imageAspect = (float)imageWidth / imageHeight;

	if (screenAspect > imageAspect)
		quadWidth = windowHeight*imageAspect;
	else
		quadHeight = windowWidth*imageAspect;

	// set ortho matrix
	orthoMatrix[0] = (float)(2 / (right - left));
	orthoMatrix[1] = 0.0;
	orthoMatrix[2] = 0.0;
	orthoMatrix[3] = 0.0;

	orthoMatrix[4] = 0.0;
	orthoMatrix[5] = (float)(2 / (top - bottom));
	orthoMatrix[6] = 0.0;
	orthoMatrix[7] = 0.0;

	orthoMatrix[8] = 0.0;
	orthoMatrix[9] = 0.0;
	orthoMatrix[10] = (float)(-2 / (1.0 - 0.0));
	orthoMatrix[11] = 0.0;

	orthoMatrix[12] = -1 * (right + left) / (right - left);
	orthoMatrix[13] = -1 * (top + bottom) / (top - bottom);
	orthoMatrix[14] = -1.0f;
	orthoMatrix[15] = 1.0;

	// set screen quad vertex array
	screenQuad.Reset();
	screenQuad.begin(GL_TRIANGLE_STRIP, 4, 1);
	screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f);
	screenQuad.MultiTexCoord2f(0, 0.0f, 0.0f);
	screenQuad.Vertex3f(0.0f, 0.0f, 0.0f);

	screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f);
	screenQuad.MultiTexCoord2f(0, 1.0f, 0.0f);
	screenQuad.Vertex3f(quadWidth, 0.0f, 0.0f);

	screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f);
	screenQuad.MultiTexCoord2f(0, 0.0f, 1.0f);
	screenQuad.Vertex3f(0.0f, quadHeight, 0.0f);

	screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f);
	screenQuad.MultiTexCoord2f(0, 1.0f, 1.0f);
	screenQuad.Vertex3f(quadWidth, quadHeight, 0.0f);
	screenQuad.end();
}
コード例 #3
0
ファイル: 10_oit.cpp プロジェクト: zijuan0810/supberbile5
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Create a matrix that maps geometry to the screen. 1 unit in the x directionequals one pixel 
// of width, same with the y direction.
//
void GenerateOrtho2DMat(GLuint imageWidth, GLuint imageHeight)
{
	float right = (float)imageWidth;
	float quadWidth = right;
	float left = 0.0f;
	float top = (float)imageHeight;
	float quadHeight = top;
	float bottom = 0.0f;

	// set ortho matrix
	orthoMatrix[0] = (float)(2 / (right));
	orthoMatrix[1] = 0.0;
	orthoMatrix[2] = 0.0;
	orthoMatrix[3] = 0.0;

	orthoMatrix[4] = 0.0;
	orthoMatrix[5] = (float)(2 / (top));
	orthoMatrix[6] = 0.0;
	orthoMatrix[7] = 0.0;

	orthoMatrix[8] = 0.0;
	orthoMatrix[9] = 0.0;
	orthoMatrix[10] = (float)(-2 / (1.0 - 0.0));
	orthoMatrix[11] = 0.0;

	orthoMatrix[12] = -1.0f;
	orthoMatrix[13] = -1.0f;
	orthoMatrix[14] = -1.0f;
	orthoMatrix[15] = 1.0;

	// set screen quad vertex array
	screenQuad.Reset();
	screenQuad.begin(GL_TRIANGLE_STRIP, 4, 1);
	screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f);
	screenQuad.MultiTexCoord2f(0, 0.0f, 0.0f);
	screenQuad.Vertex3f(0.0f, 0.0f, 0.0f);

	screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f);
	screenQuad.MultiTexCoord2f(0, 1.0f, 0.0f);
	screenQuad.Vertex3f(right, 0.0f, 0.0f);

	screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f);
	screenQuad.MultiTexCoord2f(0, 0.0f, 1.0f);
	screenQuad.Vertex3f(0.0f, top, 0.0f);

	screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f);
	screenQuad.MultiTexCoord2f(0, 1.0f, 1.0f);
	screenQuad.Vertex3f(right, top, 0.0f);
	screenQuad.end();
}
コード例 #4
0
/**
 * This function does any needed initialization on the rendering context.
 * This is the first opportunity to do any OpenGL related tasks.
 */
void SetupRC()
{
	// Blue background
	glClearColor(0.0f, 0.0f, 1.0f, 1.0f );

	shaderManager.init();

	// load up a triangle
	GLfloat vVerts[] = {
		-0.5f, 0.0f, 0.0f,
		 0.5f, 0.0f, 0.0f,
		 0.0f, 0.5f, 0.0f
	};

	GLfloat vTexCoords[] = {
		0.0f, 0.0f,
		1.0f, 0.0f,
		0.5f, 1.0f
	};

	triangleBatch.begin(GL_TRIANGLES, 3, 1);
	triangleBatch.CopyVertexData3f(vVerts);
	triangleBatch.CopyTexCoordData2f(vTexCoords, 0);
	triangleBatch.end();

	myTexturedIdentityShader = gltLoadShaderWithFileEx("TextureIndentity.vp", 
																"TextureIndentity.fp",
																2,
																GLT_ATTRIBUTE_VERTEX, "vVertex",
																GLT_ATTRIBUTE_TEXTURE0, "vTexCoords");

	glGenTextures(1, &textureId);
	glBindTexture(GL_TEXTURE_2D, textureId);

	gltLoadTextureTGA("stone.tga", GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
}
コード例 #5
0
/**
 * This function does any needed initialization on the rendering context.
 * This is the first opportunity to do any OpenGL related tasks.
 */
void SetupRC()
{
	// make sure opengl entry points are set
	glewInit();

	// Blue background
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	shaderManager.init();

	gltCreateTorus(torusBatch, 0.4f, 0.15f, 40, 20);
	gltCreateSphere(sphereBatch, 0.1f, 26, 13);

	// make the solid ground
	GLfloat texSize = 10.0f;
	floorBatch.begin(GL_TRIANGLE_FAN, 4, 1);
	floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
	floorBatch.Vertex3f(-20.0f, -0.41f, 20.0f);

	floorBatch.MultiTexCoord2f(0, texSize, 0.0f);
	floorBatch.Vertex3f(20.0f, -0.41f, 20.0f);

	floorBatch.MultiTexCoord2f(0, texSize, texSize);
	floorBatch.Vertex3f(20.0f, -0.41f, -20.0f);

	floorBatch.MultiTexCoord2f(0, 0.0f, texSize);
	floorBatch.Vertex3f(-20.0f, -0.41f, -20.0f);
	floorBatch.end();

	// draw the opengl logo
	int x = 500;
	int y = 155;
	int width = 300;
	int height = 155;
	logoBatch.begin(GL_TRIANGLE_FAN, 4, 1);

	// Upper left hand corner
	logoBatch.MultiTexCoord2f(0, 0.0f, height);
	logoBatch.Vertex3f(x, y, 0.0);

	// Lower left hand corner
	logoBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
	logoBatch.Vertex3f(x, y - height, 0.0f);

	// Lower right hand corner
	logoBatch.MultiTexCoord2f(0, width, 0.0f);
	logoBatch.Vertex3f(x + width, y - height, 0.0f);

	// Upper righ hand corner
	logoBatch.MultiTexCoord2f(0, width, height);
	logoBatch.Vertex3f(x + width, y, 0.0f);

	logoBatch.end();

	// Make 4 texture objects
	glGenTextures(4, uiTextures);

	// Load the Marble
	glBindTexture(GL_TEXTURE_2D, uiTextures[0]);
	gltLoadTextureTGA("marble.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT);

	// Load Mars
	glBindTexture(GL_TEXTURE_2D, uiTextures[1]);
	gltLoadTextureTGA("marslike.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);

	// Load Moon
	glBindTexture(GL_TEXTURE_2D, uiTextures[2]);
	gltLoadTextureTGA("moonlike.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);

	// Load the Logo
	glBindTexture(GL_TEXTURE_RECTANGLE, uiTextures[3]);
	gltLoadTextureTGARect("OpenGL-Logo.tga", GL_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE);

	rectReplaceShader = gltLoadShaderWithFileEx("RectReplace.vert", "RectReplace.frag", 2, 
		GLT_ATTRIBUTE_VERTEX, "vVertex", 
		GLT_ATTRIBUTE_TEXTURE0, "vTexCoord");

	locRectMVP = glGetUniformLocation(rectReplaceShader, "mvpMatrix");
	locRectTexture = glGetUniformLocation(rectReplaceShader, "rectangleImage");

	// Randomly place the spheres
	for (int i = 0; i < NUM_SPHERES; i++) {
		GLfloat x = ((GLfloat)((rand() % 400) - 200) * 0.1f);
		GLfloat z = ((GLfloat)((rand() % 400) - 200) * 0.1f);
		spheres[i].SetOrigin(x, 0.0f, z);
	}
}
コード例 #6
0
ファイル: 10_oit.cpp プロジェクト: zijuan0810/supberbile5
/**
 * This function does any needed initialization on the rendering context.
 * This is the first opportunity to do any OpenGL related tasks.
 */
void SetupRC()
{
	// Blue background
	glClearColor(0.0f, 0.0f, 1.0f, 1.0f );

	shaderManager.init();
	glEnable(GL_DEPTH_TEST);

	gltCreateCylinder(bckgrndCylBatch, 4.0f, 4.0f, 5.2f, 1024, 1);
	gltCreateDisk(diskBatch, 0.0f, 1.5f, 40, 10);

	glass1Batch.begin(GL_TRIANGLE_FAN, 4, 1);
	glass1Batch.Vertex3f(-1.0f, -1.0f, 0.0f);
	glass1Batch.Vertex3f(1.0f, -1.0f, 0.0f);
	glass1Batch.Vertex3f(1.0f, 1.0f, 0.0f);
	glass1Batch.Vertex3f(-1.0f, 1.0f, 0.0f);
	glass1Batch.end();

	glass2Batch.begin(GL_TRIANGLE_FAN, 4, 1);
	glass2Batch.Vertex3f(0.0f, 1.0f, 0.0f);
	glass2Batch.Vertex3f(1.0f, 0.0f, 0.0f);
	glass2Batch.Vertex3f(0.0f, -1.0f, 0.0f);
	glass2Batch.Vertex3f(-1.0f, 0.0f, 0.0f);
	glass2Batch.end();

	glass3Batch.begin(GL_TRIANGLE_FAN, 3, 1);
	glass3Batch.Vertex3f(0.0f, 1.0f, 0.0f);
	glass3Batch.Vertex3f(1.0f, -1.0f, 0.0f);
	glass3Batch.Vertex3f(-1.0f, -1.0f, 0.0f);
	glass3Batch.end();

	glass4Batch.begin(GL_TRIANGLE_FAN, 4, 1);
	glass4Batch.Vertex3f(-1.0f, 1.0f, 0.0f);
	glass4Batch.Vertex3f(1.0f, 0.5f, 0.0f);
	glass4Batch.Vertex3f(1.0f, -1.0f, 0.0f);
	glass4Batch.Vertex3f(-1.0f, -0.5f, 0.0f);
	glass4Batch.end();

	glGenTextures(2, textures);
	glBindTexture(GL_TEXTURE_2D, textures[0]);
	gltLoadTextureBMP("marble.bmp", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT);
	glBindTexture(GL_TEXTURE_2D, textures[1]);
	gltLoadTextureBMP("start_line.bmp", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT);

	// create and bind an FBO
	glGenFramebuffers(1, &msFBO);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, msFBO);

	// create depth texture
	glGenTextures(1, &depthTextureName);
	glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, depthTextureName);
	glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 8, GL_DEPTH_COMPONENT24, screenWidth, screenHeight, GL_FALSE);

	// setup HDR render texture
	glGenTextures(1, msTexture);
	glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, msTexture[0]);
	glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 8, GL_RGBA8, screenWidth, screenHeight, GL_FALSE);

	// create and bind FBO
	glGenFramebuffers(1, &msFBO);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, msFBO);

	// attach texture to first color attachment and depth RBO
	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, msTexture[0], 0);
	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, depthTextureName, 0);

	// reset framebuffer binding
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

	// Load oit resolve shader
	oitResolve = gltLoadShaderWithFileEx("basic.vs", "oitResolve.fs", 3,
		GLT_ATTRIBUTE_VERTEX, "vVertex",
		GLT_ATTRIBUTE_NORMAL, "vNormal",
		GLT_ATTRIBUTE_TEXTURE0, "vTexCoord0");
	glBindFragDataLocation(oitResolve, 0, "oColor");
	glLinkProgram(oitResolve);

	// Load multisample resolve shader
	msResolve = gltLoadShaderWithFileEx("basic.vs", "msResolve.fs", 3,
		GLT_ATTRIBUTE_VERTEX, "vVertex",
		GLT_ATTRIBUTE_NORMAL, "vNormal",
		GLT_ATTRIBUTE_TEXTURE0, "vTexCoord0");

	glBindFragDataLocation(msResolve, 0, "oColor");
	glLinkProgram(msResolve);

	// Make sure all went well
	gltCheckErrors(oitResolve);
	gltCheckErrors(msResolve);

	int numMasks = 0;
	glGetIntegerv(GL_MAX_SAMPLE_MASK_WORDS, &numMasks);
	log("GL_MAX_SAMPLE_MASK_WORDS: %d", numMasks);
}