-1

I am building an application using OpenGL and c++, to display a transparent object in the screen with multiple lights around to and simulate the different possible reflection from the transparent object. Then I will take a screenshot at a fixed camera position and moving the object randomly to simulate the reflection and save it as a BMP file in a local folder along with a text file with the same file name.

Next, I need to get the object position in the window coordinate[x, y] and save it in my output text file.

Now the problem for me is to get the object's location as in the window coordinate.For eg. The created window is 1920 X 1080. I need the objects center location in the same format as [x * y].

This is my Main cpp file

int RandGenerator()
{

std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_real_distribution<double> distribution(-3.0, 3.0);
double number = distribution(generator);    

std::this_thread::sleep_for(std::chrono::milliseconds(100));

return number;

}

int RandGenerator_1()
{

std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_real_distribution<double> distribution(-3.0, 3.0); 
double number1 = distribution(generator);
std::this_thread::sleep_for(std::chrono::milliseconds(100));

return number1;

}


int main()
{
mainWindow = Window(1920, 1080);    
mainWindow.initialise();

CreateObjects();
CreateShaders();

camera = Camera(glm::vec3(0.0f, 0.0f, 7.0f), glm::vec3(0.0f, 1.0f, 0.0f), 
-90.0f, 0.0f, 5.0f, 0.2f);

shinyMaterial = Material(4.0f, 156);
dullMaterial = Material(0.5f, 4);

groundfloor = Model();
groundfloor.LoadModel("res/models/blender/Floor.obj");  

blackhawk = Model();
blackhawk.LoadModel("res/models/blender/cup.obj");

cup2 = Model();
cup2.LoadModel("res/models/blender/cup.obj");

mainLight = DirectionalLight(1.0f, 1.0f, 1.0f, 
                            0.1f, 0.1f, 
                            0.5f, -1.0f, 20.0f);

unsigned int pointLightCount = 0;

pointLights[0] = PointLight(0.0f, 0.0f,1.0f,
                            1.0f, 1.0f,
                            3.0f, 0.0f, 1.0f,
                            1.0f, 0.2f, 0.1f);

pointLightCount++;

pointLights[1] = PointLight(1.0f, 1.0f, 1.0f,
                            1.0f, 1.0f,
                            -3.0f, 0.0f, 1.0f,
                            1.0f, 0.2f, 0.1f);

pointLightCount++;  


GLuint uniformProjection = 0, uniformModel = 0, uniformView = 0, 
   uniformEyePosition = 0,
       uniformSpecularIntensity =0,uniformShininess = 0;
glm::mat4 projection = glm::perspective(45.0f, 
 (GLfloat)mainWindow.getBufferWidth() / 
 (GLfloat)mainWindow.getBufferHeight(), 
 0.1f, 100.0f); 

//Main game Loop 
while (!mainWindow.getShouldClose()) 
{
    GLfloat now = glfwGetTime();
    deltaTime = now - lastTime;
    lastTime = now;

    // Handle user inputs and events
    glfwPollEvents();

    camera.keyControl(mainWindow.getKeys(), deltaTime);
    camera.mouseControl(mainWindow.getXchange(), mainWindow.getYchange());

    //clear the window
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    shaderList[0].UseShader();
    uniformProjection = shaderList[0].GetProjectionLocation();
    uniformModel = shaderList[0].GetModelLocation();
    uniformView = shaderList[0].GetViewLocation();
    uniformEyePosition = shaderList[0].GetEyePosition();
    uniformSpecularIntensity = shaderList[0].GetSpecularIntensityLoc();
    uniformShininess = shaderList[0].GetShininessLoc();

    glm::vec3 lowerLight = camera.GetCameraPosition();
    lowerLight.y -= 0.3f;

    glm::vec3 camerayaw = camera.GetCameraDirection();

    glm::vec3 dlightPos = mainLight.GetLightPos();      

    shaderList[0].SetDirectionalLight(&mainLight);
    shaderList[0].SetPointLights(pointLights, pointLightCount);
    shaderList[0].SetSpotLights(spotLights, spotLightCount);        

    glUniformMatrix4fv(uniformProjection, 1, GL_FALSE, 
    glm::value_ptr(projection));
    glUniformMatrix4fv(uniformView, 1, GL_FALSE, 
    glm::value_ptr(camera.calulateViewMatrix()));
    glUniform3f(uniformEyePosition, camera.GetCameraPosition().x, 
    camera.GetCameraPosition().y, camera.GetCameraPosition().z);        

    glEnable(GL_DEPTH_TEST);

    model = glm::mat4(1.0f);
    model = glm::translate(model, glm::vec3(0.0f, 0.0f, -0.46f));
    model = glm::scale(model, glm::vec3(0.5f, 0.5f, 0.5f));
    model = glm::rotate(model, 90.0f*toRad, glm::vec3(1.0f, 0.0f, 0.0f));
    glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
    dullMaterial.UseMaterial(uniformSpecularIntensity, uniformShininess);
    groundfloor.RenderModel();              

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glCullFace(GL_BACK);
    glEnable(GL_CULL_FACE);
    glDepthMask(GL_FALSE);

    glm::vec3 translation(RandGenerator(), RandGenerator_1(), 0.0f);

    model = glm::mat4(1.0f);
    model = glm::translate(model, translation);
    model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f));
    viewmat = camera.calulateViewMatrix();
    glm::mat4 model_view = projection * viewmat * model;        
    glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
    shinyMaterial.UseMaterial(uniformSpecularIntensity, uniformShininess);
    blackhawk.RenderModel();

    glDisable(GL_CULL_FACE);
    glDisable(GL_BLEND);        
    glDepthMask(GL_TRUE);


    glUseProgram(0);

    mainWindow.swapBuffers();
}

