# OpenGL Surface plot for any data

## Recommended Posts

Hi all, I need to draw surface plot using OpenGL for the following data. X-Axis | Y-Axis | Z-Axis ========================= Jan-08 | 120.0 | 25.5 Feb-08 | 125.7 | 24.15 Mar-08 | 100.0 | 42.11 Apr-08 | 190.0 | 15.62 May-08 | 109.0 | 21.88 Jun-08 | 150.5 | 56.55 Jul-08 | 191.8 | 89.93 Aug-08 | 177.7 | 15.65 I searched in the web and found some samples all of which draw a sin function or exp functional data. But I need the surface plot for the above data. So, I am not able to figure out. Can anybody tell me an idea to do this. Thanks in advance, Regards, Srinu

##### Share on other sites
I don't have any experience with surface plots but AFAIK you need a function that takes two arguments to plot it as a 3D surface (with the arguments generally representing the X- and Z-Axis).

Your data just resembles a bunch of points, so the most natural way would be to connect them to a line (converting the months to numbers, like Jan = 1). If you need a surface you might draw a quad/two triangles between two adjacent datasets (where each dataset has 2 points - one specified by X,Y,Z and one with one coordinate set to 0), e.g. for Jan and Feb you could use the points:

X/Y/Z
1/120.0/0
1/120.0/25.5
2/125.7/0
2/125.7/24.15

Hope that helps.

##### Share on other sites
Hi Lord,
Thanks for the response. Your idea sounds like a good one. I am going to implement it to see whether it works. But, I am stuck at converting these coordinates to OpenGL coordinates. Could you please tell me how to do this?

Thanks,
Srinu

##### Share on other sites
I'm not sure where you're stuck, but you could just convert the months to January = 0.0, February = 1.0, March = 2.0 and so on (or use another scale, if you like). Then you alreay have the needed coordinates and just need to call glVertex3f(x,y,z) or - which would be better - put the coordinates into a VBO and render that.

##### Share on other sites
Thats ok. But OpenGL coords should be between -1 to 1, right? So, these coords must be converted to their equivalent opengl coords.
For example Take a 3d point - 1.0, 120.0, 25.5. We can not render this point in OpenGL window. It must be converted to OpenGL window coords. I am asking how to do this convertion?

Thanks,
Srinu

##### Share on other sites
For a 2 dimensional plot, you could make the vertical axis the Y & Z variables and the horizontal the X (time), then plot the data as vectors instead of points. Or you could do something more interesting, like use the Z data to be the diameter of a circle with the X variables on the horizontal and y be the vertical. Then if you scale the axes properly, you'd have circles of diameter Z centered at height Y and the proper time X which should be spaced not to overlap. Or you could represent one of the fields as color (X makes the most sense, having exact and regular values to easily correspond to a given color). I would look at some differential equation or vector field graphs to see techniques people use for graphic 3 dimensional data in 2 dimensional graphs if thats what you want, or you could make the graph 3D if you really want.

##### Share on other sites
Quote:
 Original post by srinivaz111Thats ok. But OpenGL coords should be between -1 to 1, right? So, these coords must be converted to their equivalent opengl coords. For example Take a 3d point - 1.0, 120.0, 25.5. We can not render this point in OpenGL window. It must be converted to OpenGL window coords. I am asking how to do this convertion?Thanks,Srinu

No, you use whatever coordinates you want and OpenGL will internally scale it.

##### Share on other sites
You can use any (reasonable) number for a OpenGL coordinate. However, to get it drawn to screen you have to setup your modelview and projection matrices properly.

If you don't want to do that you'd have to convert the coordinates to range [-1,1] for x and y and to [0,1] for z. This shouldn't be too hard (e.g. if months go from 0 to 11 you can set x to month * (2.0f/11.0f) - 1.0f).

However, I'd recommend using the matrices as they would allow you to use different viewing angles.

##### Share on other sites
not able to draw the plot using the method where I took 2 points for every coordinate. I got a weird graph. I am following the Contour plot method which involves defining a texture and drawing triangles and setting the texture to those triangles. Can anybody who knows the contour plot method please help me?
I am sending the code also. Please look into it.

