water.cpp

来自「小型的3D游戏引擎」· C++ 代码 · 共 75 行

CPP
75 行
字号
#include <windows.h>
#include <gl/gl.h>
#include "water.h"


////////////////////////////////////////////////////////////////////////////////////

bool GcWater::Init(char *texName, float size, float height)
{
	// Save the properties of th water
	m_height = height;
	m_size	 = size;

	// Load the texture
	if(!m_texture.Create(texName)) {
		return false;
	}

	SetExtents(GcVector3(m_size / 2, m_height, m_size / 2));

	return true;
}

////////////////////////////////////////////////////////////////////////////////////

void GcWater::Render()
{
	// Enable blending
	glEnable(GL_BLEND);

	// Enable read only depth buffer
	glDepthMask(GL_FALSE);

	// Set blend function to transparency
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);

	// Set the color to transparent blue
	glColor4f(m_r, m_g, m_b, m_a);

	// Use the water texture
	m_texture.Bind();

	glPushMatrix();

		glMultMatrixf(m_worldMatrix);

		// Begin rendering
		glBegin(GL_QUADS);
			
			// Lower left corner
			glTexCoord2f(0.0f, 0.0f);
			glVertex3f(0.0f, m_height, 0.0f);

			// Lower right corner
			glTexCoord2f(1.0f, 0.0f);
			glVertex3f(0.0f, m_height, m_size);

			// Upper right corner
			glTexCoord2f(1.0f, 1.0f);
			glVertex3f(m_size, m_height, m_size);

			// Upper left corner
			glTexCoord2f(0.0f, 1.0f);
			glVertex3f(m_size, m_height, 0.0f);

		glEnd();

	glPopMatrix();

	// Re-set the blend and depth buffer settings
	glDepthMask(GL_TRUE);
	glDisable(GL_BLEND);
}

////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?