Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create terrain which takes height values from txt file .
During my search I realized that generally the terrains are created from bmp files or another kind of picture taking values from pixels. I read height values from file into a 2d array.
void File(){
FILE* file = fopen("data.txt", "r"); // data.txt opened with read only mode to get heights
int i,j;
for (i = 0; i < 17; i++){
for (j = 0; j < 21; j++){
fscanf(file, "%d", &data[i][j]);
}
}
fclose(file);
}
and then load these values to vertex to create triangle.
But there are triangles everywhere when I change the x y z values .
The intended project is to create a terrain.
Is there a special way or code to create terrain by using just height values from a txt file?
OpenGL renders primitives like triangles and triangle strips. You'll need to convert your terrain heightmap into primitives that OpenGL understands.
Perhaps this tutorial found by a quick google search can help you.
http://www.codeproject.com/Articles/14154/OpenGL-Terrain-Generation-An-Introduction
Depending on how the data looks in the heightmap, say its in the format of vertex/height.
You could read in a line, say its
v 1.7/2.1/3.7 h 10
store off the vertex and translate that vertex up 10 (height).
Repeat for each vertex and connect them with faces.
If it's just height values then you could substitue in arbitrary verticies for each height value.
I.E.
heightmap:
h 10
h 20
h 30
Then as a function:
void generateTerrain(int length, int width)
{
for(;length>= 0; --length)
{
for(; width >= 0; --width)
{
//read line from file
//store the height in a temp hieght variable
//createVertex(length, width, height);
//store this vertex somewhere for later
}
}
}
There are probably way more efficient ways of doing that, but for simple terrain generation that just popped in my head :)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In my fragment shader, I need to use uvec4.
My shader:
layout (location = 0) out uvec4 final_color;
void main(void) {
final_color.r = 0;
final_color.g = 4294967295;
final_color.b = 0;
return;
}
I think, this will be green because unsigned_int max is 4294967295. (2^32-1) but it is just black.
I tried singed int max, 2147483647, it's black too. but 2137483648 works. Where is the boundary?
I want know max value, like 255(8bit) or 1.0f(float, vec4).
my program draw result to window directly. I need to see it.
Well, you can't. A uvec4 contains unsigned, 32-bit integers. That output can only be written to an image that uses an unsigned integer image format. Note that this is different from a normalized integer image format.
You cannot create a default framebuffer that contains non-normalized integers, only floats and normalized integers. So you can't "see it".
As such, that value is not a "color"; it's just data stored in a texture. What it means is entirely up to how you will eventually use the value in that texture to get something displayable.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am recently implement a voxel cone tracing algorithm, the first step is to voxelize polygons...
I am using sparse octree to convert the polygon, everything was fine except for texture mapping
mipmap=6 (64x64x64)
// We find its smallest AABB
AABB aabb = FindAABB(in);
// better aabb with same width, height and depth
AABB betterAABB = FixAABB(aabb);
// Create Octree and marked the grids that are touched by triangles in the mesh
// dfunc is a callable object which return true when triangle and grid are overlapped
// cb is a callable object, it is called when octree reaches its leaf..
Octree<uint8> tree(betterAABB, mipmap);
for(auto &f: in.faces)
tree.findTouchedLeaves(f, dfunc, cb);
Callback function...
struct Callback
{
void operator()(OctreeNode<uint8> *node, const Face &obj)
{
// Check if the node is already marked.
if(!node->data.value)
{
const glm::vec3 ¢er=node->aabb.getCenter();
out.push_back(center);
// Problem here!! how to calculate precise uv for texture mapping
color.push_back(text->getPixel((obj.o.texCoord+obj.a.texCoord+obj.b.texCoord)/3.0f));
}
node->data.value=1;
}
std::vector<glm::vec3> out;
std::vector<glm::vec4> color;
const ImageTexture *text;
} cb;
as you can see... I directly average three UVs of o,a,b.(terrible right?)
It is more imprecise when a triangle is larger than a grid.
Should I need to use rasterize-based algorithm?
My question is: How to precisely coloring a grid?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm a first year engineering student and I'm working on a end of term project. Due to tight deadlines, I would like to avoid rummaging through image processing libraries. We (my group mates) need to find the easiest implementable method to get an integer for the number of dark pixels from an image. I have read many other posts regarding image processing, but they are much more complicated than we need. Is there an easy way to do this? It is important that it is easy because this is only a small part of our project and there can't be too much time committed to this.
As for languages, I would prefer to use C++.
On a side note, any exceptional help given would be cited in our report (just mention the name you want to be cited as and you'll go down in history). It would also give us time to sleep. Sleep is to engineering students what cake is to fat kids.
Here is it done in Qt (not image processing but application library)
#include <QImage>
#include <QColor>
uint countDarkPixels(QString filename, quint8 threshold) {
QImage img(filename);
uint darkPixels = 0;
for (int x = 0; x < img.width(); ++x) {
for (int y = 0; y < img.height(); ++y) {
QColor color(img.pixel(x, y));
if (color.toHsl().lightness() < threshold) darkPixels++;
}
}
return darkPixels;
}
Works for other formats besides JPG too. It uses conversion to HSL which may not be very fast, but you said "easy" not "fast".
There are two stages to this:
Load an image from a file.
Determine how many pixels in that image are "dark".
The first stage isn't too difficult - you could either use a pre-existing library, such as DevIL or FreeImage, or write your own - this and this should be enough to get you started.
Once you've loaded the image into your program somehow, you'll need to loop over the pixel data and count the number of "dark" pixels. Let's say you have an image structure that looks like this:
typedef struct
{
int w;
int h;
unsigned char *data;
} image_s;
For simplicity, let's make the following assumptions:
The image is stored in 24-bit, RGB format, so that each pixel is represented as three unsigned bytes like this: RGBRGBRGB.
A "dark" pixel is one where (R+G+B)/3 < 10
Given the above, you would simply need to loop through each pixel within the image structure like so:
int count_dark_pixels(image_s *img)
{
int dark_pixels, i;
for (dark_pixels = 0, i = 0; i < img->w * img->h; ++i)
{
int r = img->data[(i*3)+0];
int g = img->data[(i*3)+1];
int b = img->data[(i*3)+2];
if ((r+g+b)/3 < 10) { ++dark_pixels; }
}
return dark_pixels;
}
Uncompress the jpeg, get the Y channel pixel data (these values are the luminosity of each pixel), count the dark pixels in that. I don't think you need the U and V channels, these are used to reconstruct the colour information.
Working RGB may be a pain, but it all depends on what you mean by a 'dark' pixel.
JPEG images are usually encoded using the YCbCr color space. Rather than Red, Green, Blue the three components are Darkness, Blueness, and redness. The Y component is then a black and white version of the color image.
You can then determine the darkness of any point by examining the value of the Y component of the image. You can set some threshold to determine a dark pixel.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Guys I a new programmer in C++, please can you help me out in writing a program for drawing a blue colored rectangle in Visual C++ in a bitmap 24bit colored image without using OpenCV.
Also in VS C++ I'm not able to get a header graphics.h instead what shud i use.
I have to draw a rectangle in the image not the line
Here is my code in VS-C++ which only shows a black line at the bottom:
void copy_Header(FILE *,FILE *);
void main()
{
FILE *src; int offset; int width, height;
fopen_s(&src,"jaguar.bmp","rb");
FILE *dest;
fopen_s(&dest,"rect_image.bmp","wb");
fseek(src,10,SEEK_SET);
fread(&offset,4,1,src);
fseek(src,18,SEEK_SET);
fread(&width,4,1,src);
fseek(src,22,SEEK_SET);
fread(&height,4,1,src);
copy_Header(src,dest);
fseek(src,offset,SEEK_SET);
fseek(dest,offset,SEEK_SET);
unsigned char x=(unsigned char)fgetc(src);
double r,g,b,z[3];
int i;
unsigned char ch[3];
b=ch[0]=fgetc(src);
g=ch[1]=fgetc(src);
r=ch[2]=fgetc(src);
for (int j=0;j<4;j++)
{
for(; offset<width; offset++)
{
z[0]=b;
z[1]=0;
z[2]=0;
fputc(z[0],dest);
fputc(z[1],dest);
fputc(z[2],dest);
}
}
fseek(src,4096,SEEK_SET);
fseek(dest,4096,SEEK_SET);
unsigned char y= (unsigned char)fgetc(src);
while(!feof(src))
{
fputc(y,dest);
y=(unsigned char)fgetc(src);
}
fclose(src);
fclose(dest);
puts("Image Copied");
_getch();
}
void copy_Header(FILE *srcImage,FILE *dstImage)
{
unsigned char *ptrc= (unsigned char *)malloc(54*sizeof(char));
fseek(srcImage,0,SEEK_SET);
fseek(dstImage,0,SEEK_SET);
fread(ptrc,54,1,srcImage);
fwrite(ptrc,54,1,dstImage);
}
Thanx in advance.
image size is of 1024 by 1024 and is 24 bit bitmap file.
Let's break the problem.
First problem: how do you relate a pixel coordinate to its position in a file?
In other words, given a RGB pixel with coordinates (x,y), where is it located on the file?
Let's consider that it has WIDTH width and HEIGHT height. Since it has 3 channels - Red, Green, and Blue - and considering each channel has 1 byte, each pixel will have 3 bytes in size. A BMP file is just a matrix of pixels, organized line by line, so each line will have then 3*WIDTH bytes.
The x coordinate will then tell how many lines will be skipped, and y will point to the pixel in the current line. In other words:
seek_position= x*(3*WIDTH) + 3*y
With this relation, you can now write a function like
int mat2seek(int x,int y){
//converts a xy coordinate system to seek position
return x*(3*WIDTH) + 3*y + BMP_HEADER_SIZE;
}
where BMP_HEADER_SIZE is self explaining
Second problem: now that you know how to convert a (x,y) coordinate system to seek system, what is the algorithm of drawing a rectangle?
Such algorithm is a lot easier to make in (x,y) coordinate than seek coordinates:
for (int x = xini; x< xend; x++)
for (int y=yini; y<yend; y++){
int seek=mat2seek(x,y);
//do the magic...
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to read an image file in C/C++. It would be very great, if some one can post the code for me.
I work on gray scale images and the images are JPEG. I would like to read the images into a 2D array which will make my work easy.
If you decide to go for a minimal approach, without libpng/libjpeg dependencies, I suggest using stb_image and stb_image_write, found here.
It's as simple as it gets, you just need to place the header files stb_image.h and stb_image_write.h in your folder.
Here's the code that you need to read images:
#include <stdint.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int main() {
int width, height, bpp;
uint8_t* rgb_image = stbi_load("image.png", &width, &height, &bpp, 3);
stbi_image_free(rgb_image);
return 0;
}
And here's the code to write an image:
#include <stdint.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define CHANNEL_NUM 3
int main() {
int width = 800;
int height = 800;
uint8_t* rgb_image;
rgb_image = malloc(width*height*CHANNEL_NUM);
// Write your code to populate rgb_image here
stbi_write_png("image.png", width, height, CHANNEL_NUM, rgb_image, width*CHANNEL_NUM);
return 0;
}
You can compile without flags or dependencies:
g++ main.cpp
Other lightweight alternatives include:
lodepng to read and write png files
jpeg-compressor to read and write jpeg files
You could write your own by looking at the JPEG format.
That said, try a pre-existing library like CImg, or Boost's GIL. Or for strictly JPEG's, libjpeg. There is also the CxImage class on CodeProject.
Here's a big list.
Check out Intel Open CV library ...
Check this thread out: read and write image file.
Also, have a look at this other question at Stackoverflow.
corona is nice. From the tutorial:
corona::Image* image = corona::OpenImage("img.jpg", corona::PF_R8G8B8A8);
if (!image) {
// error!
}
int width = image->getWidth();
int height = image->getHeight();
void* pixels = image->getPixels();
// we're guaranteed that the first eight bits of every pixel is red,
// the next eight bits is green, and so on...
typedef unsigned char byte;
byte* p = (byte*)pixels;
for (int i = 0; i < width * height; ++i) {
byte red = *p++;
byte green = *p++;
byte blue = *p++;
byte alpha = *p++;
}
pixels would be a one dimensional array, but you could easily convert a given x and y position to a position in a 1D array. Something like pos = (y * width) + x
Try out the CImg library. The tutorial will help you get familiarized. Once you have a CImg object, the data() function will give you access to the 2D pixel buffer array.
Check out the Magick++ API to ImageMagick.