Advertisement

How to make a calculator in c++ with imgui OpenGL

Started by September 01, 2020 09:33 PM
14 comments, last by SyncViews 4 years, 3 months ago

SuperVGA said:
Dear ImGui is complicated stuff.

SuperVGA said:
then look into OpenGL

I agree with you. Topic starter should to write calculator in pure OpenGL. The most difficult thing in OpenGL is to making “picking of objects”. But it is no such difficult as it seams. I found the solution in the book: WebGL Programming Guide

Playground: https://plnkr.co/edit/hGGHKCo4UidasNirSPJF?p=preview​

// If pressed position is inside <canvas>, check if it is above object
let x_in_canvas = x - rect.left, y_in_canvas = rect.bottom - y;
 
gl.uniform1i(uClickedLocation, 1);  // Pass true to u_Clicked
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);     // Clear buffers
gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_BYTE, 0);   // Draw
 
// Read pixel at the clicked position
let pixels = new Uint8Array(4); // Array for storing the pixel value
gl.readPixels(x_in_canvas, y_in_canvas, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
 
if (pixels[0] == 255) // The mouse in on cube if R(pixels[0]) is 255
{
    Output.Instance.Print("Picked");
}
else
{
    Output.Instance.Print("No");
}
 
gl.uniform1i(uClickedLocation, 0);  // Pass false to u_Clicked(rewrite the cube)
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);     // Clear buffers
gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_BYTE, 0);   // Draw

@8observer8 You could also use an ortographic projection spanning the width and height of the frame, and then look into an internal representation, rather than reading the pixels back. I think that would be simpler. But it's cool to see you moving around and working with all kinds of languages either way!

Advertisement

@8Observer8 YOU GUYS really don't understand. I have no idea on how to make a display box and area. and render multiple buttons.

Shaanveer said:

@8Observer8 YOU GUYS really don't understand. I have no idea on how to make a display box and area. and render multiple buttons.

@shaanveer As for programming and graphics programming, what is your level? If I asked you to do the most "impressive" piece of software here, from scratch, with no help, what would you make? What would your program be able to do?

We don't get the impression that you can do much, yet you keep on with these vague questions about somewhat advanced areas. So we need to know what you can do.

Shaanveer said:

@8Observer8 YOU GUYS really don't understand. I have no idea on how to make a display box and area. and render multiple buttons.

Build and run the imgui example program (maybe this one for OpenGL https://github.com/ocornut/imgui/blob/master/examples/imgui_impl_opengl3.cpp​ )

Observe that it has many buttons etc. Then read and understand this for how they did those controls: https://github.com/ocornut/imgui/blob/master/imgui_demo.cpp#L224

And all the docs and functions you can call in this for what the options are: https://github.com/ocornut/imgui/blob/master/imgui.h

Your not going to find videos or copy-paste for a lot of this stuff. Using `ImGui::Text` to display some output and `ImGui::Button` multiple times for a number of buttons is really basic for the audience this sort of library is targeting. They might show a few of the controls as an example, but not full solutions or how exactly each of the things works.

If you have some specific piece of the demo code you don't understand, or a piece of your own code that doesn't work sure ask about that, with a runnable code example (e.g. dropped into one of the ImGui demo Main.cpp files).

This topic is closed to new replies.

Advertisement