OpenGL cohabitation of shader and drawing command - c++

In my application, I use OpenGL to draw many shapes using the classic Shader + VAO + many VBO and call glDrawArrays(...)
It is working well but to implement axis visualization, I try to use OpenGL commands.
I came up with this code:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
WhenPaint(); //The function which loop around all object and draw using proper VAO and shaders
//Here I start use OpenGL draw commands to draw my axis
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(&(camera.GetProjectionMatrix(Upp::Sizef{sizeW,sizeH})[0][0])); //Setting up projection
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(&(camera.GetViewMatrix()[0][0])); //Setting up viewMatrix
if(ShowAxis)GLDrawFunction::PaintAxis(0, 0, 0,200); //a function that draw axis
This work but only if I comment the WhenPaint() function...
Here you have the view without the WhenPaint() function:
And here you have the view when I use WhenPaint() (before or after the OpenGL Draw commands do not matter) :
Is it possible to make these two ways of drawing things work together?

Related

Why is openGL glDepthFunc() not working?

im playing with openGL and im trying to get rid of blue marked triangles. I use for it this code:
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
And yes I use
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
in my main loop. I've read the problem can be projection matrix. I use these values:
ProjectionMatrix = glm::perspective(45.5f, 4.0f / 3.0f, 0.1f, 100.0f);
I was trying to change the near and far value but its still the same. I was also trying change parameter of glDepthFunc but it also didnt help me.
So, any ideas?? Thanks a lot
This is perfectly valid behavior because you are not using filled polygons. Face culling still behaves the way you would expect when you use glPolygonMode (...), but the depth test does not.
The depth test and writes only apply to fragments during rasterization, not primitives during clipping / primitive assembly. In a nutshell, this means that anywhere that is not filled will not be affected by the depth of the primitive (e.g. triangle). So the only place the depth test applies in this example are the very few points on screen where two lines overlap.
If you want to prevent the wireframe overlay from drawing lines for triangles that would not ordinarily be visible, you will need to draw twice:
Pass 1
Set Polygon Mode to FILL
Disable color writes: glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE)
Draw primitives
Pass 2
Set Polygon Mode to LINE
Enable color writes: glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
Draw primitives
This will work because the first pass fills the depth buffer using filled (solid) primitives but does not write to the color buffer (thus everything is still transparent). The second pass draws lines at the edges of each primitive and these lines will fail a depth test if the interior (unfilled region) of another triangle covers it.
NOTE: You should use a depth test that includes equality (e.g. GL_LEQAUL) for the behavior discussed above to function correctly. So do not use GL_LESS.

Drawing model from c++ header file

I am using xcode with glut, OpenGL and c++ and I am trying to import and draw a model. I have used an obj to .h file conversion and this is a small part of the header so you can see the structure.
unsigned int M4GunNumVerts = 37812;
GLfloat M4GunVerts [] = {
// f 1/1/1 1582/2/1 4733/3/1
{0.266494348503772, 0.0252334302709736, -0.000725898139236535},
{0.265592372987502, 0.0157389511523397, -0.000725898139236535},
{0.264890836474847, 0.0182004476109518, -0.00775888079925833},
I have tried to draw this in my main with this code.
glVertexPointer(3, GL_FLOAT, 0, M4GunVerts);
glNormalPointer(GL_FLOAT, 0, M4GunNormals);
glTexCoordPointer(2, GL_FLOAT, 0, M4GunTexCoords);
glDrawArrays(GL_TRIANGLES, 0, M4GunNumVerts);
When I run I cant see the model. I have set up a glut window and have made a triangle to see if shapes were being drawn and the triangle showed up. I don't know how fix this so I can see the model.
Here is the reshape function
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45, ratio, 0.01, 1000);
glMatrixMode(GL_MODELVIEW);
Can anybody help?
Either you:
do not enable arrays correctly
and or your camera view is in wrong direction to your object
and or your camera is inside the object. (while CULL_FACE is enabled)
and or wrongly or none at all glColor !!! (can not see black on black)
and or forget to bind texture (while enabled)
and or wrong glTexCoord (while enabled)
and or wrongly set lights (while enabled)
and or wrong perspective respect to object distance and size (view angle,znear,zfar)
if object is planar and you are looking on the thin side ...
Some hints:
Try to rotate camera around the scene (ideal with some keys like arrows and see if the object is behind... or mouse drag...). Also try to move your camera forward/backward (also can use keys but my favorite is mouse wheel).
Use glBegin() glVertex() glEnd() first to avoid problems with wrongly enabled arrays and start without lighting,textures,CULL_FACEing (have them disabled!!!). When you see your model then enable all incrementally so you see what is wrong. After all is OK then try arrays.
If your object seems the wrong way about while CULL_FACE enabled then your winding rule is wrong (CW/CCW) can be seen mostly while rotating object
here is simple OpenGL scene app in BDS2006 you can use it as test of your window and camera/model matrix settings

gwen + opengl can't see anything

I'm trying to use GWEN to draw some GUI elements on top of my opengl scene. It seems to have set up correctly but nothing from gwen is actually being drawn (visibly at least). I'm using a custom renderer which is essentially GWEN's stock opengl renderer but with a different function for loading textures. And OpenGL::Begin() and OpenGL::End() replaced with these:
void coRenderer::Begin()
{
glUseProgram(0);
glDisable(GL_DEPTH_TEST);
glDepthMask(0);
glEnable(GL_BLEND);
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity();
glOrtho(0, screen->w, screen->h, 0, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glActiveTexture(GL_TEXTURE0);
}
void coRenderer::End()
{
Flush();
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPopMatrix(); // Restore The Old Projection Matrix
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glDepthMask(1);
glEnable(GL_TEXTURE_2D);
}
the code for gwen's opengl renderer is here:
http://gwen.googlecode.com/svn/trunk/trunk/gwen/Renderers/OpenGL/OpenGL.cpp
BTW I'm using OpenGL 2.1 not 3.0+
Ah GWEN. That frustrating GUI library.
When I started using it, and integrating it into the engine we wrote in school, I had the same issue as you, using the stock OpenGL renderer however. Turned out it was being positioned wrong, calling glLoadIdentity() to reset the identity matrix seemed to resolve it.
The issue you are having, could well end up being the same as what I had, or there could be a problem with your custom OpenGL renderer. I'm not sure if you know much about GWEN, or how it works, but it runs on a single texture, that skins the GUI. Are you loading that in? Perhaps your texture loader isn't loading it correctly.
Try using your Debugger and stepping through your program. Areas of interest would be where you're attempting to load the GUI skin, where you're assigning the screen space that GWEN can use, and when you're actually attempting to render the GUI.

OpenGL cube not rendering properly

I have a problem when rendering cubes in OpenGL.I am drawing two cubes, one is a wire cube and is centered around the origin, while the other is offset from the origin and is solid. I have mapped some keys to rotate the objects by some degrees wrt to the origin, so the whole scene can rotate around the origin.
The problem is, when I render the scene, when the wire cube is supposed to be infront of the other solid cube, it does not display itself correctly.
In the image above, the colored cube is supposed to be behind the wire cube. i.e. the green wire cube should be on top.
Also the cube is not behaving properly.
After I rotate it a little bit around the x axis (current horizontal line).
The cube has missing faces and is not rendering correctly.
What am I doing wrong?
I have coded the following
Note that rotateX,rotateY,rotateZ are mapped to keys, and are my global rotation variables.
//The Initialize function, called once:
void Init(){
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Depth Buffer Setup // Enables Depth Testing
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_LIGHTING);
}
void draw(){
//The main draw function
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity ();
gluPerspective(45, 640/480.0, .5, 100);
glMatrixMode(GL_MODELVIEW); //select the modelview matrix.
glLoadIdentity ();
gluLookAt(0,0,5,
0,0,0,
0,1,0);
glRotatef(rotateX,1,0,0);
glRotatef(rotateY,0,1,0);
glRotatef(rotateZ,0,0,1);
drawScene(); // this just draws the main axis lines,
glutWireCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(-2,1,0);
drawNiceCube();
glPopMatrix();
glutSwapBuffers();
}
The code for the drawNiceCube() is just using GL_QUADS, while the drawWireCube is built in in GLUT.
EDIT:
I have posted the full code at http://pastebin.com/p1kwPjEM, sorry if it is not well documented.
Did you also request a window with a depth buffer?
glutInitDisplayMode( ... | GLUT_DEPTH | ...);
Update:
Did you somewhere enable face culling?
glEnable(GL_CULL_FACE);
This is may be cause of clockwise
10.090 How does face culling work? Why doesn't it use the surface normal?
OpenGL face culling calculates the signed area of the filled primitive in window coordinate space. The signed area is positive when the window coordinates are in a counter-clockwise order and negative when clockwise. An app can use glFrontFace() to specify the ordering, counter-clockwise or clockwise, to be interpreted as a front-facing or back-facing primitive. An application can specify culling either front or back faces by calling glCullFace(). Finally, face culling must be enabled with a call to glEnable(GL_CULL_FACE); .
OpenGL uses your primitive's window space projection to determine face culling for two reasons. To create interesting lighting effects, it's often desirable to specify normals that aren't orthogonal to the surface being approximated. If these normals were used for face culling, it might cause some primitives to be culled erroneously. Also, a dot-product culling scheme could require a matrix inversion, which isn't always possible (i.e., in the case where the matrix is singular), whereas the signed area in DC space is always defined.
However, some OpenGL implementations support the GL_EXT_ cull_vertex extension. If this extension is present, an application may specify a homogeneous eye position in object space. Vertices are flagged as culled, based on the dot product of the current normal with a vector from the vertex to the eye. If all vertices of a primitive are culled, the primitive isn't rendered. In many circumstances, using this extension
from here
Also you can read here
datenwolf solved my problem. I quote him:
"#JonathanSimbahan: Parts of your code are redundant, but something is missing: You forgot to call Init(); after creating your GLUT window, hence depth testing and all the other state never get enabled. I for one suggest you don't use Init at all and move it's code into the drawing code, where it actually belongs."

