I’m having a hard time getting a simple program to work right. I’m just trying to draw a rectangle and then apply a texture map to it. The rectangle comes out fine, but it’s not textured; also strange is that it’s colored a sort of brownish-orange color for no good reason that I can tell. The color of the rectangle seems to depend on which bitmap I use to try to texture the surface with. Anyway, here’s the code…I’d be VERY grateful for any help you could give me. Oh - before anyone asks - yes, my bitmap’s dimensions are powers of 2 (256 x 256, to be exact).
#include <iostream.h>
#include <string>
#include <GL/glx.h>
#include <GL/glu.h>
#include “bitmap.h”
int main() {
Display* dpy;
string dpyName;
XVisualInfo* vi;
int viAttribList[] = {
GLX_RGBA, GLX_DEPTH_SIZE, 16, None
};
GLXContext cx;
Colormap cmap;
XSetWindowAttributes swa;
Bitmap* bmp;
Window win;
XEvent event;
GLuint texName;
/**
- Open the connection to the appropriate X server.
*/
dpyName = getenv(“DISPLAY”);
dpy = XOpenDisplay(dpyName.c_str());
if (dpy == NULL) {
cerr << "Error: couldn’t connect to X server " << dpyName << endl;
exit(1);
}
/**
- Make sure this X server has an OpenGL GLX extension.
*/
if (!glXQueryExtension(dpy, NULL, NULL)) {
cerr << "Error: X server "
<< dpyName
<< “has no OpenGL GLX extension” << endl;
exit(1);
}
/**
- Set up the visual for a double-buffered, RGBA format, 16-bit depth
- display.
*/
vi = glXChooseVisual(dpy, DefaultScreen(dpy), viAttribList);
if (vi == NULL) {
cerr << “Error: no appropriate visual” << endl;
exit(1);
}
/**
- Create the GLX rendering context that renders directly to the graphics
- system if it can.
*/
cx = glXCreateContext(dpy, vi, NULL, GL_TRUE);
if (cx == NULL) {
cerr << “Error: couldn’t create rendering context” << endl;
exit(1);
}
/**
- Create the color map.
*/
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
vi->visual, AllocNone);
/**
- Set up the window attributes.
*/
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = (ExposureMask |
StructureNotifyMask);
bmp = new Bitmap(“bmp.bmp”);
/**
- Create the window.
*/
win = XCreateWindow(dpy,
RootWindow(dpy, vi->screen),
0, 0,
640, 480,
0,
vi->depth,
InputOutput,
vi->visual,
CWBorderPixel | CWColormap | CWEventMask,
&swa);
/**
- Display the window, and bind the rendering context to it.
*/
XMapWindow(dpy, win);
glXMakeCurrent(dpy, win, cx);
/**
- Set up OpenGL.
*/
glEnable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
// Set up shading
glShadeModel(GL_SMOOTH);
// Set up lights
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Define the texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glEnable(GL_TEXTURE_2D);
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bmp->GetWidth(), bmp->GetHeight(),
0, GL_RGB, GL_UNSIGNED_BYTE, (GLubyte*)(bmp->GetData()));
GLenum e = glGetError();
if (e != GL_NO_ERROR)
cout << "Error result: " << gluErrorString(e) << endl;
glMatrixMode(GL_PROJECTION);
gluPerspective(60.0, float(640)/float(480), 1.0, 4000.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(320.0, 240.0, 2.0,
320.0, 240.0, -1.0,
0.0, 1.0, 0.0);
while (true) {
XNextEvent(dpy, &event);
switch (event.type) {
case Expose:
if (event.xexpose.count > 0) break;
// Draw the textured image
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(640.0, 0.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(640.0, 320.0, 0.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(0.0, 320.0, 0.0);
glEnd();
glFlush();
glWaitGL();
break;
}
}
delete bmp;
return 0;
}