This is the actual data for which I have to plot the graph

X-Axis | Y-Axis | Z-Axis
=========================
Jan-08 | 120.0 | 25.5
Feb-08 | 125.7 | 24.15
Mar-08 | 100.0 | 42.11
Apr-08 | 190.0 | 15.62
May-08 | 109.0 | 21.88
Jun-08 | 150.5 | 56.55
Jul-08 | 191.8 | 89.93
Aug-08 | 177.7 | 15.65

So, I have taken the following points table

float data[24][3] =
{
{1.0,120.0,0},
{1.0,120.0,0.5},
{1.0,120.0,25.5},
{2.0,125.7,0},
{2.0,125.7,0.5},
{2.0,125.7,24.15},
{3.0,100.0,0},
{3.0,100.0,0.5},
{3.0,100.0,42.11},
{4.0,190.0,0},
{4.0,190.0,0.5},
{4.0,190.0,15.62},
{5.0,109.0,0},
{5.0,109.0,0.5},
{5.0,109.0,21.88},
{6.0,150.5,0},
{6.0,150.5,0.5},
{6.0,150.5,56.55},
{7.0,191.8,0},
{7.0,191.8,0.5},
{7.0,191.8,89.93},
{8.0,177.7,0},
{8.0,177.7,0.5},
{8.0,177.7,15.65}

};

Here is the code:

void MyGraph::Generate3DData()
{
m_fWidth = 1.0f;
m_fStep = 0.05f;
int nSurface = 1;
int nVertex = nSurface + 1;

v3DVector tempVector;

for (int x=0; x<24; x+=3)
{
double ogl_x, ogl_y, ogl_z;
GetOpenGLCoords(data[x][0], data[x][1], data[x][2], &ogl_x, &ogl_y, &ogl_z);
tempVector.push_back(C3DVector(ogl_x, ogl_y, ogl_z));

GetOpenGLCoords(data[x+1][0], data[x+1][1], data[x+1][2], &ogl_x, &ogl_y, &ogl_z);
tempVector.push_back(C3DVector(ogl_x, ogl_y, ogl_z));

GetOpenGLCoords(data[x+2][0], data[x+2][1], data[x+2][2], &ogl_x, &ogl_y, &ogl_z);
tempVector.push_back(C3DVector(ogl_x, ogl_y, ogl_z));

m_vVertexNormal.push_back(tempVector);

tempVector.clear();
}

tempVector.clear();

/* int size = m_vVertexNormal.size();
std::vector<v3DVector>::iterator it;
for (it=m_vVertexNormal.begin(); it!=m_vVertexNormal.end(); it++)
{
v3DVector tVector = *it;
int vsize = tVector.size();
}
*/

// Surface Normal
for(int nX = 0; nX < 7; nX ++)
{
for(int nY(0); nY < 2; nY ++)
{
C3DVector temp3DVector(0, 0,0);
C3DVector point1 = m_vVertexNormal.at(nX).at(nY);
C3DVector point2 = m_vVertexNormal.at(nX+1).at(nY);
C3DVector point3 = m_vVertexNormal.at(nX+1).at(nY+1);

CalculateNormal(point1, point2, point3, temp3DVector);

tempVector.push_back( temp3DVector );
}
m_vSurfaceNormal.push_back(tempVector);
tempVector.clear();
}

// Vertex Normal
for(nX = 0; nX < 7; nX ++)
for(int nY(0); nY < 2; nY ++)
{
m_vVertexNormal.at(nX).at(nY) += m_vSurfaceNormal.at(nX).at(nY);
m_vVertexNormal.at(nX+1).at(nY) += m_vSurfaceNormal.at(nX).at(nY);
m_vVertexNormal.at(nX).at(nY+1) += m_vSurfaceNormal.at(nX).at(nY);
m_vVertexNormal.at(nX+1).at(nY+1) += m_vSurfaceNormal.at(nX).at(nY);
}
for(nX = 0; nX < 8; nX ++)
for(int nY(0); nY < 3; nY ++)
m_vVertexNormal.at(nX).at(nY).Average();
}

