What I'm wondering, is let's say the 3d scene is drawn. Is there a way to then draw in 2d flat on the screen? For example, if I wanted to have a HUD or mouse cursor over the scene, could I just draw those straight to the screen flat?
I know I could figure out all the business as to where those things would be in 3d space and constantly move them with the camera, but it'd be a lot easier if there was just a way to draw it 2d after the 3d was done
After drawing the 3d scene, simply reset the modelview and projection matrix, then set an ortho mode (glOrtho).
1 | glMatrixMode(GL_PROJECTION); |
2 | glLoadIdentity(); |
3 | gluPerspective(45.0f, float(SCREEN_W)/SCREEN_H, 1.0f, 10000.0f); |
4 | glMatrixMode(GL_MODELVIEW); |
5 | |
6 | while(playing) |
7 | { |
8 | glLoadIdentity(); |
9 | draw_3d_stuff_here(); |
10 | |
11 | glMatrixMode(GL_PROJECTION); |
12 | glPushMatrix(); |
13 | glLoadIdentity(); |
14 | glOrtho(0, SCREEN_W, SCREEN_H, 0, -1, 1); |
15 | glMatrixMode(GL_MODELVIEW); |
16 | |
17 | draw_2d_stuff_here(); |
18 | |
19 | glMatrixMode(GL_PROJECTION); |
20 | glPopMatrix(); |
21 | glMatrixMode(GL_MODELVIEW); |
22 | } |
It doesn't appear to be working. My mouse cursor, which worked in 2D, has stopped showing up when 3D was added. I put in the code that you suggested and nothing happened differently.
Show the code. Can't tell what's wrong without seeing what you're doing.
Initial setup
else if (projection_type == FRUSTUM) { glMatrixMode(GL_PROJECTION); glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glLoadIdentity(); gluPerspective(45.0f, (GLfloat)SCREEN_WIDTH / (GLfloat)SCREEN_HEIGHT, 1.0f, 1000.0f); glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); } }
This is the drawing function:
void objGfx::DoDraw(int s) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Do3D(s); Do2D(s); allegro_gl_flip(); }
3D function
void objGfx::Do3D(int s) { switch (s) { case SCENE_TITLE: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); break; case SCENE_NONE: glClearColor(0.0f, 0.0f, 0.0f, 0.0f); default: break; } }
2D Function
1 | void objGfx::Do2D(int s) |
2 | { |
3 | glMatrixMode(GL_PROJECTION); |
4 | glPushMatrix(); |
5 | glLoadIdentity(); |
6 | glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1); |
7 | glMatrixMode(GL_MODELVIEW); |
8 | |
9 | switch (s) |
10 | { |
11 | case SCENE_TITLE: |
12 | break; |
13 | case SCENE_NONE: |
14 | default: |
15 | break; |
16 | } |
17 | |
18 | if (Mouse->Visible()) |
19 | { |
20 | Draw->Sprite(Mouse->TextureSprite(), Mouse->TextureMask(), |
21 | Mouse->X(), Mouse->Y(), |
22 | Mouse->X() + Mouse->CursorWidth(), |
23 | Mouse->Y() + Mouse->CursorHeight()); |
24 | } |
25 | |
26 | if (DEBUG) |
27 | { |
28 | Debug->Output(); |
29 | } |
30 | |
31 | glMatrixMode(GL_PROJECTION); |
32 | glPopMatrix(); |
33 | glMatrixMode(GL_MODELVIEW); |
34 | } |
What are the Mouse and Draw objects? At a glance, everything you pasted looks alright, so the problem is somewhere else.
edit: I figured out the problem actually does have to do with how the mouse is being drawn, but I don't know what. I've since moved on to an attempt at a different solution.
Draw->Sprite
1 | void objDraw::Sprite(GLuint sprite, GLuint mask, int x1, int y1, int x2, int y2) |
2 | { |
3 | glEnable(GL_TEXTURE_2D); |
4 | glEnable(GL_BLEND); |
5 | glBlendFunc(GL_DST_COLOR, GL_ZERO); |
6 | |
7 | glBindTexture(GL_TEXTURE_2D, mask); |
8 | |
9 | glBegin(GL_QUADS); |
10 | glTexCoord3f(0, 0, 0); glVertex3f(x1, y2, 0); |
11 | glTexCoord3f(1, 0, 0); glVertex3f(x2, y2, 0); |
12 | glTexCoord3f(1, 1, 0); glVertex3f(x2, y1, 0); |
13 | glTexCoord3f(0, 1, 0); glVertex3f(x1, y1, 0); |
14 | glEnd(); |
15 | |
16 | glColor4f(1.0f, 1.0f, 1.0f, 1.0f); |
17 | |
18 | glBindTexture(GL_TEXTURE_2D, sprite); |
19 | glBlendFunc(GL_ONE, GL_ONE); |
20 | |
21 | glBegin(GL_QUADS); |
22 | glTexCoord3f(0, 0, 0); glVertex3f(x1, y2, 0); |
23 | glTexCoord3f(1, 0, 0); glVertex3f(x2, y2, 0); |
24 | glTexCoord3f(1, 1, 0); glVertex3f(x2, y1, 0); |
25 | glTexCoord3f(0, 1, 0); glVertex3f(x1, y1, 0); |
26 | glEnd(); |
27 | } |
Mouse everything
1 | #include <allegro.h> |
2 | #include <alleggl.h> |
3 | |
4 | #include "mouse.h" |
5 | |
6 | #include "defines.h" |
7 | #include "globals.h" |
8 | |
9 | objMouse::objMouse() |
10 | { |
11 | visible = false; |
12 | } |
13 | |
14 | objMouse::~objMouse() |
15 | { |
16 | destroy_bitmap(m_sprite); |
17 | destroy_bitmap(m_mask); |
18 | } |
19 | |
20 | int objMouse::X() |
21 | { |
22 | return mouse_x; |
23 | } |
24 | |
25 | int objMouse::Y() |
26 | { |
27 | return mouse_y; |
28 | } |
29 | |
30 | int objMouse::Z() |
31 | { |
32 | return mouse_z; |
33 | } |
34 | |
35 | int objMouse::LeftButton() |
36 | { |
37 | if (mouse_b & 1) |
38 | { |
39 | return true; |
40 | } |
41 | else |
42 | { |
43 | return false; |
44 | } |
45 | } |
46 | |
47 | int objMouse::RightButton() |
48 | { |
49 | if (mouse_b & 2) |
50 | { |
51 | return true; |
52 | } |
53 | else |
54 | { |
55 | return false; |
56 | } |
57 | } |
58 | |
59 | int objMouse::MiddleButton() |
60 | { |
61 | if (mouse_b & 4) |
62 | { |
63 | return true; |
64 | } |
65 | else |
66 | { |
67 | return false; |
68 | } |
69 | } |
70 | |
71 | BITMAP* objMouse::CursorSprite() |
72 | { |
73 | return m_sprite; |
74 | } |
75 | |
76 | BITMAP* objMouse::CursorMask() |
77 | { |
78 | return m_mask; |
79 | } |
80 | |
81 | bool objMouse::Visible() |
82 | { |
83 | return visible; |
84 | } |
85 | |
86 | int objMouse::CursorWidth() |
87 | { |
88 | return m_sprite->w; |
89 | } |
90 | |
91 | int objMouse::CursorHeight() |
92 | { |
93 | return m_sprite->h; |
94 | } |
95 | |
96 | GLuint objMouse::TextureSprite() |
97 | { |
98 | return texSprite; |
99 | } |
100 | |
101 | GLuint objMouse::TextureMask() |
102 | { |
103 | return texMask; |
104 | } |
105 | |
106 | void objMouse::SetCursorSprite(const char* filename) |
107 | { |
108 | m_sprite = load_bitmap(filename, NULL); |
109 | texSprite = allegro_gl_make_texture(m_sprite); |
110 | } |
111 | |
112 | void objMouse::SetCursorMask(const char* filename) |
113 | { |
114 | m_mask = load_bitmap(filename, NULL); |
115 | texMask = allegro_gl_make_texture(m_mask); |
116 | } |
117 | |
118 | void objMouse::Show() |
119 | { |
120 | visible = true; |
121 | } |
122 | |
123 | void objMouse::Scare() |
124 | { |
125 | visible = false; |
126 | } |
127 | |
128 | void objMouse::Draw() |
129 | { |
130 | allegro_gl_set_allegro_mode(); |
131 | |
132 | draw_sprite(screen, m_sprite, mouse_x, mouse_y); |
133 | |
134 | allegro_gl_unset_allegro_mode(); |
135 | } |
Note the Mouse->Draw function isn't actually used... I just forgot to take it out, hehe