OpenGL texture loading with soil help need - c++

Here is the code. Please help guys
#include <GL/glut.h>
#include <iostream>
#include <math.h>
#include "SOIL.h"
using namespace std;
GLuint texture[1];
int LoadGLTextures()
{
texture[0] = SOIL_load_OGL_texture
(
"NeHe.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
if(texture[0] == 0)
return false;
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
return true;
}
void ChangeSize(int w, int h)
{
if(h == 0)
{
h = 1;
}
float ratio = 1.0f * w/h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,6.0,
0.0,0.0,0.0,
0.0f,1.0f,0.0f);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
glFlush();
}
void idle(void)
{
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
glutCreateWindow("ZC");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(display);
glutIdleFunc(idle);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
I dont get errors, but not loading the texture.

You forgot to call LoadGLTextures in your main program. I ran your program with this change:
glEnable(GL_DEPTH_TEST);
LoadGLTextures(); //<-- added this
glutMainLoop();
}
and things worked fine.
I provided my own NeHe.bmp file, since you didn't link to one. It could also fail if your .bmp file is bad in some way.

Related

Freeglut Reshape callback

I am not using glutMainLoop() in my program but I am using glutMainLoopEvent() in a simple while loop. This is fine but the reshape callback is never called. I register my reshape event in the main(). Here's my source code:
#include <iostream>
#include "GL/freeglut.h"
float angle = 0.0f;
void ResizeEvent(int w, int h)
{
std::cout << "Resizing... << w << " " << h << std::Endl;
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLdouble)w / h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void InitializeGL()
{
glClearColor(0.0, 0.0, 0.0, 1.0f);
glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 640.0 / 480.0, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Update()
{
angle += 0.05f;
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -3.0f);
glRotatef(angle, 0.5f, 1.0f, 0.75f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutCreateWindow("Learning freeglut");
InitializeGL();
glutReshapeFunc(ResizeEvent);
while(true)
{
Update();
Display();
glutMainLoopEvent();
}
return 0;
}
Thanks in advance!
Edit: Anyone?
glutReshapeFunc requires glutDisplayFunc to be set to something. This can even be an empty function as long as you don't need the window to update while resizing.
Possible solution
...
glutReshapeFunc(ResizeEvent);
glutDisplayFunc(Display);
while(true)
{
Update();
Display();
glutMainLoopEvent();
}
...

Texture not displaying in OpenGL

The triangle i drew with OpenGL can't be displayed. When i debug my code it's only the white background appeared. Can anyone help me?
#pragma warning(disable:4996)
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <glut.h>
#include <SOIL.h>
const float piOver180 = 0.0174532925f;
float heading;
float xPos;
float zPos;
//declarations
GLuint filter;
GLuint texture[3];
int width = 720;
int height = 540;
void init();
void display();
void drawTriangle();
void reshape(int width, int height);
this is my main function
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutInitWindowPosition(0, 0);
glutCreateWindow("Muzeum");
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
void init()
{
texture[0] = SOIL_load_OGL_texture
(
"wallpaper.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
texture[1] = texture[2] = texture[0];
if (texture[0] == 0 || texture[1] == 0 || texture[2] == 0)
{
printf("SOIL loading error: '%s'\n", SOIL_last_result());
exit(0);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
my display function
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
GLfloat xTrans = -xPos;
GLfloat yTrans = -walk - 0.25f;
GLfloat zTrans = -zPos;
GLfloat sceneRotY = 360.0f - yRot;
glTranslatef(xTrans, yTrans, zTrans);
drawTriangle();
glFlush();
glutSwapBuffers();
}
where i draw my triangle
void drawTriangle()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_TRIANGLES);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(-3.0f, 0.0f); glVertex3f(-3.0f, 0.0f, 6.0f);
glTexCoord2f(-3.0f, 0.0f); glVertex3f(-3.0f, 0.0f, 0.0f);
glTexCoord2f(3.0f, 0.0f); glVertex3f(3.0f, 6.0f, 0.0f);
glTexCoord2f(-3.0f, 0.0f); glVertex3f(-3.0f, 0.0f, 6.0f);
glTexCoord2f(3.0f, 0.0f); glVertex3f(-3.0f, 6.0f, 6.0f);
glTexCoord2f(3.0f, 0.0f); glVertex3f(3.0f, 6.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void reshape(int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0f, (GLdouble)width / (GLdouble)height, 0.5f, 20.0f);
glMatrixMode(GL_MODELVIEW);
}
What i want to do is a room with texture-mapped, now this is only my floor part.
The main problem is that your geometry is not in your field of view. Your gluPerspective() call sets up a projection transformation with a camera placed on the origin, and pointing in the negative z-direction. With the values for the near and far planes, it will show z-values in the range from -0.5 to -20.0. All the z-values of your geometry are in the range 0.0 to 6.0, so all your geometry is behind the camera.
I tried most of your code, and I got things showing up by adding a translation to the modelview transformation. For example, adding a line after the first two lines of the display() function:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
A few more things that could get in your way:
No call to glutInit(). I figure you have that in the original code.
GL_LINEAR_MIPMAP_LINEAR is not a legal value for GL_TEXTURE_MAG_FILTER. This must be GL_LINEAR or GL_NEAREST. glGetError() is your friend!
Fortunately you didn't enable blending, because your glBlendFunc() call, in combination with a white clear color, would prevent anything from being drawn. You would add to white, and that whole whiter than white thing only works in detergent commercials. ;)
Clearing alpha to 1.0, as you do with the last argument of glClearColor() is quite unusual. Doesn't really hurt for now, since you only use an RGB framebuffer, and do not blend with destination alpha.
glFlush() before glutSwapBuffers() is redundant, and only hurts performance.

When I load next image using SOIL, it will replace all the images I load before

This is part of my coding, a simple balloon clicking game. I have a background image and another image for balloon, the code seems to replace all my previous loaded image. Helppppp
GLuint tex_2d;
GLuint tex_bg;
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(800, 800);
glutInitWindowPosition(100, 100);
glutCreateWindow("Lab Assignment");
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return EXIT_SUCCESS;
}
This is where I put the coding for image loading, tex_bg is background.bmp for background, and tex_2d is for balloon image. Image loaded for tex_2d replaced the background image which is loaded earlier than it.
void init()
{
tex_bg = SOIL_load_OGL_texture
(
"background.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
tex_2d = SOIL_load_OGL_texture
(
"balloonImage.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
if (tex_2d == 0 || tex_bg == 0)
{
printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
exit(0);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
void drawQuadOutline()
{
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glBindTexture(GL_TEXTURE_2D, tex_bg);
glNormal3f(0, 1, 0);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawQuadOutline();
drawCircleOutline(balloon[balloonBurst]);
glutSwapBuffers();
}
void drawCircleOutline(Circle o)
{
float angle;
glEnable(GL_TEXTURE_2D);
glBegin(GL_POLYGON);
glBindTexture(GL_TEXTURE_2D, tex_2d);
for(angle=0.0f; angle<360.0f; angle+=2.0f)
{
float radian = angle * (pi/180.0f);
float xcos = (float)cos(radian);
float ysin = (float)sin(radian);
float x = xcos * o.r + o.pos.x;
float y = ysin * o.r + o.pos.y;
float tx = xcos * 0.5f + 0.5f;
float ty = ysin * 0.5f + 0.5f;
glTexCoord2f(tx, ty);
glVertex2f(x, y);
}
glEnd();
glDisable(GL_TEXTURE_2D);
}
Is it problem with my coding? or misuse of SOIL function?
The problem with your coding is that you call glBindTexture(...) in-between glBegin (...) and glEnd (...).
That is invalid; if you checked glGetError (...) you would know this.
You need to re-write this code:
glBegin(GL_POLYGON);
glBindTexture(GL_TEXTURE_2D, tex_2d);
...
glBegin(GL_QUADS);
glBindTexture(GL_TEXTURE_2D, tex_bg);
To this:
glBindTexture(GL_TEXTURE_2D, tex_2d);
glBegin(GL_POLYGON);
...
glBindTexture(GL_TEXTURE_2D, tex_bg);
glBegin(GL_QUADS);

OpenGL 2d texture render issue

I am trying to load and draw a 2d texture using OpenGL with GLFW and SOIL. I have this code, but I only get one solid color (which seems to come from the texture).
I have tested whether the .png loads with an example that came with SOIL, and it worked fine so there has to be some issue in my code.
This is my code:
#include <cstdio>
#include "GL/glfw.h"
#include "SOIL.h"
// function declarations
void drawscene();
void idlefunc();
void updatedisplay();
// global data
GLuint texture; // our example texture
int main(int argc, char **argv) {
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return 1;
}
if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 16, 0, GLFW_WINDOW)) {
fprintf(stderr, "Failed to open GLFW window\n");
return 1;
}
// enable vsync (if available)
glfwSwapInterval(1);
// load textures
texture = SOIL_load_OGL_texture(
"tex.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_DDS_LOAD_DIRECT
);
// check for an error during the texture loading
if (!texture) {
printf("SOIL loading error: '%s'\n", SOIL_last_result());
}
while (glfwGetWindowParam(GLFW_OPENED)) {
idlefunc();
}
// if we get here something went wrong
return 0;
}
// this function gets called every frame
void idlefunc() {
updatedisplay();
drawscene();
}
// set up te display
void updatedisplay() {
int screen_width, screen_height;
glfwGetWindowSize(&screen_width, &screen_height);
if (screen_height <= 0) screen_height = 1;
if (screen_width <= 0) screen_width = 1;
glViewport(0, 0, screen_width, screen_height);
glClearColor(0.02f, 0.02f, 0.02f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, screen_width, screen_height, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// displacement trick for exact pixelization
glTranslatef(0.375f, 0.375f, 0.0f);
}
// draw the scene in this function
void drawscene() {
glBindTexture(GL_TEXTURE_2D, texture);
glPushMatrix();
glTranslatef(10.0f, 10.0f, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 128.0f);
glVertex2f(0.0f, 128.0f);
glTexCoord2f(128.0f, 128.0f);
glVertex2f(128.0f, 128.0f);
glTexCoord2f(128.0f, 0.0f);
glVertex2f(128.0f, 0.0f);
glEnd();
glPopMatrix();
glfwSwapBuffers();
}
Found the issue (thanks to user786653). No matter the vertex coords, the tex coords are between 0.0 and 1.0. This is the fixed code:
// draw the scene in this function
void drawscene() {
glBindTexture(GL_TEXTURE_2D, texture);
glPushMatrix();
glTranslatef(10.0f, 10.0f, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 128.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(128.0f, 128.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(128.0f, 0.0f);
glEnd();
glPopMatrix();
glfwSwapBuffers();
}

OpenGL texture inverted

I'm trying to map the input from my webcam to a plane in OpenGL. I'm using OpenCV to get the images from the webcam.
The problem I have is that the texture is vertically inverted, if my texture is "v", the current result is "^".
I want to fit the image taken from the webcam to my plane (2x2). Its lower left corner is -1, -1 and the upper right corner is 1,1.
The code is:
const int VIEWPORT_WIDTH = 640;
const int VIEWPORT_HEIGHT = 480;
const int KEY_ESCAPE = 27;
CvCapture* g_Capture;
IplImage* image;
GLint g_hWindow;
GLvoid InitGL();
GLvoid OnDisplay();
GLvoid OnReshape(GLint w, GLint h);
GLvoid OnKeyPress (unsigned char key, GLint x, GLint y);
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
g_hWindow = glutCreateWindow("Image");
image = cvLoadImage("average.jpg", 1);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image->width, image->height, GL_RGB, GL_UNSIGNED_BYTE, image->imageData);
InitGL();
glutMainLoop();
return 0;
}
GLvoid InitGL()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glutDisplayFunc(OnDisplay);
glutReshapeFunc(OnReshape);
glutKeyboardFunc(OnKeyPress);
}
GLvoid OnDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -2.5f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
GLvoid OnReshape(GLint width, GLint height)
{
if (height==0)
height=1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width, height);
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 1.0f, 10.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0.0, 0.0, 0.0, 0.0, 0.0, -6.0, 0.0f, 1.0f, 0.0f);
}
GLvoid OnKeyPress(unsigned char key, int x, int y)
{
switch (key) {
case KEY_ESCAPE:
cvReleaseImage(&image);
glutDestroyWindow(g_hWindow);
exit(0);
break;
}
glutPostRedisplay();
}
BTW. In this code I'm loading an image instead of getting from the webcam.
Any suggestion?
Flip the texture coordinates on the quad you're rendering. OpenGL does store texture "upside-down" from how most people assume they'd be, so the simplest way to handle it is just work with OGL.
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
should do it, I think.