Why is the sprite not rendering in OpenGL?












3















I am trying to render a 2D(Screen coordinated) sprite in OpenGL. Yet, when I compile it, it does not show up. I see that the code is fine (There are not even any shader compilation errors nor any other errors). I also have also the matrices set up(which I doubt is causing the problem, and that's where starts the CONFUSION!!)



Here is the source code, by the way(without debugging, to make it short):-



main.cpp



// Including all required headers here...

#include <iostream>

#define GLEW_STATIC
#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include "SOIL2/SOIL2.h"

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

const GLchar * vertexShaderSource =
"#version 330 coren"
"layout(location = 0) in vec4 vertex;n"
"out vec2 TexCoords;n"
"uniform mat4 model;n"
"uniform mat4 projection;n"
"void main()n"
"{n"
"TexCoords = vertex.zw;n"
"gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0);n"
"}";

const GLchar * fragmentShaderSource =
"#version 330 coren"
"in vec2 TexCoords;n"
"out vec4 color;n"
"uniform sampler2D image;n"
"uniform vec3 spriteColor;n"
"void main()n"
"{n"
"color = vec4(spriteColor, 1.0) * texture(image, TexCoords);n"
"}";

const GLint WIDTH = 800, HEIGHT = 600;

int main()
{
glfwInit();

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, GLFW_FALSE);

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Rendering Sprites", nullptr, nullptr);

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

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

glViewport(0, 0, screenWidth, screenHeight);

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

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

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);

GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);

glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);

GLuint quadVAO;
GLuint VBO;
GLfloat vertices =
{
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,

0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f
};

glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &VBO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindVertexArray(quadVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

GLuint texture;

int width, height;

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

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

unsigned char *image = SOIL_load_image("img.png", &width, &height, 0, SOIL_LOAD_RGBA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);

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

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

glUseProgram(shaderProgram);
glm::mat4 model;
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), static_cast<GLfloat>(HEIGHT), 0.0f, -1.0f, 1.0f);

glm::vec2 size = glm::vec2(10.0f, 10.0f);
glm::vec2 position = glm::vec2(-10.0f, 10.0f);
glm::vec3 color = glm::vec3(1.0f, 0.0f, 0.0f);
GLfloat rotation = 0.0f;

model = glm::translate(model, glm::vec3(position, 0.0f));

model = glm::translate(model, glm::vec3(0.5f * size.x, 0.5f * size.y, 0.0f));
model = glm::rotate(model, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, glm::vec3(-0.5f * size.x, -0.5f * size.y, 0.0f));

model = glm::scale(model, glm::vec3(size, 1.0f));

glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniform3f(glGetUniformLocation(shaderProgram, "spriteColor"), color.x, color.y, color.z);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);

glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);

glfwSwapBuffers(window);
}

glDeleteVertexArrays(1, &quadVAO);
glDeleteBuffers(1, &VBO);

glfwTerminate();

return EXIT_SUCCESS;
}









share|improve this question























  • Maybe you have to enable texturing via glEnable()?

    – Beko
    Apr 4 '18 at 12:48











  • @Beko Nope, still the same.

    – Ruks
    Apr 4 '18 at 12:57






  • 1





    @Rabbid76 Ok, I did not know that. I'm new to OpenGL myself, so thank you for the information.

    – Beko
    Apr 4 '18 at 15:32
















3















I am trying to render a 2D(Screen coordinated) sprite in OpenGL. Yet, when I compile it, it does not show up. I see that the code is fine (There are not even any shader compilation errors nor any other errors). I also have also the matrices set up(which I doubt is causing the problem, and that's where starts the CONFUSION!!)



Here is the source code, by the way(without debugging, to make it short):-



main.cpp



// Including all required headers here...

#include <iostream>

#define GLEW_STATIC
#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include "SOIL2/SOIL2.h"

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

const GLchar * vertexShaderSource =
"#version 330 coren"
"layout(location = 0) in vec4 vertex;n"
"out vec2 TexCoords;n"
"uniform mat4 model;n"
"uniform mat4 projection;n"
"void main()n"
"{n"
"TexCoords = vertex.zw;n"
"gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0);n"
"}";

const GLchar * fragmentShaderSource =
"#version 330 coren"
"in vec2 TexCoords;n"
"out vec4 color;n"
"uniform sampler2D image;n"
"uniform vec3 spriteColor;n"
"void main()n"
"{n"
"color = vec4(spriteColor, 1.0) * texture(image, TexCoords);n"
"}";

const GLint WIDTH = 800, HEIGHT = 600;

int main()
{
glfwInit();

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, GLFW_FALSE);

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Rendering Sprites", nullptr, nullptr);

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

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

glViewport(0, 0, screenWidth, screenHeight);

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

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

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);

GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);

glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);

GLuint quadVAO;
GLuint VBO;
GLfloat vertices =
{
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,

0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f
};

glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &VBO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindVertexArray(quadVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

GLuint texture;

int width, height;

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

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

unsigned char *image = SOIL_load_image("img.png", &width, &height, 0, SOIL_LOAD_RGBA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);

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

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

glUseProgram(shaderProgram);
glm::mat4 model;
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), static_cast<GLfloat>(HEIGHT), 0.0f, -1.0f, 1.0f);

glm::vec2 size = glm::vec2(10.0f, 10.0f);
glm::vec2 position = glm::vec2(-10.0f, 10.0f);
glm::vec3 color = glm::vec3(1.0f, 0.0f, 0.0f);
GLfloat rotation = 0.0f;

model = glm::translate(model, glm::vec3(position, 0.0f));

model = glm::translate(model, glm::vec3(0.5f * size.x, 0.5f * size.y, 0.0f));
model = glm::rotate(model, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, glm::vec3(-0.5f * size.x, -0.5f * size.y, 0.0f));

model = glm::scale(model, glm::vec3(size, 1.0f));

glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniform3f(glGetUniformLocation(shaderProgram, "spriteColor"), color.x, color.y, color.z);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);

glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);

glfwSwapBuffers(window);
}

glDeleteVertexArrays(1, &quadVAO);
glDeleteBuffers(1, &VBO);

glfwTerminate();

return EXIT_SUCCESS;
}









share|improve this question























  • Maybe you have to enable texturing via glEnable()?

    – Beko
    Apr 4 '18 at 12:48











  • @Beko Nope, still the same.

    – Ruks
    Apr 4 '18 at 12:57






  • 1





    @Rabbid76 Ok, I did not know that. I'm new to OpenGL myself, so thank you for the information.

    – Beko
    Apr 4 '18 at 15:32














3












3








3








I am trying to render a 2D(Screen coordinated) sprite in OpenGL. Yet, when I compile it, it does not show up. I see that the code is fine (There are not even any shader compilation errors nor any other errors). I also have also the matrices set up(which I doubt is causing the problem, and that's where starts the CONFUSION!!)



Here is the source code, by the way(without debugging, to make it short):-



main.cpp



// Including all required headers here...

#include <iostream>

#define GLEW_STATIC
#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include "SOIL2/SOIL2.h"

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

const GLchar * vertexShaderSource =
"#version 330 coren"
"layout(location = 0) in vec4 vertex;n"
"out vec2 TexCoords;n"
"uniform mat4 model;n"
"uniform mat4 projection;n"
"void main()n"
"{n"
"TexCoords = vertex.zw;n"
"gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0);n"
"}";

const GLchar * fragmentShaderSource =
"#version 330 coren"
"in vec2 TexCoords;n"
"out vec4 color;n"
"uniform sampler2D image;n"
"uniform vec3 spriteColor;n"
"void main()n"
"{n"
"color = vec4(spriteColor, 1.0) * texture(image, TexCoords);n"
"}";

const GLint WIDTH = 800, HEIGHT = 600;

int main()
{
glfwInit();

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, GLFW_FALSE);

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Rendering Sprites", nullptr, nullptr);

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

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

glViewport(0, 0, screenWidth, screenHeight);

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

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

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);

GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);

glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);

GLuint quadVAO;
GLuint VBO;
GLfloat vertices =
{
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,

0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f
};

glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &VBO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindVertexArray(quadVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

GLuint texture;

int width, height;

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

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

unsigned char *image = SOIL_load_image("img.png", &width, &height, 0, SOIL_LOAD_RGBA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);

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

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

glUseProgram(shaderProgram);
glm::mat4 model;
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), static_cast<GLfloat>(HEIGHT), 0.0f, -1.0f, 1.0f);

glm::vec2 size = glm::vec2(10.0f, 10.0f);
glm::vec2 position = glm::vec2(-10.0f, 10.0f);
glm::vec3 color = glm::vec3(1.0f, 0.0f, 0.0f);
GLfloat rotation = 0.0f;

model = glm::translate(model, glm::vec3(position, 0.0f));

model = glm::translate(model, glm::vec3(0.5f * size.x, 0.5f * size.y, 0.0f));
model = glm::rotate(model, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, glm::vec3(-0.5f * size.x, -0.5f * size.y, 0.0f));

