#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <stdio.h>
#include <OpenGL/gl.h>
#include <string.h>
using namespace std;
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
bool SetOpenGLAttributes()
{
// SDL_GL_CONTEXT_CORE gives us only the newer version, deprecated functions are disabled
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
return true;
}
int main( int argc, char* args[] )
{
SDL_Window* window = NULL;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL );
if( window == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//creating new context
SDL_GL_CreateContext(window);
SetOpenGLAttributes();
SDL_GL_SetSwapInterval(1);
//cube front
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-.5, .5, -.5);
glVertex3f(.5, .5, -.5);
glVertex3f(.5, -.5, -.5);
glVertex3f(-.5, -.5, -.5);
glEnd();
//cube top
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex3f(-.5,.5,-.5);
glVertex3f(.5, .5, -.5);
glVertex3f(.5, .5, .5);
glVertex3f(-.5, .5, .5);
glEnd();
//cube bottom
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-.5,-.5,-.5);
glVertex3f(.5, -.5, -.5);
glVertex3f(.5, -.5, .5);
glVertex3f(-.5, -.5, .5);
glEnd();
//cube right
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(.5, -.5, -.5);
glVertex3f(.5, -.5, .5);
glVertex3f(.5, .5, .5);
glVertex3f(.5, .5, -.5);
glEnd();
//cube left
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-.5, -.5, -.5);
glVertex3f(-.5, .5, -.5);
glVertex3f(-.5, .5, .5);
glVertex3f(-.5, -.5, .5);
glEnd();
//cube back
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-.5, .5, .5);
glVertex3f(-.5, -.5, .5);
glVertex3f(.5, -.5, .5);
glVertex3f(.5, .5, .5);
glEnd();
glFlush();
SDL_GL_SwapWindow(window);
bool running = true;
while(running){
glRotatef(1, 1, 0, 0);
glFlush();
SDL_GL_SwapWindow(window);
SDL_Delay(100);
}
}
}
//Destroy window
//SDL_DestroyWindow( window );
//Quit SDL subsystems
//SDL_Quit();
return 0;
}
I am in the process of learning openGL and SDL. I managed to get this "cube" to show up, but it just looks like a red rectangle. I want to rotate the cube to make sure that my code is in fact making a cube, however when I rotate and then swap windows, the cube only flickers. It seems that the second buffer window is just black, but when it switches again, it is always the same red rectangle. Why can't I rotate my cube and why is my second buffer window black?