BOOL MyGraph::CalculateNormal(C3DVector& fPoint1, C3DVector& fPoint2, C3DVector& fPoint3, C3DVector& fNormal)
{
float fx = (fPoint2.GetY()-fPoint1.GetY())*(fPoint3.GetZ()-fPoint2.GetZ())-
(fPoint3.GetY()-fPoint2.GetY())*(fPoint2.GetZ()-fPoint1.GetZ());

// (y2-y1)(z3-z2)-(y3-y2)(z2-z1)

float fy = (fPoint3.GetX()-fPoint2.GetX())*(fPoint2.GetZ()-fPoint1.GetZ())-
(fPoint2.GetX()-fPoint1.GetX())*(fPoint3.GetZ()-fPoint2.GetZ());

float fz = (fPoint2.GetX()-fPoint1.GetX())*(fPoint3.GetY()-fPoint2.GetY())-
(fPoint3.GetX()-fPoint2.GetX())*(fPoint2.GetY()-fPoint1.GetY());

float fd = (float) sqrt(fx*fx + fy*fy + fz*fz);

fNormal.SetVector(fx/fd, fy/fd, fz/fd);

return TRUE;
}

void MyGraph::GetOpenGLCoords(float x, float y, float z, double *ogl_x, double *ogl_y, double *ogl_z)
{

*ogl_x = x * (2.0/11.0) - 1.0;
*ogl_y = y * (2.0/191.8) - 1.0;
*ogl_z = z/89.93;
}

void MyGraph::CreateGraph3DList(UINT nList)
{
::glNewList(nList , GL_COMPILE);
for(int x=0, int nX(0); nX < m_vSurfaceNormal.size(); nX++, x+=2)
{
double ogl_x, ogl_y, ogl_z;
GetOpenGLCoords(data[x][0], data[x][1], data[x][2], &ogl_x, &ogl_y, &ogl_z);

float fx = ogl_x; //(nX*m_fStep - 1)*m_fWidth;

::glBegin(GL_TRIANGLE_STRIP);
for(int nY(0); nY < 3/*m_vSurfaceNormal.size()*/; nY++)
{
float fNormal[3];
m_vVertexNormal.at(nX).at(nY).GetVector(fNormal);
::glNormal3fv(fNormal);
float fy = ogl_y;//(nY*m_fStep - 1)*m_fWidth;
float fz = ogl_z;//GetZ(fx, fy);
::glTexCoord1f((fz+1)/2);
::glVertex3d(fx, fy, fz/2);

m_vVertexNormal.at(nX+1).at(nY).GetVector(fNormal);
::glNormal3fv(fNormal);

GetOpenGLCoords(data[x+1][0], data[x+1][1], data[x+1][2], &ogl_x, &ogl_y, &ogl_z);

float fx2 = ogl_x;//fx+m_fStep;
fz = ogl_z;//GetZ(fx2, fy);
::glTexCoord1f((fz+1)/2);
::glVertex3d(fx2, fy, fz/2);
}
::glEnd();
}
::glEndList();
}

Please look into it and show me a way to achieve it.

~Srinu

##### Share on other sites
First, please use the source tags

Quote:
 Original post by srinivaz111So, I have taken the following points tablefloat data[24][3] = { {1.0,120.0,0}, {1.0,120.0,0.5}, {1.0,120.0,25.5}, {2.0,125.7,0}, {2.0,125.7,0.5}, {2.0,125.7,24.15}, {3.0,100.0,0}, {3.0,100.0,0.5}, {3.0,100.0,42.11}, {4.0,190.0,0}, {4.0,190.0,0.5}, {4.0,190.0,15.62}, {5.0,109.0,0}, {5.0,109.0,0.5}, {5.0,109.0,21.88}, {6.0,150.5,0}, {6.0,150.5,0.5}, {6.0,150.5,56.55}, {7.0,191.8,0}, {7.0,191.8,0.5}, {7.0,191.8,89.93}, {8.0,177.7,0}, {8.0,177.7,0.5}, {8.0,177.7,15.65}};

I just skimmed over your code but looking at your data I can make a guess:

