Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9009295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:09:08+00:00 2026-06-16T02:09:08+00:00

I am trying to display a mathematical surface f(x,y) defined on a XY regular

  • 0

I am trying to display a mathematical surface f(x,y) defined on a XY regular mesh using OpenGL and C++ in an effective manner:

struct XYRegularSurface {
    double x0, y0;
    double dx, dy;
    int    nx, ny;
    XYRegularSurface(int nx_, int ny_) : nx(nx_), ny(ny_) {
       z = new float[nx*ny];
    }
    ~XYRegularSurface() {
       delete [] z;
    }
    float& operator()(int ix, int iy) {
       return z[ix*ny + iy];
    }

    float x(int ix, int iy) {
       return x0 + ix*dx;
    }
    float y(int ix, int iy) {
       return y0 + iy*dy;
    }
    float zmin(); 
    float zmax(); 
    float* z; 
 };

Here is my OpenGL paint code so far:

void color(QColor & col) {
    float r = col.red()/255.0f;
    float g = col.green()/255.0f;
    float b = col.blue()/255.0f;
    glColor3f(r,g,b);
}

void paintGL_XYRegularSurface(XYRegularSurface &surface, float zmin, float zmax) {
    float x, y, z;

    QColor col;
    glBegin(GL_QUADS);
    for(int ix = 0; ix < surface.nx - 1; ix++) {
        for(int iy = 0; iy < surface.ny - 1; iy++) {
            x = surface.x(ix,iy);
            y = surface.y(ix,iy);
            z = surface(ix,iy);
            col = rainbow(zmin, zmax, z);color(col);
            glVertex3f(x, y, z);

            x = surface.x(ix + 1, iy);
            y = surface.y(ix + 1, iy);
            z = surface(ix + 1,iy);
            col = rainbow(zmin, zmax, z);color(col);
            glVertex3f(x, y, z);

            x = surface.x(ix + 1, iy + 1);
            y = surface.y(ix + 1, iy + 1);
            z = surface(ix + 1,iy + 1);
            col = rainbow(zmin, zmax, z);color(col);
            glVertex3f(x, y, z);

            x = surface.x(ix, iy + 1);
            y = surface.y(ix, iy + 1);
            z = surface(ix,iy + 1);
            col = rainbow(zmin, zmax, z);color(col);
            glVertex3f(x, y, z);

        }
    }
    glEnd();
}

The problem is that this is slow, nx=ny=1000 and fps ~= 1.
How do I optimize this to be faster?

EDIT: following your suggestion (thanks!) regarding VBO
I added:

float* XYRegularSurface::xyz() {
    float* data = new float[3*nx*ny];
    long i = 0;
    for(int ix = 0; ix < nx; ix++) {
        for(int iy = 0; iy < ny; iy++) {
            data[i++] = x(ix,iy);
            data[i++] = y(ix,iy);
            data[i] = z[i]; i++;
        }
    }
    return data;
}

I think I understand how I can create a VBO, initialize it to xyz() and send it to the GPU in one go, but how do I use the VBO when drawing. I understand that this can either be done in the vertex shader or by glDrawElements? I assume the latter is easier? If so: I do not see any QUAD mode in the documentation for glDrawElements!?

Edit2:
So I can loop trough all nx*ny quads and draw each by:

GL_UNSIGNED_INT indices[4];
// ... set indices
glDrawElements(GL_QUADS, 1, GL_UNSIGNED_INT, indices);

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:09:10+00:00Added an answer on June 16, 2026 at 2:09 am

    Well, you’ve asked a rather open ended question. I’d suggest using modern (3.0+) OpenGL for everything. The point of just about any new OpenGL feature is to provide a faster way to do things. Like everyone else is suggesting, use array (vertex) buffer objects and vertex array objects. Use an element array (index) buffer object too. Most GPUs have a ‘post-transform cache’, which stores the last few transformed vertices, but this can only be used when you call the glDraw*Elements family of functions. I also suggest you store a flat mesh in your VBO, where y=0 for each vertex. Sample the y from a heightmap texture in your vertex shader. If you do this, whenever the surface changes you will only need to update the heightmap texture, which is easier than updating the VBO. Use one of the floating point or integer texture formats for a heightmap, so you aren’t restricted to having your values be between 0 and 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to display current time with PHP (using this ): $date = date('m/d/Y h:i:s
I´m trying to display a list of div with images using the repeater field
I'm trying to display text in my OpenGL / JOGL app. Here is some
im trying to display date and time from MSSQL Server datetime table using PHP,
I'm trying to display the less-than sign using MathJax to render MathML input... currently
I am trying to display a hidden div in colorbox using an <a> link's
I am trying to display multiple markers using JSON with geocoding. My code shows
im trying to display things inline using css that should be right next to
im trying to display the picture saved in the database using Php, but what
Im trying to display images from a directory uploaded_files Source : using the code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.