I’m having a little trouble understanding how to use OpenGL 2.1 with GLSL 1.2 in C++. I’m looking at code I wrote (or pasted from online together…) and I don’t understand it and I can’t get texturing to work. Right now, it all appears white. Yes, my shaders compile properly and my texture image is loaded correctly (I checked using immediate mode).
Anyways, my vertex shader is this:
#version 120
in vec4 PositionVec;
uniform mat4 ModelViewMat;
uniform mat4 ProjMat;"
void main()
{
gl_TexCoord[0]=gl_MultiTexCoord0;
gl_Position=ProjMat*ModelViewMat*PositionVec;
}
And my fragment:
#version 120
uniform sampler2D tex;
void main()
{
gl_FragColor = texture2D(tex, gl_TexCoord[0].st);
}
My coordinates:
float data[]={ 160.0f, 120.0f, -1.0f,
480.0f, 120.0f, -1.0f,
480.0f, 320.0f, -1.0f,
160.0f, 320.0f, -1.0f,};
float colors[]={154.0f/256.0f, 206.0f/256.0f, 236.0f/235.0f, 1.0f,
154.0f/256.0f, 206.0f/256.0f, 236.0f/235.0f, 0.5f,
154.0f/256.0f, 206.0f/256.0f, 236.0f/235.0f, 1.0f,
154.0f/256.0f, 206.0f/256.0f, 236.0f/235.0f, 0.5f,
1.0f, 0.0f, 0.0f, 1.0f};
float newTexCoord[]={0.0f,0.0f,
1.0f,0.0f,
1.0f,1.0f,
0.0f,1.0f};
And my drawing(the texture is called newTexture, by the way):
glClearColor(100.0f/256.0f,149.0f/256.0f,236.0f/256.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glUseProgram(mainProgram.program);
modelView=glm::mat4(1.0);
modelView*=glm::translate<float>(glm::vec3(-320+x,-240.0+y,0.0));
modelView*=glm::translate<float>(glm::vec3(0.0,0.0,0.0));
glUniformMatrix4fv(glGetUniformLocation(mainProgram.program,"ModelViewMat"),1,false,glm::value_ptr(modelView));
glActiveTexture(GL_TEXTURE0);
int texture_location = glGetUniformLocation(mainProgram.program,"color_texture");
glUniform1i(texture_location, 0);
glBindTexture(GL_TEXTURE_2D,newTexture);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer[1]);
glColorPointer(4,GL_FLOAT,0,0);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer[0]);
glVertexPointer(3,GL_FLOAT,0,0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture( GL_TEXTURE_2D, newTexture);
glTexCoordPointer(2,GL_FLOAT,0,newTexCoord);
GLubyte indices[] = {0,1,2,3};
glEnableClientState( GL_INDEX_ARRAY );
glIndexPointer( GL_UNSIGNED_BYTE, 0, indices );
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, indices);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture( GL_TEXTURE_2D,0);
glUseProgram(0);
So it draws the box at the correct position but it’s all white. Also, as you may have noticed, most of the code above is mumbo-jumbo and I don’t know what I need or what I don’t or the proper way to do it. All of the tutorials I see are of OpenGL 3.0, but my computer is limited to 2.1 so I want to learn how to use it with shaders so I don’t need to change much when I finally upgrade. Any help is appreciated. Thanks in advance!