纹理只是呈现为像我的纹理图像

时间:2016-02-25 01:01:28

标签: c++ opengl 2d freeglut texturing

当试图渲染我的opengl纹理时,我只得到一个实心四边形,这是我纹理的基色。

这是我加载纹理和渲染/我的着色器的代码

vertexShader.vert

#version 400 core

in vec3 in_Position;
in vec3 in_Color;
in vec2 in_TextureCoord;

out vec3 pass_Color;
out vec2 pass_TextureCoord;

int main(void) {
  pass_Color = in_Color;
  pass_TextureCoord = in_TextureCoord;
  gl_Position = vec4(in_Position, 1.0);
}

fragmentShader.frag

#version 400 core

uniform sampler2D modelTexture;

in vec3 pass_Color;
in vec2 pass_TextureCoord;

out vec4 out_Color;

void main(void) {
  out_Color = texture(modelTexture, pass_TextureCoord);
}

纹理加载

glEnable(GL_TEXTURE_2D);

glGenTextures(1, &textureID);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);

int channels;
rawPixelData = SOIL_load_image(fileLocation, &width, &height, &channels,  SOIL_LOAD_RGB);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, rawPixelData);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(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_MIPMAP_LINEAR);

glBindTexture(GL_TEXTURE_2D, 0);

std::cout << "========================================" << std::endl;
std::cout << "LOADING TEXTURE: " << fileLocation << std::endl;
std::cout << "========================================" << std::endl;
std::cout << "| Width: " << width << ", Height: " << height << std::endl;
std::cout << "| Channels: " << channels << std::endl;
std::cout << "| Image ID: " << textureID << std::endl;
std::cout << "| Image Data: " << rawPixelData << std::endl;
std::cout << "| RESULT: " << SOIL_last_result() << std::endl;
std::cout << "========================================" << std::endl;
std::cout << "" << std::endl;

SOIL_free_image_data(rawPixelData);

纹理图纸

glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, model->getTexture()->getTextureID());
glUniform1i(glGetUniformLocation(program->getProgramID(), "modelTexture"), 0);

glBindVertexArray(model->getModel()->getVaoID());
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->getModel()->getIndiciesVBO());

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindVertexArray(0);

glDisable(GL_TEXTURE_2D);

VERTS / INDICES / COLOR ARRAYS

float vertss[12] = { 0.5f,  -0.5f, 0.0f,
                     0.5f,   0.5f, 0.0f,
                    -0.5f,   0.5f, 0.0f,
                    -0.5f,  -0.5f, 0.0f};

float color[12] = {1.0f, 0.0f, 0.0f,
                   0.0f, 1.0f, 0.0f,
                   0.0f, 0.0f, 1.0f,
                   1.0f, 1.0f, 0.0f};

//this is my indicies dont question the name =P
unsigned int intPtr[6] = {0, 1, 2,
                          2, 3, 0};
this->indicies = intPtr;


glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);

glGenBuffers(1, &vertsVBO);
glBindBuffer(GL_ARRAY_BUFFER, vertsVBO);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(float), verts, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

unsigned int colorVBO = 0;
glGenBuffers(1, &colorVBO);
glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(float), color, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindVertexArray(0);

glGenBuffers(1, &this->indiciesVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indiciesVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indicies, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

//在不同的班级

float texCoords[8] = {0.0f, 0.0f,
                      1.0f, 0.0f,
                      1.0f, 1.0f,
                      0.0f, 1.0f};

glBindVertexArray(model->getVaoID());

unsigned int texturesVBO = 0;
glGenBuffers(1, &texturesVBO);
glBindBuffer(GL_ARRAY_BUFFER, texturesVBO);
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), texCoords, GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindVertexArray(0);

SHADER计划的一部分

programID = glCreateProgram();
glAttachShader(programID, vertexID);
glAttachShader(programID, fragmentID);

glBindAttribLocation(programID, 0, "in_Position");
glBindAttribLocation(programID, 1, "in_Color");
glBindAttribLocation(programID, 2, "in_TextureCoord");

glLinkProgram(programID);
glValidateProgram(programID);

0 个答案:

没有答案