Sign in to follow this  
vpk

OpenGL objects on terrain

Recommended Posts

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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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


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
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
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  

  • Announcements

  • Forum Statistics

    • Total Topics
      628360
    • Total Posts
      2982261
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • 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.
  • Popular Now