How do you put textures on square with OpenGL? - opengl

I tried to apply textures to squares with the following code, but they didn't apply.
Is there a problem with the code above?
"dd" is printed on the console window.
I think it is read the bmp file.
but I only see white square like the following pictures.
Bitmap files are located in the project folder.
LoadBMP() is a function that opens the bmp file.
LoadGLTextures() is a function to load textures.
ps. I am not good at English. I'm sorry.
#define _CRT_SECURE_NO_DEPRECATE
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <GL/GLAUX.H>
#pragma comment (lib,"glaux.lib")
unsigned int MyTextureObiect[1];
AUX_RGBImageRec *pTextureImage[1];
AUX_RGBImageRec *LoadBMP(char *szFilename) {
FILE * pFile = NULL;
if (!szFilename) {
return NULL;
}
pFile = fopen(szFilename, "r");
if (pFile) {
fclose(pFile);
return auxDIBImageLoad(szFilename);
}
return NULL;
}
int LoadGLTextures() {
int Status = FALSE;
memset(pTextureImage, 0, sizeof(void *) * 1);
if (pTextureImage[0] = LoadBMP("butterflyans.bmp")) {
printf("dd");
Status = TRUE;
glGenTextures(1, &MyTextureObiect[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, pTextureImage[0]->sizeX, pTextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pTextureImage[0]->data);
glEnable(GL_TEXTURE_2D);
}
if (pTextureImage[0]) {
if (pTextureImage[0]->data) {
free(pTextureImage[0]->data);
}
free(pTextureImage[0]);
}
return Status;
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, MyTextureObiect[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-0.5, -0.5, 0.0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(0.5, -0.5, 0.0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(0.5, 0.5, 0.0);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-0.5, 0.5, 0.0);
glEnd();
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutCreateWindow("OpenGL Drawing Example");
glutDisplayFunc(myDisplay);
if (LoadGLTextures()) {
glEnable(GL_TEXTURE_2D);
}
glutMainLoop();
return 0;
}

You missed to bind glBindTexture the texture object, before setting the texture parameters and specifying the texture.
Note, glTexParameter and glTexImage2D apply to the currently bound texture:
glGenTextures(1, &MyTextureObiect[0]);
glBindTexture(GL_TEXTURE_2D, MyTextureObiect[0]); // <---- bind the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D( .... );
By the way, you set GL_TEXTURE_MIN_FILTER twice, but you never set GL_TEXTURE_MAG_FILTER.

Related

Texture is uncentered

So, I'm trying to do a very basic program in OpenGL. Everything went fine, had issues with texturing but now it's all solved.
But now, I have a little issue. The texture shows, but it isn't "centered". I tried changing the vertex positions, but nope, it doesn't work.
Here's an image showing what's happening:
Here's some code that might help:
My includes:
#include"GL/glew.h"
#include"GL/glut.h"
#include"GLFW/glfw3.h"
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <stdio.h>
#include "SOIL/SOIL.h"
Main code (i hid some code lines):
int image_width, image_height;
unsigned char* image = SOIL_load_image("face.png", &image_width, &image_height, NULL,
SOIL_LOAD_RGBA);
GLuint texture0;
glBindTexture(GL_TEXTURE_2D, texture0);
glGenTextures(1, &texture0);
glActiveTexture(GL_TEXTURE0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
if (image)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image);
}
else
{
cout << "ERROR::TEXTURE_LOADING_fAILED" << "\n";
}
//Draw first 2 vertices
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor3ub(255, 255, 255); //Start vert. color
glTexCoord2f(-.5f, .5f); glVertex2f(-.5, .5); //A
glTexCoord2f(.5f, .5f); glVertex2f(.5, .5); //B
glTexCoord2f(.5f, -.5f); glVertex2f(.5, -.5); //C
glTexCoord2f(-.5f, -.5f); glVertex2f(-.5, -.5); //D
glEnd();
SOIL_free_image_data(image);
glFlush();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glutPostRedisplay();
What I want to do is center the texture to its own center. It's already on (0,0), but I can't manage to "re-center" the texture. Can anyone help me? I'd appreciate it, I'm very VERY new to OpenGL stuff.
The texture coordinates need to be in range [0.0, 1.0]:
glTexCoord2f(0.0f, 1.0f); glVertex2f(-.5, .5); //A
glTexCoord2f(1.0f, 1.0f); glVertex2f(.5, .5); //B
glTexCoord2f(1.0f, 0.0f); glVertex2f(.5, -.5); //C
glTexCoord2f(0.0f, 0.0f); glVertex2f(-.5, -.5); //D
(0, 0) is the bottom left and (1, 1) is the top right coordinate of the texture. You want to map the bottom left of the texture to the vertex coordinate (-0.5, -0.5) and the top right of the texture to the vertex coordinate (0.5, 0.5).

OpenGL image not mapping to coordinates

When I am trying to display images on a square, they are not mapping properly to corners.
These are the original images and the outputs -
I had downloaded original images from the net, and then converted them to bmp using online converter to use them in the code.
here is the code -
#include <stdio.h>
#include <GL/glut.h>
GLuint LoadBMP(const char *fileName)
{
FILE *file;
unsigned char header[54],*data;
unsigned int dataPos,size,width, height;
file = fopen(fileName, "rb");
fread(header, 1, 54, file);
dataPos = *(int*)&(header[0x0A]);
size = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
if (size == 0) size = width * height * 3;
if (dataPos == 0) dataPos = 54;
data = new unsigned char[size];
fread(data, 1, size, file);
fclose(file);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
return texture;
}
GLint texture;
void display(void)
{
glClear( GL_COLOR_BUFFER_BIT);
// glColor3f(0.0, 1.0, 0.0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_POLYGON);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5, -0.5, 0.0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5, 0.5, 0.0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5, 0.5, 0.0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5, -0.5, 0.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glFlush();
}
int main(int argc, char **argv)
{
printf("hello world\n");
glutInit(&argc, argv);
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glViewport(0, 0, 500, 500);
glutCreateWindow ("square");
glClearColor(0.0, 0.0, 0.0, 0.0); // black background
glMatrixMode(GL_PROJECTION); // setup viewing projection
glLoadIdentity(); // start with identity matrix
glutDisplayFunc(display);
texture = LoadBMP("wall.bmp");
glutMainLoop();
return 0;
}
Your code uses a static header size, thus it looks like it could be a problem due to the differences in the BMP header changes due to OS changes by Microsoft.
If you are using Linux, try using the file command, to find out which type of BMP you are using:
Your code looks like it works well for Window 3.x format, and you should try converting into that format and check again. You can use ffmpeg for that purpose:

Problems with Frambuffer Object - limitation related to my screen resolution

I need to have an image as output of my code (dimension: 3507x3281) composed by two triangles (upper-left corner and lower-right corner), each triangle will give me one different color/image on my output.
I read both images and bound them with 2 textures (texture_1 and texture_2).
Afterwards I created both triangles related to their respective image.
I created and bound the Framebuffer Object and attached the textures. Finally, I saved the content to a image file (.ppm).
PROBLEM:
My result (output image) is coherent and geometrically correct. However, only part of my output image is visible. The part that it is visible has 1366x768 as dimension, which is exactely my screen resolution. Am I missing something on the code?
Is there anything related to the options "GL_DRAW_FRAMEBUFFER, GL_READ_FRAMEBUFFER and GL_FRAMEBUFFER"? Only "GL_DRAW_FRAMEBUFFER" gives the coherent result.
Am I really using the Framebuffer? Because, I read that since you use the FBO this kind of problem does not appear.
CODE:
#include <windows.h>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cstdio>
#include <string>
/// Definitions
#define checkImageWidth 500 /// for image - texture 1
#define checkImageHeight 500
#define width 2464 /// for image - texture 2
#define height 1648
static GLuint texture_1, texture_2;
static GLubyte checkImage[checkImageHeight][checkImageWidth][3];
GLuint fb = 1; ///Frame-buffer Object
/// Headers
GLuint raw_texture_load();
void FBO_2_PPM_file(int output_width, int output_height);
/// Functions
GLuint raw_texture_load()
{
/// image used for texture 1
unsigned char *data;
FILE *file;
// open texture data
file = fopen("C:\\Dataset\\image_1.raw", "rb");
if (file == NULL) return 0;
// allocate buffer
data = (unsigned char*) malloc(width * height * 3);
// read texture data
fread(data, width * height * 3, 1, file);
fclose(file);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
/// Texture 1
// allocate a texture name
glGenTextures(1, &texture_1);
// select our current texture
glBindTexture(GL_TEXTURE_2D, texture_1);
// select modulate to mix texture with color for shading
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_DECAL);
// when texture area is small, bilinear filter the closest mipmap
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// when texture area is large, bilinear filter the first mipmap
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// texture should tile
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// build our texture mipmaps
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
// free buffer
free(data);
/// image used for texture 2
FILE *image_4tx2;
if( (image_4tx2 = fopen("C:\\dataset\\image2.ppm","r")) == NULL )
{
printf("\n Problem with input for texture 2\n");
system("pause");
exit(1);
}
int i, j, max_ND, n_col, n_lin, Red, G, B;
char s1[20];
/// Read header
fscanf(image_4tx2,"%s",&s1);
fscanf(image_4tx2,"%s %s %s %s",&s1, &s1, &s1, &s1);
fscanf(image_4tx2,"%d %d",&n_col, &n_lin);
fscanf(image_4tx2,"%d",&max_ND);
for (i = 0; i < checkImageWidth; i++)
{
for (j = 0; j < checkImageHeight; j++)
{
fscanf(image_4tx2,"%d %d %d ",&Red, &G, &B);
checkImage[i][j][0] = (GLubyte) Red;
checkImage[i][j][1] = (GLubyte) G;
checkImage[i][j][2] = (GLubyte) B;
}
}
fclose(image_4tx2);
/// Texture 2
glGenTextures(2, &texture_2);
glBindTexture(GL_TEXTURE_2D, texture_2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, checkImageWidth, checkImageHeight,
0, GL_RGB, GL_UNSIGNED_BYTE, checkImage);
return texture_1;
}
void render()
{
GLdouble COORD_1[3], COORD_2[3], COORD_3[3], COORD[3];
/// Output image size
double col_siz_double = 3507.0;
double row_siz_double = 3281.0;
int col_siz_output_image = 3507;
int row_siz_output_image = 3281;
/// Aspect ratio
double ratio_col_row = col_siz_double/row_siz_double;
double aspect_E, aspect_N;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(ratio_col_row>=0.0)
{
aspect_E = 1.0;
aspect_N = 1.0/ratio_col_row;
glOrtho(0.0, 1.0*aspect_E, 0.0, 1.0*aspect_N, 0.0, 1.0);
}
else
{
aspect_E = 1.0/ratio_col_row;
aspect_N = 1.0;
glOrtho(0.0, 1.0*aspect_E, 0.0, 1.0*aspect_N, 0.0, 1.0);
}
gluLookAt( 0.0, 0.0, 1.0, /* eye */
0.0, 0.0, 0.0, /* center */
0, 1, 0); /* up */
/// Viewport
glViewport(0,0,col_siz_output_image,row_siz_output_image);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
/// Triangle 1 (upper-left half of the output)
glBindTexture(GL_TEXTURE_2D, texture_1);
glBegin(GL_TRIANGLES);
glTexCoord2d(0.0, 0.0);
COORD[0]= 0.0*aspect_E;
COORD[1]= 0.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glTexCoord2d(1.0, 1.0);
COORD[0]= 1.0*aspect_E;
COORD[1]= 1.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glTexCoord2d(0.0, 1.0);
COORD[0]= 0.0*aspect_E;
COORD[1]= 1.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glEnd();
/// Triangle 2 (low-right half of the output)
glBindTexture(GL_TEXTURE_2D, texture_2);
glBegin(GL_TRIANGLES);
glTexCoord2d(0.0, 0.0);
COORD[0]= 0.0*aspect_E;
COORD[1]= 0.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glTexCoord2d(1.0, 1.0);
COORD[0]= 1.0*aspect_E;
COORD[1]= 1.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glTexCoord2d(1.0, 0.0);
COORD[0]= 1.0*aspect_E;
COORD[1]= 0.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
static const GLenum draw_buffers[] =
{
GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT1
};
/// Generating and Biding the Framebuffer Object (FBO)
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb);
///Attaching both 2D texture to the FBO as color attachments
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, draw_buffers[0], GL_TEXTURE_2D, texture_2, 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, draw_buffers[1], GL_TEXTURE_2D, texture_1, 0);
/// Specifying a list of color buffers to be drawn into
glDrawBuffers(2, draw_buffers);
/// Checking the FBO status
GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if( status != GL_FRAMEBUFFER_COMPLETE)
{
printf("\n\nFramebuffer status: %s\n\n",status);
system("pause");
}
/// Saving the results from the FBO (fb) to a PPM image file
FBO_2_PPM_file(col_siz_output_image,row_siz_output_image);
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
texture_1 = raw_texture_load();
render();
}
void FBO_2_PPM_file(int output_width, int output_height)
{
FILE *output_image;
/// READ THE PIXELS VALUES from FBO AND SAVE TO A .PPM FILE
int i, j, k;
unsigned char *pixels = (unsigned char*)malloc(output_width*output_height*4);
/// READ THE CONTENT FROM THE FBO
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadBuffer(GL_COLOR_ATTACHMENT1);
glReadPixels(0, 0, output_width, output_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
output_image = fopen("C:\\Dataset\\output.ppm", "wt");
if(output_image == NULL)
{
printf("Problem # output file!\n\n");
system("pause");
}
fprintf(output_image,"P3\n");
fprintf(output_image,"# Created by Ricao\n");
fprintf(output_image,"%d %d\n",output_width,output_height);
fprintf(output_image,"255\n");
k = 0;
for(i=output_height-1; i>-1; i--)
{
for(j=0; j<output_width; j++)
{
k = (i*output_width)+j;
/// saving only RGB
fprintf(output_image,"%u %u %u ",(unsigned int)pixels[4*k],(unsigned int)pixels[(4*k)+1],
(unsigned int)pixels[(4*k)+2]);
}
fprintf(output_image,"\n");
}
free(pixels);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(3507, 3281);
glutCreateWindow("Do not close this window!");
glutHideWindow();
/// Checking GLEW library
glewExperimental=TRUE;
GLenum err=glewInit();
if(err!=GLEW_OK)
{
printf("glewInit failed, aborting.");
printf("\tError: %s\n\n",glewGetErrorString(err));
system("pause");
}
if (GLEW_EXT_framebuffer_object != GL_TRUE)
{
printf("\n\n\t ** Error! GLEW_EXT_framebuffer_object != GL_TRUE \n\n");
system("pause");
}
/// To check the max size of the Framebuffer
int dims[2];
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, &dims[0]);
///Executing the rendering process
init();
return 0;

Why texture mapping dose not work?

#include <Windows.h>
#include "glut.h"
#include "GLAUX.H"
#include <stdio.h>
GLuint texID;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0,0,0,0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
gluLookAt(10,10,10,0,0,0,0,0,1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texID);
glColor3f(0.9f,0.9f,0.9f);
glBegin(GL_QUADS);
glTexCoord2f(0.f,0.f);
glVertex3f(-100.0f, 0.0f, -100.0f);
glTexCoord2f(0.f,1.f);
glVertex3f(-100.0f,0.0f,100.0f);
glTexCoord2f(1.f,1.f);
glVertex3f(100.0f,0.0f,100.0f);
glTexCoord2f(1.f,0.f);
glVertex3f(100.0f, 0.0f, -100.0f);
glEnd();
glutSwapBuffers();
}
void InitTexture()
{
glGenTextures(1,&texID);
AUX_RGBImageRec * image = auxDIBImageLoad("earth.bmp");
if(!image)
puts("image open err\n");
glBindTexture(GL_TEXTURE_2D, texID);
// glTexImage2D(GL_TEXTURE_2D, 0, 3, image->sizeX,image->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,
// image->data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,image->data);
delete image->data;
delete image;
image = NULL;
}
void reshape (int w, int h) {
float ratio = w/(float)h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,ratio,10,1000);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("3D FORTRESS");
InitTexture();
glClear(GL_COLOR_BUFFER_BIT);
glutDisplayFunc(display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutMainLoop();
return 0;
}
i'm trying to make a simple texture mapping..
but it doesn't work at all. it just displays black screen. though It reads bmp files well...
Several things wrong with this code:
glClearColor after glClear does not have any effect (until the next time through the display function).
You are switching to the texture matrix with glMatrixMode(GL_TEXTURE); but then load a view matrix into it. If you're just getting started with OpenGL, the texture matrix is not something you should be using; this should probably be GL_MODELVIEW.
The first argument to gluBuild2DMipmaps must be GLU_TEXTURE_2D.
You're not checking the return value of gluBuild2DMipmaps.
You're not checking for any OpenGL errors. Use glGetError() to pinpoint the source of any problems.
In general, with problems like this, narrow it down by commenting out features first. Here, I'd start with drawing a coloured quad without any texture, then add the texture to it.