You draw the data as a triangle strip, so you get triangles like (in your coords):

{1.0,120.0,0}/{1.0,120.0,0.5}/{1.0,120.0,25.5} -- this basically is a line and wouldn't be rendered (degenerate triangle)
{1.0,120.0,0.5}/{1.0,120.0,25.5}/{2.0,125.7,0}
{1.0,120.0,25.5}/{2.0,125.7,0}/{2.0,125.7,0.5}
{2.0,125.7,0}/{2.0,125.7,0.5}/{2.0,125.7,24.15} -- this is a line, too
...

Why are those {..,..,0.5} point in there? If you remove them, the graph should look better.
If not, I maybe misunderstood you and a screenshot would help me to get a better impression on what your graph looks like.

##### Share on other sites
I have taken 3 points for each coordinate to draw a triangle. (As actually Contour plot drawing involves triangles). But I am really confused whether the 3 points are taken correctly or not?
And I think you understood the code. Please suggest if any changes can be done to the code. And can you please tell me how to attach files to posts (I am trying to send the scrrenshot of the plot I got, But not able to find the way to do that).

Thanks,
Srinu

##### Share on other sites
Well, take out the coords with z=0.5 and it should work with GL_TRIANGLE_STRIP.
If not, there might be another error in the code.

##### Share on other sites
Please look into the following code which draws the surface plot using contour method. Actually this code was written to take data in a random manner. I modified it to take the data from an array. I am not able to find the places where the required changes have to done for my data. Please help me.

//Data:
float data[16][3] =
{
{1.0,120.0,0},
{1.0,120.0,25.5},
{2.0,125.7,0},
{2.0,125.7,24.15},
{3.0,100.0,0},
{3.0,100.0,42.11},
{4.0,190.0,0},
{4.0,190.0,15.62},
{5.0,109.0,0},
{5.0,109.0,21.88},
{6.0,150.5,0},
{6.0,150.5,56.55},
{7.0,191.8,0},
{7.0,191.8,89.93},
{8.0,177.7,0},
{8.0,177.7,15.65}
};

void Generate3DData()
{
int nSurface = 3;
int nVertex = nSurface + 1;

v3DVector tempVector;

int i = 0;

// Vertex coordinates
for(int nX(0); nX < nVertex; nX ++)
{
for(int nY(0); nY < nVertex; nY ++)
{
float fx, fy, fz;
GetOpenGLCoords(data[i][0], data[i][1], data[i][2], &fx, &fy, &fz);
i++;

tempVector.push_back( C3DVector(fx, fy, fz) );
}

m_vVertexNormal.push_back(tempVector);
tempVector.clear();

}

tempVector.clear();

// Surface Normal
for(nX = 0; nX < nSurface; nX ++)
{
for(int nY(0); nY < nSurface; nY ++)
{
C3DVector temp3DVector(0, 0,0);

CalculateNormal(m_vVertexNormal.at(nX).at(nY), m_vVertexNormal.at(nX+1).at(nY), m_vVertexNormal.at(nX+1).at(nY+1), temp3DVector);

tempVector.push_back( temp3DVector );
}
m_vSurfaceNormal.push_back(tempVector);
tempVector.clear();
}

// Vertex Normal
for(nX = 0; nX < nSurface; nX ++)
for(int nY(0); nY < nSurface; nY ++)
{
m_vVertexNormal.at(nX).at(nY) += m_vSurfaceNormal.at(nX).at(nY);
m_vVertexNormal.at(nX+1).at(nY) += m_vSurfaceNormal.at(nX).at(nY);
m_vVertexNormal.at(nX).at(nY+1) += m_vSurfaceNormal.at(nX).at(nY);
m_vVertexNormal.at(nX+1).at(nY+1) += m_vSurfaceNormal.at(nX).at(nY);
}

for(nX = 0; nX < nVertex; nX ++)
for(int nY(0); nY < nVertex; nY ++)
m_vVertexNormal.at(nX).at(nY).Average();

}

void CreateGraph3dList()
{
int i = 0;
::glNewList(nList , GL_COMPILE);
for(int nX(0); nX < m_vSurfaceNormal.size(); nX++)
{
::glBegin(GL_TRIANGLE_STRIP);
for(int nY(0); nY < m_vSurfaceNormal.size(); nY++)
{
float fNormal[3];
m_vVertexNormal.at(nX).at(nY).GetVector(fNormal);
::glNormal3fv(fNormal);

float fx, fy, fz;
GetOpenGLCoords(data[i][0], data[i][1], data[i][2], &fx, &fy, &fz);

::glTexCoord1f((fz+1)/2);
::glVertex3d(fx, fy, fz/2);

m_vVertexNormal.at(nX).at(nY+2).GetVector(fNormal);
::glNormal3fv(fNormal);

GetOpenGLCoords(data[i+2][0], data[i+2][1], data[i+2][2], &fx, &fy, &fz);

::glTexCoord1f((fz+1)/2);
::glVertex3d(fx, fy, fz/2);

i++;
}
::glEnd();
}
::glEndList();
}

void GetOpenGLCoords(float x, float y, float z, float *ogl_x, float *ogl_y, float *ogl_z)
{
*ogl_x = x * (2.0/8.0) - 1.0;
*ogl_y = y * (2.0/191.8) - 1.0;
*ogl_z = z/89.93;
}

##### Share on other sites
The code seems to operate on a quadratic grid, i.e. you have the same number of vertices in x and y.

It then loops over the grid vertices column by column and sets the position according to the converted data. After that it iterates over the grid cells (called surfaces in the code) and calculates the normals per surface and per vertex.

The problem seems to be that your data does not form a quadratic grid, but rather a 8x2 grid.

You need to change the loops to accommodate for that, i.e. in the loops that iterate over the vertices set maxX=8 and maxY=2. For the loops iterating over the surfaces you have to use the same values decremented, i.e. 7/1.

That said, I'd recommend you carefully read the code and try to figure out what it does (especially which data it reads). It would certainly help you if you had some basic knowledge of 3D rendering concepts, but I guess it's more a matter of what data is to be read in your case.

Basically, you have to geometrically layout your data like this (using vertex indices):

1--3--5--7--9--..
|\ |\ |\ |\ |\ |
| \| \| \| \| \|
0--2--4--6--8--..

I assume you're not that fit in OpenGL, so I'll help you with the display list function. The data part will be your excercise [smile].

void CreateGraph3dList()
{
int i = 0;
//create a new display list and store its id in nList
glNewList(nList , GL_COMPILE);

//begin drawing triangles - I don't use triangle strip because I think it's easier for you to understand
glBegin(GL_TRIANGLES);
//loop through the columns, maxCols would be 8 in your case
for(int col(0); col < maxCols; col++)
{
//assuming your data is laid out as described above you can now read the relevant data

//we'll use 2 triangles for each cell
glNormal3f(/*normal for index col * 2*/);
glVertex3f(/*position for index col * 2*/);
glNormal3f(/*normal for index col * 2 + 1*/);
glVertex3f(/*position for index col * 2 + 1*/);
glNormal3f(/*normal for index (col + 1) * 2*/);
glVertex3f(/*position for index (col + 1) * 2*/);

glNormal3f(/*normal for index col * 2 + 1*/);
glVertex3f(/*position for index col * 2 + 1*/);
glNormal3f(/*normal for index (col + 1) * 2*/);
glVertex3f(/*position for index (col + 1) * 2*/);
glNormal3f(/*normal for index (col + 1) * 2 + 1*/);
glVertex3f(/*position for index (col + 1) * 2 + 1*/);

}
//end triangle drawing
glEnd();
//end display list definition
glEndList();
}

Another side note: reading the code I'm not sure whether the normal data is correctly set. I didn't check the normal calculation and thus assume it to be correct, but the normal is then added to the position, which doesn't seem correct (but I'm no expert in plots, so I'm not sure what exactly you want - maybe an image of what you'd expect would be helpful).

##### Share on other sites
Hi Lord,
Thank you for the explanation and the code.
I am trying to integrate this code in my application. But I think there has to be still more to do with this code. Actually I am trying to plot a surface from the X, Y and Z values which looks like the plot http://www.scribd.com/doc/17716693/Surface-Plot .
This plot is drawn for the values z = sin(3*x)*cos(3*y). So, Could you please tell me how to use this code to modify so that it draws a surface plot for my data.

Thanks,
Srinu

##### Share on other sites
Well, the plot in the link represents the value of z for x/y-pairs. Since z is a function of x and y you can calculate z at any point you want and thus get a value for every point on the grid.

The problem with your data is that it just represents some specific points in 3D, like so:

y o o
| o o o
| o
|
|
|
-----------------------x

Thus the first thing is to decide how to map all the other x/y pairs.

One solution (that we talked about above) would be to map the exact x values (1.0, 2.0, ...) and y-values from 0 to the value you provided to the z-value in your table. This would result in something like this:

_
y / \__
| /| | |\ /|
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
-----------------------x

when seen from above (the z-value would represent different depths and would be interpolated between two months).

We already discussed most of the code you need to get this done. Take your data array and the draw method I posted above.

Keep in mind that in OpenGL y is up, so you might want to swap y and z (if z should represent the height).

Since lighting seems to be beyond your skill for now, you should stick to something easier, e.g. simple colors. Replace the glNormal3f(...) calls with glColor3f(...) and pass in a color that you calculate somehow (I guess you want to use z and/or y). Keep in mind that every channel has to be in range [0,1] so you might for example use shades of red and set r = z-value at the specified index / 100.0 . Or use something more complex (e.g. to use other colors, too). That's something I leave you to play with.

##### Share on other sites
Thanks Lord,
I implemented the procedure you explained. Its working fine. The plot is drawn correctly. But it has very sharp curves. Can it be made smoother like the one which I sent to you (Sinusoidal graph).

##### Share on other sites
Well, to smooth it you'd need more data or some algorithm to fit a curve onto your data.
Finding an algorithm is not that trivial and depends on what that data really means. One approach could be a moving average of the z values.

Then you'd need to subdivide the mesh by adding additional steps on x and/or y (e.g. month 1.0, 1.2, 1.4 etc.) and calculate the corresponding z for the new x/y pairs (for example by using the algorithm you used for the curve).

##### Share on other sites
Hi Lord,
Thanks a lot for the help you provided me so far. I am able to draw the plot correctly.
I am having a problem with colors. That is, I am drawing X, Y and Z axis(lines) with red, green and blue colors. But all these lines are being drawn with a single color. I mean whatever color I set using glColor(), the color is not getting set.
But it is getting set If i dont call the function glEnable(GL_TEXTURE_1D). Is there constraint as the color can not be set if we use textures?
I am suing the following gl initilizations.

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_DEPTH_TEST);
glDrawBuffer(GL_BACK);

// Setup light 0
GLfloat fLightAmbient0[] = { 0.7f, 0.0f, 0.1f, 0.5f };
GLfloat fLightDiffuse0[] = { 0.1f, 0.3f, 0.7f, 0.5f };
GLfloat fLightPosition0[] = { 10.0f, 10.0f, 10.0f, 1.0f };

glLightfv(GL_LIGHT0, GL_AMBIENT, fLightAmbient0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, fLightDiffuse0);
glLightfv(GL_LIGHT0, GL_POSITION, fLightPosition0);

// Enable light 1
::glEnable(GL_LIGHT0);

// Setup light 1
GLfloat fLightAmbient1[] = { 0.1f, 0.3f, 0.5f, 0.5f };
GLfloat fLightDiffuse1[] = { 0.3f, 0.0f, 0.1f, 0.5f };
GLfloat fLightPosition1[] = { -10.0f, 5.0f, -1.0f, 1.0f };

::glLightfv(GL_LIGHT1, GL_AMBIENT, fLightAmbient1);
::glLightfv(GL_LIGHT1, GL_DIFFUSE, fLightDiffuse1);
::glLightfv(GL_LIGHT1, GL_POSITION, fLightPosition1);

// Enable light 1
::glEnable(GL_LIGHT1);

// Enable lighting
::glEnable(GL_LIGHTING);

// Enable 1D texture
::glEnable(GL_TEXTURE_1D);

// Setup material
GLfloat materialSpecular[] = { 0.9f, 0.9f, 0.9f, 0.5f };
GLfloat materialAmbDiff[] = { 0.6f, 0.6f, 0.6f, 0.5f };
GLfloat materialShininess[1] = { 20.0f };
::glMaterialfv( GL_FRONT, GL_SPECULAR, materialSpecular);
::glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE,
materialAmbDiff);
::glMaterialfv( GL_FRONT, GL_SHININESS, materialShininess);

The axis drawing code

void DrawAxis()
{
// draw carthesian axes
glBegin(GL_LINES);

// red x axis
glColor3f(1.0f,0.0f,0.f);

glVertex3f(m_XMin,m_YMin,m_ZMin);
glVertex3f(m_XMax,m_YMin,m_ZMin);

// Draw 'X'
glVertex3f(0.2f,-0.9f,m_ZMin);
glVertex3f(0.23f,-0.96f,m_ZMin);
glVertex3f(0.2f,-0.96f,m_ZMin);
glVertex3f(0.23f,-0.9f,m_ZMin);

// green y axis
glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(m_XMin,m_YMin,m_ZMin);
glVertex3f(m_XMin,m_YMax,m_ZMin);

// Draw 'Y'
glVertex3f(-0.9f, 0.0f, m_ZMin);
glVertex3f(-0.87f, -0.04f, m_ZMin);
glVertex3f(-0.84f, 0.0f, m_ZMin);
glVertex3f(-0.87f, -0.04f, m_ZMin);
glVertex3f(-0.87f, -0.04f, m_ZMin);
glVertex3f(-0.87f, -0.08f, m_ZMin);

// blue z axis
glColor3f(0.f,0.f,1.f);
glVertex3f(m_XMin,m_YMin,m_ZMin);
glVertex3f(m_XMin,m_YMin,m_ZMax);

// Draw 'Z'
glVertex3f(m_XMin,-0.5f,0.2f);
glVertex3f(m_XMin,-0.5f,0.16f);
glVertex3f(m_XMin,-0.5f,0.16f);
glVertex3f(m_XMin,-0.55f,0.2f);
glVertex3f(m_XMin,-0.55f,0.2f);
glVertex3f(m_XMin,-0.55f,0.16f);
glEnd();
}

Is there any mistake I am doing? Please guide me.

Thanks for the help.

Regards,
Srinu

##### Share on other sites
When you enable texturing the color will be modulated with the texture color by default.
I assume you don't use a texture for the plot and thus when the driver tried to read texels from the "texture" returns black. Now your colors are multiplied with black, which results in ... black [smile].

If you want to use a texture, e.g. for having a gradient, you have to bind a texture (which should be grey scale if you want to use vertex colors, too) and provide texture coords for your vertices.

If you don't want to use textures, just disable texturing when drawing your axes.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628278
• Total Posts
2981785
• ### Similar Content

• By mellinoe
Hi all,
First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
• By aejt
I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
I have these classes:
For GPU resources:
Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).
And my plan is to define everything into these XML documents to abstract away files:
Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
Factory classes for resources:
For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
Factory classes for assets:
Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).

Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
Thanks!
• By nedondev
I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
Thanks.

• So I've recently started learning some GLSL and now I'm toying with a POM shader. I'm trying to optimize it and notice that it starts having issues at high texture sizes, especially with self-shadowing.
Now I know POM is expensive either way, but would pulling the heightmap out of the normalmap alpha channel and in it's own 8bit texture make doing all those dozens of texture fetches more cheap? Or is everything in the cache aligned to 32bit anyway? I haven't implemented texture compression yet, I think that would help? But regardless, should there be a performance boost from decoupling the heightmap? I could also keep it in a lower resolution than the normalmap if that would improve performance.
Any help is much appreciated, please keep in mind I'm somewhat of a newbie. Thanks!

• Hi,
I'm trying to learn OpenGL through a website and have proceeded until this page of it. The output is a simple triangle. The problem is the complexity.
I have read that page several times and tried to analyse the code but I haven't understood the code properly and completely yet. This is the code: