Opengl SOIL纹理Coords无法识别

时间:2016-06-10 17:40:30

标签: c++ opengl textures soil

我正在编写我的程序,用纹理绘制四边形。这是我的代码:

#include <stdio.h>
 #include <stdlib.h>
 #define GLEW_STATIC
 #include <GL/glew.h>
 #include <GLFW/glfw3.h>
 #include <SOIL.h>
 #include <math.h>

 extern const char* VertexShaderCode;
 extern const char* FragmentShaderCode;




 int main()
 {
    glfwInit();
glewInit();
//initialize glew
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

int width = 1920, height = 1080;

glViewport(0, 0, width, height);


GLFWwindow* window = glfwCreateWindow(width, height, "Opengl program", NULL, NULL);
glfwGetFramebufferSize(window, &width, &height);
if (!window)
{
    printf("Someting is wrong with window");
}
glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
    printf("Something is wrong with glew");
}


/*GLfloat vertecies[] = {
    0.75f, 0.75f, 0.0f, //0
    0.75f, -0.75f, 0.0f, //1
    -0.75f, -0.75f, 0.0f, //2
    -0.75f, 0.75f, 0.0f, //3
    0.5f, 0.5f, 0.0f, //0
    0.5f, -0.5f, 0.0f, //1
    -0.5f, -0.5f, 0.0f, //2
    -0.5f, 0.5f, 0.0f, //3
};

GLuint index[] =
{
    0,4,1,
    4,1,5,
    5,1,2,
    6,2,5,
    2,6,3,
    6,7,3,
    3,7,4,
    3,4,0
};*/

GLfloat vertecies[] = {
    //vertices              colors
     0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,     1.0f, 1.0f,     // Top Right
     0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,     1.0f, 0.0f,       // Bottom Right
    -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,     0.0f, 0.0f,     // Bottom Left
    -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,     0.0f, 1.0f       // Top Left 
};

GLuint index[] =
{
    0,1,3,
    1,2,3
};

GLfloat texCoords[] = {
    0.0f, 0.0f,  // Lower-left corner  
    1.0f, 0.0f,  // Lower-right corner
    0.5f, 1.0f   // Top-center corner
};


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_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int widthText, heightText;
unsigned char* image = SOIL_load_image("container.jpg", &widthText, &heightText, 0, SOIL_LOAD_RGB);

GLuint texture;
glGenTextures(1, &texture);

glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, widthText, heightText, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);

SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);











glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

GLuint EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index), index, GL_STATIC_DRAW);

GLuint VAO;
glGenVertexArrays(1, &VAO);

GLuint VBO;
glGenBuffers(1, &VBO);

glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertecies), vertecies, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index), index, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// Color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);

glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);

glBindVertexArray(0);


const char* adapter[1];
GLuint vertexShaderID;
vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
adapter[0] = VertexShaderCode;
glShaderSource(vertexShaderID, 1, adapter, 0);
glCompileShader(vertexShaderID);

GLint success;
GLchar infoLog[512];
glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &success);
if (!success)
{
    glGetShaderInfoLog(vertexShaderID, 512, NULL, infoLog);
    printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED %s\n", infoLog);
}

//declare fragment shader
GLuint fragmentShaderID;
//create fragment shader
fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
//set fragment shader source
adapter[0] = FragmentShaderCode;
glShaderSource(fragmentShaderID, 1, adapter, 0);
//compile fragment shader
glCompileShader(fragmentShaderID);


//declare a shader program
GLuint shaderProgram;
shaderProgram = glCreateProgram();
//attach haders to the program
glAttachShader(shaderProgram, vertexShaderID);
glAttachShader(shaderProgram, fragmentShaderID);
//link program
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);


//rendering
while (!glfwWindowShouldClose(window))
{
    glfwPollEvents();

    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

    glBindTexture(GL_TEXTURE_2D, texture);
    glUseProgram(shaderProgram);
    glBindVertexArray(VAO);
    //glEnableClientState(GL_VERTEX_ARRAY);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

    //glDisableClientState(GL_VERTEX_ARRAY);
    glBindVertexArray(0);
    glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;

}

顶点属性数组未将纹理顶点输入设置为我的第三个输入。任何人都可以看到,为什么我做错了?哦,这是我的着色器代码:

const char* VertexShaderCode =
 "#version 330 core\r\n"
 ""
 "layout(location=0) in vec3 position;"
 "layout(location=1) in vec3 color;"
 "layout(location=2) in vec2 textCoord;"
 ""
 "out vec2 TextCoord;"
 ""
 ""
 "out vec3 theColor;"
 ""
 "void main()"
 "{"
 "  gl_Position = vec4(position, 1.0);"
 "  theColor = color;"
 "  TextCoord = textCoord;"
 "}";

 const char* FragmentShaderCode =
 "#version 330 core\r\n"
 ""
 "in vec3 theColor;"
 "in vec2 TexCoord;"
 ""
 "uniform sampler2D ourTexture;"
 ""
 ""
 ""
 "out vec4 color;"
 "" 
 "void main()"
 "{"
 "  color = texture(ourTexture, TexCoord);"
 "}"; 

帮助。

1 个答案:

答案 0 :(得分:2)

变量在顶点着色器中称为out vec2 TextCoord,但在片段着色器中称为in vec2 TexCoord;(请注意顶点着色器中的附加t)。

您只检查顶点着色器的编译状态,但不检查片段着色器的编译状态或程序的链接状态。