• Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a \$50 Amazon gift card. Click here to get started!

# Rendering to texture. (Efficent dynamic clouds)

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Syerjchep  Members   -  Reputation: 183

Like
0Likes
Like

Posted 22 September 2013 - 09:06 AM

I've got some dynamicly generated clouds made from perlin noise. Then they're rendered onto a grid of white triangles with varying transparencies (the result of the perlin noise) where they make a square above my ground.

GLuint cloudVAO;
GLuint cloudVertBuffer;
GLuint cloudTransBuffer;

vector<float> cloudTrans;
vector<glm::vec3> cloudVerts;

float cloudHeight = 250;

void renderClouds(Perlin cloudPerlin)
{
cloudVerts.empty();
cloudTrans.empty();

for(float x = 0; x < 1024; x++)
{
for(float y = 0; y<1024; y++)
{
float perlin = cloudPerlin.Get(x/1024.0f,y/1024.0f);
perlin++;
perlin/=2.0f;

//log(istr(x) + " " + istr(y) + " " + fstr(perlin));

/*glm::vec3 tmp(x,y,100);
cloudVerts.push_back(tmp);*/

glm::vec3 temp1(x,cloudHeight,y);
glm::vec3 temp2(x,cloudHeight,y+1);
glm::vec3 temp3(x+1,cloudHeight,y);

glm::vec3 temp4(x+1,cloudHeight,y+1);
glm::vec3 temp5(x+1,cloudHeight,y);
glm::vec3 temp6(x,cloudHeight,y+1);

cloudVerts.push_back(temp1);
cloudVerts.push_back(temp2);
cloudVerts.push_back(temp3);
cloudVerts.push_back(temp4);
cloudVerts.push_back(temp5);
cloudVerts.push_back(temp6);

cloudTrans.push_back(perlin);
cloudTrans.push_back(perlin);
cloudTrans.push_back(perlin);
cloudTrans.push_back(perlin);
cloudTrans.push_back(perlin);
cloudTrans.push_back(perlin);
}
}

glGenVertexArrays(1,&cloudVAO);
glBindVertexArray(cloudVAO);

glGenBuffers(1, &cloudVertBuffer);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, cloudVertBuffer);
glBufferData(GL_ARRAY_BUFFER, cloudVerts.size() * sizeof(glm::vec3), &cloudVerts[0], GL_STATIC_DRAW);
glVertexAttribPointer(
0,                  // attribute
3,                  // size
GL_FLOAT,           // type
GL_FALSE,           // normalized?
0,                  // stride
(void*)0            // array buffer offset
);

glGenBuffers(1, &cloudTransBuffer);
glEnableVertexAttribArray(6);
glBindBuffer(GL_ARRAY_BUFFER, cloudTransBuffer);
glBufferData(GL_ARRAY_BUFFER, cloudTrans.size() * sizeof(float), &cloudTrans[0], GL_STATIC_DRAW);
glVertexAttribPointer(
6,                  // attribute
1,                  // size
GL_FLOAT,           // type
GL_FALSE,           // normalized?
0,                  // stride
(void*)0            // array buffer offset
);
}

GLuint cloudID;

void displayClouds()
{
glDisable(GL_CULL_FACE);
transform(0,0,0);
glUniform1i(cloudID,1);
glBindVertexArray(cloudVAO);
glDrawArrays(GL_TRIANGLES, 0, cloudVerts.size() );
glUniform1i(cloudID,0);
glEnable(GL_CULL_FACE);
}

#version 330 core

// Interpolated values from the vertex shaders
in vec2 UV;
in vec3 Position_worldspace;
in vec3 Normal_cameraspace;
in vec3 EyeDirection_cameraspace;
in vec3 ambientColor;
in vec3 diffuseColor;
in vec3 specularColor;
in float cloudTrans;

in vec3 Light0Direction_cameraspace;
in vec3 Light1Direction_cameraspace;
in vec3 Light2Direction_cameraspace;
in vec3 Light3Direction_cameraspace;
in vec3 Light4Direction_cameraspace;
in vec3 Light5Direction_cameraspace;
in vec3 Light6Direction_cameraspace;
in vec3 Light7Direction_cameraspace;

// Ouput data
out vec4 color;

// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;
uniform mat4 MV;
uniform int textured;
uniform int terrain;
uniform vec3 worldAmbient;
uniform int cloud;

