[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netCDF #WRU-342191]: NetCDF VIsualization in c++



Hello,

I'm not sure how you'd be able to render the netCDF data using OpenGL; my 
OpenGL is quite a bit out of date, and I'm unfamiliar with VTK.  I know it's an 
open source program, published by KitWare, you might be able take a look at 
their 3D rendering code to see what they do with the data once they've imported 
it from the netCDF storage layer.

Sorry I can't provide more immediate help, hopefully this will point you in the 
right direction,

-Ward

> how  can i render netcdf data to opengl viewport similar to what vtk does
> int ncid;
> int status =
> nc_open("C:/Users/---/Desktop/BOLTgui/BOLT/OR_ABI-L2-ACMF-M6_G18_s20230880050215_e20230880059523_c20230880100259.nc",
> NC_NOWRITE, &ncid);
> 
> if (status != NC_NOERR) {
> std::cout << "Error opening file: " << nc_strerror(status) <<
> std::endl;
> return -1;
> }
> 
> int ndims;
> status = nc_inq_ndims(ncid, &ndims);
> if (status != NC_NOERR) {
> std::cout << "Error inquiring about number of dimensions: " <<
> nc_strerror(status) << std::endl;
> return -1;
> }
> 
> std::cout << "Number of dimensions: " << ndims << std::endl;
> 
> for (int i = 0; i < ndims; i++) {
> char dimname[NC_MAX_NAME + 1];
> size_t dimlen;
> 
> status = nc_inq_dim(ncid, i, dimname, &dimlen);
> if (status != NC_NOERR) {
> std::cout << "Error inquiring about dimension: " <<
> nc_strerror(status) << std::endl;
> return -1;
> }
> 
> std::cout << "Dimension name: " << dimname << ", length: " <<
> dimlen << std::endl;
> }
> 
> status = nc_close(ncid);
> if (status != NC_NOERR) {
> std::cout << "Error closing file: " << nc_strerror(status) <<
> std::endl;
> return -1;
> }
> 
> while (window.isOpen()) {
> sf::Event event;
> while (window.pollEvent(event)) {
> if (event.type == sf::Event::Closed) {
> window.close();
> break;
> }
> 
> else if (event.type == sf::Event::MouseWheelScrolled) {
> zoomLevel += event.mouseWheelScroll.delta * 0.1f;
> }
> else if (event.type == sf::Event::MouseWheelScrolled) {
> zoomLevel += event.mouseWheelScroll.delta * 0.1f;
> }
> if (event.type == sf::Event::MouseButtonPressed) {
> if (event.mouseButton.button == sf::Mouse::Left) {
> bool isOnTopPanel = sf::Mouse::getPosition(window).y <=
> WIDGET_PANEL_HEIGHT;
> bool isOnBottomPanel = sf::Mouse::getPosition(window).y
> >= WINDOW_HEIGHT - WIDGET_PANEL_HEIGHT;
> if (!isOnTopPanel && !isOnBottomPanel) {
> leftButtonPressed = true;
> previousMousePosition =
> sf::Mouse::getPosition(window);
> 
> }
> }
> }
> else if (event.type == sf::Event::MouseButtonReleased) {
> if (event.mouseButton.button == sf::Mouse::Left) {
> leftButtonPressed = false;
> }
> }
> }
> 
> 
> now = std::chrono::system_clock::now();
> currentTime = std::chrono::system_clock::to_time_t(now);
> localtime_s(&localTime, &currentTime);
> 
> dateStream.str(""); // Clear the stream before updating
> dateStream << std::put_time(&localTime, "%m/%d/%Y");
> dateString = dateStream.str();
> text16.setString(dateString);
> 
> timeStream.str(""); // Clear the stream before updating
> timeStream << std::put_time(&localTime, "%I:%M:%S %p");
> timeString = timeStream.str();
> text17.setString(timeString);
> slider.handleEvent(event, window);
> 
> bool isPopupWindowOpen = false;
> sf::Mutex mutex;
> if (event.type == sf::Event::MouseButtonPressed) {
> if (event.mouseButton.button == sf::Mouse::Left) {
> bool isOnTopPanel = sf::Mouse::getPosition(window).y <=
> WIDGET_PANEL_HEIGHT;
> bool isOnBottomPanel = sf::Mouse::getPosition(window).y >=
> WINDOW_HEIGHT - WIDGET_PANEL_HEIGHT;
> if (!isOnTopPanel && !isOnBottomPanel) {
> leftButtonPressed = true;
> previousMousePosition = sf::Mouse::getPosition(window);
> }
> 
> // Convert the mouse position from screen coordinates to
> window coordinates
> sf::Vector2f mousePosition =
> window.mapPixelToCoords(sf::Mouse::getPosition(window));
> 
> // Soundings Popup Window
> sf::FloatRect buttonBounds2(button.getPosition().x,
> button.getPosition().y, button.getSize().x, button.getSize().y);
> if (buttonBounds2.contains(mousePosition.x,
> mousePosition.y))
> {
> sf::Lock lock(mutex);
> if (!isPopupWindowOpen)
> {
> isPopupWindowOpen = true;
> Soundings soundings;
> soundings.run();
> isPopupWindowOpen = false;
> }
> }
> // GOES Popup Window
> sf::FloatRect buttonBounds(button5.getPosition().x,
> button5.getPosition().y, button5.getSize().x, button5.getSize().y);
> if (buttonBounds.contains(mousePosition.x, mousePosition.y))
> {
> sf::Lock lock(mutex);
> if (!isPopupWindowOpen)
> {
> isPopupWindowOpen = true;
> GOES GOES;
> GOES.run();
> isPopupWindowOpen = false;
> }
> }
> }
> }
> 
> 
> // Add the code to fix the earth model from going to the bottom
> left when resizing the window
> // Check if the window size has changed
> if (window.getSize().x != window.getView().getSize().x ||
> window.getSize().y != window.getView().getSize().y)
> {
> // Update the view to the new window size
> glMatrixMode(GL_PROJECTION);
> glLoadIdentity();
> gluPerspective(45.0f, (GLfloat)window.getSize().x /
> (GLfloat)window.getSize().y, 0.1f, 100.0f);
> 
> // Set up the viewport to the new window size
> glViewport(0, 0, window.getSize().x, window.getSize().y);
> }
> 
> 
> glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
> 
> // Set up the modelview matrix and apply the rotation
> glMatrixMode(GL_MODELVIEW);
> glLoadIdentity();
> gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
> glTranslatef(0.0f, 0.0f, -zoomLevel); // Add this line to translate
> in the z-axis
> 
> glRotatef(rotationAngleX, 0.0f, 1.0f, 0.0f); // Rotate around the
> Y-axis
> glRotatef(rotationAngleY, 1.0f, 0.0f, 0.0f); // Rotate around the
> X-axis
> 
> // Enable texture and bind it
> glEnable(GL_TEXTURE_2D);
> glBindTexture(GL_TEXTURE_2D, textureId);
> 
> 
> // Draw the model
> glBegin(GL_TRIANGLES);
> for (size_t i = 0; i < indices.size(); i++) {
> glTexCoord2f(texcoords[2 * i], texcoords[2 * i + 1]);
> glVertex3f(vertices[3 * i], vertices[3 * i + 1], vertices[3 * i
> + 2]);
> }
> if (leftButtonPressed) {
> // Calculate the mouse movement since the last frame
> sf::Vector2i currentMousePosition =
> sf::Mouse::getPosition(window);
> sf::Vector2i mouseMovement = currentMousePosition -
> previousMousePosition;
> previousMousePosition = currentMousePosition;
> 
> // Calculate the rotation angles based on the mouse movement
> rotationAngleX += mouseMovement.x * 0.5f;
> rotationAngleY += mouseMovement.y * 0.5f;
> 
> }
> 
> glEnd();
> 
> // Disable texture
> glDisable(GL_TEXTURE_2D);
> drawShapefileObjects(allObjects);
> 
> glColor3f(1.0f, 1.0f, 1.0f);
> 
> window.resetGLStates();
> 
> glDisable(GL_DEPTH_TEST);
> 
> 


Ticket Details
===================
Ticket ID: WRU-342191
Department: Support netCDF
Priority: Normal
Status: Closed
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata 
inquiry tracking system and then made publicly available through the web.  If 
you do not want to have your interactions made available in this way, you must 
let us know in each email you send to us.