Hey guys,
I’m kind of new to openGL programming, but what I have so far is a room I can walk around with some tiled textures on the walls and floor.
What I want to do is to have a painting on the wall, and would prefer to do that by overlapping a texture across the tiled brick, so I can manipulate room size independently from the size of the painting.
I am fairly sure that TexSubImage2D is the function I want to use to achieve this, but I am having trouble finding some usage examples for it. From what I have read, I gather that it is very similar to TexImage2D, in that it will assign a texture image to a texture object that is already bound. I assume that to use TexSubImage I would do the same thing. Here is some code I wrote for texture assignment (note that it is in MATLAB, not C++)
%% TEXTURE MAPPING
% Enable texture mapping
glEnable(GL_TEXTURE_2D);
%glShadeModel(GL_SMOOTH);
% Assign formatted images to image index
imIndex = {permute(uint8(imread('brick.jpeg')), [3 2 1]), ...
permute(uint8(imread('painting1.jpg')), [3 2 1]), ...
permute(uint8(imread('painting2.jpg')), [3 2 1]), ...
permute(uint8(imread('carpet.jpg')), [3 2 1])};
% Generate a texture "object"
texname = glGenTextures(length(imIndex));
% Bind texture object to images from index
for i = 1:length(imIndex)
glBindTexture(GL_TEXTURE_2D, texname(i));
%Specify the image texture
tmp = size(imIndex{i});
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tmp(2), tmp(3), 0, GL_RGB, GL_UNSIGNED_BYTE, imIndex{i});
% Texture wrapping
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
% Setup filtering
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
% Specify a texture application function (this will control how light
% reflects off the texture
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
end
glBindTexture(GL_TEXTURE_2D, texname(2));
glTexSubImage2d(GL_TEXTURE_2D, 0, .5, .5, 25, 25, GL_RGB, GL_UNSIGNED_BYTE,imIndex{2});
% Texture wrapping
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
% Setup filtering
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
% Specify a texture application function (this will control how light
% reflects off the texture
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
If you could let me know if this was done right, that would be great. If it was done right, I am kind of at a loss as to how to implement it onto the wall of the room I am rendering.
If it helps, here is the code which draws the room:
% Clear buffer and bind texture
glClear;
glBindTexture(GL_TEXTURE_2D, texname(1));
% Draw a sphere
glPushMatrix;
glTranslatef(0,0,-5.0);
mysphere = gluNewQuadric;
gluQuadricDrawStyle(mysphere, GLU_FILL);
gluSphere(mysphere, .5,36,18);
glPopMatrix;
% DRAW A CUBE
glBegin(GL_QUADS)
% Vertex Vectors
A = [-rx -ry rz];
B = [rx -ry rz];
C = [-rx -ry -rz];
D = [rx -ry -rz];
E = [-rx ry rz];
F = [rx ry rz];
G = [-rx ry -rz];
H = [rx ry -rz];
% Front face
glNormal3d(0,-1,0);
glTexCoord2f(tx,ty); glVertex3fv(B); % Top right
glTexCoord2f(0,ty); glVertex3fv(A); % Top left
glTexCoord2f(0,0); glVertex3fv(C); % Bottom left
glTexCoord2f(tx,0); glVertex3fv(D); % Bottom right
% Rear face
glNormal3d(0,-1,0);
glTexCoord2f(0,ty); glVertex3fv(E); % Top left
glTexCoord2f(tx,ty); glVertex3fv(F); % Top right
glTexCoord2f(tx,0); glVertex3fv(H); % Bottom right
glTexCoord2f(0,0); glVertex3fv(G); % Bottom left
% Ceiling
glNormal3d(0,0,-1);
glTexCoord2f(tx,0); glVertex3fv(A); % Bottom right
glTexCoord2f(0,0); glVertex3fv(B); % Bottom left
glTexCoord2f(0,ty); glVertex3fv(F); % Top left
glTexCoord2f(tx,ty); glVertex3fv(E); % Top right
% Right face
glNormal3d(1,0,0);
glTexCoord2f(0,ty); glVertex3fv(F); % Top left
glTexCoord2f(0,0); glVertex3fv(H); % Bottom left
glTexCoord2f(tx,0); glVertex3fv(D); % Bottom right
glTexCoord2f(tx,ty); glVertex3fv(B); % Top right
% Left face
glNormal3d(-1,0,0);
glTexCoord2f(0,0); glVertex3fv(G); % Bottom right
glTexCoord2f(tx,0); glVertex3fv(C); % Bottom left
glTexCoord2f(tx,ty); glVertex3fv(A); % Top left
glTexCoord2f(0,ty); glVertex3fv(E); % Top right
glEnd;
% New Texture
glBindTexture(GL_TEXTURE_2D, texname(4));
glBegin(GL_QUADS);
% Floor
glNormal3d(0,0,1);
glTexCoord2f(0,0); glVertex3fv(C); % Bottom left
glTexCoord2f(tx,0); glVertex3fv(D); % Bottom right
glTexCoord2f(tx,ty); glVertex3fv(H); % Top right
glTexCoord2f(0,ty); glVertex3fv(G); % Top left
glEnd;
Screen('EndOpenGL', wPtr);
Screen('Flip', wPtr);
Screen('BeginOpenGL', wPtr);
Thanks!