can't translate objects/primitives along z-axis with opengl

I can translate objects along the x and y axis by passing variables to them that are incremented or decremented by 0.05 upon pressing the a,d or space bar. But when i try to translate along the z-axis with the same glTranslatef function nothing happens. well it just doesn't render it depending on the value i put in the z parameter. I have listed my code below. Im trying to stimulate "camera" movement in the 3d world and it works even though i haven't set up a PROJECTIONS mode with the glMartrixMode function or haven't set up a camera.

I want to think that since there is no camera moving along the z-axis with any large amount the object or primitive is rendered out of view giving the appearance of a blank scene. If you guys could help me with setting up the camera or just get it where i can translate objects along the z-axis to stimulate movement then that would be great. And also as you can see from my code im having a hard time rotating the camera on its point (or technically...rotating the world around the cameras point.) also i try to set up my rendering code in a seperate function "drawNewScene" outside of the winmain function but nothing is rendered and if it is it is only for a second.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**************************
 * Includes
 *
 **************************/

#include <windows.h>
#include <gl/gl.h>
#include <GL/glut.h>


/**************************
 * Function Declarations
 *
 **************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
double INzpos, INxpos, INypos, INRypos;



/**************************
 * WinMain
 *
 **************************/

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int iCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    INzpos= 0.0;
    INxpos= 0.0;
    INypos= 0.0;
    INRypos= 0.0;

    /* register window class */
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);

    /* create main window */
    hWnd = CreateWindow (
      "GLSample", "OpenGL Sample", 
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      0, 0, 1300, 760,
      NULL, NULL, hInstance, NULL);

    /* enable OpenGL for the window */
    EnableOpenGL (hWnd, &hDC, &hRC);

    /* program main loop */
    while (!bQuit)
    {
        /* check for messages */
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else
        {
            /* OpenGL animation code goes here */
            
         

/* Program entry point */




            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            glClear (GL_COLOR_BUFFER_BIT);

            glPushMatrix ();
           
            glTranslated(INxpos, 0.0, INzpos);
            glRotatef(INRypos, 0.0, 1.0, 0.0);
            glBegin (GL_TRIANGLES);
            glColor3f (1.0f, 0.0f, 0.0f);   glVertex3f (0.0f, 1.0f, 0.0f);
            glColor3f (0.0f, 1.0f, 0.0f);   glVertex3f (0.87f, -0.5f, 0.0f);
            glColor3f (0.0f, 0.0f, 1.0f);   glVertex3f (-0.87f, -0.5f, 0.0f);
            glEnd ();
            glPopMatrix ();

            SwapBuffers (hDC);
            Sleep (1);

        }
    }

    /* shutdown OpenGL */
    DisableOpenGL (hWnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow (hWnd);

    return msg.wParam;
}


/********************
 * Window Procedure
 *
 ********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
                          WPARAM wParam, LPARAM lParam)
{

    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        case 'w':
             INzpos = INzpos + 0.05;
             return 0;
        case 'D':
             INxpos = INxpos - 0.051;
             return 0;
        case 'A':
             INxpos = INxpos + 0.05;
             return 0;
        case 'S':
             INzpos = INzpos - 0.05;
             return 0;
        case VK_SPACE:
             INypos = INypos + 0.015;
             return 0;
        case 'J':
             INRypos = INRypos + 10.0;
             INxpos = INxpos + 0.02;
        }
        return 0;

    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}


/*******************
 * Enable OpenGL
 *
 *******************/

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC (hWnd);

    /* set the pixel format for the DC */
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}


/******************
 * Disable OpenGL
 *
 ******************/

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}
You need to setup the projection matrix. The projection matrix essentially tells OpenGL how to map eye-space coordinates into screen-space. If you don't set a projection matrix, it'll probably use some sort of orthographic projection by default though the OpenGL standard is mum on what the default is. You can't translate in Z because it looks like your projection matrix is set to an orthographic projection.

What you need to do is setup a perspective projection:

e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* create main window */
hWnd = CreateWindow (
    "GLSample", "OpenGL Sample", 
    WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
    0, 0, 1300, 760,
    NULL, NULL, hInstance, NULL);

/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);

/* setup a perspective projection matrix */

//tell OpenGL we're playing with the projection matrix
glMatrixMode(GL_PROJECTION); 

//set the transformation
//first parameter: degrees in Y (bigger = wider view, smaller = narrower)
//second parameter: aspect ratio of width : height (set this to the dimensions of your viewport)
//third parameter: distance to near clipping plane (in eye space, 0.0 is at the eye, 1.0 is furthest ahead)
//fourth parameter: distance to far clipping plane (in eye space)
gluPerspective(45.0, (double)1300/(double)760, 0.0, 1.0);

//tell OpenGL we want to play with the view (translate, rotate, etc.)
glMatrixMode(GL_MODELVIEW);

/* program main loop */
while (!bQuit)
{
//... 


This also fixes your rotation problem. Note that the starting point makes it so that the triangle is really close to the eye. You'll have to translate the camera a little bit in Z at the beginning for the triangle to be completely visible.

Also, I had to set case 'w': to be a capital W for it to properly read this key.

One final note: OpenGL has no notion of a "camera". Essentially, the combination of the projection and modelview matrices forms a "camera". You can make a camera object with rotate(), translate(), and zoom() methods, but all of these will inevitably wrap some kind of transformation (rotate and translate would use the MODELVIEW matrix and zoom would use the PROJECTION matrix). Note that modelview transformations affect the objects (not the eye), so a "camera" would have to apply inverse transformations. e.g. translate left means translate object to the right, rotating around the x axis positively means rotating the objects around the x axis negatively, etc.

http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml
http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml
Last edited on
Topic archived. No new replies allowed.