model = glm::scale(model, glm::vec3(size, 1.0f));

glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniform3f(glGetUniformLocation(shaderProgram, "spriteColor"), color.x, color.y, color.z);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);

glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);

glfwSwapBuffers(window);
}

glDeleteVertexArrays(1, &quadVAO);
glDeleteBuffers(1, &VBO);

glfwTerminate();

return EXIT_SUCCESS;
}









share|improve this question














I am trying to render a 2D(Screen coordinated) sprite in OpenGL. Yet, when I compile it, it does not show up. I see that the code is fine (There are not even any shader compilation errors nor any other errors). I also have also the matrices set up(which I doubt is causing the problem, and that's where starts the CONFUSION!!)



Here is the source code, by the way(without debugging, to make it short):-



main.cpp



// Including all required headers here...

#include <iostream>

#define GLEW_STATIC
#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include "SOIL2/SOIL2.h"

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

const GLchar * vertexShaderSource =
"#version 330 coren"
"layout(location = 0) in vec4 vertex;n"
"out vec2 TexCoords;n"
"uniform mat4 model;n"
"uniform mat4 projection;n"
"void main()n"
"{n"
"TexCoords = vertex.zw;n"
"gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0);n"
"}";

const GLchar * fragmentShaderSource =
"#version 330 coren"
"in vec2 TexCoords;n"
"out vec4 color;n"
"uniform sampler2D image;n"
"uniform vec3 spriteColor;n"
"void main()n"
"{n"
"color = vec4(spriteColor, 1.0) * texture(image, TexCoords);n"
"}";

const GLint WIDTH = 800, HEIGHT = 600;

int main()
{
glfwInit();

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, GLFW_FALSE);

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Rendering Sprites", nullptr, nullptr);

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

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

glViewport(0, 0, screenWidth, screenHeight);

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

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

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);

GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);

glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);

GLuint quadVAO;
GLuint VBO;
GLfloat vertices =
{
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,

0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f
};

glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &VBO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindVertexArray(quadVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

GLuint texture;

int width, height;

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

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

unsigned char *image = SOIL_load_image("img.png", &width, &height, 0, SOIL_LOAD_RGBA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);

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

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

glUseProgram(shaderProgram);
glm::mat4 model;
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), static_cast<GLfloat>(HEIGHT), 0.0f, -1.0f, 1.0f);

glm::vec2 size = glm::vec2(10.0f, 10.0f);
glm::vec2 position = glm::vec2(-10.0f, 10.0f);
glm::vec3 color = glm::vec3(1.0f, 0.0f, 0.0f);
GLfloat rotation = 0.0f;

model = glm::translate(model, glm::vec3(position, 0.0f));

model = glm::translate(model, glm::vec3(0.5f * size.x, 0.5f * size.y, 0.0f));
model = glm::rotate(model, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, glm::vec3(-0.5f * size.x, -0.5f * size.y, 0.0f));

model = glm::scale(model, glm::vec3(size, 1.0f));

glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniform3f(glGetUniformLocation(shaderProgram, "spriteColor"), color.x, color.y, color.z);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);

glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);

glfwSwapBuffers(window);
}

glDeleteVertexArrays(1, &quadVAO);
glDeleteBuffers(1, &VBO);

glfwTerminate();

return EXIT_SUCCESS;
}






c++ opengl sprite render






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 4 '18 at 12:45









RuksRuks

949212




949212













  • Maybe you have to enable texturing via glEnable()?

    – Beko
    Apr 4 '18 at 12:48











  • @Beko Nope, still the same.

    – Ruks
    Apr 4 '18 at 12:57






  • 1





    @Rabbid76 Ok, I did not know that. I'm new to OpenGL myself, so thank you for the information.

    – Beko
    Apr 4 '18 at 15:32



















  • Maybe you have to enable texturing via glEnable()?

    – Beko
    Apr 4 '18 at 12:48











  • @Beko Nope, still the same.

    – Ruks
    Apr 4 '18 at 12:57






  • 1





    @Rabbid76 Ok, I did not know that. I'm new to OpenGL myself, so thank you for the information.

    – Beko
    Apr 4 '18 at 15:32

















Maybe you have to enable texturing via glEnable()?

– Beko
Apr 4 '18 at 12:48





Maybe you have to enable texturing via glEnable()?

– Beko
Apr 4 '18 at 12:48













@Beko Nope, still the same.

– Ruks
Apr 4 '18 at 12:57





@Beko Nope, still the same.

– Ruks
Apr 4 '18 at 12:57




1




1





