Problem with SDL + OpenGL

Pages: 12
closed account (N36fSL3A)
Well first off I'm almost sure the problem is with me being stupid, or OpenGL, not SDL.

Well I'm following the tutorials from "thecplusplusguy" on youtube, and I get not compiler errors, but the screen is messed up. Whenever It loads up, it displays the image behind it, not what I coded it to do.

Again I'm not sure what exactly is the problem. Here are screenies.
http://prntscr.com/1agn61
http://prntscr.com/1agn8f


Here's my code.

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
#include <iostream>

#include <SDL.h>
#include <SDL_opengl.h>
#include <gl/GL.h>
#include <gl/GLU.h>

Uint32 start;

int Triangle;

bool Running;
char* Title;

SDL_Surface* Screen = NULL;
SDL_Event event;

float angle;

int Init();  
void Render();
void DrawCube(float size);

int main(int argc, char* argv[])
{
	if(Init() == 1) {std::cout << "Failure to set up SDL &/or OpenGL.\n"; system("PAUSE"); SDL_Quit(); return 1;}
	
	while(Running)
	{
		start = SDL_GetTicks();
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
			{
				case SDL_QUIT:
					Running = false;
				break;
			}
		}
		Render();

		angle += 0.5;
		if(angle < 360) {angle -= 360;}
		if(1000/30 > SDL_GetTicks() - start)
		{
			SDL_Delay(1000/30 - (SDL_GetTicks() - start));
		}
	}

	SDL_Quit();

	return 0;
}

int Init()
{
	SDL_Init(SDL_INIT_EVERYTHING);

	SDL_WM_SetCaption("OpenGL Tut", "OpenGL Tutorial");

	Screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_OPENGL);

	if(Screen != NULL) {Running = true;}
	else               {return 1;}

	start = SDL_GetTicks();

	glClearColor(0.0, 0.0, 0.0, 1.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45, 640.0/480.0, 1.0, 500.0);
	glMatrixMode(GL_MODELVIEW);
	glEnable(GL_DEPTH_TEST);

	return 0;
}

void Render()
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	glTranslatef(0.0, 0.0, -0.5);
	glRotatef(angle, 1.0, 1.0, 1.0);
	
	DrawCube(1.0);
	/*glBegin(GL_TRIANGLES);
		
		glColor3f(1.0, 0.0, 0.0);
		glVertex3f(0.0, 2.0, -5.0);   glColor3f(0.0, 1.0, 0.0);
		glVertex3f(-2.0, -2.0, -5.0); glColor3f(0.0, 0.0, 1.0);
		glVertex3f(2.0, -2.0, -5.0);
	glEnd();*/
}

void DrawCube(float size)
{
	glBegin(GL_QUADS);
		// front face
		glColor3f(1.0,0.0,0.0);
		glVertex3f(size/2,size/2,size/2);
		glVertex3f(-size/2,size/2,size/2);
		glVertex3f(-size/2,-size/2,size/2);
		glVertex3f(size/2,-size/2,size/2);
		// left face
		glColor3f(0.0,1.0,0.0);
		glVertex3f(-size/2,size/2,size/2);
		glVertex3f(-size/2,-size/2,size/2);
		glVertex3f(-size/2,-size/2,-size/2);
		glVertex3f(-size/2,size/2,-size/2);
		// back face
		glColor3f(0.0,0.0,1.0);
		glVertex3f(size/2,size/2,-size/2);
		glVertex3f(-size/2,size/2,-size/2);
		glVertex3f(-size/2,-size/2,-size/2);
		glVertex3f(size/2,-size/2,-size/2);
		// right face
		glColor3f(1.0,1.0,0.0);
		glVertex3f(size/2,size/2,size/2);
		glVertex3f(size/2,-size/2,size/2);
		glVertex3f(size/2,-size/2,-size/2);
		glVertex3f(size/2,size/2,-size/2);
		// top face
		glColor3f(1.0,0.0,1.0);
		glVertex3f(size/2,size/2,size/2);
		glVertex3f(-size/2,size/2,size/2);
		glVertex3f(-size/2,size/2,-size/2);
		glVertex3f(size/2,size/2,-size/2);
		// bottom face
		glColor3f(0.0,1.0,1.0);
		glVertex3f(size/2,-size/2,size/2);
		glVertex3f(-size/2,-size/2,size/2);
		glVertex3f(-size/2,-size/2,-size/2);
		glVertex3f(size/2,-size/2,-size/2);
	glEnd();
}


The program doesn't slow down or anything, it exits when I hit X with no lag, the problem is it simply won't display the rotating cube.
This does not address your immediate problem... but you are using very old and outdated version of OpenGL.

I recommend you ditch whatever tutorial you're reading, as it is clearly out of date.

This is a very good tutorial on getting started with modern OpenGL:

http://www.arcsynthesis.org/gltut/
closed account (o1vk4iN6)
Doesn't seem to make any mention of bump mapping and other texturing techniques, though it seems advanced lighting hasn't been written yet ?
closed account (N36fSL3A)
Yea I know but I want to finish these few tutorials to make a wolfenstien-type game. Then I will move on.
By looking at the screenshots, it looks as if you never clear your buffer(s).
Debug your program and set a breakpoint at your render() function, to see if it's ever executed. Maybe your global bool "running" somehow never gets initialized(?).
Last edited on
I really like lazyfoo's tutorial on SDL found here:
http://lazyfoo.net/SDL_tutorials/
¿shouldn't you swap the buffer (send what you draw to the screen)?
closed account (N36fSL3A)
I didn't? Lol wow. Kevin I know SDL quite well, I'm moving on to opengl.

Let me check. I'll post here again if I have any troubles.
xerzi wrote:
Doesn't seem to make any mention of bump mapping and other texturing techniques, though it seems advanced lighting hasn't been written yet ?


Yeah, but it's still a great tutorial for getting started with modern OpenGL.

Fredbill30 wrote:
Yea I know but I want to finish these few tutorials to make a wolfenstien-type game. Then I will move on.


If that's really what you want, that's cool. But it sounds to me like you're just wasting a step.

Modern OpenGL isn't any harder to learn. So it makes more sense to just learn it right out of the gates.
Add SDL_GL_SwapBuffers(); after the call to DrawCube();
closed account (N36fSL3A)
Did that and now this happened

http://prntscr.com/1aua2n
I assume you're referring to it not being a cube, I don't know opengl but I would guess the vertices are wrong.
closed account (N36fSL3A)
I seriously have no clue. I just want to be able to read from models and then work on my game. Eh.
closed account (o1vk4iN6)
The vertices are modified by matrices on the GPU, in the terms of the fixed pipeline there are only 4 ... http://www.opengl.org/sdk/docs/man2/xhtml/glMatrixMode.xml
I've never used the COLOR or TEXTURE matrices though it doesn't seem like they modify the position of the vertices like ModelView and Projection do.

If you were to use opengl 3.1+ you would have had to written your own shader and would have had to have known what the matrices actually do. Though I mean you shouldn't just be copying and pasting code without knowing what it does just so you can move on to rendering models. If you plan on doing any kind of animation you will need to learn more about matrices. Try moving the object further away from the camera as 0.5 isn't that far away if the cube is 1.0 in size and is centred on the origin.

 
glTranslatef(0.0, 0.0, -2.0f);


Also because you are probably clipping most of the cube...

 
gluPerspective(45, 640.0/480.0, 1.0, 500.0); // 1.0 near plane is a bit big for the scene you have 
Last edited on
closed account (N36fSL3A)
I didn't copy and paste, I wrote it myself by hand.
What would a good perspective be
Last edited on by Fredbill30
closed account (o1vk4iN6)
So if I went to look at the tutorial you used, you'd have different values set in gluPerspective than the tutorial uses ?
Last edited on
closed account (N36fSL3A)
Yes.
1
2
glTranslatef(0.0, 0.0, -2.5f);
gluPerspective(45, 640.0/480.0, 0.5, 500.0);
Worked nicely for me.
Fredbill30 wrote:
Did that and now this happened

http://prntscr.com/1aua2n


That cube is being rendered, the problem is that most of it is being clipped because it's too close to the camera. What you're seeing on screen is only the corner of the cube that is furthest away from the camera.

Move the cube further away on the Z axis (you want a 'lower' number for glTranslate, like -20 or something)


EDIT:

Didn't see naraku9333's reply. Moving the camera backwards post projection like he is doing will work too, but is a little weird.
Last edited on
closed account (o1vk4iN6)
Fredbill30 wrote:
Yes.


...

http://i.imgur.com/8Uw0Lwg.png

Not only do you have all the same values, you also have the same format for the first parameter. He has it as an integer even though the function takes a double, just like you.
Last edited on
Pages: 12