I'm programming a voxel game. I have implented procedurally generated terrain. Now I want to program a map that should show on the upper right corner of the screen and that shows the biomes that are nearby. To do this my idea was to create a separate shader program, vbo, and vao, and a texture and that I would write to as the player moved around.
Is there a better way to do this?
Here I create the map:
GLuint VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &EBO);
glGenBuffers(1, &VBO);
float vertices[] = {
-.5f, -.5f, 0, 0, 0,
-.5f, .5f, 0, 0, 1,
.5f, .5f, 0, 1, 1,
.5f, -.5f, 0, 1, 0
};
/*float vertices[] = {
-1, -.5f, 0,
0, .5f, 0,
.5f, -.5f, 0
};*/
unsigned int indices[] = {
0, 1, 2,
0, 2, 3
};
glBindVertexArray(VAO);
// 2. copy our vertices array in a vertex buffer for OpenGL to use
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// 3. copy our index array in a element buffer for OpenGL to use
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// 4. then set the vertex attributes pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
GLuint tbo;
int apothem = 256;
unsigned char pixels[apothem*apothem*3];
for(size_t i = 0; i < apothem*apothem*3; i++) {
pixels[i] = 1;
//std::cout << "changed pixels[" << i << "]" << std::endl;
}
glGenTextures(1, &tbo);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, tbo);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, apothem, apothem, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
//glTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, apothem, apothem, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
// set the texture wrapping/filtering options (on the currently bound texture object)
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_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
And here's the rendering loop:
while (!glfwWindowShouldClose(window))
{
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
glfwPollEvents();
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
static float scale[3] = {1, 1, 1};
static float rotation[3] = {0, 0, 0};
static float position[3] = {0, 0, 0};
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
{
ImGui::Begin("Chunk settings"); // Create a window called "Hello, world!" and append into it.
ImGui::SliderFloat3("scale", scale, -5, 5);
ImGui::SliderFloat3("rotation", rotation, -360, 360);
ImGui::SliderFloat3("position", position, -5, 5);
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}
{
int x = mainPlayer.GetPos().x(), z = mainPlayer.GetPos().z();
ImGui::Begin("World settings");
float continentalness = voxelEngine::mainWorld.GetContinentalness(x, z);
ImGui::Text("Continentalness: %.4f, Continentalness degree: %d", continentalness,
voxelEngine::mainWorld.ContinentalnessDegree(continentalness));
float erosion = voxelEngine::mainWorld.GetErosion(x, z);
ImGui::Text("Erosion: %.4f, Erosion Degree: %d", erosion,
voxelEngine::mainWorld.GetErosionDegree(erosion));
float humidity = voxelEngine::mainWorld.GetHumidity(x, z);
ImGui::Text("Humidity: %.4f, Humidity Degree: %d", humidity,
voxelEngine::mainWorld.GetHumidityDegree(humidity));
float temperature = voxelEngine::mainWorld.GetTemperature(x, z);
ImGui::Text("Temperature: %.4f, Temperature Degree: %d", temperature,
voxelEngine::mainWorld.GetTemperatureDegree(temperature));
float elevation = voxelEngine::mainWorld.GetElevation(x, z);
ImGui::Text("Elevation: %.4f, Elevation Degree: %d", elevation,
voxelEngine::mainWorld.GetElevationDegree(elevation));
ImGui::End();
}
static float n = .3, f = .5, speed, sensitivity, fov, pitch, yaw;
static Eigen::Vector3f cam_pos;
speed = voxelEngine::players->GetSpeed();
sensitivity = voxelEngine::players->GetSensitivity();
fov = voxelEngine::players->GetFOV();
pitch = voxelEngine::players->GetPitch();
yaw = voxelEngine::players->GetYaw();
cam_pos = voxelEngine::players->GetPos();
{
ImGui::Begin("Camera settings");
ImGui::SliderFloat("cam x", &cam_pos.x(), -10, 10);
ImGui::SliderFloat("cam y", &cam_pos.y(), -10, 10);
ImGui::SliderFloat("cam z", &cam_pos.z(), -10, 10);
ImGui::SliderFloat("speed", &speed, 0, 1);
ImGui::SliderFloat("sensitivity", &sensitivity, 0, 1);
ImGui::SliderFloat("fov", &fov, 0, 90);
ImGui::SliderFloat("pitch", &pitch, -360, 360);
ImGui::SliderFloat("yaw", &yaw, -360, 360);
ImGui::SliderFloat("near plane", &n, 0, 1);
ImGui::SliderFloat("far plane", &f, 0, 1);
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
ImGui::End();
}
voxelEngine::players->SetPos(cam_pos);
voxelEngine::players->SetSpeed(speed);
voxelEngine::players->SetSensitivity(sensitivity);
voxelEngine::players->SetFOV(fov);
voxelEngine::players->SetDirection(pitch, yaw);
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float projMat[16];
float viewMat[16];
//float modelMat[16];
/*voxelEngine::genModelMatrix({scale[0], scale[1], scale[2]}, {rotation[0], rotation[1], rotation[2]},
{position[0],
position[1],
position[2]}, modelMat);*/
voxelEngine::players->genViewMatrix(viewMat);
voxelEngine::genProjMatrix(n, f, (float)display_h/display_w, voxelEngine::players->GetFOV(), projMat);
//glUniformMatrix4fv(glGetUniformLocation(voxelEngine::program, "modelMatrix"), 1, GL_FALSE, modelMat);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, blockTextures);
glUseProgram(worldProgram);
glUniformMatrix4fv(glGetUniformLocation(worldProgram, "viewMatrix"), 1, GL_FALSE, viewMat);
glUniformMatrix4fv(glGetUniformLocation(worldProgram, "projMatrix"), 1, GL_TRUE, projMat);
mainPlayer.Render();
float mapProj[16];
float mapModel[16];
voxelEngine::genModelMatrix(Eigen::Vector3f(.7, .7, .7), Eigen::Vector3f(0, 0, 0), Eigen::Vector3f(0, 0, 1), mapModel);
voxelEngine::genProjMatrix(n, f, (float)display_h/display_w, 90, mapProj);
glUseProgram(guiProgram);
glUniform1i(glGetUniformLocation(guiProgram, "my_sampler"), 1);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, tbo);
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glUniformMatrix4fv(glGetUniformLocation(guiProgram, "projMat"), 1, GL_TRUE, mapProj);
glUniformMatrix4fv(glGetUniformLocation(guiProgram, "modelMat"), 1, GL_FALSE, mapModel);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
//glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, apothem, apothem, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
//glTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, apothem, apothem, GL_RGB, GL_UNSIGNED_BYTE, pixels);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
/*voxelEngine::mainBiomeMap->Bind();
voxelEngine::mainBiomeMap->Render();*/
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Update and Render additional Platform Windows
// (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
// For this specific demo app we could also call glfwMakeContextCurrent(window) directly)
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}
glfwSwapBuffers(window);
}
Everything renders fine except the map which is just black.
This is where I render the map:
float mapProj[16];
float mapModel[16];
voxelEngine::genModelMatrix(Eigen::Vector3f(.7, .7, .7), Eigen::Vector3f(0, 0, 0), Eigen::Vector3f(0, 0, 1), mapModel);
voxelEngine::genProjMatrix(n, f, (float)display_h/display_w, 90, mapProj);
glUseProgram(guiProgram);
glUniform1i(glGetUniformLocation(guiProgram, "my_sampler"), 1);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, tbo);
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glUniformMatrix4fv(glGetUniformLocation(guiProgram, "projMat"), 1, GL_TRUE, mapProj);
glUniformMatrix4fv(glGetUniformLocation(guiProgram, "modelMat"), 1, GL_FALSE, mapModel);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
//glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, apothem, apothem, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
//glTexSubImage2D(GL_TEXTURE_2D, 1, 0, 0, apothem, apothem, GL_RGB, GL_UNSIGNED_BYTE, pixels);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
How can I get this to work?
Related
I am facing an issue with font rendering. I am using the tutorial https://learnopengl.com/In-Practice/Text-Rendering for this.
I am developing the application in OpenGLESV3.
I have made different shader programs for 3D object drawing, for loading textures and lastly for the font rendering.
I am drawing the fonts after the 3D objects and texture drawing is done.
However the issue is that the font is not seen when i draw using orthographic projection. I am able to only see the 3D objects and texture. (Even though the shaders are different)
when I only draw font only with orthographic projection it is drawing perfectly.
I am sure it is the issue with the settings in projection, but i am unable to figure out the issue.
Can anybody help please ?
Here is the code inside my render loop
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, (GLsizei)SCR_WIDTH, (GLsizei)SCR_HEIGHT);
/********************************* 3D objects Rendering ********************************************************/
glUseProgram(shaderpgmPoints);
projection = glm::perspective(glm::radians(camspec.fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glUniformMatrix4fv(glGetUniformLocation(shaderpgmPoints, "projection"), 1, false, &projection[0][0]);
view = glm::lookAt(camspec.cameraPos, camspec.cameraPos + camspec.cameraFront, camspec.cameraUp);
glUniformMatrix4fv(glGetUniformLocation(shaderpgmPoints, "view"), 1, false, &view[0][0]);
model = glm::mat4(1.0f);
glUniformMatrix4fv(glGetUniformLocation(shaderpgmPoints, "model"), 1, false, &model[0][0]);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, quadBufferObject);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)64);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindBuffer(GL_ARRAY_BUFFER, leftlaneBufferObject);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)192);
;
glEnable(GL_PROGRAM_POINT_SIZE);
glDrawArrays(GL_LINES, 0, 12);
glDisable(GL_BLEND);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindBuffer(GL_ARRAY_BUFFER, rightlaneBufferObject);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)32);
glLineWidth(10.0f);
glDrawArrays(GL_LINES, 0, 2);
glDisable(GL_BLEND);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glUseProgram(0);
/********************************* 3D objects Rendering -End ********************************************************/
/********************************* Texture Rendering ********************************************************/
glUseProgram(shaderpgmTexture);
glm::mat4 transform = glm::mat4(1.0f);
transform = glm::translate(transform, glm::vec3(0.0f, 0.0f, 0.0f));
glUniformMatrix4fv(glGetUniformLocation(shaderpgmTexture, "transform"), 1, false, &transform[0][0]);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUniform1i(glGetUniformLocation(shaderpgmTexture, "outputTexture"), 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, camTexture);
glBindBuffer(GL_ARRAY_BUFFER, camTextureBufferObject);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glDrawArrays(GL_QUADS, 0, 4);
glDisable(GL_BLEND);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glUseProgram(0);
/********************************* Texture Rendering - End ********************************************************/
/********************************* Font Rendering ********************************************************/
glDisable(GL_DEPTH_TEST);
glUseProgram(shaderpgmFont);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glm::mat4 project = glm::ortho(0.0f, static_cast<GLfloat>(SCR_WIDTH), 0.0f, static_cast<GLfloat>(SCR_HEIGHT));
glUniformMatrix4fv(glGetUniformLocation(shaderpgmFont, "projection"), 1, false, &project[0][0]);
glUniform3f(glGetUniformLocation(shaderpgmFont, "textColor"), 1.0f, 0.0f, 0.0f);
glActiveTexture(GL_TEXTURE0);
GLfloat scale = 1.0f;
GLfloat x = 10.0f;
GLfloat y = 10.0f;
std::string text = "THE";
std::string::const_iterator c;
for (c = text.begin(); c != text.end(); c++)
{
Character ch = Characters[*c];
GLfloat xpos = x + ch.Bearing.x * scale;
GLfloat ypos = y - (ch.Size.y - ch.Bearing.y) * scale;
GLfloat w = ch.Size.x * scale;
GLfloat h = ch.Size.y * scale;
GLfloat vertices[6][4] = {
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos, ypos, 0.0, 1.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{ xpos + w, ypos + h, 1.0, 0.0 }
};
// Render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
// Update content of VBO memory
glBindBuffer(GL_ARRAY_BUFFER, FontVertexBufferObject);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Be sure to use glBufferSubData and not glBufferData
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
x += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))
}
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_BLEND);
glDisable(GL_CULL_FACE);
glUseProgram(0);
glEnable(GL_DEPTH_TEST);
/********************************* Font Rendering - End********************************************************/
I am Initializing the font buffer like this
glDisable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(shaderpgmFont);
// FreeType
FT_Library ft;
// All functions return a value different than 0 whenever an error occurred
if (FT_Init_FreeType(&ft))
std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
// Load font as face
FT_Face face;
if (FT_New_Face(ft, "fonts/arial.ttf", 0, &face))
{
std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
}
else
{
// Set size to load glyphs as
FT_Set_Pixel_Sizes(face, 0, 48);
// Disable byte-alignment restriction
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Load first 128 characters of ASCII set
for (GLubyte c = 0; c < 128; c++)
{
// Load character glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
continue;
}
// Generate texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_ALPHA,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_ALPHA,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
// Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Now store character for later use
Character character = {
texture,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x
};
Characters.insert(std::pair<GLchar, Character>(c, character));
}
glBindTexture(GL_TEXTURE_2D, 0);
// Destroy FreeType once we're finished
FT_Done_Face(face);
FT_Done_FreeType(ft);
// Configure VAO/VBO for texture quads
glGenBuffers(1, &FontVertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, FontVertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
glUseProgram(0);
glDisable(GL_CULL_FACE);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
In your initialization function, you've set up some OpenGL state via glVertexAttribPointer, which later gets clobbered.
To fix this, modify your text-rendering code so that it makes two calls glVertexAttribPointer after glBindBuffer.
I'm learning openGL programming from tutorials of Chernov # sparky engine on youtube. currently I have a vertex & a fragment shader with required classes for window ,shader etc management. I can use single vertex array object to draw on screen but when the same is done by creating a vertexArray class , it fails.
vertex array class :
#include"vertexArray.h"
namespace graphics{
VertexArray::VertexArray(){
glGenVertexArrays(1,&arrayID);
}
void VertexArray::addBuffer(Buffer* buffer, GLint index){
bind();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, arrayID);
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, buffer->getComCount(), GL_FLOAT, GL_FALSE, 0, 0);
}
void VertexArray::bind() const{
glBindVertexArray(arrayID);
}
void VertexArray::unbind() const{
glBindVertexArray(0);
}
VertexArray::~VertexArray(){
}
}
my main.cpp file
#include"graphics\window.h"
#include"utils\reader.h"
#include"graphics\shader.h"
#include"math\vec.h"
#include"math\mat4.h"
#include"graphics\buffers\buffer.h"
#include"graphics\buffers\indexbuffer.h"
#include"graphics\buffers\vertexArray.h"
using namespace graphics;
using namespace utils;
using namespace math;
int main(){
Window window;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
Reader read1("src/shaders/test.vert");
Reader read2("src/shaders/test.frag");
char * r1 = read1.getData();
char * r2 = read2.getData();
GLfloat vert[] = {
0, 0, 0,
0, 3, 0,
8, 0, 0,
8, 3, 0,
};
Buffer* vbo = new Buffer(vert,4*3,3);
GLushort indices[] = {
0,1,2,
1,3,2
};
indexBuffer ibo(indices,6);
Shader shader(r1, r2);
shader.enable();
#if 0
GLuint sprite1;
glGenVertexArrays(1, &sprite1);
glBindVertexArray(sprite1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sprite1);
vbo->bind();
glVertexAttribPointer(0, vbo->getComCount(), GL_FLOAT, 0, 0, 0);
shader.setUniformMat4("pr_matrix", mat4::orthographic(0.0f, 16.0f, 0.0f, 9.0f, -1.0f, 1.0f));
shader.setUniformMat4("ml_matrix", mat4::translation(vec3(0, 0, 0)));
shader.setUniform2f("light_pos", vec2(8.0f, 4.5f));
shader.setUniform4f("colour", vec4(0.2, 0.0, 0.4, 1));
glEnableVertexAttribArray(0);
glBindVertexArray(0);
GLuint sprite2;
glGenVertexArrays(1, &sprite2);
glBindVertexArray(sprite2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sprite2);
vbo->bind();
glVertexAttribPointer(5, vbo->getComCount(), GL_FLOAT, 0, 0, 0);
shader.setUniformMat4("pr_matrix", mat4::orthographic(0.0f, 16.0f, 0.0f, 9.0f, -1.0f, 1.0f));
shader.setUniformMat4("ml_matrix", mat4::translation(vec3(4, 3, 0)));
shader.setUniform2f("light_pos", vec2(8.0f, 4.5f));
shader.setUniform4f("colour", vec4(0.3, 0.0, 0.2, 1));
glEnableVertexAttribArray(0);
glBindVertexArray(0);
#endif
VertexArray vao;
vao.addBuffer(vbo, 0);
while (!window.closed()){
#if 0
window.clear();
glDrawArrays(GL_TRIANGLES, 0,6);
#endif
double x, y;
x = window.getX();
y = window.getY();
vao.bind();
ibo.bind();
shader.setUniform2f("light_pos", vec2((float) (x*16.0f/960.0f) , (float) (9- 9*y/540.0f)));
glDrawElements(GL_TRIANGLES, ibo.getCount(), GL_UNSIGNED_SHORT, 0);
window.update();
vao.unbind();
ibo.unbind();
}
return 0;
}
Please note that Everything works if I just create a vertex array to an GLuint variable in the main & use it .
I cant seem to find the issue here.
Any help is highly appreciated ..
You have to bind the data to the buffer (See glBufferData).
Vertex attribute buffers can be created like this:
GLfloat vert[] = {
0, 0, 0,
0, 3, 0,
8, 0, 0,
8, 3, 0,
};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*4, vert, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Element buffers can be created like this:
GLushort indices[] = {
0,1,2,
1,3,2
};
GLuint ibo;
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*3*2, indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
A Vertex Array Object and the vertex attribute pointers are specified like this:
GLuint attr_index = 0; // attribute index according to the shader program
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(attr_index , 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attr_index );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBindVertexArray( 0 );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Note, a Vertex Array Object stores all the information which you specify about the vertex attributes (format, size, attribute index ...) and it refers to the element array buffer.
Finally you can draw the mesh like this:
glBindVertexArray( vao );
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
glBindVertexArray( 0 );
I'm trying to render an image, but when the program runs, I get distorted lines for both triangles making up the quad:
init()
GLfloat vertexData[] = // Counter Clockwise
{
-0.5f, 0.5f, 0.0f, // V0 : Top Left
-0.5f, -0.5f, 0.0f, // V1 : Bottom Left
0.5f, -0.5f, 0.0f, // v2 : Bottom Right
0.5f, 0.5f, 0.0f // v3 : Top Right
};
GLint indexData[] =
{
0, 2, 3,
2, 0, 1
};
GLfloat textureCoords[] =
{
0, 0,
0, 1,
1, 1,
0, 0
};
int vSize = (sizeof(vertexData) / sizeof(vertexData[0]));
int iSize = (sizeof(indexData) / sizeof(indexData[0]));
// VAO
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
// IBO
glGenBuffers(1, &iboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, iSize * sizeof(GLuint), indexData, GL_STATIC_DRAW);
// VBO
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, vSize * sizeof(GLfloat), vertexData, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*) 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Texture
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SDL_FreeSurface(surface);
_textureLoc = glGetUniformLocation(shader.getID(), "textureSampler");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glUniform1i(_textureLoc, 0);
And within draw()
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindVertexArray(vaoID);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
I am working on a project with openGL part. The scenegraph part is based on core OpenGL1.0 and still use glBegin and glEnd.
Now I am adding new stuff on it and has to use VAO. I am a beginner and use QOpenGLFunctions_3_0 because I cannot find gl3.h from openGL registry
The problem is now that I use GL_TEXTURE_2D with glBegin(GL_QUADS), it works,
and I draw single glDrawElements( GL_QUADS, 4, GL_UNSIGNED_BYTE, 0); it works too.
But when I use GL_TEXTURE_2D and glDrawElements( GL_QUADS... , the texture is not generated correctly. Please tell me the right way to use it.
Here is thd code
/** draws Texture */
class TextureNode
: public Node
{
public:
TextureNode( const cv::Mat& mat )
{
m_mat = mat;
}
TextureNode( const std::string& id, const cv::Mat& mat )
: Node( id )
{
m_mat = mat;
}
~TextureNode()
{
//glDeleteTextures( 1, &m_texture[0] );
}
void init( TraversalContext& context )
{
initializeOpenGLFunctions();
struct Vertex {
GLfloat position[3];
GLfloat texcoord[2];
GLfloat normal[3];
};
const int NUM_VERTS = 4;
const int NUM_INDICES = 4;
Vertex vertexdata[NUM_VERTS] = {
{{0, 0, 0}, {0,0}, {0,0,1}},
{{0, 100, 0}, {0,1}, {0,0,1}},
{{100, 100, 0}, {1,1}, {0,0,1}},
{{100, 0, 0}, {1,0}, {0,0,1}},
};
GLubyte indexdata[NUM_INDICES] = { 0, 1, 2, 3 };
// Create and bind a VAO
glGenVertexArrays(1, &m_quadVAO);
glBindVertexArray(m_quadVAO);
glGenBuffers(1, &m_quadPositionVBO);
glBindBuffer(GL_ARRAY_BUFFER, m_quadPositionVBO);
// copy data into the buffer object
glBufferData(GL_ARRAY_BUFFER, NUM_VERTS * sizeof(Vertex), vertexdata, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoord));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glGenBuffers(1, &m_quadIndexVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_quadIndexVBO);
// copy data into the buffer object
glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUM_INDICES * sizeof(GLubyte), indexdata, GL_STATIC_DRAW);
// Create and bind a texture
glGenTextures(1, &m_texture); // Create The Texture
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_mat.cols, m_mat.rows, 0, GL_BGR, GL_UNSIGNED_BYTE, m_mat.data);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
//////// At this point the VAO is set up with two vertex attributes
//////// referencing the same buffer object, and another buffer object
//////// as source for index data. We can now unbind the VAO, go do
//////// something else, and bind it again later when we want to render
//////// with it.
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(NULL);
//////glBindBuffer(GL_ARRAY_BUFFER, NULL);
//glBindTexture( GL_TEXTURE_2D, NULL );
//////glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);
}
/** apply transformation */
void doWork( TraversalContext& context )
{
//QTime time;
//time.start();
initializeOpenGLFunctions();
glDisable( GL_LIGHTING );
glEnable(GL_TEXTURE_2D);
glBindVertexArray( m_quadVAO );
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glActiveTexture(GL_TEXTURE0 + m_texture - 1 );
glBindTexture(GL_TEXTURE_2D, m_texture);
glDrawElements( GL_QUADS, 4, GL_UNSIGNED_BYTE, 0);
//glBegin(GL_QUADS);
// glColor3d( 0,0,0 );
// glTexCoord2f(1.0f, 0.0f); glVertex2d( 0, 0 );
// glTexCoord2f(1.0f, 1.0f); glVertex2d( 0, m_mat.rows );
// glTexCoord2f(0.0f, 1.0f); glVertex2d( m_mat.cols, m_mat.rows );
// glTexCoord2f(0.0f, 0.0f); glVertex2d( m_mat.cols, 0 );
//glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
glBindVertexArray(0);
glEnable( GL_LIGHTING );
}
void destroy( TraversalContext& context )
{
glDeleteVertexArrays( 1, &m_quadVAO );
glDeleteTextures(1,&m_texture);
glDeleteBuffers(1, &m_quadPositionVBO);
glDeleteBuffers(1, &m_quadTexcoordVBO);
glDeleteBuffers(1, &m_quadIndexVBO);
}
protected:
GLuint m_quadVAO;
GLuint m_quadPositionVBO;
GLuint m_quadTexcoordVBO;
GLuint m_quadIndexVBO;
GLuint m_texture;
///** list of vertices */
GLfloat m_vertices[4][2];
cv::Mat m_mat;
};
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoord));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
This is using the generic vertex attributes, but that only makes sense when using sharers, and afaik don't work with the fixed-function pipeline. Either create a shader to use them, or use glVertexPointer, glTexCoordPointer, and/or glNormalPointer instead.
glActiveTexture(GL_TEXTURE0 + m_texture - 1 );
This is incorrect. glActiveTexture selects which texture unit to bind to; it does not use texture names at all. Since you're not using shaders, this should just be glActiveTexture(GL_TEXTURE0).
Side note: You don't need to use an index buffer for drawing a single quad; you can use glDrawArrays instead.
The problem:
The code is supposed to render the scene and write the results into a depth texture. When I check the contents of the texture after the render, it contains all 0 values.
What I have tried:
I have tried outputting to the screen instead of the texture and it draws the scene as expected, so the vertex and matrix values are being read correctly. I have tried multiple tutorials an they all basically say to bind a framebuffer to a texture, which is exactly what I do. I believe the trouble is probably some sort of versioning issue or something I overlooked like a parameter or something. I've been at it for a few hours and I can't quite spot it. Hopefully someone can.
Details:
OpenGL 3.1
GLSL 140
Note: The purpose of this is to allow shadow mapping to work. So there are 2 sets of shaders and draw functions. One to draw to the depth buffer and the other to draw the objects.
Code:
Shader initialization code:
void Engine::loadMObject()
{
glGenFramebuffers(1, &MObject::fbo);
glGenTextures(1, &MObject::shadowMap);
glBindFramebuffer(GL_FRAMEBUFFER, MObject::fbo);
glBindTexture(GL_TEXTURE_2D, MObject::shadowMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, windowWidth, windowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, MObject::shadowMap, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
assi(status == GL_FRAMEBUFFER_COMPLETE, "FBO error!");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
MObject::shadowProgram = Shader::compile((char*)MObject::shadowVertexShaderFile, (char*)MObject::shadowFramentShaderFile);
MObject::shadowPositionLoc = glGetAttribLocation(MObject::shadowProgram, "position");
MObject::shadowModelProjectionMatrixLoc = glGetUniformLocation(MObject::shadowProgram, "modelProjectionMatrix");
MObject::objectProgram = Shader::compile((char*)MObject::objectVertexShaderFile, (char*)MObject::objectFragmentShaderFile);
MObject::objectPositionLoc = glGetAttribLocation(MObject::objectProgram, "position");
MObject::objectTexCoordLoc = glGetAttribLocation(MObject::objectProgram, "texCoord");
MObject::objectNormalLoc = glGetAttribLocation(MObject::objectProgram, "normal");
MObject::objectWorldMatrixLoc = glGetUniformLocation(MObject::objectProgram, "worldMatrix");
MObject::objectViewMatrixLoc = glGetUniformLocation(MObject::objectProgram, "viewMatrix");
MObject::objectProjectionMatrixLoc = glGetUniformLocation(MObject::objectProgram, "projectionMatrix");
MObject::objectTextureLoc = glGetUniformLocation(MObject::objectProgram, "texture");
MObject::objectDepthModelProjectionMatrixLoc = glGetUniformLocation(MObject::objectProgram, "depthModelProjectionMatrix");
}
Object initialization code
void Engine::load(MObject* object)
{
unload(object);
// |Physics|
object->mesh = memoryManager.alloc<btTriangleIndexVertexArray>(
object->elements.length()/3,
(int*)&object->elements[0],
3*sizeof(GLuint),
object->attributes.length(),
(btScalar*)&object->attributes[0],
sizeof(MObject::Attribute));
btCollisionShape* shape;
btScalar mass = 0;
btVector3 inertia(0, 0, 0);
if(object->dynamic)
{
shape = memoryManager.alloc<btGImpactMeshShape>(object->mesh);
((btGImpactMeshShape*)shape)->updateBound();
mass = 1;
shape->calculateLocalInertia(mass, inertia);
}else{
shape = memoryManager.alloc<btBvhTriangleMeshShape>(object->mesh, true);
}
object->rigidBody = (btRigidBody*)memoryManager.galloc(sizeof(btRigidBody), memoryManager.alignment<btRigidBody>());
new (object->rigidBody) btRigidBody(mass, object, shape, inertia); // 16-byte aligned param work around...
physicsWorld->addRigidBody(object->rigidBody);
// |Graphics|
// Vertex array object
glGenVertexArrays(1, &object->objectvao);
glBindVertexArray(object->objectvao);
// Vertex buffer object
glGenBuffers(1, &object->vbo);
glBindBuffer(GL_ARRAY_BUFFER, object->vbo);
glBufferData(GL_ARRAY_BUFFER, object->attributes.length()*sizeof(MObject::Attribute), object->attributes.data, GL_STATIC_DRAW);
// Vertex attributes
glEnableVertexAttribArray(object->objectPositionLoc);
glVertexAttribPointer(object->objectPositionLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MObject::Attribute), (void*)0);
glEnableVertexAttribArray(object->objectTexCoordLoc);
glVertexAttribPointer(object->objectTexCoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(MObject::Attribute), (void*)offsetof(class MObject::Attribute, texCoord));
glEnableVertexAttribArray(object->objectNormalLoc);
glVertexAttribPointer(object->objectNormalLoc, 3, GL_SHORT, GL_TRUE, sizeof(MObject::Attribute), (void*)offsetof(class MObject::Attribute, normal));
// Element buffer object
object->eboNumElements = object->elements.length();
glGenBuffers(1, &object->ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, object->elements.length()*sizeof(GLuint), object->elements.data, GL_STATIC_DRAW);
glBindVertexArray(0);
// Shadow vao
glGenVertexArrays(1, &object->shadowvao);
glBindVertexArray(object->shadowvao);
glBindBuffer(GL_ARRAY_BUFFER, object->vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->ebo);
glEnableVertexAttribArray(object->shadowPositionLoc);
glVertexAttribPointer(object->shadowPositionLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MObject::Attribute), (void*)0);
glBindVertexArray(0);
mObjectList.pushBack(object);
}
Draw code:
/* START - SECTION OF MAIN UPDATE LOOP */
// |Draw update|
// Shadow Pass
// testing...
if(isKeyDown(SDL_SCANCODE_0)){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(MObject::shadowProgram);
glBindFramebuffer(GL_FRAMEBUFFER, MObject::fbo);
glBindTexture(GL_TEXTURE_2D, MObject::shadowMap);
for(U32 i=0;i<mObjectList.length();++i)
{
drawShadow(mObjectList[i]);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, MObject::shadowMap);
GLfloat* pixels = new GLfloat[windowWidth*windowHeight];
glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, pixels);
for(U32 i=0;i<windowHeight*windowHeight;++i)
{
if(pixels[i] != 0.0f)
printd("I GOT IT!");
}
delete[] pixels;
}else{
// Object Pass
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(MObject::objectProgram);
for(U32 i=0;i<mObjectList.length();++i)
{
drawObject(mObjectList[i]);
}
}
// Swapping buffers
SDL_GL_SwapWindow(mainWindow);
/* END - SECTION OF MAIN UPDATE LOOP*/
void Engine::drawObject(MObject* object)
{
TransformMatrix matrix;
getOrthographicMatrix(matrix, -10, 10, 10, -10, -10, 20);
glBindVertexArray(object->objectvao);
glUniformMatrix4fv(MObject::objectWorldMatrixLoc, 1, GL_FALSE, (GLfloat*)&object->worldMatrix);
glUniformMatrix4fv(MObject::objectViewMatrixLoc, 1, GL_FALSE, (GLfloat*)&getViewMatrix());
glUniformMatrix4fv(MObject::objectProjectionMatrixLoc, 1, GL_FALSE, (GLfloat*)&getProjectionMatrix());
glUniformMatrix4fv(MObject::objectDepthModelProjectionMatrixLoc, 1, GL_FALSE, (GLfloat*)&getProjectionMatrix());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, object->tbo);
glUniform1i(MObject::objectTextureLoc, 0);
glDrawElements(GL_TRIANGLES, object->eboNumElements, GL_UNSIGNED_INT, 0);
}
void Engine::drawShadow(MObject* object)
{
TransformMatrix matrix;
//getOrthographicMatrix(matrix, -10, 10, 10, -10, -10, 20);
matrix = getProjectionMatrix() * getViewMatrix() * object->worldMatrix;
glBindVertexArray(object->shadowvao);
glUniformMatrix4fv(MObject::shadowModelProjectionMatrixLoc, 1, GL_FALSE, (GLfloat*)&matrix);
glDrawElements(GL_TRIANGLES, object->eboNumElements, GL_UNSIGNED_INT, 0);
}
Vertex Shader:
#version 140
in vec3 position;
uniform mat4 modelProjectionMatrix;
void main(){
gl_Position = modelProjectionMatrix * vec4(position, 1.0f);
}
Fragment Shader:
#version 140
out float fragmentdepth;
void main(){
fragmentdepth = gl_FragCoord.z;
}
/*
out vec4 color;
void main(){
color = vec4(clamp(gl_FragCoord.z, 0.0f, 1.0f), 0.0f, 0.0f, 1.0f);
}*/
I figured it out. Apparently if you rearrange the opengl calls a little bit it works. I believe what I did was put glBindFramebuffer before glUseProgram and it worked.