Hi, I have problem loading texture from an image using SOIL and OpenGL.
Here is the code :
the file "test.png" is attached.
I 'm sure that the path is right, as I changed "test.png" to "test1.png", it loaded a white square, but with "test.png", a dark-brown square was loaded.
----------
can't upload image. wonder why
Here is the code :
Code:
#include <stdlib.h>
#include <GLUT/GLUT.h>
#include "SOIL.h"
int lastFrameTime = 0;
float degree = 0.0f;
float boxX = 0.0f;
float initX = 200.0f;
float initY = 200.0f;
float transX = 3.0f;
float transY = 3.0f;
float rot = 0.1f;
int kWindowWidth = 640;
int kWindowHeight = 420;
GLuint tex_2d;
void init(void){
tex_2d = SOIL_load_OGL_texture("test.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT);
}
void display(void){
if (lastFrameTime == 0) {
lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
}
int now = glutGet(GLUT_ELAPSED_TIME);
int elapsedMiliseconds = now - lastFrameTime;
float elapsedTime = elapsedMiliseconds / 1000.0f;
lastFrameTime = now;
int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
boxX += 100.0f * elapsedTime;
if (boxX > windowWidth) {
boxX -= windowWidth;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex_2d);
glTranslatef(initX, initY, 0.0f);
//glRotatef(degree, 0.0f, 0.0f, 1.0f);
degree+=rot;
glBegin(GL_QUADS);
glVertex2f(-70.0f, -70.0f);
glVertex2f(70.0f, -70.0f);
glVertex2f(70.0f, 70.0f);
glVertex2f(-70.0f, 70.0f);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
void reshape(int width, int height){
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}
void idle(void){
glutPostRedisplay();
}
void keyPress(unsigned char key, int x, int y){
switch (key) {
case 'a':
initX -= transX;
if (initX < 0.0f) {
initX = 0.0f;
}
break;
case 'd':
initX += transX;
if (initX > glutGet(GLUT_WINDOW_WIDTH)) {
initX = (float) glutGet(GLUT_WINDOW_WIDTH);
}
break;
case 'w':
initY += transY;
if (initY > glutGet(GLUT_WINDOW_HEIGHT)) {
initY = (float) glutGet(GLUT_WINDOW_HEIGHT);
}
break;
case 's':
initY -= transY;
if (initY < 0.0f) {
initY = 0.0f;
}
break;
case 'r':
rot = -rot;
break;
default:
break;
}
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(kWindowWidth, kWindowHeight);
glutCreateWindow("GLUT Program");
init();
glutKeyboardFunc(keyPress);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMainLoop();
return EXIT_SUCCESS;
}
the file "test.png" is attached.
I 'm sure that the path is right, as I changed "test.png" to "test1.png", it loaded a white square, but with "test.png", a dark-brown square was loaded.
----------
can't upload image. wonder why