uniform vec3 Light0_Position;
uniform vec3 Light1_Position;
uniform vec3 Light2_Position;
uniform vec3 Light3_Position;
uniform vec3 Light4_Position;
uniform vec3 Light5_Position;
uniform vec3 Light6_Position;
uniform vec3 Light7_Position;

uniform vec3 Light0_Color;
uniform vec3 Light1_Color;
uniform vec3 Light2_Color;
uniform vec3 Light3_Color;
uniform vec3 Light4_Color;
uniform vec3 Light5_Color;
uniform vec3 Light6_Color;
uniform vec3 Light7_Color;

uniform float Light0_Power;
uniform float Light1_Power;
uniform float Light2_Power;
uniform float Light3_Power;
uniform float Light4_Power;
uniform float Light5_Power;
uniform float Light6_Power;
uniform float Light7_Power;

uniform vec3 sunColor;
uniform vec3 sunPos;
in vec3 sunDirection_cameraspace;

void main()
{
// Material properties
vec3 MaterialDiffuseColor = diffuseColor;
vec3 MaterialAmbientColor = ambientColor;
vec3 MaterialSpecularColor = specularColor;

if(cloud == 1)
{
color = vec4(1,1,1,cloudTrans);
}
else
{

if(terrain == 1)
{
MaterialAmbientColor = texture2D( myTextureSampler, UV ).rgb * worldAmbient;
MaterialDiffuseColor = MaterialAmbientColor;
MaterialSpecularColor = vec3(0,0,0);
}
else
{

if(textured == 1)
{
MaterialAmbientColor = texture2D( myTextureSampler, UV ).rgb * worldAmbient;
MaterialDiffuseColor = diffuseColor * texture2D( myTextureSampler, UV ).rgb;
MaterialSpecularColor = specularColor;
}
else
{
//blender wasn't exporting ambient colors, so we're using diffuse for now
MaterialAmbientColor = MaterialDiffuseColor * worldAmbient;
}

}

float distance0 = length( Light0_Position - Position_worldspace );
float distance1 = length( Light1_Position - Position_worldspace );
float distance2 = length( Light2_Position - Position_worldspace );
float distance3 = length( Light3_Position - Position_worldspace );
float distance4 = length( Light4_Position - Position_worldspace );
float distance5 = length( Light5_Position - Position_worldspace );
float distance6 = length( Light6_Position - Position_worldspace );
float distance7 = length( Light7_Position - Position_worldspace );

vec3 n = normalize( Normal_cameraspace );
vec3 E = normalize(EyeDirection_cameraspace);

vec3 l0 = normalize( Light0Direction_cameraspace );
float cosTheta0 = clamp( dot( n,l0 ), 0,1 );

vec3 l1 = normalize( Light1Direction_cameraspace );
float cosTheta1 = clamp( dot( n,l1 ), 0,1 );

vec3 l2 = normalize( Light2Direction_cameraspace );
float cosTheta2 = clamp( dot( n,l2 ), 0,1 );

vec3 l3 = normalize( Light3Direction_cameraspace );
float cosTheta3 = clamp( dot( n,l3 ), 0,1 );

vec3 l4 = normalize( Light4Direction_cameraspace );
float cosTheta4 = clamp( dot( n,l4 ), 0,1 );

vec3 l5 = normalize( Light5Direction_cameraspace );
float cosTheta5 = clamp( dot( n,l5 ), 0,1 );

vec3 l6 = normalize( Light6Direction_cameraspace );
float cosTheta6 = clamp( dot( n,l6 ), 0,1 );

vec3 l7 = normalize( Light7Direction_cameraspace );
float cosTheta7 = clamp( dot( n,l7 ), 0,1 );

vec3 lS = normalize( sunDirection_cameraspace );
float cosThetaS = clamp( dot( n,lS ), 0,1 );

vec3 R0 = reflect(-l0,n);
float cosAlpha0 = clamp( dot( E,R0 ), 0,1 );

vec3 R1 = reflect(-l1,n);
float cosAlpha1 = clamp( dot( E,R1 ), 0,1 );

vec3 R2 = reflect(-l2,n);
float cosAlpha2 = clamp( dot( E,R2 ), 0,1 );

vec3 R3 = reflect(-l3,n);
float cosAlpha3 = clamp( dot( E,R3 ), 0,1 );

vec3 R4 = reflect(-l4,n);
float cosAlpha4 = clamp( dot( E,R4 ), 0,1 );

vec3 R5 = reflect(-l5,n);
float cosAlpha5 = clamp( dot( E,R5 ), 0,1 );

vec3 R6 = reflect(-l6,n);
float cosAlpha6 = clamp( dot( E,R6 ), 0,1 );

vec3 R7 = reflect(-l7,n);
float cosAlpha7 = clamp( dot( E,R7 ), 0,1 );

vec3 colorPower0 = Light0_Color * Light0_Power;
vec3 colorPower1 = Light1_Color * Light1_Power;
vec3 colorPower2 = Light2_Color * Light2_Power;
vec3 colorPower3 = Light3_Color * Light3_Power;
vec3 colorPower4 = Light4_Color * Light4_Power;
vec3 colorPower5 = Light5_Color * Light5_Power;
vec3 colorPower6 = Light6_Color * Light6_Power;
vec3 colorPower7 = Light7_Color * Light7_Power;

vec3 MaterialDiffuseColor0 = colorPower0 * cosTheta0 / (distance0*distance0);
vec3 MaterialSpecularColor0 = Light0_Color * Light0_Power * pow(cosAlpha0,5) / (distance0*distance0);

vec3 MaterialDiffuseColor1 = colorPower1 * cosTheta1 / (distance1*distance1);
vec3 MaterialSpecularColor1 = Light1_Color * Light1_Power * pow(cosAlpha1,5) / (distance1*distance1);

vec3 MaterialDiffuseColor2 = colorPower2 * cosTheta2 / (distance2*distance2);
vec3 MaterialSpecularColor2 = Light2_Color * Light2_Power * pow(cosAlpha2,5) / (distance2*distance2);

vec3 MaterialDiffuseColor3 = colorPower3 * cosTheta3 / (distance3*distance3);
vec3 MaterialSpecularColor3 = Light3_Color * Light3_Power * pow(cosAlpha3,5) / (distance3*distance3);

vec3 MaterialDiffuseColor4 = colorPower4 * cosTheta4 / (distance4*distance4);
vec3 MaterialSpecularColor4 = Light4_Color * Light4_Power * pow(cosAlpha4,5) / (distance4*distance4);

vec3 MaterialDiffuseColor5 = colorPower5 * cosTheta5 / (distance5*distance5);
vec3 MaterialSpecularColor5 = Light5_Color * Light5_Power * pow(cosAlpha5,5) / (distance5*distance5);

vec3 MaterialDiffuseColor6 = colorPower6 * cosTheta6 / (distance6*distance6);
vec3 MaterialSpecularColor6 = Light6_Color * Light6_Power * pow(cosAlpha6,5) / (distance6*distance6);

vec3 MaterialDiffuseColor7 = colorPower7 * cosTheta7 / (distance7*distance7);
vec3 MaterialSpecularColor7 = Light7_Color * Light7_Power * pow(cosAlpha7,5) / (distance7*distance7);

vec3 tmpcolor = MaterialAmbientColor;

tmpcolor += MaterialDiffuseColor * sunColor * cosThetaS;

tmpcolor += (MaterialDiffuseColor * MaterialDiffuseColor0) + (MaterialSpecularColor * MaterialSpecularColor0);
tmpcolor += (MaterialDiffuseColor * MaterialDiffuseColor1) + (MaterialSpecularColor * MaterialSpecularColor1);
tmpcolor += (MaterialDiffuseColor * MaterialDiffuseColor2) + (MaterialSpecularColor * MaterialSpecularColor2);
tmpcolor += (MaterialDiffuseColor * MaterialDiffuseColor3) + (MaterialSpecularColor * MaterialSpecularColor3);
tmpcolor += (MaterialDiffuseColor * MaterialDiffuseColor4) + (MaterialSpecularColor * MaterialSpecularColor4);
tmpcolor += (MaterialDiffuseColor * MaterialDiffuseColor5) + (MaterialSpecularColor * MaterialSpecularColor5);
tmpcolor += (MaterialDiffuseColor * MaterialDiffuseColor6) + (MaterialSpecularColor * MaterialSpecularColor6);
tmpcolor += (MaterialDiffuseColor * MaterialDiffuseColor7) + (MaterialSpecularColor * MaterialSpecularColor7);
color = vec4(tmpcolor,1);

}
}

