Sign in to follow this  
vpk

OpenGL objects on terrain

Recommended Posts

vpk    100
hi, i have created a terrain using c and opengl. now i wish to place some objects like a tree or cattle on the terrain. how do i do it? can it be done using opengl??

Share this post


Link to post
Share on other sites
Wyrframe    2426
glBegin(GL_TRIANGLES);
drawCastle();
glEnd();


Okay, seriously. If you ask an open-ended question like this, you have to expect some pretty open-ended answers.

So. I'd suggest you make a castle or tree in a 3D modeller, like Blender or Milkshape 3D, both freeware and with either very simple file formats or very easy to make custom exporters for. In fact, Blender has a 3DS exporter, and I'm sure there's a half-dozen free 3DS readers out there which produce a model object with built-in OpenGL drawing routines.

Alternatively, you can just draw your castle using hard-coded GL primitives. You know, yourself, what a castle looks like. So make a drawWall() method that draws a wall from (0,0,0) to (1,0,0) and tile or rotate a few of those to make the walls. Make a drawTower() method that draws a tower from (0,0,0) to (0,0,1), and scale a few of those to the size you want. I suggest using a display list or vertex buffer, storing your entire castle into it, and then drawing it with a single batched call later on (after you get your slow prototype working, of course).

Share this post


Link to post
Share on other sites
vpk    100
sorry for my query being insufficient..

i have a glb model of the object (cattle/man/woman)and a terrain.
what i wanted to know is how do i make model appear on the terrain.

any suggestions..

Share this post


Link to post
Share on other sites
vpk    100
Quote:
Original post by Hodgman
Which part are you having problems with - loading the glb model, rendering the loaded model, or positioning the model at the correct place in the world?


actually i havent yet started with any of these..so i need help regarding all the parts..

Share this post


Link to post
Share on other sites
vpk    100
instead of the model i would like to place an image eg. tree.jpg/tree.bmp on the terrain.
can anyone tell me how this can be done.
or else suggest me some site/tutorial for the same.

Share this post


Link to post
Share on other sites
Wyrframe    2426
Look up billboards and impostors.

I'm not familiar with GLB files, but you could always try wotsit, a huge repository of information about open and proprietary file formats: http://www.wotsit.org/

As far as getting around to the rendering, if you're drawing a terrain you've honestly already got 3/4s of what you need to know to draw the trees and other objects on it down pat.

Share this post


Link to post
Share on other sites
vpk    100
hi

thats exactly what i want to know. how do i render the tree which is an image jpg/bmp on to the terrain..

first i load the image from the file,
then how do i render it??

Share this post


Link to post
Share on other sites
Hodgman    51334
Quote:
Original post by vpk
Quote:
Original post by Wyrframe
Quote:
Original post by vpk
i would like to place an image eg. tree.jpg/tree.bmp on the terrain.
Look up billboards and impostors.
how do i render the tree which is an image jpg/bmp on to the terrain..
Billboards @ Flipcode.
Billboards @ NeHe.

Share this post


Link to post
Share on other sites
vpk    100
i have gone thru the sites..

i dont want to rotate the tree , i just want to place it on the terrain after loading it..

Share this post


Link to post
Share on other sites
FeanorLobelia    122
I don't know much about opengl, but logic dictates that if you can render a terrain - you can render other objects like a tree. Export a textured quad that looks like a tree, possibly with some alpha blending - then render it in the same way you rendered your terrain.

The rotation is neccessary if you want the tree to always face the camera to trick the viewer into thinking it's 3d - i.e. billboarding.

Share this post


Link to post
Share on other sites
vpk    100
i understand what u are saying, but..

see i have a tga file with the image of a tree, now i want to render it on the terrain. i know i have to first load the file and then render it..

but i am not able to do it..

can anyone suggest a simple code for doing the same??

with anticipation
regards

Share this post


Link to post
Share on other sites
Shirakana2    500
Does the word "texture" mean something for you ? If no you should start to learn it first, and how to use it (cf. nehe tutorial about texture in OpenGL). Then your problem is solved: drawing trees as billboards is just drawing a quad and applying your tree texture on it.

Share this post


Link to post
Share on other sites
vpk    100
i know you all are experts in the domain on this forum, but please understand that i am just a beginner..so sorry if u feel i am asking nonsensical questions.

i have the image of a tree do i still have to draw a quad??

can u please post a simple code wherein i load the file,render it to the terrain??

Share this post


Link to post
Share on other sites
Gage64    1235
Create a quad with the texture of the tree. This will allow you to draw the tree as a quad.

Now, there are two problems here:

First, the quad is flat, so if you look at it sideways, the 3D illusion will be ruined. To fix this, you have to make sure that the quad always faces the camera, and this will make it look like a 3D tree (well sort of). This is called billboarding and is covered in the articles linked earlier.

Second, you need the tree to be placed on the terrain. You can decide on the tree's x and z position, but the y position will be determined by the height of the terrain at that coordinate, so the tree's world position would be:

(x, terrainHeightAt(x, z), z)

So given an (x, z) pair, you need to know the terrain's height at that location. You can find a good explanation of that here.

If this doesn't answer your question, you'll need to be more specific.

Share this post


Link to post
Share on other sites
vpk    100
i understand i have to create a quad for the tree, but how do i do it??