@Rabbid76 Ok, I did not know that. I'm new to OpenGL myself, so thank you for the information.

– Beko
Apr 4 '18 at 15:32





@Rabbid76 Ok, I did not know that. I'm new to OpenGL myself, so thank you for the information.

– Beko
Apr 4 '18 at 15:32












1 Answer
1






active

oldest

votes


















6














You have to initialize the model matrix variable glm::mat4 model.



The glm API documentation refers to The OpenGL Shading Language specification 4.20.




5.4.2 Vector and Matrix Constructors



If there is a single scalar parameter to a vector constructor, it is used to initialize all components of the constructed vector to that scalar’s value. If there is a single scalar parameter to a matrix constructor, it is used to initialize all the components on the matrix’s diagonal, with the remaining components initialized to 0.0.




This means, that an identity matrix can be initialized by the single parameter 1.0:



glm::mat4 model(1.0f);


Further your sprite is very small and it is out of the viewport (clip space) at the left side:



Change your code like this:



glm::vec2 position = glm::vec2(10.0f, 10.0f); // 10.0f instead of -10.0f





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49651388%2fwhy-is-the-sprite-not-rendering-in-opengl%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    You have to initialize the model matrix variable glm::mat4 model.



    The glm API documentation refers to The OpenGL Shading Language specification 4.20.




    5.4.2 Vector and Matrix Constructors



    If there is a single scalar parameter to a vector constructor, it is used to initialize all components of the constructed vector to that scalar’s value. If there is a single scalar parameter to a matrix constructor, it is used to initialize all the components on the matrix’s diagonal, with the remaining components initialized to 0.0.




    This means, that an identity matrix can be initialized by the single parameter 1.0:



    glm::mat4 model(1.0f);


    Further your sprite is very small and it is out of the viewport (clip space) at the left side:



    Change your code like this:



    glm::vec2 position = glm::vec2(10.0f, 10.0f); // 10.0f instead of -10.0f





    share|improve this answer






























      6














      You have to initialize the model matrix variable glm::mat4 model.



      The glm API documentation refers to The OpenGL Shading Language specification 4.20.




      5.4.2 Vector and Matrix Constructors



      If there is a single scalar parameter to a vector constructor, it is used to initialize all components of the constructed vector to that scalar’s value. If there is a single scalar parameter to a matrix constructor, it is used to initialize all the components on the matrix’s diagonal, with the remaining components initialized to 0.0.




      This means, that an identity matrix can be initialized by the single parameter 1.0:



      glm::mat4 model(1.0f);


      Further your sprite is very small and it is out of the viewport (clip space) at the left side:



      Change your code like this:



      glm::vec2 position = glm::vec2(10.0f, 10.0f); // 10.0f instead of -10.0f





      share|improve this answer




























        6












        6








        6







        You have to initialize the model matrix variable glm::mat4 model.



        The glm API documentation refers to The OpenGL Shading Language specification 4.20.




        5.4.2 Vector and Matrix Constructors



        If there is a single scalar parameter to a vector constructor, it is used to initialize all components of the constructed vector to that scalar’s value. If there is a single scalar parameter to a matrix constructor, it is used to initialize all the components on the matrix’s diagonal, with the remaining components initialized to 0.0.




        This means, that an identity matrix can be initialized by the single parameter 1.0:



        glm::mat4 model(1.0f);


        Further your sprite is very small and it is out of the viewport (clip space) at the left side:



        Change your code like this:



        glm::vec2 position = glm::vec2(10.0f, 10.0f); // 10.0f instead of -10.0f





        share|improve this answer















        You have to initialize the model matrix variable glm::mat4 model.



        The glm API documentation refers to The OpenGL Shading Language specification 4.20.




        5.4.2 Vector and Matrix Constructors



        If there is a single scalar parameter to a vector constructor, it is used to initialize all components of the constructed vector to that scalar’s value. If there is a single scalar parameter to a matrix constructor, it is used to initialize all the components on the matrix’s diagonal, with the remaining components initialized to 0.0.




        This means, that an identity matrix can be initialized by the single parameter 1.0:



        glm::mat4 model(1.0f);


        Further your sprite is very small and it is out of the viewport (clip space) at the left side:



        Change your code like this:



        glm::vec2 position = glm::vec2(10.0f, 10.0f); // 10.0f instead of -10.0f






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 1 '18 at 7:15

























        answered Apr 4 '18 at 13:17









        Rabbid76Rabbid76

        37.3k113248




        37.3k113248
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49651388%2fwhy-is-the-sprite-not-rendering-in-opengl%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen