|
Allegro 5 + OpenGL + GL_DEPTH_TEST problem |
joecarl
Member #14,983
March 2013
|
Hi guys! Im uploading a screenshot: {"name":"thump_8343734crap.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/e\/3ec224dba32d566b596a8a7f9825b519.png","w":331,"h":200,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/3\/e\/3ec224dba32d566b596a8a7f9825b519"} As u can see it must be related with the way I configure the DEPTH BUFFER... but I really dont know what I'm doing wrong. I'm using Code::Blocks 12.11 C++ with MINGW 4.7 and the I'm not sure what openGL version im using but it's the one provided with MINGW 4.7 And here is my whole code: 1#include <stdio.h>
2#include <allegro5/allegro.h>
3#include <gl/glu.h>
4#include <math.h>
5void DrawCube(void);
6
7int main(int argc, char **argv){
8 bool finish=false, draw= true;
9 const float FPS = 60.0;
10 int resX=1280,resY=720;
11 double Angle=90, Ratio=float(resX)/float(resY),ClippingBorder1=0,ClippingBorder2=100;
12 float x=0,z=0, y=0,rotX=0,incl=0,moveSpeed = 0.1;
13
14 ALLEGRO_DISPLAY *display = NULL;
15 if(!al_init()) {
16 fprintf(stderr, "failed to initialize allegro!\n");
17 return -1;
18 }
19
20
21 al_set_new_display_flags( ALLEGRO_OPENGL );
22 al_set_new_display_option( ALLEGRO_COLOR_SIZE, 32, ALLEGRO_REQUIRE);
23 al_set_new_display_option( ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_REQUIRE);
24 al_set_new_display_option( ALLEGRO_STENCIL_SIZE, 8, ALLEGRO_REQUIRE);
25 al_set_new_display_option( ALLEGRO_AUX_BUFFERS, 0, ALLEGRO_REQUIRE);
26 al_set_new_display_option( ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
27 al_set_new_display_option( ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST);
28
29 display = al_create_display(resX, resY);
30 if(!display){
31 fprintf(stderr, "failed to create display!\n");
32 return -1;
33 }
34 al_clear_to_color(al_map_rgb(0,0,0));
35 al_hide_mouse_cursor(display);
36
37 al_install_keyboard();
38 al_install_mouse();
39 ALLEGRO_KEYBOARD_STATE keyState;
40 ALLEGRO_MOUSE_STATE mouseState;
41 ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
42 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
43 al_register_event_source(event_queue, al_get_keyboard_event_source());
44 al_register_event_source(event_queue, al_get_mouse_event_source());
45 al_register_event_source(event_queue, al_get_timer_event_source(timer));
46 al_start_timer(timer);
47
48glEnable(GL_CULL_FACE);
49glEnable(GL_DEPTH_TEST);
50
51//glDepthMask(GL_TRUE);
52glDepthFunc(GL_LEQUAL);
53//glDepthRange(0.0f, 1.0f);
54glShadeModel(GL_FLAT);
55
56 while(!finish){
57
58 ALLEGRO_EVENT events;
59 al_wait_for_event(event_queue, &events);
60
61 if(events.type == ALLEGRO_EVENT_KEY_UP)
62 {
63 switch(events.keyboard.keycode)
64 {
65 case ALLEGRO_KEY_ESCAPE:
66 finish = true;
67 }
68 }
69 if(events.type == ALLEGRO_EVENT_TIMER)
70 {
71 al_get_keyboard_state(&keyState);
72 al_get_mouse_state(&mouseState);
73
74 rotX+=(mouseState.x-resX/2)/100.0;
75 if(incl>-1.45 && incl<1.45)
76 incl-=float(mouseState.y-resY/2)/200.0;
77 else{
78 if(incl<-1.45)incl=-1.44;
79 if(incl>1.45)incl=1.44;
80
81 }
82
83 if(al_key_down(&keyState, ALLEGRO_KEY_RIGHT))
84 rotX += 0.1;
85 if(al_key_down(&keyState, ALLEGRO_KEY_LEFT))
86 rotX -= 0.1;
87 if(al_key_down(&keyState, ALLEGRO_KEY_UP)){
88 if(incl<1.45)incl += 0.1;//limitamos el angulo max de inclinación a PI/2
89 }
90 if(al_key_down(&keyState, ALLEGRO_KEY_DOWN)){
91 if(incl>-1.45)incl -= 0.1;//limitamos el angulo min de inclinación a -PI/2
92 }
93 if(al_key_down(&keyState, ALLEGRO_KEY_A)){
94 x-=moveSpeed*cos(rotX+1.57);
95 z-=moveSpeed*sin(rotX+1.57);
96 }
97 if(al_key_down(&keyState, ALLEGRO_KEY_D)){
98 x+=moveSpeed*cos(rotX+1.57);
99 z+=moveSpeed*sin(rotX+1.57);
100 }
101 if(al_key_down(&keyState, ALLEGRO_KEY_W)){
102 x+=moveSpeed*cos(rotX);
103 z+=moveSpeed*sin(rotX);
104 }
105 if(al_key_down(&keyState, ALLEGRO_KEY_S)){
106 x-=moveSpeed*cos(rotX);
107 z-=moveSpeed*sin(rotX);
108 }
109 if(al_key_down(&keyState, ALLEGRO_KEY_Z))
110 Angle++;
111 if(al_key_down(&keyState, ALLEGRO_KEY_X))
112 Angle--;
113
114 al_set_mouse_xy(display, resX/2, resY/2);
115 draw = true;
116 }
117
118 if(draw)
119 {
120 draw = false;
121 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT| GL_STENCIL_BUFFER_BIT);
122
123 glMatrixMode(GL_PROJECTION); //Open the projection matrix as current
124 glLoadIdentity(); //clear its contents to an identity matrix (which doesn't change anything when applied)
125 gluPerspective( Angle, Ratio, ClippingBorder1, ClippingBorder2);
126 glMatrixMode(GL_MODELVIEW); //select the matrix
127 glLoadIdentity(); //clear ...
128 gluLookAt (x,y,z,x+cos(incl)*cos(rotX),y+sin(incl),z+cos(incl)*sin(rotX),0,1,0);//CameraX , CameraY, CameraZ, SceneX, SceneY, SceneZ, UpVectorApexX, UpVectorApexY, UpVectorApexZ );
129
130 DrawCube(); //Draw CUBE
131 glBegin(GL_QUADS); // Draw SINLGE FACE behing CUBE
132
133 glColor3f(0.0f,1.0f,0.0f); // Color Red
134 glVertex3f( 1.0f, 1.0f, -3.0f); // Top Right Of The Quad (Front)
135 glVertex3f(-1.0f, 1.0f, -3.0f); // Top Left Of The Quad (Front)
136 glVertex3f(-1.0f,-1.0f, -3.0f); // Bottom Left Of The Quad (Front)
137 glVertex3f( 1.0f,-1.0f, -3.0f); // Bottom Right Of The Quad (Front)
138 glEnd();
139
140 glFlush();
141 al_wait_for_vsync();
142 al_flip_display();
143 al_clear_to_color(al_map_rgb(0, 0, 0));
144 }
145 }
146
147 printf("Finalizado por el usuario.");
148 al_rest(1.0);
149 al_destroy_display(display);
150 al_destroy_timer(timer);
151 al_destroy_event_queue(event_queue);
152 return 0;
153}
154
155
156void DrawCube(void)
157{
158 // Draw The Cube Using quads
159 glColor3f(0.0f,1.0f,0.0f); // Color Blue
160 glBegin(GL_QUADS);
161 glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
162 glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
163 glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
164 glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
165glEnd();
166 glColor3f(1.0f,0.5f,0.0f); // Color Orange
167 glBegin(GL_QUADS);
168 glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
169 glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
170 glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
171 glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
172glEnd();
173 glColor3f(1.0f,0.0f,0.0f); // Color Red
174 glBegin(GL_QUADS);
175 glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
176 glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
177 glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
178 glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
179glEnd();
180 glColor3f(1.0f,1.0f,0.0f); // Color Yellow
181 glBegin(GL_QUADS);
182 glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
183 glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
184 glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
185 glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
186glEnd();
187
188 glColor3f(0.0f,0.0f,1.0f); // Color Blue
189 glBegin(GL_QUADS);
190 glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
191 glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
192 glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
193 glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
194glEnd();
195 glColor3f(1.0f,0.0f,1.0f); // Color Violet
196 glBegin(GL_QUADS);
197 glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
198 glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
199 glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
200 glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
201 glEnd(); // End Drawing The Cube
202
203}
THANKYOU so much in advance!! Id be so pleased if you can solve this... |
Arthur Kalliokoski
Second in Command
February 2005
|
If you're on Linux and you're using the noveau (sp?) display driver, that's probably the reason. Use the proprietary binary driver from the OEM. They all watch too much MSNBC... they get ideas. |
Trent Gamblin
Member #261
April 2000
|
joecarl said: As u can see it must be related No, I can't see anything from that tiny screenshot and little bit of code. Explain the problem, what you expect to see and what you actually see. BTW Allegro 5.1 has functions for managing the depth buffer. If you don't want to use them at least look at the documentation. The function names are different but you can easily match them up to OpenGL functions, and the documentation is pretty clear about how you use the depth buffer (at least it was what I needed to figure it out.)
|
joecarl
Member #14,983
March 2013
|
Ok sorry for my bad explanation. What I see: a cube and a green face, the green face is further than the cube but I can see it somewhat overlapping the cube.(weird rendering maybe?) What I want to see: a cube and a green face, the green face should just be seen where the cube is not covering it. Arthur: Im using Windows7, sorry for not telling that in my 1st post :S Trent: I can't find any of those funcions to hadle depth buffer in allegro5. U sure they are in the documentation? PS: I think I should forget about this and jump to OpenGL3.x and maybe this was just the result of using deprecated libraries, what you think? |
someone972
Member #7,719
August 2006
|
Although learning OpenGL 3 would be a good idea, the example you have should still work. I couldn't find anything wrong with it, so I can only guess that the driver is to blame. Have you updated your graphics driver recently? Also what graphics card do you have? ______________________________________ |
pkrcel
Member #14,001
February 2012
|
Link to Allegro 5.1 reference manual: http://alleg.sourceforge.net/a5docs/refman Have a look at the graphics routines, I think there you can find something of interest, even thou me too can't really see what could be wrong with your code. It is unlikely that Google shares your distaste for capitalism. - Derezo |
joecarl
Member #14,983
March 2013
|
Thanks for your answers. I have an ATI Mobility Radeon HD 3650 ive just updated my drivers but still get this weird result... Im hitting my head against the wall... I really dont know what to do to solve this D: EDIT: I run the .exe on my friend's PC and got the same problem :S |
pkrcel
Member #14,001
February 2012
|
I've compiled and run this with MSVC11 on windows7. The program behaves a bit differently from what I expected.....but I do not see any "weird" transparency....instead seems that depth is inddeed managed wrong (in the screenshot I have attached I expected the green face to stand BEHIND the violet cube face). Using the keyboard thou, I would have expected to be able to rotare allround the two objects but seems to me that the shapes are mostly deforming instead....so for me it's a bit hard to understand what's going on. It is unlikely that Google shares your distaste for capitalism. - Derezo |
Elias
Member #358
May 2000
|
Your code is wrong, from the gluPerspective documentation http://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml: Quote:
void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar); But you have: ClippingBorder1=0 ... gluPerspective( Angle, Ratio, ClippingBorder1, ClippingBorder2);
-- |
joecarl
Member #14,983
March 2013
|
OH MY GOD! SO stupid me! Ty so much!!! it was just that I set zNear to 0.1 and now it runs perfect!! Thanks so much really thanks so much!! |
pkrcel
Member #14,001
February 2012
|
Okay, now it's official...I do not understamd what's going on in this demo. It is unlikely that Google shares your distaste for capitalism. - Derezo |
joecarl
Member #14,983
March 2013
|
pkrcel said: Okay, now it's official...I do not understamd what's going on in this demo. Why? Its just a bit of code which draws a cube and a single face, I set up the keyboard to be able to move with WASD, increase or reduce field of vision with ZX, and look around with mouse. I had a mistake setting zNear to 0 that's all. |
pkrcel
Member #14,001
February 2012
|
Actually, most of the time the cube and the face get badly deformed both by mouse movement and using keys.....that's what I do not understand It is unlikely that Google shares your distaste for capitalism. - Derezo |
Arthur Kalliokoski
Second in Command
February 2005
|
pkrcel said: the cube and the face get badly deformed both by mouse movement and using keys I didn't look at the code, but that can happen if you keep rotating the same matrix because errors accumulate. You're supposed to set a matrix to the identity, then rotate by the accumulated rotation angle. They all watch too much MSNBC... they get ideas. |
pkrcel
Member #14,001
February 2012
|
Didnt' really got around checking the code or anything....you know I had a very hard Friday there....it finished 11AM this morning (thou I was out skiing nonetheless). I actually feel very uncomfortable with the way this demo rotates/stretches things. Setting the clippingborder1 to positive resolved the depth check problems, but then the rest to me is really diffcult to copy with. Anyway cookies to joecarl, this ismple demo got me thinking about a couple thing I could do in the future....
It is unlikely that Google shares your distaste for capitalism. - Derezo |
|