Reputation: 139
Here is my code:
int main(int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
return false;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 ) ;
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 ) ;
if((window = SDL_CreateWindow("SDL opengl",100,100,640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN)) == NULL) {
return false;
}
maincontext = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(0);
glewExperimental=true;
GLenum err=glewInit();
Chapter* chapter1 = new Chapter();
chapter1->init();
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,640.0/480.0,1.0,500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
SDL_Event Event;
while(Running) {
while(SDL_PollEvent(&Event)) {
event_update(&Event);
}
//main loop
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
glTranslatef(3.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glEnd();
SDL_GL_SwapWindow(window);
}
return 0;
}
And my problem is that nothing more is visible just the cleared color. What can be the problem with it? (and when Im init.ing there is no error or something) Im using Visual Stuido 2010 if its important.
Upvotes: 0
Views: 2684
Reputation: 2603
From the looks of it you have your near clipping plane at 1.0 and your far clipping plane at 500.0
but you draw at a z-distance of 0.0
I would try to move the drawing into the frustum (i.e. inside the range of [1.0, 500.0]) to see if you can get your geometry visible as a first step.
glVertex3f (x, y, z); // here your z value is 0, I suggest this could be the problem.
I thought I might elaborate a bit more here, as I didn't have much time earlier.
When you do the glTranslate...
in your update code this will be called once every update, which is pretty often one might assume, resulting in your geometry moving 3 units in the x-axis every time it's being called (i.e. probably not what you meant). Instead I would suggest pushing the matrix to do this transform, and popping it when you are done with your translation. OpenGL provide a matrix stack so basically what you do when you push the matrix is that you "store it for later", then you do your transforms and rendering, then you pop the matrix, resulting in you being back at square one. This way you will not translate your matrix a new distance every update, but rather you will move your matrix from origo to the positions where you want to draw every update.
So my suggestion would be to change your code into something like this for the Main loop:
//main loop
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
/* push the current state of the matrix (i.e. store it so we don't make the mentioned change per update */
glPushMatrix();
/* think of it as moving your camera, you positioned your camera (frustum) at origo looking at everything inside a frustum shape one unit in front of the camera to 500 units in front of the camera, at an angle and so on.. so we want to draw our triangle to the left (from your view) 3 units and 10 units into the screen */
glTranslate3f(-3.0f, 0.0f, -10.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
/* translate again, i.e. "move your camera" this time 6 units to the right, 3 units to compensate for the move you did to draw your triangle, and 3 units to place it in the right end. this time we do not move it in the z-axis, because we are already 10 pixels into the screen. */
glTranslatef(6.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glEnd();
/* pop the matrix changes we made with glTranslatef, so that we are back at the origo looking down negative z-axis so that the next draw-call we will again go 3 units left, 10 units down the z-axis and draw a triangle and so on.. hope it makes sense */
glPopMatrix();
SDL_GL_SwapWindow(window);
I hope it at least help you to get something to render in a visible way! If it all seem unclear I can try to explain it a bit further, but hopefully this renders in a way so that you can start playing around with it a bit and get a feel for how it fit together.
Upvotes: 1