Perspective-correct - Trapezoid - 2D - OpenGL GLSL - opengl

I am drawing a textured trapezoid in OpenGL and affine problem occurs:
http://upload.wikimedia.org/wikipedia/commons/5/57/Perspective_correct_texture_mapping.jpg
I want my texture this in perspective-correct.
I have to interpolate in the image space (sw tw w) and I don't know how to do it:
http://i.stack.imgur.com/O0AnC.png
I paste my current code project:
c++:
ttps://gist.github.com/danicomas/a1f5a0e6849b3ac8b51c169c2c030e37 (Add http)
vertex:
ttps://gist.github.com/danicomas/fee77cf48fc5085f61a2fcf7a2c6d5de (Add http)
fragment:
ttps://gist.github.com/danicomas/0bbd679d2d7da18bc61ee23b36096a16 (Add http)
How can I do this? Some example code?

Finally. I found it a simple solution!
c++
glPushMatrix();
glBegin(GL_QUADS);
float scale_texcoord = 0.7;
float top = 0.7;
float tx = scale_texcoord * top;
glTexCoord2f(-1 / 2, -1);
glVertex2f(-1.4, -1);
glTexCoord4f(0, 0, 0, tx);
glVertex2f(-top, 1);
glTexCoord4f( tx, 0, 0, tx);
glVertex2f( top, 1);
glTexCoord2f( 1, -1);
glVertex2f( 1.4, -1);
glEnd();
glPopMatrix();
fragment:
uniform sampler2D sampler;
void main()
{
vec2 interpolated = vec2(gl_TexCoord[0].x / gl_TexCoord[0].w, gl_TexCoord[0].y);
gl_FragColor = texture2D(sampler, vec2(interpolated.x, interpolated.y));
}

Related

OpenGL - Line disappear when move camera

