PDA

View Full Version : Vertex positions and an UV coords don't get passed properly to the vertex shader



rafftyl
07-15-2017, 03:37 AM
EDIT: The title should read 'Vertex colors' not 'Vertex positions'
Hello, I'm new to OpenGL and I'm currently messing around with using properties in my shaders. I tried to display a rotating textured rectangle and got the 'rotating' and 'rectangle' parts right, but I cannot force OpenGL to texture it. For the purpose of debugging I got rid of the texture and decided to draw uv coordinates instead (while also passing unused vertex color to the shader) - to no effect. It would be greatly appreciated if any of you could provide some help.

Here's the code:

main.cpp:


#include <iostream>
#include <GL/glew.h>

#include <GL/glfw3.h>

#include <GL/glm/glm.hpp>
#include <GL/glm/gtc/matrix_transform.hpp>
#include <GL/glm/gtc/type_ptr.hpp>

#include "Shader.h"

// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;

int main()
{
glfwInit();

// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL Test", nullptr, nullptr);

int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

if (nullptr == window)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();

return EXIT_FAILURE;
}

glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;

if (GLEW_OK != glewInit())
{
std::cout << "Failed to initialize GLEW" << std::endl;
return EXIT_FAILURE;
}

glViewport(0, 0, screenWidth, screenHeight);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// Build and compile the shader program - core.vs and core.frag hold vertex and fragment shader code, respectively
Shader shader("core.vs", "core.frag");

// Set up vertex data (and buffer(s)) and attribute pointers
GLfloat vertices[] =
{
// Positions // Colors // uv coords
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 indices[] =
{ // Note that we start from 0!
0, 1, 2, // Bottom right half of the rectangle
2, 3, 0 // Top left half
};

GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO); //generate Vertex Array Object
//generate buffers
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);

glBindVertexArray(VAO); //bind VAO

// bind buffers:
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

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

// set attributes:
// Position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid *)0);
glEnableVertexAttribArray(0);

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

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

glBindVertexArray(0); //Unbind VAO

// Game loop
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();

// Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

shader.Use();

glm::mat4 transform;
transform = glm::translate(transform, glm::vec3(0.0f, 0.0f, 0.0f));
transform = glm::rotate(transform, (GLfloat)glfwGetTime() * -5.0f, glm::vec3(0.0f, 0.0f, 1.0f));

GLint transformLoc = glGetUniformLocation(shader.GetProgram(), "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));

// Draw
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

// Swap the screen buffers
glfwSwapBuffers(window);
}

// Deallocate
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);

glfwTerminate();

return EXIT_SUCCESS;
}



Shader.h


#pragma once

#include <string>
#include <fstream>
#include <sstream>
#include <iostream>

#include <GL/glew.h>

class Shader
{
protected:
GLuint mProgram;
public:
Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
{
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;

vShaderFile.exceptions(std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::badbit);

try
{
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;

vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();

vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl
<< "Reason: " << e.what() << std::endl;
system("pause");
}
const GLchar* vShaderCode = vertexCode.c_str();
const GLchar* fShaderCode = fragmentCode.c_str();

GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vShaderCode, NULL);
glCompileShader(vertexShader);

GLint success;
GLchar infoLog[512];

glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fShaderCode, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);

if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}

mProgram = glCreateProgram();
glAttachShader(mProgram, vertexShader);
glAttachShader(mProgram, fragmentShader);
glLinkProgram(mProgram);

glGetProgramiv(mProgram, GL_LINK_STATUS, &success);

if (!success)
{
glGetProgramInfoLog(mProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}

glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}

void Use()
{
glUseProgram(mProgram);
}

const GLuint& GetProgram() const { return mProgram; }
};

core.vs (vertex shader)


#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 uv;

out vec3 outColor;
out vec2 outUv;

uniform mat4 transform;

void main()
{
gl_Position = transform * vec4( position.x, position.y, position.z, 1.0 );
outColor = color;
outUv= vec2( uv.x, 1.0 - uv.y);
};


core.frag (fragment shader)


#version 330 core
in vec3 inColor;
in vec2 uv;

out vec4 finalColor;

uniform sampler2D texture_1;

void main()
{
finalColor = vec4( uv, 1.0f, 1.0f); //texture(texture_1, uv) * vec4( inColor, 1.0f );
};


The result that I get is a rotating rectangle that's uniformly blue - that would indicate that UVs are zeroed-out. I also tried to display vertex color only and it's black.