Hi All,
I've been learning SDL2 and Opengl for about a month now and need advice on my code alot of which from various resources online.
I've also been adding it all up ie. background music and texture so now I would like to add background Image to the rotating textured cube which is essentially a skybox. Any advice on the skybox and on my code is welcomed. Feel free to use this code because I'm pretty sure you'll find similar ones out there.
My Code here:
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_image.h>
#include <GL/glu.h>
void Reshape();
void Render();
double rotquad;
int WIDTH = 640;
int HEIGHT = 480;
SDL_Window *window;
SDL_Surface *surface;
Mix_Music *music = NULL;
GLuint texture;
int main(int argc, char* argv[])
{
Uint32 last_time = SDL_GetTicks();
Uint32 current_time,ellapsed_time;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
Mix_Init(MIX_INIT_MP3);
IMG_Init(IMG_INIT_JPG);
SDL_ShowCursor(0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
window = SDL_CreateWindow("3D",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
surface = IMG_Load("texture.jpg");
Reshape();
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
Mix_AllocateChannels(0);
music = Mix_LoadMUS("sound.wav");
Mix_PlayMusic(music, -1);
while (1)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
Mix_FreeMusic(music);
Mix_CloseAudio();
Mix_Quit();
IMG_Quit();
SDL_Quit();
return 0;
break;
}
}
Mix_PlayingMusic();
current_time = SDL_GetTicks();
ellapsed_time = current_time - last_time;
last_time = current_time;
rotquad += 0.05 * ellapsed_time;
Render();
ellapsed_time = SDL_GetTicks();
if (ellapsed_time < 10)
{
SDL_Delay(10 - ellapsed_time);
}
}
return 0;
}
void Reshape()
{
glClearColor(0.3, 0.3, 0.3, 1.0);
glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, WIDTH / HEIGHT, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w,surface->h, 0, GL_RGB,GL_UNSIGNED_BYTE,surface->pixels);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
glBindTexture(GL_TEXTURE_2D,texture);
glRotated(rotquad, 0, 1, 0);
glRotated(rotquad, 1, 1, 1);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, -1, 1);
glTexCoord2i(1, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 0); glVertex3f(-1, 1, -1);
glTexCoord2i(0, 0); glVertex3f( 1, -1, -1);
glTexCoord2i(0, 1); glVertex3f( 1, -1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, 1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, -1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, -1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, 1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, 1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, -1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, -1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);
glTexCoord2i(0, 0); glVertex3f(-1, -1, 1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, 1);
glEnd();
glDisable(GL_TEXTURE_2D);
glFlush();
SDL_GL_SwapWindow(window);
}
See attached file for screenshot.