I write a program to draw one line.
The line sometimes disappear when I move camera to positive z-axis (especially when z-axis greater than 10000).
There are some test result.
When z set 20541, the line can be seen.
When z set 20542, the line CAN'T be seen.
When z set 30320, the line can be seen.
When z set 30321, the line CAN'T be seen.
and so forth ...
The code is attached. What's wrong?
P.S.
The code is written by OpenGL 1.0, but I can still get the same test result when written by OpenGL 3.0 + glm library.
#include <glut.h>
/*
System Info
-------------
OS: Win7 professional 64-bit SP1
CPU: Intel i3-4170 # 3.70GHz
GPU: HD Graphics 4400
*/
void display(void) {
// 20541 ok, 20542 not visible
// 30320 ok, 30321 not visible
const GLfloat z = 20541;
const GLfloat far = 1000, near = 0.1;
GLfloat vertices[4 * 3] = {
-far, -far, z - far,
far, far, z - far,
};
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, z, 0, 0, z - 1, 0, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.1, 0.1, -0.1, 0.1, near, far);
glColor3f(0, 1, 1); // blue
glBegin(GL_LINES);
glVertex3f(vertices[0], vertices[1], vertices[2]);
glVertex3f(vertices[3], vertices[4], vertices[5]);
glEnd();
glFlush();
}
int main() {
glutCreateWindow("");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
This issue seems to be a numerical instability of the floating point arithmetic. Since you are projecting points that are exactly on the far-plane, they get clipped when the floating-point result is a little bit larger than the expected result.
Let's assume a C++ implementation of what the gpu basically does:
glm::vec4 test_fp(float z)
{
//Construct matrices
auto ortho = glm::frustum(-0.1f, 0.1f, -0.1f, 0.1f, 0.1f, 1000.0f);
auto lookat = glm::lookAt(glm::vec3(0, 0, z), glm::vec3(0, 0, z - 1.0f), glm::vec3(0, 1, 0));
//We are only interested in the z-value
glm::vec4 tvec(0, 0, z - 1000.0f, 1);
//Calculate ndc vector
auto result = ortho * lookat * tvec;
//Homogenize
result /= result.w;
return result;
}
When now calling this function with the values you provided we get the following results:
auto a = test_fp(20541.0); //< [0, 0, 1.00000000, 1]
auto b = test_fp(20542.0); //< [0, 0, 1.00000191, 1]
auto c = test_fp(30320.0); //< [0, 0, 1.00000000, 1]
auto d = test_fp(30321.0); //< [0, 0, 1.00000191, 1]
As you can see, the results of b and d diverge from the mathematical correct result and are slightly above 1.0. Since values above 1.0 are behind the far-plane, they are clipped away and are not visible, which is exactly the behavior you have.

(OpenGL ShadowMap)Shadow cast on incorrect faces

Here is the demo image:
(Left top 256x256 rect is the depth texture)
I render the shadow map in the first pass(with parallel projection),
then render the scene in the second pass,
then render the scene with shadow map in the final pass.
the shadow is sometimes rendered twice or on the wrong surfaces
Is there any solution?
Full code here:
typedef struct
{
vec3_t org;//origin
vec3_t off;//position offset
vec3_t ang;//angle
float dist;//radius
int w;//default 512
int h;//default 512
int depth;//depth texture
int color;//color texture
int dimension;//default = 8
float mvmatrix[16];
float projmatrix[16];
cl_entity_t *followent;
int inuse;
}sdlight_t;
void R_RenderDepthMap()
{
qglPolygonOffset( 5.0, 0.0 );
qglEnable(GL_POLYGON_OFFSET_FILL);
if(cursdlight->followent)
{
VectorCopy(cursdlight->followent->origin, cursdlight->org);
}
qglMatrixMode(GL_PROJECTION);
qglLoadIdentity();
qglOrtho(-cursdlight->w / cursdlight->dimension, cursdlight->w / cursdlight->dimension, -cursdlight->h / cursdlight->dimension, cursdlight->h / cursdlight->dimension, -9999, 9999);//cursdlight->dist
qglMatrixMode(GL_MODELVIEW);
qglLoadIdentity();
qglRotatef(-90, 1, 0, 0);
qglRotatef(90, 0, 0, 1);
qglRotatef(-cursdlight->ang[2], 1, 0, 0);
qglRotatef(-cursdlight->ang[0], 0, 1, 0);
qglRotatef(-cursdlight->ang[1], 0, 0, 1);
qglTranslatef(-cursdlight->org[0], -cursdlight->org[1], -cursdlight->org[2]);
qglViewport(0, 0, cursdlight->w, cursdlight->h);
glGetFloatv(GL_PROJECTION_MATRIX, cursdlight->projmatrix);
glGetFloatv(GL_MODELVIEW_MATRIX, cursdlight->mvmatrix);
qglDepthRange(1.0, 0.0);
qglDepthFunc(GL_LEQUAL);
qglEnable(GL_CULL_FACE);
//qglCullFace(GL_FRONT);
qglClearColor(1, 1, 1, 1);
qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Render Models..
R_DrawEntitiesOnList();
qglDisable(GL_POLYGON_OFFSET_FILL);
qglBindFramebufferEXT(GL_READ_FRAMEBUFFER, s_BackBufferFBO.s_hBackBufferFBO);
qglActiveTextureARB(GL_TEXTURE0);
qglBindTexture(GL_TEXTURE_2D, cursdlight->depth);
qglCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 0, 0, cursdlight->w, cursdlight->h, 0);
}
void R_SetupShadowLight(void)
{
// enable automatic texture coordinates generation
GLfloat planeS[] = {1.0, 0.0, 0.0, 0.0};
GLfloat planeT[] = {0.0, 1.0, 0.0, 0.0};
GLfloat planeR[] = {0.0, 0.0, 1.0, 0.0};
GLfloat planeQ[] = {0.0, 0.0, 0.0, 1.0};
// setup texture stages
qglActiveTextureARB(GL_TEXTURE0_ARB);
qglBindTexture(GL_TEXTURE_2D, cursdlight->depth);
qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_REF_TO_TEXTURE);
qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
qglTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
qglEnable(GL_TEXTURE_GEN_S);
qglEnable(GL_TEXTURE_GEN_T);
qglEnable(GL_TEXTURE_GEN_R);
qglEnable(GL_TEXTURE_GEN_Q);
qglTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
qglTexGenfv(GL_S, GL_EYE_PLANE, planeS);
qglTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
qglTexGenfv(GL_T, GL_EYE_PLANE, planeT);
qglTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
qglTexGenfv(GL_R, GL_EYE_PLANE, planeR);
qglTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
qglTexGenfv(GL_Q, GL_EYE_PLANE, planeQ);
// load texture projection matrix
qglMatrixMode(GL_TEXTURE);
qglLoadIdentity();
qglTranslatef(0.5, 0.5, 0.5);
qglScalef(0.5, 0.5, 0.5);
qglMultMatrixf(cursdlight->projmatrix);
qglMultMatrixf(cursdlight->mvmatrix);
qglMatrixMode(GL_MODELVIEW);
if (gl_polyoffset->value)
{
qglEnable(GL_POLYGON_OFFSET_FILL);
qglPolygonOffset(-1, -gl_polyoffset->value);
}
qglDepthMask(GL_FALSE);
qglEnable(GL_BLEND);
qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
qglColor4f(1,1,1,1);
qglUseProgramObjectARB(shadow_program);
qglUniform1iARB(shadow_uniform.shadowmap, 0);
}
void R_FinishShadowLight(void)
{
qglUseProgramObjectARB(0);
if (gl_polyoffset->value)
{
qglDisable(GL_POLYGON_OFFSET_FILL);
}
qglDepthMask(GL_TRUE);
qglDisable(GL_BLEND);
qglActiveTextureARB(GL_TEXTURE0_ARB);
qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
qglMatrixMode(GL_TEXTURE);
qglLoadIdentity();
qglDisable(GL_TEXTURE_GEN_S);
qglDisable(GL_TEXTURE_GEN_T);
qglDisable(GL_TEXTURE_GEN_R);
qglDisable(GL_TEXTURE_GEN_Q);
qglMatrixMode(GL_MODELVIEW);
}
void R_DrawShadows(void)
{
for (int i = 0; i < numsdlights; i++)
{
cursdlight = &sdlights[i];
if(!R_ShouldCastShadow())
continue;
R_SetupShadowLight();
R_DrawSceneShadow();
R_FinishShadowLight();
}
}
GLSL part:
//vertex shader
varying vec4 shadowcoord;
void main()
{
shadowcoord = gl_TextureMatrix[0] * gl_Vertex;
gl_Position = ftransform();
}
//fragment shader
#version 120
uniform sampler2DShadow shadowmap;
varying vec4 shadowcoord;
uniform float xoffset = 1.0/512.0;
uniform float yoffset = 1.0/512.0;
float lookup(vec4 coord, vec2 offSet)
{
return shadow2DProj(shadowmap, coord + vec4(offSet.x * xoffset, offSet.y * yoffset, 0.0, 0.0) ).w;
}
void main()
{
float shadow;
shadow = lookup(shadowcoord, vec2(0.0,0.0)) + lookup(shadowcoord, vec2(0.035,0.0)) + lookup(shadowcoord, vec2(-0.035,0.0)) + lookup(shadowcoord, vec2(0.0,0.035)) + lookup(shadowcoord, vec2(0.0,-0.035));
shadow /= 5.0;
if(shadow == 1.0)
discard;
else
gl_FragColor = vec4(0.0, 0.0, 0.0, (1.0-shadow) * 0.5);
}

