Sign in to follow this  

OpenGL Rendering to texture. (Efficent dynamic clouds)

This topic is 1577 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
}
Fragment shader:
#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.png
Clouds from below.
clouds_from_above.png
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Similar Content

    • By xhcao
      Does sync be needed to read texture content after access texture image in compute shader?
      My simple code is as below,
      glUseProgram(program.get());
      glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
      glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
      glDispatchCompute(1, 1, 1);
      // Does sync be needed here?
      glUseProgram(0);
      glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
      glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
      glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
       
      Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?
    • By Jonathan2006
      My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:
      // This works quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); quat quaternion2 = GEMultiplyQuaternions(quaternion1, GEQuaternionFromAngleRadians(angleRadiansVector2)); quat quaternion3 = GEMultiplyQuaternions(quaternion2, GEQuaternionFromAngleRadians(angleRadiansVector3)); glMultMatrixf(GEMat4FromQuaternion(quaternion3).array); // The first two work fine but not the third. Why? quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); vec3 vector1 = GETransformQuaternionAndVector(quaternion1, angularVelocity1); quat quaternion2 = GEQuaternionFromAngleRadians(angleRadiansVector2); vec3 vector2 = GETransformQuaternionAndVector(quaternion2, angularVelocity2); // This doesn't work //quat quaternion3 = GEQuaternionFromAngleRadians(angleRadiansVector3); //vec3 vector3 = GETransformQuaternionAndVector(quaternion3, angularVelocity3); vec3 angleVelocity = GEAddVectors(vector1, vector2); // Does not work: vec3 angleVelocity = GEAddVectors(vector1, GEAddVectors(vector2, vector3)); static vec3 angleRadiansVector; vec3 angularAcceleration = GESetVector(0.0, 0.0, 0.0); // Sending it through one angular velocity later in my motion engine angleVelocity = GEAddVectors(angleVelocity, GEMultiplyVectorAndScalar(angularAcceleration, timeStep)); angleRadiansVector = GEAddVectors(angleRadiansVector, GEMultiplyVectorAndScalar(angleVelocity, timeStep)); glMultMatrixf(GEMat4FromEulerAngle(angleRadiansVector).array); Also how do I combine multiple angularAcceleration variables? Is there an easier way to transform the angular values?
    • By dpadam450
      I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.
       
      #define NUM_LIGHTS 2
      struct Light
      {
          vec3 position;
          vec3 diffuse;
          float attenuation;
      };
      uniform Light Lights[NUM_LIGHTS];
       
       
    • By pr033r
      Hello,
      I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things. 
      I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help. 
      My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
      Exe file (if you want to look) and models folder (for those who will download the sources):
      http://leteckaposta.cz/367190436
      Thanks for any help...

    • By Andrija
      I am currently trying to implement shadow mapping into my project , but although i can render my depth map to the screen and it looks okay , when i sample it with shadowCoords there is no shadow.
      Here is my light space matrix calculation
      mat4x4 lightViewMatrix; vec3 sun_pos = {SUN_OFFSET * the_sun->direction[0], SUN_OFFSET * the_sun->direction[1], SUN_OFFSET * the_sun->direction[2]}; mat4x4_look_at(lightViewMatrix,sun_pos,player->pos,up); mat4x4_mul(lightSpaceMatrix,lightProjMatrix,lightViewMatrix); I will tweak the values for the size and frustum of the shadow map, but for now i just want to draw shadows around the player position
      the_sun->direction is a normalized vector so i multiply it by a constant to get the position.
      player->pos is the camera position in world space
      the light projection matrix is calculated like this:
      mat4x4_ortho(lightProjMatrix,-SHADOW_FAR,SHADOW_FAR,-SHADOW_FAR,SHADOW_FAR,NEAR,SHADOW_FAR); Shadow vertex shader:
      uniform mat4 light_space_matrix; void main() { gl_Position = light_space_matrix * transfMatrix * vec4(position, 1.0f); } Shadow fragment shader:
      out float fragDepth; void main() { fragDepth = gl_FragCoord.z; } I am using deferred rendering so i have all my world positions in the g_positions buffer
      My shadow calculation in the deferred fragment shader:
      float get_shadow_fac(vec4 light_space_pos) { vec3 shadow_coords = light_space_pos.xyz / light_space_pos.w; shadow_coords = shadow_coords * 0.5 + 0.5; float closest_depth = texture(shadow_map, shadow_coords.xy).r; float current_depth = shadow_coords.z; float shadow_fac = 1.0; if(closest_depth < current_depth) shadow_fac = 0.5; return shadow_fac; } I call the function like this:
      get_shadow_fac(light_space_matrix * vec4(position,1.0)); Where position is the value i got from sampling the g_position buffer
      Here is my depth texture (i know it will produce low quality shadows but i just want to get it working for now):
      sorry because of the compression , the black smudges are trees ... https://i.stack.imgur.com/T43aK.jpg
      EDIT: Depth texture attachment:
      glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT24,fbo->width,fbo->height,0,GL_DEPTH_COMPONENT,GL_FLOAT,NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, fbo->depthTexture, 0);
  • Popular Now