Ejemplo n.º 1
0
void CDisk_ConcentricRings::Init()

{
	// Generate the verticies and elements
	vector<vec3> vbo_data;
	vector<unsigned int> elements;
	unsigned int z_divisions = 20;
	unsigned int phi_divisions = 50;
	unsigned int r_divisions = 20;

	mRimStart = 0;
	CCylinder::GenerateRim(vbo_data, elements, z_divisions, phi_divisions);
	mRimSize = elements.size();
	// Calculate the offset in vertex indexes for the GenerateMidplane function
	// to generate correct element indices.
	unsigned int vertex_offset = vbo_data.size();
	mMidplaneStart = mRimSize;
	CCylinder::GenerateMidplane(vbo_data, elements, vertex_offset, r_divisions, phi_divisions);
	mMidplaneSize = elements.size() - mRimSize;

	// Create a new Vertex Array Object, Vertex Buffer Object, and Element Buffer
	// object to store the model's information.
	//
	// First generate the VAO, this stores all buffer information related to this object
	glGenVertexArrays(1, &mVAO);
	glBindVertexArray(mVAO);
	// Generate and bind to the VBO. Upload the verticies.
	glGenBuffers(1, &mVBO); // Generate 1 buffer
	glBindBuffer(GL_ARRAY_BUFFER, mVBO);
	glBufferData(GL_ARRAY_BUFFER, vbo_data.size() * sizeof(vec3), &vbo_data[0], GL_STATIC_DRAW);
	// Generate and bind to the EBO. Upload the elements.
	glGenBuffers(1, &mEBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mEBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(unsigned int), &elements[0], GL_STATIC_DRAW);

	CHECK_OPENGL_STATUS_ERROR(glGetError(), "Could not create buffers");

	// Initialize the shader variables and texture following the default packing
	// scheme.
	InitShaderVariables();
	InitTexture();

	// All done. Un-bind from the VAO, VBO, and EBO to prevent it from being
	// modified by subsequent calls.
	glBindVertexArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

	CHECK_OPENGL_STATUS_ERROR(glGetError(), "Failed to bind back default buffers");

	// Indicate the model is ready to use.
	mModelReady = true;
}
Ejemplo n.º 2
0
void CRocheLobe_FF::Render(const glm::mat4 & view, const GLfloat & max_flux)
{
    const unsigned int n_sides = pow(2, mParams["n_side_power"].getValue());

    NormalizeFlux(max_flux);

    mat4 scale = glm::scale(mat4(), glm::vec3(1, 1, 1));

    // Activate the shader
    GLuint shader_program = mShader->GetProgram();
    mShader->UseShader();

    // bind back to the VAO
    glBindVertexArray(mVAO);

    // Define the view:
    GLint uniView = glGetUniformLocation(shader_program, "view");
    glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));

    GLint uniTranslation = glGetUniformLocation(shader_program, "translation");
    glUniformMatrix4fv(uniTranslation, 1, GL_FALSE,
            glm::value_ptr(Translate()));

    GLint uniRotation = glGetUniformLocation(shader_program, "rotation");
    glUniformMatrix4fv(uniRotation, 1, GL_FALSE, glm::value_ptr(Rotate()));

    GLint uniScale = glGetUniformLocation(shader_program, "scale");
    glUniformMatrix4fv(uniScale, 1, GL_FALSE, glm::value_ptr(scale));

    // Bind to the texture, upload it.
    glBindTexture(GL_TEXTURE_RECTANGLE, mFluxTextureID);
    glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, 12 * n_sides, n_sides, 0, GL_RGBA,
            GL_FLOAT, &mFluxTexture[0]);

    // Upload the VBO data:
    glBindBuffer(GL_ARRAY_BUFFER, mVBO);
    glBufferData(GL_ARRAY_BUFFER, mVBOData.size() * sizeof(vec3), &mVBOData[0],
            GL_DYNAMIC_DRAW);

    // render
    glDrawElements(GL_TRIANGLES, mElements.size(), GL_UNSIGNED_INT, 0);

    glBindTexture(GL_TEXTURE_RECTANGLE, 0);

    // unbind from the Vertex Array Object, Vertex Buffer Object, and Element buffer object.
    glBindVertexArray(0);

    CHECK_OPENGL_STATUS_ERROR(glGetError(), "Rendering failed");
}
Ejemplo n.º 3
0
void CDisk_ConcentricRings::Render(const glm::mat4 & view, const GLfloat & max_flux)
{
	// Look up the parameters:
	const double r_in = mParams["r_in"].getValue();
	const double MaxRadius  = mParams["radius"].getValue();
	const double MaxHeight  = mParams["height"].getValue();
	int n_rings  = ceil(mParams["n_rings"].getValue());

	NormalizeFlux(max_flux);

	// Activate the shader
	GLuint shader_program = mShader->GetProgram();
	mShader->UseShader();

	// bind back to the VAO
	glBindVertexArray(mVAO);

	// Define the maximum height and radius
	GLint uniMaxRadius = glGetUniformLocation(shader_program, "r_max");
	glUniform1f(uniMaxRadius, MaxRadius);

	GLint uniMaxHeight = glGetUniformLocation(shader_program, "z_max");
	glUniform1f(uniMaxHeight, MaxHeight);

	// Set the value for the inner radius.
	// NOTE: In order to accelerate the rendering of the midplane and ensure
	// the inner-most rim is rendered, we have elected to subtract a tiny
	// amount off of the inner radius. This is noted on the wiki.
	GLint uniInnerRadius = glGetUniformLocation(shader_program, "r_in");
	glUniform1f(uniInnerRadius, r_in - 0.01);

	// Define the view:
	GLint uniView = glGetUniformLocation(shader_program, "view");
	glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));

	GLint uniTranslation = glGetUniformLocation(shader_program, "translation");
	glUniformMatrix4fv(uniTranslation, 1, GL_FALSE, glm::value_ptr(Translate()));

	GLint uniRotation = glGetUniformLocation(shader_program, "rotation");
	glUniformMatrix4fv(uniRotation, 1, GL_FALSE, glm::value_ptr(Rotate()));

	// Look up the scale variable location. We use it below.
	GLint uniScale = glGetUniformLocation(shader_program, "scale");

	// bind to this object's texture
	glBindTexture(GL_TEXTURE_RECTANGLE, mFluxTextureID);
	// upload the image
	glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, mFluxTexture.size(), 1, 0, GL_RGBA,
			GL_FLOAT, &mFluxTexture[0]);

	// Disable depth testing and face culling, we need both sides to render
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);

	// Render each cylindrical wall:

    // Init scale variables
	double radius = 0;
	double height = MaxHeight;

    glm::mat4 scale;
    glm::mat4 r_scale;
    glm::mat4 h_scale = glm::scale(mat4(), glm::vec3(1.0, 1.0, height));

    // Render each of the concentric rings
	double dr = (MaxRadius - r_in) / (n_rings - 1);
	for(unsigned int i = 0; i < n_rings; i++)
	{
		// Scale the radius:
		radius = r_in + i * dr;
		r_scale = glm::scale(mat4(), glm::vec3(radius, radius, 1.0));
		scale = h_scale * r_scale;

		glUniformMatrix4fv(uniScale, 1, GL_FALSE, glm::value_ptr(scale));
		glDrawElements(GL_TRIANGLE_STRIP, mRimSize, GL_UNSIGNED_INT, 0);
	}

	// Render the midplane
	r_scale = glm::scale(mat4(), glm::vec3(MaxRadius, MaxRadius, 1.0));
	glUniformMatrix4fv(uniScale, 1, GL_FALSE, glm::value_ptr(scale));
	glDrawElements(GL_TRIANGLE_STRIP, mMidplaneSize, GL_UNSIGNED_INT, (void*) (mMidplaneStart * sizeof(float)));

	// Disable depth testing and face culling
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	// bind back to the default texture.
	glBindTexture(GL_TEXTURE_RECTANGLE, 0);

	CHECK_OPENGL_STATUS_ERROR(glGetError(), "Rendering failed.");
}
Ejemplo n.º 4
0
void CCylinder::Render(const glm::mat4 & view, const GLfloat & max_flux)
{
	NormalizeFlux(max_flux);

	// Look up the parameters:
	const double diameter = mParams["diameter"].getValue();
	const double radius = diameter / 2;
	const double height  = mParams["height"].getValue();

	// Activate the shader
	GLuint shader_program = mShader->GetProgram();
	mShader->UseShader();

	// bind back to the VAO
	glBindVertexArray(mVAO);

	// Define the view:
	GLint uniView = glGetUniformLocation(shader_program, "view");
	glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));

	GLint uniTranslation = glGetUniformLocation(shader_program, "translation");
	glUniformMatrix4fv(uniTranslation, 1, GL_FALSE, glm::value_ptr(Translate()));

	GLint uniRotation = glGetUniformLocation(shader_program, "rotation");
	glUniformMatrix4fv(uniRotation, 1, GL_FALSE, glm::value_ptr(Rotate()));

	GLint uniScale = glGetUniformLocation(shader_program, "scale");
    glm::mat4 r_scale = glm::scale(mat4(), glm::vec3(radius, radius, 1.0));
    glm::mat4 h_scale = glm::scale(mat4(), glm::vec3(1.0, 1.0, height));
    glm::mat4 scale = r_scale * h_scale;
	glUniformMatrix4fv(uniScale, 1, GL_FALSE, glm::value_ptr(scale));

	// bind to this object's texture, upload the image.
	glBindTexture(GL_TEXTURE_RECTANGLE, mFluxTextureID);
	glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, mFluxTexture.size(), 1, 0, GL_RGBA,
			GL_FLOAT, &mFluxTexture[0]);

	// Render
	glDisable(GL_DEPTH_TEST);

	// Draw the cylindrical wall.
	glDrawElements(GL_TRIANGLE_STRIP, mRimSize, GL_UNSIGNED_INT, 0);

	// Draw the top
	mat4 z_offset;
	z_offset = glm::translate(mat4(), vec3(0, 0, 0.5));
	scale = r_scale * h_scale * z_offset;
	glUniformMatrix4fv(uniScale, 1, GL_FALSE, glm::value_ptr(scale));
	glDrawElements(GL_TRIANGLE_STRIP, mMidplaneSize, GL_UNSIGNED_INT, (void*) (mMidplaneStart * sizeof(float)));

	// Draw the bottom
	z_offset = glm::translate(mat4(), vec3(0, 0, -0.5));
	scale = r_scale * h_scale * z_offset;
	glUniformMatrix4fv(uniScale, 1, GL_FALSE, glm::value_ptr(scale));
	glDrawElements(GL_TRIANGLE_STRIP, mMidplaneSize, GL_UNSIGNED_INT, (void*) (mMidplaneStart * sizeof(float)));

	glEnable(GL_DEPTH_TEST);

	// Unbind
	glBindTexture(GL_TEXTURE_RECTANGLE, 0);

	CHECK_OPENGL_STATUS_ERROR(glGetError(), "Rendering failed.");
}
Ejemplo n.º 5
0
void CHealpixSpheroid::Init()
{
	// See if buffers are allocated, if so free them before re-initing them
	if(mEBO) glDeleteBuffers(1, &mEBO);
	if(mVBO) glDeleteBuffers(1, &mVBO);
	if(mVAO) glDeleteVertexArrays(1, &mVAO);

	const unsigned int n_sides = pow(2, mParams["n_side_power"].getValue());

	n_pixels = nside2npix(n_sides);

	gravity.resize(n_pixels);
	g_x.resize(n_pixels);
	g_y.resize(n_pixels);
	g_z.resize(n_pixels);
	mPixelTemperatures.resize(n_pixels);

	// Generate the verticies and elements
	GenerateModel(mVBOData, mElements);
	// Create a new Vertex Array Object, Vertex Buffer Object, and Element Buffer
	// object to store the model's information.
	//
	// First generate the VAO, this stores all buffer information related to this object
	glGenVertexArrays(1, &mVAO);
	glGenBuffers(1, &mVBO); // Generate 1 buffer
	glGenBuffers(1, &mEBO);

	glBindVertexArray(mVAO);
	// Generate and bind to the VBO. Upload the verticies.
	glBindBuffer(GL_ARRAY_BUFFER, mVBO);
	glBufferData(GL_ARRAY_BUFFER, mVBOData.size() * sizeof(vec3), &mVBOData[0],
			GL_DYNAMIC_DRAW);

	// Generate and bind to the EBO. Upload the elements.
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mEBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,
			mElements.size() * sizeof(unsigned int), &mElements[0],
			GL_DYNAMIC_DRAW);

	CHECK_OPENGL_STATUS_ERROR(glGetError(), "Failed to create buffers");

	InitShaderVariables();

	// All done. Un-bind from the VAO, VBO, and EBO to prevent it from being
	// modified by subsequent calls.
	glBindVertexArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

	glActiveTexture(GL_TEXTURE0);
	// Load image as a texture
	glGenTextures(1, &mFluxTextureID);
	glBindTexture(GL_TEXTURE_RECTANGLE, mFluxTextureID);

	glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, 12 * n_sides, n_sides, 0, GL_RGBA,
			GL_FLOAT, &mFluxTexture[0]);

	glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	// Set sampler
	GLuint shader_program = mShader->GetProgram();
	GLuint TextureSamp = glGetUniformLocation(shader_program, "TexSampler");
	glUniform1i(TextureSamp, 0); // Set "TexSampler" to user texture Unit 0

	// Return to the default texture.
	glBindTexture(GL_TEXTURE_RECTANGLE, 0);

	// Check that things loaded correctly.
	CHECK_OPENGL_STATUS_ERROR(glGetError(), "Failed bind back to default buffer.");

	// Indicate the model is ready to use.
	mModelReady = true;
}