How to ripple on a sphere

I'm trying to implement a program that turns a cube into a sphere based on key presses, and ripples whenever it's clicked. I managed to implement the cube-to-sphere-and-back part, but I have completely no idea where to start on the rippling. I've looked at tons of sources online, I get the math, but I have no idea how to implement it on my vertex shader. Can anyone help me with my dilemma? Thank you!
Here's my cpp, vsh, and fsh: https://drive.google.com/file/d/0B4hkcF9foOTgbUozMjZmSHJhQWM/view?usp=sharing
I'm using GLSL, OpenGL 4.4.0
Here's my code for the vertex shader:
#version 120
attribute vec3 pos;
varying vec4 out_color;
uniform float t;
float PI = 3.14159265357;
int factor = 2; //for determining colors
int num_colors; // = factor * 3 (because RGB)
float currang = 0;
float angfac;
vec4 calculate( float a )
{
//this is just to calculate for the color
}
void main() {
num_colors = factor*3;
angfac = 2*PI/num_colors;
float ang = atan( pos.z, pos.x )+PI;
out_color = calculate(ang);
//rotation
mat3 rotateX = mat3(
vec3( 1, 0, 0),
vec3( 0, cos(t), sin(t)),
vec3( 0, -sin(t), cos(t))
);
mat3 rotateY = mat3(
vec3( cos(t), 0, -sin(t)),
vec3( 0, 1, 0),
vec3( sin(t), 0, cos(t))
);
mat3 rotateZ = mat3(
vec3( cos(t), sin(t), 0),
vec3(-sin(t), cos(t), 0),
vec3( 0, 0, cos(t))
);
gl_Position = gl_ModelViewProjectionMatrix * vec4((pos.xyz*rotateY*rotateX) , 1.0 );
}
and here's parts of my cpp file:
//usual include statements
using namespace std;
enum { ATTRIB_POS };
GLuint mainProgram = 0;
// I use this to indicate the position of the vertices
struct Vtx {
GLfloat x, y, z;
};
const GLfloat PI = 3.14159265357;
const int sideLength = 10;
const size_t nVertices = (sideLength*sideLength*sideLength)-((sideLength-2)*(sideLength-2)*(sideLength-2));
Vtx cube[nVertices];
Vtx sphere[nVertices];
Vtx diff[nVertices];
const double TIME_SPEED = 0.01;
int mI = 4*(sideLength-1);
const int sLCubed = sideLength*sideLength*sideLength;
int indices[nVertices*nVertices];
GLfloat originX = 0.0f; //offset
GLfloat originY = 0.0f; //offset
bool loadShaderSource(GLuint shader, const char *path) {...}
void checkShaderStatus(GLuint shader) {...}
bool initShader() {...}
//in this part of the code, I instantiate an array of indices to be used by glDrawElements()
void transform(int fac)
{
//move from cube to sphere and back by adding/subtracting values and updating cube[].xyz
//moveSpeed = diff[]/speedFac
//fac is to determine direction (going to sphere or going to cube; going to sphere is plus, going back to cube is minus)
for( int i = 0; i<nVertices; i++ )
{
cube[i].x += fac*diff[i].x;
cube[i].y += fac*diff[i].y;
cube[i].z += fac*diff[i].z;
}
}
void initCube() {...} //computation for the vertices of the cube depending on sideLength
void initSphere() {...} //computation for the vertices of the sphere based on the vertices of the cube
void toSphere() {...} //changes the values of the array of vertices of the cube to those of the sphere
void initDiff() {...} //computes for the difference of the values of the vertices of the sphere and the vertices of the cube for the slow transformation
int main() {
//error checking (GLEW, OpenGL versions, etc)
glfwSetWindowTitle("CS177 Final Project");
glfwEnable( GLFW_STICKY_KEYS );
glfwSwapInterval( 1 );
glClearColor(0,0,0,0);
if ( !initShader() ) {
return -1;
}
glEnableVertexAttribArray(ATTRIB_POS);
glVertexAttribPointer(ATTRIB_POS, 3, GL_FLOAT, GL_FALSE, sizeof(Vtx), cube);
initCube();
initIndices();
initSphere();
initDiff();
glUseProgram(mainProgram);
GLuint UNIF_T = glGetUniformLocation(mainProgram, "t");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float t = 0;
glUniform1f(UNIF_T, t);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glPointSize(2.0);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glfwOpenWindowHint(GLFW_FSAA_SAMPLES,16);
glEnable(GL_MULTISAMPLE);
do {
int width, height;
glfwGetWindowSize( &width, &height );
glViewport( 0, 0, width, height );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
t += TIME_SPEED;
glUniform1f(UNIF_T, t);
if (glfwGetKey(GLFW_KEY_DEL)) transform(-1);
if (glfwGetKey(GLFW_KEY_INSERT)) transform( 1 );
if (glfwGetKey(GLFW_KEY_HOME)) initCube();
if (glfwGetKey(GLFW_KEY_END)) toSphere();
glDrawElements( GL_TRIANGLES, nVertices*nVertices, GL_UNSIGNED_INT, indices);
glfwSwapBuffers();
} while ( glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS &&
glfwGetWindowParam(GLFW_OPENED) );
glDeleteProgram(mainProgram);
glfwTerminate();
return 0;
}

