Cześć mam problem ze zrozumieniem kodu. Mianowicie skąd OpenGL wie, które liczby traktować jako punkty na osiach X i Y, a które jako kolory [patrz vertices]? Co się dzieje kiedy w glVertexAttribPointer pierwszy argument ma wartość 1 i co to oznacza, bo w docs.gl pisze o jakimś index, ale nie rozumiem o jaki index chodzi. O co chodzi w glVertexAttribPointer, gdy pierwszy argument utawiony jest na 1 i dlaczego ostatni ustawiony jest na (void*)(3 * sizeof(float) i co te argumenty oznaczają? Dzięki za odpowiedzi.
Plik główny:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <string>
static unsigned int CompileShader(unsigned int type, std::string source)
{
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*)_malloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << "shader" << std::endl;
std::cout << message << std::endl;
glDeleteShader(id);
return 0;
}
return id;
}
static unsigned int CreateShader(const std::string& vertexShader, const std::string fragmentShader)
{
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
std::string readFromFile(const char* pathToFile)
{
std::string content;
std::ifstream fileStream(pathToFile, std::ios::in);
if (!fileStream.is_open()) {
std::cerr << "Could not read file " << pathToFile << ". File does not exist." << std::endl;
return "";
}
std::string line;
while (std::getline(fileStream, line)) {
content.append(line + "\n");
}
fileStream.close();
std::cout << content << std::endl;
return content;
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
//glfwSwapInterval(1); // Update a scene per "x" frames.
if (glewInit() != GLEW_OK)
std::cout << "Error" << std::endl;
std::cout << glGetString(GL_VERSION) << std::endl;
float vertices[] = {
// positions // Colors
0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
};
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
std::string vertexShaderSource = readFromFile("vertexShader.shader");
std::string fragmentShaderSource = readFromFile("fragmentShader.shader");
unsigned int shaderProgram = CreateShader(vertexShaderSource, fragmentShaderSource);
int vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glDrawArrays(GL_TRIANGLES, 0, 3);
float timeValue = glfwGetTime();
float greenValue = sin(timeValue) / 2.0f + 0.5f;
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Vertex shader:
#version 330 core
layout(location = 0) in vec3 aPos; // the position variable has attribute position 0
layout(location = 1) in vec3 aColor; // the color variable has attribute position 1
out vec3 ourColor; // output a color to the fragment shader
void main()
{
gl_Position = vec4(aPos, 1.0);
ourColor = aColor; // set ourColor to the input color we got from the vertex data
}
Fragment shader:
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
void main()
{
FragColor = vec4(ourColor, 1.0);
}