youpi1

Members
  • Content count

    8
  • Joined

  • Last visited

Community Reputation

124 Neutral

About youpi1

  • Rank
    Newbie
  1. Thanks for your remarks, I think where the issue comes from :   I have in the source code above (GLWidget.cpp) : extern const char *vertexShader; GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) {   m_particleRadius = 0.6f;   m_pointScale = 100.0f;   w_width = 600;   w_height = 600;   g_nearPlane = 0.1f;   g_farPlane = 1000.0f; ... } void GLWidget::draw() {   // Boolean for initializing the first display of the scene if (isDisplayFirst) { isDisplayFirst = false;   // Create VBO createVBO();   // Reset line scale value // lineScaleValue = abs(Galaxy->pos[3]-Galaxy->pos[0])*3; lineScaleValue = 100.0f;   // Initialize View glViewport(0, 0, w_width, w_height);   glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glLoadIdentity(); // Reset The Projection Matrix   // perspective good initial gluPerspective(45.0f, (float) w_width/w_height, g_nearPlane, g_farPlane);   glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrixi   // I would like to remove the two lines below and put directly // gluLookAt (0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // This way, the camera would be put at z = 3 / 0.03 = 100   gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef(0.03f, 0.03f, 0.03f);   }   rotateScene();   glClearColor(0.0 ,0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   glEnable(GL_POINT_SPRITE); glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE); glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);   glEnable(GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE);   glUseProgram(mprogram); glUniform1f( glGetUniformLocation(mprogram, "pointRadius"), m_particleRadius ); glUniform1f( glGetUniformLocation(mprogram, "pointScale"),m_pointScale);   // Drawing particles ... // see the following into above code snippet } void GLWidget::initializeGL() {   LoadGLTextures();  // load the textures.   glClearColor(0.0 ,0.0, 0.0, 0.0);   glMatrixMode(GL_PROJECTION);   glLoadIdentity();   mprogram = compileProgram(vertexShader);   glClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE); } with vertexShader is defined into shaders.cpp : #define STRINGIFY(A) #A // vertex shader const char *vertexShader = STRINGIFY( uniform float pointRadius;  // point size in world space uniform float pointScale;   // scale to calculate size in pixels void main() {     // calculate window-space point size     vec3 posEye = vec3(gl_ModelViewMatrix * vec4(gl_Vertex.xyz, 1.0));     float dist = length(posEye);     gl_PointSize = pointRadius * (pointScale / dist);         gl_TexCoord[0] = gl_MultiTexCoord0;     gl_Position = gl_ModelViewProjectionMatrix * vec4(gl_Vertex.xyz, 1.0);     gl_FrontColor = gl_Color; } ); If I replace gl_PointSize = pointRadius * (pointScale / dist); by gl_PointSize = 20.0 then the particles are displayed but their size doesn't increase when I zoom the scene.   Could you explain how to keep the original formula (gl_PointSize = pointRadius * (pointScale / dist);) and in the same time having scaling when zooming ?   it seems that variable float dist = length(posEye); is too high in this formula, so the size of the particles are too small to be visible.   How to circumvent this issue knowing I have the following values for particle radius and pointScale : m_particleRadius = 0.6f; m_pointScale = 100.0f; Thanks
  2. w_width = 600; w_height = 600; g_nearPlane = 0.1f; g_farPlane = 1000.0f;   void GLWidget::draw() {   // Boolean for initializing the first display of the scene if (isDisplayFirst) { isDisplayFirst = false;   // Create VBO createVBO();   // Reset line scale value // lineScaleValue = abs(Galaxy->pos[3]-Galaxy->pos[0])*3; lineScaleValue = 100.0f;   // Initialize View glViewport(0, 0, w_width, w_height);   glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glLoadIdentity(); // Reset The Projection Matrix   // perspective good initial gluPerspective(45.0f, (float) w_width/w_height, g_nearPlane, g_farPlane);   glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrixi   // I would like to remove the two lines below and put directly // gluLookAt (0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // This way, the camera would be put at z = 3 / 0.03 = 100   gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef(0.03f, 0.03f, 0.03f);   }   rotateScene();   glClearColor(0.0 ,0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   glEnable(GL_POINT_SPRITE); glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE); glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);   glEnable(GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE);   glUseProgram(mprogram); glUniform1f( glGetUniformLocation(mprogram, "pointRadius"), m_particleRadius ); glUniform1f( glGetUniformLocation(mprogram, "pointScale"),m_pointScale);   // Drawing particles if (not hideClassicMatter) { GLuint vbo_disk = 0;   glBindBuffer(GL_ARRAY_BUFFER, vbo_disk); glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), Galaxy->pos); glEnableClientState(GL_VERTEX_ARRAY);   glColor4f(1.0f, 1.0f, 1.0f, 0.2f); glDrawArrays(GL_POINTS, 0, Galaxy->getNumParticles_disk());   glBindBuffer(GL_ARRAY_BUFFER, 0); glDisableClientState(GL_VERTEX_ARRAY); }   // Drawing particles if (not hideDarkMatter) { GLuint vbo_halo = 0;   glBindBuffer(GL_ARRAY_BUFFER, vbo_halo); glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), &Galaxy->pos[4*Galaxy->getNumParticles_disk()]); glEnableClientState(GL_VERTEX_ARRAY);   glColor4f(0.0f, 0.0f, 1.0f, 0.2f); glDrawArrays(GL_POINTS, 0, Galaxy->getNumParticles_halo());   glBindBuffer(GL_ARRAY_BUFFER, 0); glDisableClientState(GL_VERTEX_ARRAY); }   glDisable(GL_BLEND);         glDisable(GL_POINT_SPRITE); } So instead of having : gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef(0.03f, 0.03f, 0.03f); I would have : gluLookAt (0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); But with this replacement, I have strange results, it seems that a lot of particles are hidden or have disappeared. To illustrate this, I show you on the two figures below this problem; The first one corresponds to the original code (using gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef(0.03f, 0.03f, 0.03f):       The second one corresponds to the replacement case ( using only gluLookAt (0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)) :       As you can see, a lot of particles seem to have disappeared.   Maybe the problem is about a wrong near or far values but I have set : g_nearPlane = 0.1f; g_farPlane = 1000.0f; From your experience, What this issue is related to ? Thanks
  3.     So when I do :   gluPerspective(45.0f, (float)w_width / w_height, g_nearPlane, g_farPlane);" gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);   I can't say that the width and height are equals to : width = height = 2 *tan(45) * distance ;  with distance = 3 ?
  4. I am not sure I have all understood. There are 2 things : - the line I draw is done in "headupdisplay" function by : //Setup for 2D glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0, w_width, w_height, 0, -1, 1); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); // begin draw scale line glDisable(GL_TEXTURE_2D); glLineWidth(2.0f); glBegin(GL_LINES); glVertex2d(350, 12); glVertex2d(450, 12); glVertex2d(350, 9); glVertex2d(350, 15); glVertex2d(450, 9); glVertex2d(450, 15); glEnd(); glEnable(GL_TEXTURE_2D); // end draw glMatrixMode( GL_PROJECTION ); glPopMatrix(); glMatrixMode( GL_MODELVIEW ); glPopMatrix(); // end for 2D So, this line has 100 pixel length. - I want the value right to this line to represent the distance of the foreground plane, i.e the 2D projection of the 3D scene. For example, I show you the result when I zoom (the value equals to 45.8 kpc) : [attachment=13275:test3.png] Now another picture with the same zoom but with also a rotation by mouse, this is a view by side of the galaxy : [attachment=13276:test4.png] wintertime, you say this distance is difficult to calculate correctly with perspective projection, could you give me some clue ? I just want to get a value corresponding to the foreground objects, i.e the 2D projection in (xy) plane of the 3D scene without taking into account of the z coordinates. That's why you advise me to use Ortho projection ? Thanks
  5. I don't understand why I have to use Ortho projection, Perspective allows me to make diplay  particles smaller/bigger when they get far away/closer (with zoom out/in that I have implemented) while Ortho is for 2D stuff.
  6. I try to find the good initial value at the start of simulation. I took on the following image a value equal to 100 kpc (kpc is the unity used in my code for the positions of each particles)
  7. Thanks a lot mhagain, It works ! I understand better now. Actually, I was confusing between array passed to glVertexPointer and glTexCoordPointer. Your code shows that we have to compute the coordinates of each quad vertex and this FOR each body. I thought that we would just need the "raw" positions of each body (containing in the "pos" array) without doing the shift. So now, I am going to increase the performance of display by using texured objects with VBO. Maybe I will bore you again on this forum
  8. I am working on a n-body code with "glut functions" display. I would like to display each body with a 2D texture from a bmp image (a star). The coordinates of each body (x, y, z) are in the "pos" array. I try to use glTexCoordPointer with the following display function : [CODE] void drawPoints() { glEnable(GL_TEXTURE_2D); // Enable texture mapping. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glBindTexture(GL_TEXTURE_2D, texture[0]); // pick the texture. glLoadIdentity(); // reset the view before we draw each star. glTranslatef(0.0f, 0.0f, zoom); // zoom into the screen. glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), pos); glTexCoordPointer(4, GL_DOUBLE, 4*sizeof(double), pos); glColor4ub(30, 100, 120, 255); glBegin(GL_QUADS); // Begin Drawing The Textured Quad glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glEnd(); // Done Drawing The Textured Quad glDrawArrays(GL_POINTS, 0, numBodies); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glutSwapBuffers(); } [/CODE] before this, I call the following function for loading the 2D texture : [CODE] // Load Bitmaps And Convert To Textures void LoadGLTextures(void) { // Load Texture Image *image1; // allocate space for texture image1 = (Image *) malloc(sizeof(Image)); if (image1 == NULL) { printf("Error allocating space for image"); exit(0); } if (!ImageLoad("Data/Star.bmp", image1)) { exit(1); } // Create Textures glGenTextures(3, &texture[0]); // linear filtered texture glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data); }; [/CODE] My main problem is that only one textured element is displayed at the center of the window (x=0, y=0) ( see figure in attachment). It seems that I don't use correctly the function [b]glTexCoordPointer [/b]. In the above drawPoints() function, I call it with : [CODE]glTexCoordPointer(4, GL_DOUBLE, 4*sizeof(double), pos);[/CODE] where pos is the array of coordinates of each body (x, y, z). I found another solution for plotting all the bodies with glTranslate in a loop on all the elements : [CODE] for(int i=0;i<numBodies;i++) { glPushMatrix(); glTranslatef(pos[i],pos[i+1],pos[i+2]); glColor4ub(30, 100, 120, 255); glBegin(GL_QUADS); // Begin Drawing The Textured Quad glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glEnd(); // Done Drawing The Textured Quad glPopMatrix(); } [/CODE] You can see the expected result on the second image in attachment ( N_Bodies.png). but I would like to avoid this solution and find a way to draw all the bodies at once (with glDrawArrays) Any idea ?