Rendering visually perfect squares in OpenGL?

In OpenGL's fixed pipeline, by default, specifying vertex coordinates using glVertex3f is equivalent to specifying a location between -1.0 and +1.0 in screen space. Therefore, given a set of 4 perfectly adjacent screen-space vertices using GL_TRIANGLE_STRIP (or even GL_QUADS), and unless your window is already perfectly square, you will always render a rectangle instead of a perfect square...
Knowing the width, height and aspect ratio of a window, is there some way to correct this?
I have tried multiplying the vertex coordinates by the aspect ratio, which unfortunately seemed to achieve the same visual effect.
Here's the full source code I'm currently using:
#include "main.h"
#pragma comment(lib, "glut32.lib")
int g_width = 800;
int g_height = 600;
int g_aspectRatio = double(g_width) / double(g_height);
bool g_bInitialized = false;
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0, 0);
glutInitWindowSize(g_width, g_height);
glutCreateWindow("OpenGL Test App");
glutDisplayFunc(onRender);
glutReshapeFunc(onSize);
glutIdleFunc(onRender);
glutMainLoop();
return 0;
}
void onInit()
{
glFrontFace(GL_CW);
}
void onRender()
{
if(!g_bInitialized)
onInit();
static float angle = 0.0f;
const float p = 0.5f * g_aspectRatio;
glLoadIdentity();
gluLookAt(
0.0f, 0.0f, 10.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glScalef(1, -1, 1); // Flip the Y-axis
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLE_STRIP);
{
glColor4f(1.0, 0.0, 0.0, 1.0); // Red
glVertex3f(-p, -p, 0.0); // Top-Left
glColor4f(0.0, 1.0, 0.0, 1.0); // Green
glVertex3f(p, -p, 0.0); // Top-Right
glColor4f(0.0, 0.0, 1.0, 1.0); // Blue
glVertex3f(-p, p, 0.0); // Bottom-Left
glColor4f(1.0, 1.0, 0.0, 1.0); // Yellow
glVertex3f(p, p, 0.0); // Bottom-Left
}
glEnd();
angle += 0.6f;
glutSwapBuffers();
}
void onSize(int w, int h)
{
g_width = max(w, 1);
g_height = max(h, 1);
g_aspectRatio = double(g_width) / double(g_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45, g_aspectRatio, 1, 1000);
glMatrixMode(GL_MODELVIEW);
}
EDIT:
This has been solved... In the above code, I had defined g_aspectRatio as an int instead of a floating-point value. Therefore, it's value was always 1...
In my (old) experience, that's just why you have an aspect ratio argument to gluPerspective().
The manual page says:
In general, the aspect ratio in gluPerspective should match
the aspect ratio of the associated viewport. For example, aspect = 2.0
means the viewer's angle of view is twice as wide in x as it is in y.
If the viewport is twice as wide as it is tall, it displays the image
without distortion.
Check your g_aspectRatio value.
by default, specifying vertex coordinates using glVertex3f is equivalent to specifying a location between -1.0 and +1.0 in screen space
Wrong. Coordinates passed to OpenGL through glVertex or a glVertexPointer vertex array are in model space. The transformation to screen space happens by transforming into view space by the modelview matrix and from view space to clip space by the projection matrix. Then clipping is applied and the perspective divide applied to reach normalized coordinate space.
Hence the value range for glVertex can be whatever you like it to be. By applying the right projection matrix you get your view space to be in [-aspect; aspect]×[-1, 1] if you like that.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-aspect, aspect, -1, 1, -1, 1);

Apply transformations to Bounding Box

I am trying to make a tank game. I have successfully loaded an OBJ model, and calculated its bounding box for the model at the origin.
I am now trying to apply the transformations done to my model in the game logic to the original coordinates for the bounding box. For this, I grab the modelview matrix right before drawing my model, then multiply this matrix for the two vectors that define the BBox.
Here is the code that draws my tank:
void drawTank()
{
bBox = calcBBox(modelo, 1);
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, texTank);
glScalef(0.2, 0.2, 0.2);
glTranslatef(posTank.x,posTank.y,posTank.z);
glRotatef(anguloTanque, 0, 1, 0); // rotate around Y (horizontal)
glRotatef(90, 0, 1, 0);
glRotatef(-90, 1, 0, 0);
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
glmDraw(modelo, GLM_TEXTURE | GLM_MATERIAL);
glColor3f(1,0,0);
drawBBox(bBox);
glPopMatrix();
}
With this snippet, my bbox is properly drawn over the tank model (transformations are applied in rendering by the glTranslate & glRotate functions). As you can see I also grab here my ModelView matrix.
Then I apply this matrix as follows (this is my entire display function):
void Display(void) {
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
camera();
glEnable(GL_TEXTURE_2D);
//glTranslatef(0,-40,150);
//PLANE
glBindTexture(GL_TEXTURE_2D, texArena);
glBegin(GL_POLYGON);
glTexCoord2f( 0.0f, 0.0f );
glVertex3f(-500, 0, -500);
glTexCoord2f( 5.0f, 5.0f );
glVertex3f(500, 0, -500);
glTexCoord2f(5.0f, 0.0f );
glVertex3f(500, 0, 500);
glTexCoord2f( 0.0f, 5.0f );
glVertex3f(-500, 0, 500);
glEnd();
drawTank();
glPopMatrix();
point3D max = bBox.max;
point3D min = bBox.min;
point3D resultMax;
point3D resultMin;
//Transformacion
multVectorByMatrix(matrix, max, resultMax);
multVectorByMatrix(matrix, min, resultMin);
bBox.max.x = resultMax.x; bBox.max.y = resultMax.y; bBox.max.z = resultMax.z;
bBox.min.x = resultMin.x; bBox.min.y = resultMin.y; bBox.min.z = resultMin.z;
glPushMatrix();
glColor3f(1,1,1);
drawBBox(bBox);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
The function that multiplies a vector by a matrix:
void multVectorByMatrix(float* matrix, point3D vector, point3D &result)
{
result.x = (matrix[0] * vector.x) +
(matrix[4] * vector.y) +
(matrix[8] * vector.z) +
matrix[12];
result.y = (matrix[1] * vector.x) +
(matrix[5] * vector.y) +
(matrix[9] * vector.z) +
matrix[13];
result.z = (matrix[2] * vector.x) +
(matrix[6] * vector.y) +
(matrix[10] * vector.z) +
matrix[14];
}
If I draw the bounding box with this render loop, then my bounding box gets drawn but transformations are not applied properly. I can see the bounding box moving correctly with translations, but rotations are not done right.
What might be the problem here?
edit: some screenshots
Your problem is in this code.
point3D max = bBox.max;
point3D min = bBox.min;
point3D resultMax;
point3D resultMin;
//Transformacion
multVectorByMatrix(matrix, max, resultMax);
multVectorByMatrix(matrix, min, resultMin);
bBox.max.x = resultMax.x; bBox.max.y = resultMax.y; bBox.max.z = resultMax.z;
bBox.min.x = resultMin.x; bBox.min.y = resultMin.y; bBox.min.z = resultMin.z;
glPushMatrix();
glColor3f(1,1,1);
drawBBox(bBox);
glPopMatrix();
You take two vertices from your box and then apply transformations to them, then you use this transformed vertices to display a box, which of course will be axis aligned, because that's the only box you can get from just two opposite vertices. And you can see on your screenshot, that you bbox and the correct bbox have common vertices - these are exactly the vertices you applied your transformations to. So, in order to get a correct bbox, you need to get all vertices of the bbox and apply these transformations to all of them. Then you'll get exactly what you want.