• Create Account

# FXACE

Member Since 18 Sep 2009
Offline Last Active Feb 18 2013 04:06 AM

### #4941931Recreating Polygon Offset in shader

Posted by on 21 May 2012 - 09:29 AM

```vec4 mv_vertex = mvMatrix * gl_Vertex; // mvMatrix = model-view matrix
mv_vertex.z -= offset; // shift the value how much you are need
gl_Position = projMatrix * mv_vertex; // projMatrix = projection matrix
```
Does it?

Best wishes, FXACE.

### #49402642D icons in a 3D projection?

Posted by on 14 May 2012 - 07:48 PM

This formula would be useful for you in near future but for this you don't really need that.
Let me show you complete example:
```// Set up projection
glMatrixMode(GL_PROJECTION);
gluPerspective(...);
glMatrixMode(GL_MODELVIEW);
// place your view to look at
DrawScene();
double proj[16];
double mv[16];
int vp[4];
glGetDoublev(GL_MODELVIEW_MATRIX, mv);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, vp);
double pos[3] = {0, 0, 0}; // position of your icon in 3D space
double wpos[3];

gluProject(pos[0], pos[1], pos[2], mv, proj, vp, &wpos[0], &wpos[1], &wpos[2]);
wpos[2] *= -1; // negate z value because OpenGL's forward axis is '-Z'
double iconSize = 50;

// go to orthogonal projection
glMatrixMode(GL_PROJECTION);
glOrtho(0, vp[2], 0, vp[3], 0,1);
glMatrixMode(GL_MODELVIEW);

glVertex3d(wpos[0] - 1 * iconSize, wpos[1] - 1 * iconSize, wpos[2]);
glVertex3d(wpos[0] + 1 * iconSize, wpos[1] - 1 * iconSize, wpos[2]);
glVertex3d(wpos[0] + 1 * iconSize, wpos[1] + 1 * iconSize, wpos[2]);
glVertex3d(wpos[0] - 1 * iconSize, wpos[1] + 1 * iconSize, wpos[2]);
glEnd();
```
And finally you will get nice quad which would be drawn as you wanted (depth tested and culled).

Does that make sense? The only part that is still confusing me is whether or not the billboard corners can correctly have the same z value as the center point that was returned through gluProject? I think I might be imagining it wrong, but in perspective mode if I ran a ray from the camera to the center of the billboard, it would get a different distance than a ray from camera to a corner, so I would think the depth values would be different as well.

Does the perspective depth buffer somehow make the depths 'normalized' from a plane the camera is on perpendicular to its line of sight, or do the depths represent distance from the actual spot the camera sits?

Nope, any vertex is clipped by planes (left, right, bottom, top, near, far). In orthogonal projection distance of center, corner of billboard to near (&far) plane are equivalent. OpenGL doesn't have a camera, it has a view port (where you move/transform all vertices into, by using of matrices, etc)
But with shaders you can construct a depth buffer as you wish...

Best wishes, FXACE.

### #4926307Trouble using glGetTexImage

Posted by on 29 March 2012 - 05:22 AM

Extra help. Example "Getting image data from texture":
```GLint width,height,internalFormat;
glBindTexture(GL_TEXTURE_2D, your_texture_id);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &internalFormat); // get internal format type of GL texture
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width); // get width of GL texture
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height); // get height of GL texture
// GL_TEXTURE_COMPONENTS and GL_INTERNAL_FORMAT are the same.
// just work with RGB8 and RGBA8
GLint numBytes = 0;
switch(internalFormat) // determine what type GL texture has...
{
case GL_RGB:
numBytes = width * height * 3;
break;
case GL_RGBA:
numBytes = width * height * 4;
break;
default: // unsupported type (or you can put some code to support more formats if you need)
break;
}

if(numBytes)
{
unsigned char *pixels = (unsigned char*)malloc(numBytes); // allocate image data into RAM
glGetTexImage(GL_TEXTURE_2D, 0, internalFormat, GL_UNSIGNED_BYTE, pixels);
{
// TODO with pixels
}
free(pixels); // when you don't need 'pixels' anymore clean a memory page to avoid memory leak.
}

```

Best wishes, FXACE.

### #4925367Resolution settings, screen behaving unexplainably

Posted by on 26 March 2012 - 08:48 AM

Another thing can be happen:
when your display (monitor) first time using these WxH settings sometimes it could do some shift&amp;scale operation which produces strange screen results...
If this was happend, on LCD\LED monitors do: Menu->Settings->Reset:true

I'm on a laptop, I'm not sure if I have these settings.