return 0;}

This is my Vertex Shader file

#version 330

layout (location =0) in vec3 pos;
layout (location =1) in vec2 tex;
layout (location =2) in vec3 normal;

out vec4 vCol;
out vec2 TexCoords;
out vec3 NormalValue;
out vec3 FragPos;


uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform vec3 clipSpacePos;

void main()
{
gl_Position = projection * view *  model* vec4(pos, 1.0f);  

vCol = vec4(clamp(pos, 0.0f, 1.0f), 0.2f);

TexCoords = tex;

NormalValue = mat3(transpose(inverse(model))) * normal;

FragPos = (model* vec4(pos, 1.0f)).xyz;
} 

Please forgive me if the question is very basic, I am a beginner in OpenGL as well as Programming. Thanks in advance, Output Image:

img

1 Answers1

0

To obtain screen coordinates from your scene coordinates you need to apply all transform matrices on it and perform perspective division.

You did not provide any sample code so is unclear if you are using old API or the new one ... and also what matrices (or whatever else) and multiplication order you got...

See these two related QAs:

for the math stuff how to do it.

Now what kind of reflection and transparency?

  1. For the real deal you need ray-tracing rendering

    for example something like this:

    but those are usually time expensive...

  2. For the cheap fake you can use Blending + environment cube map techniques

    The transparency can be done like this:

    for the reflection you just add environment cube map. That means you have a skybox (of your environment) in a GL_CUBE_MAP_TEXTURE and compute reflected ray from surface normal and direction to camera... and add a corresponding cube map texel to the resulting color ... Its also doable in old style API but much easier is to use shaders instead.

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Thank you for the reply @Spektre, I am using modern OpenGL 4.5 API for my application and regarding the reflection and transparency, I am using the second method which you mentioned. using GL_BLEND. I have achieved the desired result as far as my model is considered. But I am stuck with retrieving the coordinates of the object with respect to the screen coordinate. – Prashanth Sridharan Oct 21 '19 at 07:48
  • As you mentioned I tried to calculate the perspective division vec3 from the clipping view in the shader program. `vec4 matrix = projection * view * model* vec4(pos, 1.0f); vec3 matrixndc = matrix.xyz / matrix.w; gl_Position = matrix;` But I am not sure how to get the value back to CPU to calculate with the window resolution. – Prashanth Sridharan Oct 21 '19 at 14:24
  • @PrashanthSridharan no you need to do the whole calculation on CPU side using your objects center or major vertex or whatever ... – Spektre Oct 21 '19 at 15:07