There's a vertex shader too, but it's not relevant.

Clouds from below.

Clouds from above.

Problems:
1. They end abruptly.
2. The clouds end up being over 2 million faces and bring the fps down to almost 1.

So, how do I do decent dynamic (moving/changeable) clouds in opengl 4?

Edited by Syerjchep, 22 September 2013 - 02:49 PM.

### #2Barbossa  Members   -  Reputation: 967

Like
0Likes
Like

Posted 22 September 2013 - 11:08 AM

Those clouds look flat for me.

If you are not trying to create volumetric clouds, you should not use the noise to create vertices with it, but instead try to write the noise to a texture and replace the 2 million faces with just a simple one. Moving and changing is then a lot easier without a noticable fps-drop.

IF you are trying to create volumetric clouds you should use a different approach as explained here http://graphicsrunner.blogspot.de/2008/03/volumetric-clouds.html for example.

### #3Syerjchep  Members   -  Reputation: 183

Like
0Likes
Like

Posted 22 September 2013 - 12:16 PM

Those clouds look flat for me.
If you are not trying to create volumetric clouds, you should not use the noise to create vertices with it, but instead try to write the noise to a texture and replace the 2 million faces with just a simple one. Moving and changing is then a lot easier without a noticable fps-drop.

IF you are trying to create volumetric clouds you should use a different approach as explained here http://graphicsrunner.blogspot.de/2008/03/volumetric-clouds.html for example.

I know they're flat. That doesn't matter, in what I'm doing the user will never be as high as the clouds.

Anyway, I tried rendering to texture, but no matter what I try my texture remains solid black:

GLuint cloudFramebufferName;
GLuint cloudRenderedTexture;
GLuint cloudVAO;
GLuint cloudVertBuffer;
GLuint cloudTransBuffer;

vector<float> cloudTrans;
vector<glm::vec3> cloudVerts;

float cloudHeight = 250;

void renderClouds(Perlin cloudPerlin)
{
cloudVerts.empty();
cloudTrans.empty();

for(float x = 0; x < 1024; x++)
{
for(float y = 0; y<1024; y++)
{
float perlin = cloudPerlin.Get(x/1024.0f,y/1024.0f);
perlin++;
perlin/=2.0f;

//log(istr(x) + " " + istr(y) + " " + fstr(perlin));

/*glm::vec3 tmp(x,y,100);
cloudVerts.push_back(tmp);*/

glm::vec3 temp1(x,cloudHeight,y);
glm::vec3 temp2(x,cloudHeight,y+1);
glm::vec3 temp3(x+1,cloudHeight,y);

glm::vec3 temp4(x+1,cloudHeight,y+1);
glm::vec3 temp5(x+1,cloudHeight,y);
glm::vec3 temp6(x,cloudHeight,y+1);

cloudVerts.push_back(temp1);
cloudVerts.push_back(temp2);
cloudVerts.push_back(temp3);
cloudVerts.push_back(temp4);
cloudVerts.push_back(temp5);
cloudVerts.push_back(temp6);

cloudTrans.push_back(perlin);
cloudTrans.push_back(perlin);
cloudTrans.push_back(perlin);
cloudTrans.push_back(perlin);
cloudTrans.push_back(perlin);
cloudTrans.push_back(perlin);
}
}

glGenVertexArrays(1,&cloudVAO);
glBindVertexArray(cloudVAO);

glGenBuffers(1, &cloudVertBuffer);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, cloudVertBuffer);
glBufferData(GL_ARRAY_BUFFER, cloudVerts.size() * sizeof(glm::vec3), &cloudVerts[0], GL_STATIC_DRAW);
glVertexAttribPointer(
0,                  // attribute
3,                  // size
GL_FLOAT,           // type
GL_FALSE,           // normalized?
0,                  // stride
(void*)0            // array buffer offset
);