From my own experience:
When I'm launching game application which enters to full-screen mode (640x480) on my monitors (LG FLATRONs 1920x1080 & 1680x1050) some part of game's screen becomes outside display or not fully stretched to entire display (some parts of screen are not used). If I would set Menu->Settings->Reset:true (on 640x480 mode) it would automatically, correctly changed (as I expect) and saves in monitor's memory (to load next time shift/scale settings when switching to 640x480 again, this settings are customized and located in each resolution mode).

Labtops have some settings for display (accessed with 'fn' key) but I'm not sure do they have these settings.

If you tested some full-screen applications (with modes:960x540, 1024x576, 1200x675) and they were correctly showed, so this is not a display issue.

```
static void ChangeResolution(SDL_Surface* Screen,int Width,int Height, bool FullScreen)
..........
Screen = SDL_SetVideoMode(...)
```
Is your 'Screen' an output variable too? If yes, you need to change from:
```SDL_Surface* Screen // when calling a function with this argument instruct a pointer (which holds an address) would be dublicated
```
to:
```SDL_Surface* &Screen // with this your variable is now linked to input pointer
```

Best wishes, FXACE.

### #4925354Issue with Opaque Objects Appearing Transluscent

Posted by on 26 March 2012 - 08:11 AM

This line:
```glEnable(GL_BLEND);
```
Use blending mode only when you really need that (but not always).
Just put this code before object (example: text) which you planned to make it transparent and when object drawing is done disable blending mode:
```glDisable(GL_BLEND);
```

Best wishes, FXACE.

### #4924888Resolution settings, screen behaving unexplainably

Posted by on 24 March 2012 - 08:51 AM

For each screen size you need to set up a custom glViewport parameters which can be done manually or by formula...
usage:
'Width' - a full-screen window width.
'Height' - a full-screen window heght.
example#1:
I want to do make my screen quadratic (adopting to window width and height) which aligned to center:
```glViewport((Width-Height)/2,0,Height,Height); // I took 'Height' as minimal value, so I would have it work on displays which it's width is larget than height
```
result will look like this:
+-------+--------------+-------+ < my display
|*******|...............|*******|
|*******|.......my...|*******|
|*******|..viewport..|*******|
|*******|...............|*******|
+-------+--------------+-------+
'*' - black pixel.
example#2:
I want to make my screen like film look (let say: 800x320) to get a result like watching a video (with these dimensions) in media player full-screen mode:
```float aspectToY = 320.0f/800.0f;
int w = Width;
int h = int(float(w)*aspectToY); //!!! correct me if this is wrong.
glViewport(0, (Height - h) / 2, w, h); // NOTE: this would work if 'height'(ex:320) <= 'width'(ex:800).
```
the result should be like this (for example if my display would be 800x600):
+---------------------------+ << my display
|************************|
+---------------------------+
|.............my...........|
|.........viewport........|
+---------------------------+
|************************|
+---------------------------+

extra information: OpenGL viewport coordinate system (for glViewport setup):
glViewport(x,y,width,height)
---------window top-------
^-height
|
|....+........+
|................\dimensions(width,height)
|....+........+
|......\offset (x,y)
------------------------> -width
---window bottom-----

As you see (in your screen-shots) screen always aligned on left-bottom corner.
Another thing can be happen:
when your display (monitor) first time using these WxH settings sometimes it could do some shift&amp;scale operation which produces strange screen results...
If this was happend, on LCD\LED monitors do: Menu->Settings->Reset:true

Best wishes, FXACE.

Posted by on 24 March 2012 - 07:34 AM

I checked the following link. I found them on "archive:cwc\source\glsl.cpp"
Are you sure that you included this file to your project?

Best wishes, FXACE.

### #4924315Opengl - Lighting problem

Posted by on 22 March 2012 - 08:18 AM

Because a default OpenGL state renderer computes a lighting per vertex (not per pixel). There are two solutions:
1: Make the floor more "high-poly".
2: Render with shaders (which computes lighting per pixel). (<< I recommend to use this).

Best wishes, FXACE.

### #4923967Opengl ES BitBlt

Posted by on 21 March 2012 - 09:55 AM

see OpenGL's glTexSubImage2D. It should be on OpenGL ES too, because I am working with WebGL (which is based on that).

Edit: here is the reference for OpenGL ES.

### #4923911Grouping images into a mega texture?

Posted by on 21 March 2012 - 06:46 AM

You can use either GL_TEXTURE_2D_ARRAY in GLSL eigther to combine some textures into one:
+-------+
|....A....|
|..........|
+-------+
combine with:
+-------+
|....B....|
|..........|
+-------+
result: "C" =
+-------++-------+
|....A.....||....B....|
|...........||...........|
+-------++-------+
- where "A" and "B" are different textures but with the same dimensions.
Note: you need here to adjust texture coordinates. For example:
```float new_s_coord = old_s_coord / float(num_combined_textures) + (float(use_texture_index) / float(num_combined_textures));
float new_t_coord = old_t_coord; // no changes, because we do texture combination by horizontal axis
```

