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.
Related
I'm facing a problem in my opengl code
I'm trying to build a house, and rotate it 360°, for simplicity let's assume the house has the front wall with window and dor, and a back wall.
I'm using DEPTH_BUFFER not to see the back wall when viewing the front wall, and the other way around, but when I rotate the house the door and window start to shake and get distorced.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 40.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glEnable(GL_DEPTH_TEST);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(8.0, 3.0, 0.0);
glVertex3f(8.0, -10.0, 0.0);
glVertex3f(1.0, -10.0, 0.0);
glVertex3f(1.0, 3.0, 0.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(-9.0, -4.0, 0.0);
glVertex3f(-9.0, 3.0, 0.0);
glVertex3f(-2.0, 3.0, 0.0);
glVertex3f(-2.0, -4.0, 0.0);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(10.0, -10.0, -20.0);
glVertex3f(-10.0, -10.0, -20.0);
glVertex3f(-10.0, 10.0, -20.0);
glVertex3f(10.0, 10.0, -20.0);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(10.0, -10.0, 0.0);
glVertex3f(10.0, 10.0, 0.0);
glVertex3f(-10.0, 10.0, 0.0);
glVertex3f(-10.0, -10.0, 0.0);
glEnd();
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
The issue is called Z-fighting. This is caused, because depth of the "door", "window" and "wall" are equal. The vertiex coordinates are transformed by the model view matrix and projection matrix and interpolated for each fragment which is covered by the polygon. This results in inaccuracies of the final z coordinate (depth).
Enable the polygon fill offset (glPolygonOffset) by before drawing the walls, to solve the issue:
glEnable(GL_DEPTH_TEST);
glDisable( GL_POLYGON_OFFSET_FILL );
// draw door and window
// ...
glEnable( GL_POLYGON_OFFSET_FILL );
glPolygonOffset( 1, 1 );
// draw walls
// ...
Polygon fill offset manipulates the depth of a fragment by a minimum amount. This causes that the depth of the "walls" is different to the depth of the "window" and "door", even after the transformation by the model view and projection matrix.
Since an offset is added to the depth of the "wall", the "wall" is always behind the window and the door, independent of the point of view.
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.
I'v been learning OpenGL2.0 rendering stuffs for a while and here are some personal understanding on alpha rendering I want to know if they are right:
Say I have a PNG (32 bit) file (none premultiplied alpha image) with only one pixel (src image)
PNG Shader - glFragColor GPU - SRC_ALPHA SRC_ONE
(255, 0, 0, 255) - red / none transparent (1.0, 0.0, 0.0, 1.0) (1.0, 0.0, 0.0, 1.0) (1.0, 0.0, 0.0, 1.0)
(255, 0, 0, 128) - red / half transparent (1.0, 0.0, 0.0, 0.5) (0.5, 0.0, 0.0, 0.75) (1.0, 0.0, 0.0, 1.0)
(128, 0, 0, 255) - drak red / none transparent (0.5, 0.0, 0.0, 1.0) (0.5, 0.0, 0.0, 1.0) (0.5, 0.0, 0.0, 1.0)
(128, 0, 0, 128) - drak red / half transparent (0.5, 0.0, 0.0, 0.5) (0.25, 0.0, 0.0, 0.75) (0.5, 0.0, 0.0, 1.0)
Here are four examples and 4 columns for each.
Take row NO.2 for example:
1. The PNG image is red but half transparent so the rgba should be (255, 0, 0, 128)
2. The texture info is passed to frag shader and the value for glFragColor shoud be (1.0, 0.0, 0.0, 0.5)
3. When rendering the src color to GPU OpenGL will try to bend it with the dst color
4. let's say the dst color is (0, 0, 0, 255) and blend func for dst is GL_ONE_MINUS_SRC_ALPHA
5. if the blend func for dst is GL_SRC_ALPHA, the final pixel rendered will be (0.5, 0.0, 0.0, 0.75)
6. if the blend func for dst is GL_ONE, the final pixel rendered will be (1.0, 0.0, 0.0, 1.0)
The result for (6) is definitely wrong since the transparency lost while the result for (5) should be good.
And that's why I should use GL_SRC_ALPHA for none premultiplied alpha image and GL_ONE for premultiplied alpha image.
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.
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);