SDL+OpenGL objects don't render if z axis isn't 0.

So I have this, medium sized code chunk, as a part of a game I'm starting to make:
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
#include <SDL.h>
#include <SDL_net.h>
#include <SDL_opengl.h>
#include <iostream>
#include <fstream>
#include <gl\glaux.h>

using namespace std;

SDL_Surface* screen = NULL;
TCPsocket gameconnection;
char buffer[512];
GLuint	texture[100];
float scale = 100;
SDL_Event event;

/*snip*/

bool loadtex(int position,char* filename)
{
SDL_Surface *surface;	// This surface will tell us the details of the image
GLenum texture_format;
GLint  nOfColors;

if ( (surface = SDL_LoadBMP(filename)) ) {

	// Check that the image's width is a power of 2
	if ( (surface->w & (surface->w - 1)) != 0 ) {
		cerr<<"One of the textures sizes has been modified to an inproper size.\n";
		return false;
	}

	// Also check if the height is a power of 2
	if ( (surface->h & (surface->h - 1)) != 0 ) {
        cerr<<"One of the textures sizes has been modified to an inproper size.\n";
		return false;
    }

        // get the number of channels in the SDL surface
        nOfColors = surface->format->BytesPerPixel;
        if (nOfColors == 4)     // contains an alpha channel
        {
                if (surface->format->Rmask == 0x000000ff)
                        texture_format = GL_RGBA;
                else
                        texture_format = GL_BGRA;
        } else if (nOfColors == 3)     // no alpha channel
        {
                if (surface->format->Rmask == 0x000000ff)
                        texture_format = GL_RGB;
                else
                        texture_format = GL_BGR;
        } else {
                cerr<<"The images color format has been changed improperly.\n";
                return false;
                // this error should not go unhandled
        }

	// Have OpenGL generate a texture object handle for us
	glGenTextures( 1, &texture[position] );

	// Bind the texture object
	glBindTexture( GL_TEXTURE_2D, texture[position] );

	// Set the texture's stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

	// Edit the texture object's image data using the information SDL_Surface gives us
	glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
                      texture_format, GL_UNSIGNED_BYTE, surface->pixels );
}
else {
	cerr<<"A texture that tried to load either didn't exist or was protected.\n";
	return false;
}

// Free the SDL_Surface only if it was successfully created
if ( surface ) {
	SDL_FreeSurface( surface );
}

	return true;
}



bool LoadGLTextures()								// Load Bitmaps And Convert To Textures
{
    if(!loadtex(0,"exe.bmp"))
    return false;

    return true;
}
/*draw cube function goes here*/

/*authentication functions snip*/

int main ( int argc, char** argv )
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        cerr<<"Could not start up SDL graphics and input system.\n";
        return 1;
    }
    else
        cerr<<"Started up SDL.\n";

    if(SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ) == -1)
    {
        cerr<<"Could not bind graphics to OpenGL.\n";
        return 2;
    }
    else
        cerr<<"Graphics bound to OpenGL.\n";

    if(SDLNet_Init() == -1)
    {
        cerr<<"Could not start up SDL_Net.\n";
        return 3;
    }
    else
        cerr<<"Started up SDL_Net.\n";

    if(!LoadGLTextures())
    {
        cerr<<"Had errors when trying to load textures.\n";
        return 12;
    }
    else
        cerr<<"Textures loaded.\n";

    FreeConsole();

    screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL|SDL_SWSURFACE );
    if(!screen || screen == NULL)
    {
        cerr<<"Failed to create screen.\n";
        return 4;
    }
    else
        cerr<<"Screen created.\n";

    SDL_WM_SetCaption( "Syerjchep", NULL );
        cerr<<"Caption set.\n";

    glEnable( GL_TEXTURE_2D );
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glViewport( 0, 0, 640, 480 );
    glClear( GL_COLOR_BUFFER_BIT );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho(2.0f, 640, 480, 1.0f, -1.0f, 1.0f);
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    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);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

    cerr<<"Screen identity set.\n";
	SDL_GL_SwapBuffers();
    cerr<<"Screen set up.\n";

    IPaddress ip;

    if(SDLNet_ResolveHost(&ip, "127.0.0.1", 4525) == -1)
    {
        cerr<<"Could not resolve host IP of server.\n";
        return 5;
    }
    else
        cerr<<"Host location found.\n";


    if((gameconnection = SDLNet_TCP_Open(&ip)) == NULL)
    {
        cerr<<"Could not connect to server.\n";
        return 6;
    }
    else
        cerr<<"Connected to server.\n";

    authenticate();


    cerr<<"Starting game loop.\n";

    bool done = false;
    while(!done)
    {
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
                done = true;
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();

        glTranslatef(0,0,0);						// Move Left 1.5 Units And Into The Screen 6.0
        glRotatef(0,0.0f,1.0f,0.0f);						// Rotate The Triangle On The Y axis ( NEW )

        drawcube(50,50,0,0);

        SDL_GL_SwapBuffers();
        SDL_Delay(100);
    }

    glDeleteTextures( 1, &texture[0] );
    SDLNet_Quit();
    SDL_Quit();
    return 4525;
}

When I change the third argument of the "drawcube(50,50,0,0);" by even 0.0001 the white square (texture rendering errors?) it dosn't show, but it does when it's at 0. (same goes for if I try to rotate it)

Could anyone help me get this to work for 3d stuff?
And maybe get textures to load?
And here's the draw cube function:
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
int drawcube(float x,float y,float z,int tex)
{

	glBindTexture(GL_TEXTURE_2D, texture[tex]);				// Select Our Texture
	glBegin(GL_QUADS);

		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y,  z);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(x+scale, y,  z);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(x+scale,  y+scale,  z);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(x,  y+scale,  z);	// Top Left Of The Texture and Quad
		// Back Face
		glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z-scale);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(x,  y+scale, z-scale);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(x+scale,  y+scale, z-scale);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(x+scale, y, z-scale);	// Bottom Left Of The Texture and Quad
		// Top Face
		glTexCoord2f(0.0f, 1.0f); glVertex3f(x,  y+scale, z-scale);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(x,  y+scale,  z);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(x+scale,  y+scale,  z);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(x+scale,  y+scale, z-scale);	// Top Right Of The Texture and Quad
		// Bottom Face
		glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y, z-scale);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(x+scale, y, z-scale);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(x+scale, y,  z);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z);	// Bottom Right Of The Texture and Quad
		// Right face
		glTexCoord2f(1.0f, 0.0f); glVertex3f(x+scale, y, z-scale);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(x+scale,  y+scale, z-scale);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(x+scale,  y+scale,  z);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(x+scale, y, z);	// Bottom Left Of The Texture and Quad
		// Left Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z-scale);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y,  z);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(x,  y+scale,  z);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(x,  y+scale, z-scale);	// Top Left Of The Texture and Quad

	glEnd();

return true;
} 
Topic archived. No new replies allowed.