I'm trying to display a spinning cube with 6 different textures (all jpg and jpeg) using SDL library. But the program only shows white squares only. Anyone can please explain what happened to these textures?
each function behavior
opengl_init()
: init opengl settings
init()
: init SDL settings including opengl_init()
clean_up()
: runs right before program ends
RenewTexture()
: here I want to map texture with this function
main()
: about drawing vertexes and playing each frame with spinning function
const int window_width = 1024;
const int window_height = 1024;
const int SCREEN_BPP = 32;
SDL_Event event;
SDL_Window* win = NULL;
SDL_Renderer* renderer = NULL;
GLuint texId[6];
SDL_GLContext context;
/****** Name Space for Game Variables ******/
namespace var
{
int frame_start_time = 0;
int frame_current_time = 0;
int frame_count = 0;
}
/****** Initializes Core OpenGL Features ******/
bool opengl_init()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (640.0 / 480.0), 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
std::cout << glGetError << std::endl;
if (glGetError() != GL_NO_ERROR)
{
return false;
}
return true;
}
/****** Initializes SDL, OpenGL and Video and Window ******/
bool init()
{
//if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
return false;
}
win = SDL_CreateWindow("WINDOW", 100, 100, window_width, window_height, SDL_WINDOW_OPENGL);
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
context = SDL_GL_CreateContext(win);
if (opengl_init() == false)
{
return false;
}
}
/* Removes objects before game closes */
void clean_up()
{
SDL_Quit();
}
void RenewTexture(SDL_Surface* tmpimg, int index) {
GLubyte* map = (GLubyte*)tmpimg->pixels;
glBindTexture(GL_TEXTURE_2D, texId[index]);
int w, h;
w = tmpimg->w;
h = tmpimg->h;
/*glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);*/
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGB,
w, h, 0,
GL_RGB, GL_UNSIGNED_BYTE,
map
);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glBindTexture(GL_TEXTURE_2D, texId[index]);
}
int main(int argc, char* args[])
{
std::vector<SDL_Surface*> image;
GLfloat rotation = 0.0f;
bool quit = false;
if (init() == false)
{
return 1;
}
const char* ImageNames[6] = { "res/KGU-logo.jpg", "res/Me.jpeg", "res/beach-1838501.jpg", "res/bench-560435.jpg", "res/wolf-1341881_1920.jpg", "res/spring-bird-2295434.jpg" };
for (int i = 0; i < 6; i++) {
SDL_RWops* tmprwop = SDL_RWFromFile(ImageNames[i], "rb");
SDL_Surface* tmpimg = IMG_LoadJPG_RW(tmprwop);
image.push_back(tmpimg);
}
for(int i=0;i<6;i++)
glGenTextures(1, &texId[i]);
/*SDL_Rect position;
position.x = 1;
position.y = 1;
position.h = 10;
position.w = 10;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, SDL_CreateTextureFromSurface(renderer, image.at(0)), NULL, &position);
SDL_RenderPresent(renderer);*/
/****** Main Game Loop ******/
while (quit == false)
{
/****** Get Most Current Time ******/
var::frame_start_time = SDL_GetTicks();
/****** Draw Rectangle ******/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -6.0f);
glRotatef(rotation, 1.0f, 1.0f, 0.0f);
RenewTexture(image.at(0), 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);
glTexCoord2f(1.0, 0.0); glVertex2f(550, 0);
glTexCoord2f(1.0, 1.0); glVertex2f(550, 550);
glTexCoord2f(0.0, 1.0); glVertex2f(0, 550);
glEnd();
RenewTexture(image.at(0), 0);
glBegin(GL_QUADS);
/* Cube Top */
//glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
RenewTexture(image.at(1), 1);
/* Cube Bottom */
//glColor4f(1.0f, 0.5f, 0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
RenewTexture(image.at(2), 2);
/* Cube Front */
//glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
RenewTexture(image.at(3), 3);
/* Cube Back */
//glColor4f(0.0f, 1.0f, 0.5f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
RenewTexture(image.at(4), 4);
/* Cube Left Side */
//glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
RenewTexture(image.at(5), 5);
/* Cube Right Side */
//glColor4f(0.15f, 0.25f, 0.75f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
rotation -= 0.5f;
/****** Check for Key & System input ******/
while (SDL_PollEvent(&event))
{
/****** Application Quit Event ******/
if (event.type == SDL_QUIT)
{
quit = true;
}
}
/****** Update Screen And Frame Counts ******/
SDL_GL_SwapWindow(win);
var::frame_count++;
var::frame_current_time = SDL_GetTicks();
/****** Frame Rate Handle ******/
if ((var::frame_current_time - var::frame_start_time) < (1000 / 60))
{
var::frame_count = 0;
SDL_Delay((1000 / 60) - (var::frame_current_time - var::frame_start_time));
}
}
clean_up();
return 0;
}
Edit
Solved via changing all glTexCoord3f()
to glTexCoord2f()
and it worked... my fault :D
additionaly, I misunterstood glEnable(GL_TEXTURE_3D)
so changed to glEnable(GL_TEXTURE_2D)
, and checked all opengl errors via error check function
void errorcheck() {
int errormsg;
if ((errormsg = glGetError()) != GL_NO_ERROR)
{
std::cout << "glerror: " << errormsg << std::endl;
exit(0);
}
}