If you are using GL_REPEAT texture coordinate wrapping (for example: walls, ground, etc..), it is better to use first method (GL_TEXTURE_2D_ARRAY), because with this method you will get some strange graphic results.

Best wishes, FXACE.

### #4922893Opengl - Texture problem

Posted by on 17 March 2012 - 03:32 PM

You did not used glTexCoord right (this is important!).

```glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);<< check this
glVertex3f(startX + size*x, 0.0f, startZ + -size*z);//bottom-left
glTexCoord2f(0.0f, 0.0f);<<
glVertex3f(startX + size + size*x, 0.0f, startZ + -size*z);//bottom-right
glTexCoord2f(0.0f, 0.0f);<<
glVertex3f(startX + size + size*x, 0.0f, startZ + -size + -size*z);//top-right
glTexCoord2f(0.0f, 0.0f);<<
glVertex3f(startX + size*x, 0.0f, startZ + -size + -size*z);//top-left
glEnd();
```

I think it should be:

```glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size*z);//bottom-left
glTexCoord2f(1.0f, 0.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size*z);//bottom-right
glTexCoord2f(1.0f, 1.0f);
glVertex3f(startX + size + size*x, 0.0f, startZ + -size + -size*z);//top-right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(startX + size*x, 0.0f, startZ + -size + -size*z);//top-left
glEnd();
```

edit:
```
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
```
It would be much better to keep this code into texture initialization function (because you are not changing it to another values in loop),

Best wishes, FXACE.

### #4907694color per pixel

Posted by on 30 January 2012 - 11:03 AM

2nd formula from my first post?... Never mind. (It was variant "did you want to make it blended or not")

Hmmm, I would try to rewrite my previous post to make it more clear...

1. Dot product calculation looks like this:
```float dp = dot(normalize(EyePos),normalize(Normal));
```

2. Compute value for 'gradient' (from post #4, 0 degrees - black and 30 degrees - transparent):
```float cMaxDegreesOfFacing = cos(30 * Pi / 180); // which equals to (0.866025...) <<<!
```

0.866025..... was returned from calculation I made in 'Microsoft Calculator' (calc.exe) -> cos of 30 degrees. I don't think that was incorrect.

```float shade = max((dp - cMaxDegreesOfFacing) / (1.0 - cMaxDegreesOfFacing),0.0);
```

That's all what I meaned...

Best wishes, FXACE.

### #4907454color per pixel

Posted by on 29 January 2012 - 04:52 PM

shade = max((dp - 0.866025) / (1.0 - 0.866025),0.0); where: "dp" is a dot production of EyePos to Normal; 0.866025 - cos(30 <your degrees> * Pi / 180).

Best wishes, FXACE.

### #4907352color per pixel

Posted by on 29 January 2012 - 10:08 AM

Basically what i want to do is check wether a pixel is facing the camera or not. If the pixel is directly facing the camera, then i want to color it completely black. When it's 90 degrees rotated, then it should have full transparency.

If it's 45 degrees rotated is it should contain 50% - black and 50% - transparent? If yes:
``` float shade = max(dot(normalize(EyeDir), normalize(Normal)),0.0); // fixing 'solve' because dot returns [-1.0..1.0] but mix expects result in range [0.0...1.0]
```
Otherwise:
``` float shade = float(dot(normalize(EyeDir), normalize(Normal))>0.0);
```

Is this what you want?

Best wishes, FXACE.

### #4897358Newbie to programming

Posted by on 25 December 2011 - 05:58 PM

What is a Z Buffer?

This helps you to construct a solid 3D scene. P.S. If did you know OpenGL (and Direct3D too) is\are triangle rasterizer(s)....

EXAMPLE: (without Z - Buffer)

Lets draw 2 different triangles (where 1st triangle which is closer to viewer than 2nd triangle (which is behind of 1st triangle)) like this:

1: set up viewer;

2: translateMatrix(in z order);

3: select green color;

3: drawTriangle(); // draws a white triangle

4: translateMatrix(in z order);

3: select red color;

5: drawTriangle(); // draws a red triangle

As a result we would have a red triangle on the screen (but we expected to see a green triangle which is closer to viewer).

NOW we set a use of Z Buffer. And a result is : yes, a green triangle on the screen. (where is a red triangle ?: - behind it)

WHAT HOLDS Z BUFFER?

That holds "z" values (something like "distance from camera/eye" but produced from multiplication of matrix (mvp = ProjectionMatrix * ModelViewMatrix) and input vertex (of primitive)).

There are many references and tutorials over the internet.

Best wishes, FXACE.

PARTNERS