C++/Qt, OpenGL texture mapping

I'm attempting to render a texture that has been mapped to the graphics video memory. I have used qDebug to test that input of my image, indeed it possesses a 512x512 image which is a power of 2. Yet still when I attempt to render, instead of a texture the result is merely a quad which is white.
To note: GLuint m_textureIdents[1]; is correctly defined in GLWidget.h
#include "GLWidget.h"
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
setMouseTracking(true);
m_sceneIndex = SinQuest::SplashScreen;
}
void GLWidget::setApplicationPath(QString strAppPath)
{
m_strAppPath = strAppPath;
}
#include <QDebug>
void GLWidget::loadResources(void)
{
m_imageHandler.addImage(QString(m_strAppPath).append(QDir().toNativeSeparators("\\resources\\images\\")).append("splash_logo.png"), "splash-logo");
QImage glImageData = QGLWidget::convertToGLFormat(*m_imageHandler.getImage("splash-logo"));
qDebug() << glImageData.width() << ":" << glImageData.height();
m_textureIdents[0] = 0;
glGenTextures(1, &m_textureIdents[0]);
glBindTexture(GL_TEXTURE_2D, m_textureIdents[0]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, glImageData.width(), glImageData.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, glImageData.bits());
}
void GLWidget::initializeGL()
{
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 0);
}
void GLWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1.0l, 1.0l); // set up origin
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::paintGL()
{
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f(1.0f, 1.0f, 1.0f);
glShadeModel( GL_FLAT );
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_textureIdents[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2i(0, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex2i(512, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex2i(512, 512);
glTexCoord2f(0.0f, 1.0f);
glVertex2i(0, 512);
glEnd();
}
void GLWidget::mousePressEvent(QMouseEvent *event) {
}
void GLWidget::mouseMoveEvent(QMouseEvent *event) {
printf("%d, %d\n", event->x(), event->y());
}
void GLWidget::keyPressEvent(QKeyEvent* event) {
switch(event->key()) {
case Qt::Key_Escape:
close();
break;
default:
event->ignore();
break;
}
}
...
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDesktopWidget *pDeskWidget = app.desktop();
// available geometry to obtain resolution for primary screen
QRect avGeometry = pDeskWidget->availableGeometry(pDeskWidget->primaryScreen());
// create window, set resolution and invoke fullscreen window state
GLWidget window;
window.setApplicationPath(QCoreApplication::applicationDirPath());
window.loadResources();
window.resize(avGeometry.width(), avGeometry.height());
window.setWindowState(Qt::WindowFullScreen);
window.show();
return app.exec();
}