glGenBuffers(1, &cloudTransBuffer);
glEnableVertexAttribArray(6);
glBindBuffer(GL_ARRAY_BUFFER, cloudTransBuffer);
glBufferData(GL_ARRAY_BUFFER, cloudTrans.size() * sizeof(float), &cloudTrans[0], GL_STATIC_DRAW);
glVertexAttribPointer(
6,                  // attribute
1,                  // size
GL_FLOAT,           // type
GL_FALSE,           // normalized?
0,                  // stride
(void*)0            // array buffer offset
);
}

GLuint cloudID;

vector<glm::vec3> newCloudVerts;
vector<glm::vec2> newCloudUvs;

GLuint newCloudVAO;
GLuint newCloudVertBuffer;
GLuint newCloudUVBuffer;

void displayClouds()
{
glGenFramebuffers(1, &cloudFramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, cloudFramebufferName);

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

glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, cloudRenderedTexture, 0);

GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers);

glBindFramebuffer(GL_FRAMEBUFFER, cloudFramebufferName);
glViewport(0,0,1024,1024);

if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
log("Error with framebuffer.");
}

GLuint cloudProgramID = LoadShaders( "cloudVert.glsl", "cloudFrag.glsl" );
glUseProgram(cloudProgramID);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDisable(GL_CULL_FACE);

transform(0,0,0);
glBindVertexArray(cloudVAO);
glDrawArrays(GL_TRIANGLES, 0, cloudVerts.size() );

glEnable(GL_CULL_FACE);

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0,screenx,screeny);
glUseProgram(programID);

newCloudVerts.empty();
newCloudUvs.empty();

glm::vec3 tmp1(0,cloudHeight,0);
newCloudVerts.push_back(tmp1);
glm::vec3 tmp2(0,cloudHeight,1024);
newCloudVerts.push_back(tmp2);
glm::vec3 tmp3(1024,cloudHeight,1024);
newCloudVerts.push_back(tmp3);

glm::vec3 tmp4(0,cloudHeight,0);
newCloudVerts.push_back(tmp4);
glm::vec3 tmp5(1024,cloudHeight,0);
newCloudVerts.push_back(tmp5);
glm::vec3 tmp6(1024,cloudHeight,1024);
newCloudVerts.push_back(tmp6);

glm::vec2 uv1(0,0);
newCloudUvs.push_back(uv1);
glm::vec2 uv2(0,1);
newCloudUvs.push_back(uv2);
glm::vec2 uv3(1,1);
newCloudUvs.push_back(uv3);

glm::vec2 uv4(0,0);
newCloudUvs.push_back(uv4);
glm::vec2 uv5(1,0);
newCloudUvs.push_back(uv5);
glm::vec2 uv6(1,1);
newCloudUvs.push_back(uv6);

glGenVertexArrays(1,&newCloudVAO);
glBindVertexArray(newCloudVAO);

glGenBuffers(1, &newCloudVertBuffer);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, newCloudVertBuffer);
glBufferData(GL_ARRAY_BUFFER, newCloudVerts.size() * sizeof(glm::vec3), &newCloudVerts[0], GL_STATIC_DRAW);
glVertexAttribPointer(
0,                  // attribute
3,                  // size
GL_FLOAT,           // type
GL_FALSE,           // normalized?
0,                  // stride
(void*)0            // array buffer offset
);

glGenBuffers(1, &newCloudUVBuffer);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, newCloudUVBuffer);
glBufferData(GL_ARRAY_BUFFER, newCloudUvs.size() * sizeof(float), &newCloudUvs[0], GL_STATIC_DRAW);
glVertexAttribPointer(
1,                  // attribute
2,                  // size
GL_FLOAT,           // type
GL_FALSE,           // normalized?
0,                  // stride
(void*)0            // array buffer offset
);
}

void showClouds()
{
glDisable(GL_CULL_FACE);
transform(0,0,0);
glBindVertexArray(newCloudVAO);
glDrawArrays(GL_TRIANGLES, 0, newCloudVerts.size() );
glEnable(GL_CULL_FACE);
}

cloudFrag.glsl:
#version 330 core

in float cloudColor;
out vec4 color;

void main()
{
color = vec4(1,1,1,cloudColor);
}

cloudVert.glsl:
#version 330 core

layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 6) in float vertexCloudTrans;

out float cloudColor;

void main()
{
gl_Position = vec4(vertexPosition_modelspace,1);
cloudColor = vertexCloudTrans;
}

No error as far as I know, it's just not putting anything on the texture.
I don't know what I'm doing.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS