PDA

View Full Version : Visualize an object with the coordinate system tied to it



iris23
03-16-2016, 05:12 AM
Hi,

I am completely new to opengl so I would really appreciate any help I can get.

So, I'm trying to visualize a terrain ( obtained using a height matrix ) with the coordinate system tied to it. The reason for this is to be able to see the scale of the terrain in the real world.

I am not sure how to go about doing this, so could anyone point me toward a tutorial or something that could help.

thank you in advance

Carmine
03-16-2016, 05:46 PM
I'm trying to visualize a terrain ... One step at a time.
Can you display the terrain data without any indication of scale yet?
Can you post a picture?

iris23
03-17-2016, 04:55 AM
Thank you for replying, so to visualize the terrain I've followed this tutorial : https://tutorialsplay.com/opengl/lesson-12-terrain-generation-and-rendering/ But when I run the code I get a black screen, I don't see the rendered terrain.

Carmine
03-17-2016, 03:27 PM
Are you able to compile and link successfully?
What IDE are you using - Microsoft Visual C++, CodeBlocks, etc?
I looked at the tutorial. Are you trying it run it as is (i.e. with their terrain data)?
That would be a good first step.
It would help if you posted a simple, cleaned-up version of your code.
Lots of basic things could be going wrong such as placement of data relative to
clipping planes, incorrect specification of projection matrices, etc.

iris23
03-18-2016, 02:54 AM
For the IDE, I'm using codeBlocks. For the tutorial, yes I'm trying to run it using their data and it compiles but when I run it I get a black screen.
Here's the code: (I tried to post the whole code but I kept getting the message " post denied, new users are limited...")


int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
/* register window class */
.....

/* create main window */
......


int M[8][8] ={
{ 0, 0, 0, 0,0,0,0,0,0 },
{ 1, 1, 1, 1,1,1,1,1,1 },
{ 1, 3, 2, 3,3,4,2,1,1 },
{ 1, 2, 1, 2,2,2,2,1,1 },
{ 1, 2, 1, 2,2,2,2,1,1 },
{ 1, 2, 1, 2,2,2,2,1,1 },
{ 1, 2, 2, 3,3,3,3,2,1 },
{ 1, 2, 2, 3,4,4,3,3,1 },
{ 1, 2, 1, 1,1,1,1,1,1 },
{ 0, 1, 1, 1,1,1,1,1,1 } };
/* enable OpenGL for the window */
EnableOpenGL(hwnd, &hDC, &hRC);

/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);

int x;
int z;
int MAP_SIZE = 10;


glPushMatrix();
for ( x = 1; x < MAP_SIZE-1; x++) {
for (z = 1; z < MAP_SIZE-1; z++) {
glBegin(GL_QUADS);
glVertex3f( x, M[x][z] , z );
glVertex3f( x+1, M[x+1][z] , z );
glVertex3f( x+1, M[x+1][z+1], z + 1);
glVertex3f( x, M[x][z+1] , z + 1);
glEnd();
}
}
glPopMatrix();


SwapBuffers(hDC);

theta += 1.0f;
Sleep (1);
}
}

/* shutdown OpenGL */
DisableOpenGL(hwnd, hDC, hRC);

/* destroy the window explicitly */
DestroyWindow(hwnd);

return msg.wParam;
}

Carmine
03-18-2016, 04:35 PM
You realize that the tutorial you are following is incomplete, right?
There's stuff left out because they've been covered in previous tutorials.
For example, that tutorial AND your code never set up a projection matrix.
Clipping planes are established when defining projections.
Do you know where your Z clipping planes are?
If not, it's likely that the data you're trying to display is outside the clipping volume.

iris23
03-19-2016, 06:09 PM
For example, that tutorial AND your code never set up a projection matrix.

Thanks for replying, yes you're right I didn't set up a projection matrix. I did now but I still don't know how to choose the right values. For example if I do this :

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0, 1.0, 3.0, 7.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);

This the output I get : (I'm not allowed to post images for some reason) I get a small white triangle at the upper right corner of the screen but not the whole terrain.

Carmine
03-20-2016, 12:10 AM
The Z coordinates of the terrain data in the tutorial run from 1 to 10.
You've put the camera at z = 5.0, right in the middle of the data field.
Not surprising you don't see much.

I try to avoid using gluLookAt because it's unintuitive.
But if you want to use it, try substituting 50.0 for 5.0.
Also, change the Z values in gluPerspective from [3.0, 7.0] to [1.0, 100.0].

iris23
03-20-2016, 03:27 PM
Hi, I tried the values you suggested but I got a black screen, then I tried : gluPerspective( 150 , 1 , 5 , 100 ) ; and gluLookAt (0,0,-1 , 0,0,0 ,0,1,0) ; and now I can see the terrain but it's really small and I still don't know how to make the x,y,z axes show up tied to it.