How to disable the stencil - c

I am developing a small tool. I want to draw a geometry where I have already used stencil. How do I do that?
// drawing stencil
glEnable(GL_STENCIL_TEST);
glColorMask(false, false, false, false);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glStencilFunc(GL_EQUAL, 0, 1);
// drawing geometry
glColorMask(true, true, true, true);
glStencilFunc(GL_ALWAYS, 0, 1);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glDisable(GL_STENCIL_TEST);

Well, if you want to draw your geometry stenciled out, then you need the stencil test enabled for that, not disabled. But you disable stencil testing in the very last line of code you posted.

Related

How to pack widgets in a gtk_dialog window using gtk3

In gtk2 the following code snippet works to pack widgets into a gtk_dialog window, using the vbox and action_area of the GtkDialog structure:
window=gtk_dialog_new();
gtk_container_set_border_width((GtkContainer *)window, 0);
scrolled_window=gtk_scrolled_window_new(NULL,NULL);
gtk_container_set_border_width((GtkContainer *)scrolled_window, 10);
gtk_scrolled_window_set_shadow_type((GtkScrolledWindow *)scrolled_window, GTK_SHADOW_IN);
gtk_scrolled_window_set_policy((GtkScrolledWindow *)scrolled_window, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_box_pack_start((GtkBox *) (GtkDialog *)window->vbox), scrolled_window, TRUE, TRUE, 0);
gtk_widget_show(scrolled_window);
label=gtk_label_new(text);
gtk_label_set_use_markup((GtkLabel *)label, TRUE);
gtk_label_set_selectable((GtkLabel *)label, TRUE);
gtk_label_set_line_wrap((GtkLabel *)label, FALSE);
gtk_scrolled_window_add_with_viewport((GtkScrolledWindow *)scrolled_window, label);
gtk_widget_show(label);
button=gtk_button_new_with_label("close");
g_signal_connect_swapped(button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
gtk_widget_set_can_default(button, TRUE);
gtk_box_pack_start((GtkBox *) (GtkDialog *)window->action_area), button, TRUE, TRUE, 0);
gtk_widget_grab_default(button);
gtk_widget_show (button);
gtk_widget_show(window);
However in gtk3 this is not completely valid code any more. And it will not compile.
If I replace instances of:
(GtkBox *) (GtkDialog *)window->vbox
with:
(GtkBox *) (GtkDialog *)window
And do the same with instances of action_area the code will compile but the window will not show the extra packed widgets, just the ones the dialog comes with by default.
More information, which to me is a bit contradictory https://developer.gnome.org/gtk3/stable/GtkDialog.html#GtkDialog-struct
The GtkDialog contains only private fields and should not be directly
accessed.
But when I read this on the same page it seems to contradict the previous quote https://developer.gnome.org/gtk3/stable/GtkDialog.html#gtk-dialog-add-action-widget
If you want to add a non-activatable widget, simply pack it into the
action_area field of the GtkDialog struct.
Earlier in that document it states under GtkDialog as GtkBuildable
The GtkDialog implementation of the GtkBuildable interface exposes the
vbox and action_area as internal children with the names “vbox” and
“action_area”.
But I don't really know how to do that using the GtkBuildable interface neither do I want to. Or perhaps that is exactly what I did in gtk2 and it stopped working in gtk3...?
My question is how can I convert the gtk2 code to work with gtk3 with as few changes as possible. I have been searching for quite a while but found no answer yet. The existing gtk3 documentation leaves me going in circles. Maybe I just miss something totally obvious. Or perhaps you are not supposed to do this anymore and have to use a window instead of a dialog?
Thanks to the answer below I was able to change the code to this working one, it will also eliminate a deprecated warning about gtk_dialog_get_action_area() by using gtk_dialog_add_button()
window=gtk_dialog_new();
gtk_container_set_border_width((GtkContainer *)window, 0);
scrolled_window=gtk_scrolled_window_new(NULL,NULL);
gtk_container_set_border_width((GtkContainer *)scrolled_window, 10);
gtk_scrolled_window_set_shadow_type((GtkScrolledWindow *)scrolled_window, GTK_SHADOW_IN);
gtk_scrolled_window_set_policy((GtkScrolledWindow *)scrolled_window, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_box_pack_start((GtkBox *) (GtkDialog *) (gtk_dialog_get_content_area(window)), scrolled_window, TRUE, TRUE, 0);
gtk_widget_show(scrolled_window);
label=gtk_label_new(text);
gtk_label_set_use_markup((GtkLabel *)label, TRUE);
gtk_label_set_selectable((GtkLabel *)label, TRUE);
gtk_label_set_line_wrap((GtkLabel *)label, FALSE);
gtk_container_add((GtkContainer *)scrolled_window, label);
gtk_widget_show(label);
button=gtk_dialog_add_button((GtkDialog *)window, "close", GTK_RESPONSE_CLOSE);
g_signal_connect_swapped(button, "response", (GCallback *)gtk_widget_destroy, window);
gtk_widget_set_can_default(button, TRUE);
gtk_widget_grab_default(button);
gtk_widget_show(button);
gtk_widget_show(window);
gtk_dialog_run((GtkDialog*)window);
gtk_widget_destroy(window);
For the top section you gtk_dialog_get_content_area. This will return a VBox, where you can pack your own widgets.
For the bottom section you either gtk_dialog_add_button or gtk_dialog_add_action_widget.
The GtkDialog contains only private fields and should not be directly accessed.
If you want to add a non-activatable widget, simply pack it into the action_area field of the GtkDialog struct.
You should obtain a pointer to action_area with deprecated (!)gtk_dialog_get_action_area, but that will not allow to register a response id.

How to make a hole on top of the object in openGL cubestack?

i want to make a hole in openGL cube.i have tried certain methods like using stencils and alpha blending.but problem with stencils is it is dividing and displaying the only half part.My requirement is i have to stack cubes and should make a user specified number of hole(rectangle/ellipse shaped)to only the top object.I am able to stack the objects but not able to make a hole if needed. I am new to openGL and i din't find any direct solution for this.can someone give a sample program for this requirement?
Stencils code:
glClear(GL_STENCIL_BUFFER_BIT);
glColorMask(false, false, false, false);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_EQUAL, 0, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glDisable(GL_DEPTH_TEST);
glColor4f(0,0,1,1.0f);
//code for cube
glEnable(GL_DEPTH_TEST);
glColorMask(true, true, true, true);
glStencilFunc(GL_ALWAYS,0, 1);
glStencilOp(GL_REPLACE,GL_KEEP, GL_ZERO);
//code for cylinder
glDisable(GL_STENCIL_TEST);
In your sample, why do you render hole after the cube?? if you want your stencil to do something you should do the opposite.
With this pseudo code, hole should be plane mesh or nearly.
-enable depth test if not already done; (depth test should not be disabled, if the point of view place hole on back faces, you dont want to have it reflected in front...)
-Enable stencil test;
-set colormask to false;
-set stencil op replace; (if your hole are rendered and maybe moving each frame,
use replace to update the stencil)
-set stencil func always 1 1; (if you wants your hole to be rendered into the stencil
buffer you have to pass always)
-render your hole mesh with your current model view matrices;
-restore colormask to true;
-set stencilfunc notequal 1 1; (dont draw if something appear in stencil at this place)
-set stencil op keep:; (keep in any case)
-draw your scene: holes will be masked;
Use FBO to Create new texture with alpha channel used for creating the hole.

OpenGL - Nothing appearing on screen

I'm using Windows 7 with VC++ 2010
I'm trying to draw a simple point to a screen but it's not showing.
The screen is clearing to black so I know that I have a valid OpenGL context etc...
Basically my OpenGL code boils down to this (I don't have a depth buffer at this point):
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, 1018.0 / 743.0, 5.0, 999.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor4f( 1, 1, 1, 1 );
glPointSize( 100 );
glBegin( GL_POINTS );
glVertex2i( 0, 0 );
glEnd();
SwapBuffers( hdc );
The initialization code for OpenGL is this:
glClearColor( 0, 0, 0, 1 );
glShadeModel( GL_SMOOTH );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
The problem is that nothing appears on the screen, the only thing that happens is the screen gets cleared.
Go through the following checklist (which is the general opengl checklist from delphigl.com (de_DE), which we usually give people to go through when they don't see anything):
Is your object accidentially painted in black? Try and change the glClearColor.
Do you have texturing enabled accidentially? Disable it before drawing with glDisable(GL_TEXTURE_2D).
Try disabling the following tests:
GL_DEPTH_TEST
GL_CULL_FACE
GL_ALPHA_TEST
Check whether your glViewport is setup correctly.
Try translating your Model View Matrix out of the near-clipping-plane (5.0 in your case) with glTranslatef(0, 0, -6.0)
There are several potential issues. The main problem will be how you are using the gluPerspective projection. gluPerspective is for perspectivic view and as such, it won't display anything at the (0, 0, 0) in View Coordinates. In your setup, you forbid displaying anything before (0, 0, 5) in View Coordinates (near clipping plane). I suggest setting your point to glVertex3f(0., 0., 10.) and try again. Another solution would be to use glTranslatef to move your View Coordinates around by more than 5 units.
Also glPointSize will probably not accept your value of 100, as common implementations are limited to a point size of 64.
For a good start with OpenGL, I'd also recommend reading up on Nehes Tutorials. They might not be State-Of-The-Art, but cover anything you're facing right now.
The problem was because I had called glDepthRange misunderstanding what it actually did, I was calling it like this: glDepthRange( nearPlane, farPlane ). (which was 5.0f and 999.0f) When I removed this call everything was able to draw correctly. Thankyou very much for your help. :)

Draw Image to form? (size related issue)

I'm trying to draw a 400x400 px image to a 400x400 form that I made. What I'm doing is:
Graphics.DrawImage Method (Image, 0, 0, 400, 400) 0, 0, 400, 400, ...
But when I run the form, the image seems to stretch slightly upon the y-axis, the x-axis seems to be working correctly.
This was what I was doing before (stretching the old smaller images to fit the size)
... (Image, 0, 0, 264, 231) 0, 0, 400, 400, ...
or something like that. Now that I'm trying to do it the correct way, I can't seem to get it to work properly.
Edit: I wonder if using a simpler verson of Graphics.DrawImage would work? Although I still need to figure out what is wrong with what I'm doing.
Thanks in advance.
If you have a borderless form at 400 x 400, then it should be ok.
If not, then you have to account for the non-client dimensions of the form to reach the size you want.
You can just do this for drawing:
e.Graphics.DrawImage(image, 0, 0)
To set your form size, you can try this:
Me.ClientSize = New Size(400, 400)

How to take screenshot from Panel in mono C#?

How to take all screenshot from Panel in mono C# ?
I need not used winAPI.
My panel can not be fully visible.
Your question is not exactly clear but in case you meant taking a screenshot of the desktop: below is a small example of how you could do this using gtk:
using Gtk;
...
Gdk.Window window = Gdk.Global.DefaultRootWindow;
if (window!=null)
{
Gdk.Pixbuf pixBuf = new Gdk.Pixbuf(Gdk.Colorspace.Rgb, false, 8,
window.Screen.Width, window.Screen.Height);
pixBuf.GetFromDrawable(window, Gdk.Colormap.System, 0, 0, 0, 0,
window.Screen.Width, window.Screen.Height);
pixBuf.ScaleSimple(400, 300, Gdk.InterpType.Bilinear);
pixBuf.Save("screenshot0.jpeg", "jpeg");
}
hope this helps, regards

Resources