Quaternion rotation x-axis 45 Degrees - opengl

Evening everyone,
I'm using glMultMatrixf in OpenGL to rotate my scene using the matrix:
float matrix[16] = { 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 };
I've been following this guide (link) but its a little bit over the top for what I need.
How could I simply rotate the x-axis by 45 degrees?
Cheers

Multiplying your transformation matrix by this rotation matrix should do the trick:
float rot45X[16] = { 1.0, 0.0, 0.0, 0.0,
0.0, cos(PI/4), -sin(PI/4), 0.0,
0.0, sin(PI/4), cos(PI/4), 0.0,
0.0, 0.0, 0.0, 1.0 };
Edit: You can also of course use the utility function
glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
where [x,y,z] indicate the axis of rotation (yes, it performs rotations around an arbitrary vector).
In your case you would need to call like this:
glRotatef(45, 1, 0, 0);

Related

Saving rotation Matrix in OpenGL

I'm writing an OpenGl program about a cone rotating in 3D space and I have a txt with datas of the angles and the axes around whom the rotation takes place.
The problem is that OpenGL requires the total rotation angle while in the file I have the increment in angle for every time step.
here my code :
GLdouble matrice1 [] {1.,0,0,0,0,1.,0,0,0,0,1.,0,0,0,0,1.};
GLdouble * matrice = matrice1;
void displayCone(void){
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //
// set matrix mode
glMatrixMode(GL_MODELVIEW);
// clear model view matrix
glLoadIdentity();
//gluLookAt(3.0, 3.0, 3.0-4.5, 0.0, 0.0,-4.5,0,0,1);
gluLookAt(3.0, 3.0, 3.0, 0.0, 0.0,0.0,0,0,1);
glBegin(GL_LINES);
glColor3f (1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(2.0, 0.0, 0.0);
glColor3f (0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 2.0, 0.0);
glColor3f (0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 2.0);
glEnd();
//glLoadMatrixd(matrice);
glColor3f(0.8, 0.2, 0.1);
glMultMatrixd(matrice); // <------ ** me trying to implement the previous rotation before //rotating of the increment angle xRotated
glRotated(xRotated,a1,a2,a3); // <----- **here the rotation**
glGetDoublev(GL_MODELVIEW, matrice); // <----- ** me trying to save the total rotation //matrix **
glTranslatef(0.0, 0.0, height);
glutSolidCone(base,height,slices,stacks);
glPushMatrix();
glTranslatef(0.0, 0.0, height-0.1);
glutSolidCylinder(0.07,height_c,slices,stacks);
glPopMatrix();
glPushMatrix();
glScalef(1.0,1.0,-1.0);
//glTranslatef(0.0, 0.0, 2*height);
//glPopMatrix();
//glPushMatrix();
glutSolidCone(base,height,slices,stacks);
// Flush buffers to screen
glPopMatrix();
glFlush();
}
What I'm trying to do here with glGetDoublevand glMultMatrixdis to save the precedent Rotation Angle but this doesn't work. Does anyone know how to save the precedent rotation matrix in order to get the total rotation and not the RotationAngle of the increment angle ?
For one, that code you have there is using the old and busted fixed function, legacy API that's been out of fashion for well over 15 years.
More to the point, you can use a proper 3D graphics math library (GLM, Eigen, linmath.h) to manage a rotation matrix and apply it to the OpenGL transformation matrix stack using glMultMatrix instead of using multiple calls to glRotate. Or manage the whole transformation matrix yourself and just load it with glLoadMatrix.
And when you do your own matrix management, it's only a small step to ditch the fixed function pipeline and use shaders, where you pass the matrix as so called uniform values.

glDrawArraysInstanced is not doing multiple draw calls in OpenGL?

I'm trying to draw a two patches of rectangle (for tessellation) and I want to draw them from 0,0 to 1,1 and other from 1,0 to 2,1
I'm using GL_PATCHES to send a quad to my graphics pipeline
My vertex data in homogeneous coordinates is
float vertices[32] = {
0.0, 0.0, 0.0, 1.0, //1st rec
1.0, 0.0, 0.0, 1.0,
1.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0, //2nd rec
2.0, 0.0, 0.0, 1.0,
2.0, 1.0, 0.0, 1.0,
1.0, 1.0, 0.0, 1.0
};
And in C++ code
glPatchParameteri(GL_PATCH_VERTICES, 4);
glDrawArraysInstanced(GL_PATCHES, 0, 4, 2);
But I'm only getting one rectangle patch from 0,0 to 1,1 on my screen. I don't understand why it it doesn't draw the second rectangle
My tessellation evaluation shader is
vec4 vert= vec4(0.0, 0.0, 0.0, 1.0);
vert.x = gl_in[0].gl_Position.x + gl_TessCoord.x;
vert.y = gl_in[0].gl_Position.y + gl_TessCoord.y;
I convert this vert to vec4 and pass it to gl_Position
glDrawArraysInstanced draws several instances of the data specified. In your case, it draws two times the vertices 0 to 4, which gives you two quads lying on the same position.
I would suggest you simply use glDrawArrays(GL_PATCHES, 0, 8) instead, but you could also keep your draw call and translate in the vertex shader according to the gl_InstanceID.

how to get glLookAt's values once rotated

so if I were to have this code
gluLookAt(0.0, 0.0, 1.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0); //Set the point looking at
glRotatef(-90, 0.0,1.0,0.0);
how can i then get each value of the new glLookAt
You probably need to go the route of using glGetDoublev to get the GL_MODELVIEW and GL_PROJECTION matrixes.

3D texture coordinates for a cube

I want to use glTexImage3D with cube. what will be the texture coordinates for it? i am using GL_TEXTURE_3D as target.
I tried with u v coordinates same as 2d texture coordinates with z component 0-depth for each face. But that goes wrong.
These are the texture coordinates i was using which seems to be incorrect.
GLfloat texcoords[]={
0.0, 0.0,0.0,
1.0, 0.0,1.0,
1.0, 1.0,1.0,
0.0, 1.0,0.0,
0.0, 0.0,0.0,
1.0, 0.0,1.0,
1.0, 1.0,1.0,
0.0, 1.0,0.0,
0.0, 0.0,0.0,
1.0, 0.0,1.0,
1.0, 1.0,1.0,
0.0, 1.0,0.0,
0.0, 0.0,0.0,
1.0, 0.0,1.0,
1.0, 1.0,1.0,
0.0, 1.0,0.0,
0.0, 0.0,0.0,
1.0, 0.0,1.0,
1.0, 1.0,1.0,
0.0, 1.0,0.0,
0.0, 0.0,0.0,
1.0, 0.0,1.0,
1.0, 1.0,1.0,
0.0, 1.0,0.0
};
You probably do not want to use a 3D texture just for texturing the faces of a cube. More likely you want to use a cube map – essentially a set of 6 2D textures one for each face of a cube – which by its very nature nicely matches the topology of a cube.

Unexpected behaviour with GLSL Matrix

The following simple fragment shader exhibits very odd behaviour. Without the loop the output is all red, as expected. But with the loop it turns yellow, i. e. rgb(1,1,0). Might someone enlighten me as to what is happening here?
void main(void)
{
mat4 redUnit = mat4(
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0
);
mat4 greenUnit = mat4(
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0
);
mat4 blueUnit = mat4(
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0
);
for (int x = -1; x < 3; x++) {
for (int y = -1; y < 3; y++) {
redUnit[x+1][y+1] = 1.0;
greenUnit[x+1][y+1] = 0.0;
blueUnit[x+1][y+1] = 0.0;
}
}
gl_FragColor = vec4(redUnit[0][0], greenUnit[0][0], blueUnit[0][0], 1.0);
}
EDIT: The output color depends on the order in which the variables are declared. So somehow memory boundaries are being violated. Still doesn't explain the behaviour though.
In case anyone else encounters this: It is a bug in the Intel GMA graphics driver. On another machine with a different graphics card everything works just fine.