PDA

View Full Version : texture issue



vikramsingh
06-23-2011, 08:00 AM
when is am using the following code i am not getting the texture i am just getting some random color from the texture that is filled on the entire object

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, textureID);

shaderManager.UseStockShader(GLT_SHADER_TEXTURE_PO INT_LIGHT_DIFF,

transformPipeline.GetModelViewMatrix(),

transformPipeline.GetProjectionMatrix(),

vLightPos, vWhite, 0);

//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

//glLineWidth(4.5f);


glEnableClientState(GL_NORMAL_ARRAY);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glEnableClientState(GL_VERTEX_ARRAY);


glVertexPointer(3, GL_FLOAT, 0, bananaVerts);
glNormalPointer(GL_FLOAT, 0, bananaNormals);
glTexCoordPointer(2, GL_FLOAT, 0, bananaTexCoords);

glDrawArrays(GL_TRIANGLES, 0, bananaNumVerts);


glDisableClientState(GL_VERTEX_ARRAY);

glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glDisableClientState(GL_NORMAL_ARRAY);

BionicBytes
06-24-2011, 08:25 AM
Use [CODE] [/ code] to make your post more readable.
However, what you have posted does not contain enough detail to say what/where the problem is.

Are you sure you have linked the shader and provided the sampler uniform with the correct texture unit value? I.E. bound and linked the texture to the shader uniform.

What's in the shader?

vikramsingh
06-27-2011, 02:59 AM
#include <GLTools.h>

#include <GLMatrixStack.h>

#include <GLFrame.h>

#include <GLFrustum.h>

#include <GLBatch.h>

#include <GLGeometryTransform.h>

#include <GL/glut.h>

#include "myCube.h"



GLShaderManager shaderManager;

GLMatrixStack modelViewMatrix;

GLMatrixStack projectionMatrix;

GLFrame cameraFrame;

GLFrame objectFrame;

GLFrustum viewFrustum;

GLGeometryTransform transformPipeline;

M3DMatrix44f shadowMatrix;

GLuint textureID;

GLfloat vBlack[] = { 0.0f, 0.0f, 0.0f, 1.0f };







bool LoadTGATexture(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode)

{

GLbyte *pBits;

int nWidth, nHeight, nComponents;

GLenum eFormat;



// Read the texture bits

pBits = gltReadTGABits(szFileName, &amp;nWidth, &amp;nHeight, &amp;nComponents, &amp;eFormat);

if(pBits == NULL)

return false;



glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);



glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);



glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0,

eFormat, GL_UNSIGNED_BYTE, pBits);



free(pBits);



if(minFilter == GL_LINEAR_MIPMAP_LINEAR ||

minFilter == GL_LINEAR_MIPMAP_NEAREST ||

minFilter == GL_NEAREST_MIPMAP_LINEAR ||

minFilter == GL_NEAREST_MIPMAP_NEAREST)

glGenerateMipmap(GL_TEXTURE_2D);



return true;

}



void ChangeSize(int w, int h)

{

glViewport(0, 0, w, h);

viewFrustum.SetPerspective(35.0f, float(w) / float(h), 1.0f, 500.0f);

projectionMatrix.LoadMatrix(viewFrustum.GetProject ionMatrix());

modelViewMatrix.LoadIdentity();

}



void SetupRC()

{

glClearColor(1.0f, 1.0f, 1.0f, 0.0f );

shaderManager.InitializeStockShaders();

glEnable(GL_DEPTH_TEST);

glEnable(GL_NORMALIZE);

glEnable(GL_TEXTURE_2D);

glEnable(GL_LIGHTING);

transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);

cameraFrame.MoveForward(-4.0f);



glGenTextures(1, &amp;textureID);

glBindTexture(GL_TEXTURE_2D, textureID);

LoadTGATexture("tex.tga", GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);

//MakePyramid(pyramidBatch);

}



void ShutdownRC(void)

{

glDeleteTextures(1, &amp;textureID);

}



void RenderScene(void)

{

static GLfloat vLightPos [] = { 1.0f, 1.0f, 0.0f };

static GLfloat vWhite [] = { 1.0f, 1.0f, 1.0f, 1.0f };



glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

modelViewMatrix.PushMatrix();

M3DMatrix44f mCamera;

cameraFrame.GetCameraMatrix(mCamera);

modelViewMatrix.MultMatrix(mCamera);



M3DMatrix44f mObjectFrame;

objectFrame.GetMatrix(mObjectFrame);

modelViewMatrix.MultMatrix(mObjectFrame);



glBindTexture(GL_TEXTURE_2D, textureID);

shaderManager.UseStockShader(GLT_SHADER_TEXTURE_PO INT_LIGHT_DIFF,

transformPipeline.GetModelViewMatrix(),

transformPipeline.GetProjectionMatrix(),

vLightPos, vWhite, 0);

//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

//glLineWidth(4.5f);

glEnable(GL_TEXTURE_2D);

glEnableClientState(GL_NORMAL_ARRAY);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glEnableClientState(GL_VERTEX_ARRAY);



glVertexPointer(3, GL_FLOAT, 0, myCubeVerts);

glNormalPointer(GL_FLOAT, 0, myCubeNormals);

glTexCoordPointer(2, GL_FLOAT, 0, myCubeTexCoords);

glDrawArrays(GL_TRIANGLES, 0, myCubeNumVerts);



glDisableClientState(GL_VERTEX_ARRAY);

glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glDisableClientState(GL_NORMAL_ARRAY);



modelViewMatrix.PopMatrix();

glutSwapBuffers();

}

void KeyPressFunc(unsigned char key, int x, int y)

{

switch (key)

{

case 27:

exit(0);

}

}



void SpecialKeys(int key, int x, int y)

{

if(key == GLUT_KEY_UP)

objectFrame.RotateWorld(m3dDegToRad(-5.0f), 1.0f, 0.0f, 0.0f);



if(key == GLUT_KEY_DOWN)

objectFrame.RotateWorld(m3dDegToRad(5.0f), 1.0f, 0.0f, 0.0f);



if(key == GLUT_KEY_LEFT)

objectFrame.RotateWorld(m3dDegToRad(-5.0f), 0.0f, 1.0f, 0.0f);



if(key == GLUT_KEY_RIGHT)

objectFrame.RotateWorld(m3dDegToRad(5.0f), 0.0f, 1.0f, 0.0f);

glutPostRedisplay();

}



int main(int argc, char* argv[])

{

gltSetWorkingDirectory(argv[0]);

glutInit(&amp;argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);

glutInitWindowSize(800, 600);

glutCreateWindow("yo ho ho");



glutReshapeFunc(ChangeSize);

glutDisplayFunc(RenderScene);

glutSpecialFunc(SpecialKeys);

glutKeyboardFunc(KeyPressFunc);

//printf("\n\n%lf",bananaVerts[0]);

GLenum err = glewInit();

if (GLEW_OK != err) {

fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));

return 1;

}

SetupRC();

glutMainLoop();

return 0;

}


as you can see i have used the stock shader
and can you tell me what should be the last argument in the shader function