in case of terrain i had the height values which i used to display the heightmap..
but in case of tree i have a image file, how do i do the tree??

pleaseee give me an example..

Share this post


Link to post
Share on other sites
Gage64    1235
I don't use OpenGL so I don't know it well enough to write the code, but there are some video tutorials here that you might want to go over. Look at their outline to see what's covered (you'll probably find tutorial 11 especially interesting).

Of course, doing a google search will turn up many more tutorials.

Share this post


Link to post
Share on other sites
Wyrframe    2426
If you've drawn the terrain already, you either know how to create quads and/or triangles, or else you've just copy/pasted some source and don't understand it.

Read the tutorials linked-to above; you'll see both the why and the how explained in them.

Share this post


Link to post
Share on other sites
vpk    100
Quote:
Original post by Wyrframe
If you've drawn the terrain already, you either know how to create quads and/or triangles, or else you've just copy/pasted some source and don't understand it.



are you angry with me for asking questions?? i have no experience in this hence i am asking you questions..

ok, i have gone thru some of the links but what i understand is that they tell how to texture the whole object after reading a single bmp file.

thats fine, i understood.

but whats happening with me is that the terrain has one texture and i am trying to put a tree with another texture.there are 2 files. when i read and load the texture, the texture of the terrain gets changed and the tree is not displayed.


Share this post


Link to post
Share on other sites
Gage64    1235
Quote:
Original post by vpk
whats happening with me is that the terrain has one texture and i am trying to put a tree with another texture.there are 2 files. when i read and load the texture, the texture of the terrain gets changed and the tree is not displayed.


You need to set the texture for each object before drawing it, so your code should look something like this:


setTexture(terrainTexture);
drawTerrain();
setTexture(treeTexture);
drawQuad();

Share this post


Link to post
Share on other sites
Shirakana2    500
If you read nehe tuttorial about texture the you know how to load a file and associate it with a unique texture Id. Then you just have to switch from one texture to another before drawing your objects, like Gage64 said.

BTW drawing a quad in OpenGL is something like this:

glMatrixMode (GL_MODELVIEW);
glPushMatrix();
glTranslatef(quadPosition[0],quadPosition[1],quadPosition[2]);
glRotatef(...); // calculate some angles to make it face the camera
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex3f(0.0,0.0,0.0);

glTexCoord2f(1.0,0.0);
glVertex3f(width,0.0,0.0);

glTexCoord2f(1.0,1.0);
glVertex3f(width,height,0.0);

glTexCoord2f(0.0,1.0);
glVertex3f(0.0,height,0.0);
glEnd();
glPopMatrix();

Share this post


Link to post
Share on other sites
vpk    100
i tried doing the same . but the tree appears as a flat texture on the terrain
ie. it should be vertically placed but it gets covered horizontally.

can u give some sample code for the same which is easy to follow, please..

Share this post


Link to post
Share on other sites
vpk    100


void TreeRender()
{
glMatrixMode(GL_MODELVIEW);
int Height;
unsigned char * data1;
FILE * file;

//for texture
file = fopen("tree1.bmp", "rb" );
data1 = (unsigned char *)malloc( 256 * 256 * 3 );
fread( data1, 256 * 256 * 3, 1, file );
fclose( file );

glGenTextures( 1, &texture1 ); //generate the texture with the loaded data
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

//Generate the texture with mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, data1 );
free( data1 );
//till here

//for display
treeList.m_position[0]=absCameraPos.x+1.0;
treeList.m_position[1]=GetY(absCameraPos.x,absCameraPos.z);
treeList.m_position[2]=absCameraPos.z+1.0;
glPushMatrix();
glTranslatef(treeList.m_position[0],treeList.m_position[1],treeList.m_position[2]);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex3f(0.0,0.0,0.0);

glTexCoord2f(1.0,0.0);
glVertex3f(1.0,0.0,0.0);

glTexCoord2f(1.0,1.0);
glVertex3f(1.0,1.0,0.0);

glTexCoord2f(0.0,1.0);
glVertex3f(0.0,1.0,0.0);
glEnd();

glDisable(GL_TEXTURE_2D);
//till here

}


please tell me where i am going wrong??

i am loading the texture for terrain,drawing the terrain then loading texture for tree and then rendering it..

Share this post


Link to post
Share on other sites
Shirakana2    500
Loading and creating texture should be done only once so you'd better move corresponding instructions in an init() function which you call once at the beginning of your program. Then in the main loop you can call your drawing functions. In my example, the coordinates system may differs from your: I use y-axis as the vertical axis this may be why it looks flat in your program. BTW you can use the glRotatef(..) function to make it stand up.

Also don't forget the glPopMatrix(); after drawing the tree.

Share this post


Link to post
Share on other sites
vpk    100
Quote:
Original post by Shirakana2
Loading and creating texture should be done only once so you'd better move corresponding instructions in an init() function which you call once at the beginning of your program. Then in the main loop you can call your drawing functions.


yes, i have done that and now my terrain has its initial texture.thank you

In my example, the coordinates system may differs from your: I use y-axis as the vertical axis this may be why it looks flat in your program. BTW you can use the glRotatef(..) function to make it stand up.

Also don't forget the glPopMatrix(); after drawing the tree.[/quote]

but the tree rendering still has some problem. i am not clear what coordinates do i give in the GL_QUADS. should i specify the terrain vertices??


Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now