Why does my colored cube not work with GL_BLEND?

My cube isn't rendering as expected when I use GL_BLEND.
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
I'm also having a similar problem with drawing some semi-opaque vertices in front, which could well be related.
Related: Why do my semi-opaque vertices make background objects brighter in OpenGL?
Here's what it's supposed to look like:
Normal cube http://img408.imageshack.us/img408/2853/normalcube.png
And here's what it actually looks like:
Dark cube http://img7.imageshack.us/img7/7133/darkcube.png
Please see the code used to create the colored cube, and the code used to actually draw the cube.
The cube is being drawn like so:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glLoadIdentity();
// ... do some translation, rotation, etc ...
drawCube();
glPopMatrix();
// ... swap the buffers ...
You could try disabling all lighting before drawing the cube:
glDisable(GL_LIGHTING);
It looks like you have lighting enabled on the second one,
try with a glShadeModel( GL_FLAT ) before drawing,
This has me stomped. What it looks like is that some vertices have some alpha values that are non-opaque. However the code you posted has all 1. for alpha. So... in order to debug more, did you try to change your clear color to something non-black ? Say green ?
From the code, I doubt lighting is turned on, since no normals were specified.
Last comment, offtopic... You should really not use glBegin/glEnd (2 function calls per vertex + 2 per primitive is really not a good usage of the recent developments in OpenGL). Try glDrawElements with QUAD_LIST, or even better, TRIANGLE_LIST. You already have the data nicely laid out for that.