Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Drawing Terrain

  1. #1
    Senior Member Regular Contributor
    Join Date
    May 2002
    Posts
    189

    Drawing Terrain

    Ok for my upcoming paintball game's terrain engine I've ran into a problem. The problem is I read a raw image file into memory, specifically an array of unsigned chars. Now how should I go about drawing it? I wanto be able to draw it with line strips, triangle strips, and texture it depending on what mode the user selects. Thank you

  2. #2
    Senior Member Regular Contributor
    Join Date
    May 2002
    Posts
    189

    Re: Drawing Terrain

    No ideas? Perhaps if I ellaborate some more:

    I'm loading my terrain for my game from a grayscale raw file. I want the grayscale raw file to indicate how high the corresponding spot of terrain is. For example if a pixel is completely black then the terrain at that spot will be very low. If the pixel is completely white then the terrain at that spot will be very high. So, how do I draw it?

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: Drawing Terrain

    If you are reading in a raw file, then just turn that data into vertex data.

    You read in the first and second line of data to create your tiangle strips.

    Then move the second line data to the fist line and read in aother line until all lines have been processed.


    Originally posted by 31337:
    No ideas? Perhaps if I ellaborate some more:

    I'm loading my terrain for my game from a grayscale raw file. I want the grayscale raw file to indicate how high the corresponding spot of terrain is. For example if a pixel is completely black then the terrain at that spot will be very low. If the pixel is completely white then the terrain at that spot will be very high. So, how do I draw it?

  4. #4
    Senior Member Regular Contributor
    Join Date
    May 2002
    Posts
    189

    Re: Drawing Terrain

    Ok now after I've debugged my reading code I'm not really getting the results I like. I don't think its reading the raw file correctly because the array gets the same value for all the slots. But after reading your insight nexusone I think I can draw it after I get it reading correctly. Here's what my reading code looks like (somewhat):

    Code :
    // dynamic file io.cpp : Defines the entry point for the console application.
    //
     
    #include "stdafx.h"
    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>
    #include <stdio.h>
     
    int main(int argc, char* argv[])
    {
    	if (FILE *inputfile = fopen("test.raw", "rb")) {
    		char c;
    		int howLarge = 0;
    		int counter = 0;
    		unsigned char *data;
    		while(c = fgetc(inputfile) != EOF) {
    			howLarge++;
    		}
    		data = new unsigned char [howLarge];
     
    		fclose(inputfile);
    		FILE *inputfileAGAIN = fopen("test.raw", "rb");
     
    		while(c = fgetc(inputfileAGAIN) != EOF) {
    			data[counter] = c;
    			counter++;
    		}
    		fclose(inputfileAGAIN);
    		for(int i = 0; i<howLarge; i++) {
    			if(int(data[i])!=1) {
    			cout << int(data[i]) << " ";
    			}
    		}
    	}
     
    	return 0;
    }

  5. #5
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    California, USA
    Posts
    167

    Re: Drawing Terrain

    .raw format is simply rgbrgbrgb over and over
    where each value is in the range [0,255]? If thats the case, then you have to make c an unsigned char or any value you try to store in c greater than 127 will become garbage.

    Also, you dont have to close and reopen the file, just fseek(inputfile,0,SEEK_SET) to return to the beginning of the file.

    Old GLman

  6. #6
    Senior Member Regular Contributor
    Join Date
    May 2002
    Posts
    189

    Re: Drawing Terrain

    sweet! thanks a lot I'll add in the fseek thing and try changing c to being unsigned.

    I should have known that! doh!

  7. #7
    Senior Member Regular Contributor
    Join Date
    May 2002
    Posts
    189

    Re: Drawing Terrain

    ok I made the changes you suggested however it seems that everything still equals 1. This is not good seeing as how the raw file is an image created using the clouds filter in photoshop to produce grays throughout the picture. Any other ideas? Thanks GL Man you rock

  8. #8
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    California, USA
    Posts
    167

    Re: Drawing Terrain

    Hey, I think its a problem with using fgetc. Do something like this instead:
    Code :
    	if (FILE *inputfile = fopen("test.raw", "rb")) {		
    		unsigned char c;
    		int howLarge = 0;
    		int counter = 0;
    		unsigned char *data;
     
    		while(c = fgetc(inputfile) != EOF) {
    			howLarge++;		
    		}
     
    		data = new unsigned char [howLarge];
     
    		fseek(inputfile,0,SEEK_SET);
     
    		fread(data,1,howLarge,inputfile);
     
    		fclose(inputfile);
     
    		for(int i = 0; i<howLarge; i++) {
    				printf("%i ",data[i]);
    		}	
    	}	
    	return 0;
    Old GLman

  9. #9
    Senior Member Regular Contributor
    Join Date
    May 2002
    Posts
    189

    Re: Drawing Terrain

    wahoo that worked, but can you explain why? Thanks

  10. #10
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    California, USA
    Posts
    167

    Re: Drawing Terrain

    Im not sure how fgetc reads from the file. fread works better though because you can read the whole thing in at once, plus you can specify how many elements to read in, and what size they are, in this case 1, or sizeof(unsigned char).

    Old GLman

Page 1 of 2 12 LastLast

Similar Threads

  1. Drawing water over terrain
    By Oldanko in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 06-05-2015, 07:37 PM
  2. Terrain Generation / Displaying terrain
    By _Syndic in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 05-06-2013, 04:29 AM
  3. Using VBOs for drawing terrain with LOD (SOLVED)
    By pixelita in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-24-2011, 02:21 AM
  4. Drawing roads and rivers onto a 3D terrain
    By lehmanne in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 10-09-2003, 09:35 AM
  5. Terrain drawing: Ideal quadtree size?
    By Structural in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-29-2003, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean