2016-06-10 18 views
-1

Ich schreibe mein Programm, das ein Quad mit einer Textur zeichnet. Hier ist mein Code:Openll SOIL textureCoords nicht erkennen

#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; 

}

Der Scheitelpunkt attrib Array nicht Textur Vertex-Eingang als meine dritte Eingangseinstellung. Kann jemand sehen, warum mache ich falsch? Oh, und hier ist mein Shader-Code:

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);" 
"}"; 

Hilfe.

Antwort

2

Die Variable heißt out vec2 TextCoord im Vertex-Shader, aber im Fragment-Shader (beachten Sie das zusätzliche t im Vertex-Shader).

Sie überprüfen nur den Kompilierungsstatus des Vertex-Shaders, aber nicht den Kompilierungsstatus des Fragment-Shaders oder den Verbindungsstatus des Programms.

+0

Ok Ich werde den Code bearbeiten und sehen, welche Fehler kommen aus dem Fragment Shader-Code – Alenprintf

+0

Nein, ich bekomme keine Fehler – Alenprintf

+0

Es funktioniert, wenn ich diese "t" s im Fragment-Shader hinzugefügt. Vielen Dank – Alenprintf