OpenGL glm::perspective() doesn't work properly, objects aren't 3d - c++

This is my game loop. I know I don't need to declare these variables twice, but I did them just in case:
while (!glfwWindowShouldClose(window))
{
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderProgram);
glm::mat4 view;
view = glm::rotate(view, 50.0f, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 projection;
projection = glm::perspective(-45.0f, float(WIDTH / HEIGHT), 0.1f, 100.0f);
GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(VAO);
glm::mat4 model;
model = glm::scale(model, glm::vec3(0.5f, 0.5f, 0.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glUseProgram(shaderProgram2);
glm::mat4 view2;
view2 = translate(view2, glm::vec3(0.0f, 0.0f, -0.6f));
GLint modelLoc2 = glGetUniformLocation(shaderProgram2, "lightmodel");
GLint viewLoc2 = glGetUniformLocation(shaderProgram2, "lightview");
GLint projLoc2 = glGetUniformLocation(shaderProgram2, "lightprojection");
glm::mat4 model2;
model2 = glm::scale(model2, glm::vec3(0.5f, 0.5f, 0.5f));
glUniformMatrix4fv(viewLoc2, 1, GL_FALSE, glm::value_ptr(view2));
glUniformMatrix4fv(projLoc2, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(modelLoc2, 1, GL_FALSE, glm::value_ptr(model2));
glBindVertexArray(lightVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
And this the output:
https://imgur.com/a/arEd7
I'm not clear if glm::perspective() input should be radians or degrees, but I tried both and they all gave the same zoomed-in, non-3D result. I don't know what I'm doing wrong here. I'm doing these according to this tutorial. My body of code looks exactly like the tutorial's save for a few classes that I created myself. What could be the problem here?

Related

OpenGL: Program renders plain white square instead of a transformed square

I am trying to render a 2D square that has color and is transformed due to 4x4 matrices. The output is supposed to look like this:
However, I am instead getting this:
This is frustrating because I had this problem in another project. My professor walked me through how to solve it and all we did was play around with a few things here and there and it just magically worked, no code was altered. Now I am experiencing this issue again and there are no clues as to why this is happening. I typed the tutorial code correctly character for character and it is not being rendered correctly. It did however render correctly on my professor's computer.
Here is the code:
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#define WINDOW_TITLE "Modern OpenGL"
#ifndef GLSL
#define GLSL(Version, Source) "#version" #Version "\n" #Source
#endif
GLint shaderProgram, windowWidth = 800, windowHeight = 600;
GLuint VBO, VAO, EBO, texture;
void uResizeWindow(int, int);
void uRenderGraphics();
void uCreateShader();
void uCreateBuffers();
//Vertex shader source code
const GLchar* vertexShaderSource = GLSL(330,
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
out vec3 mobileColor;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(position, 1.0f); //transforms vertices to clip coordinates
mobileColor = color; //references incoming color data
});
//Fragment shader source code
const GLchar* fragmentShaderSource = GLSL(330,
in vec3 mobileColor;
out vec4 gpuColor;
void main() {
gpuColor = vec4(mobileColor, 1.0f);
});
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow(WINDOW_TITLE);
glutReshapeFunc(uResizeWindow);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cout << "Failed to initialize GLEW" << std::endl;
}
uCreateShader();
uCreateBuffers();
glUseProgram(shaderProgram);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glutDisplayFunc(uRenderGraphics);
glutMainLoop();
//Destroys buffer objects once used
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
return 0;
}
void uResizeWindow(int w, int h) {
windowWidth = w;
windowHeight = h;
glViewport(0, 0, windowWidth, windowHeight);
}
void uRenderGraphics() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(VAO);
glm::mat4 model(1.0f);
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); //place object at center of viewport
model = glm::rotate(model, 15.0f, glm::vec3(1.0f, 0.0f, 0.0f)); //rotate object 15 degrees on x-axis
model = glm::scale(model, glm::vec3(2.0f, 2.0f, 2.0f)); //increase object size by factor of 2
//transforms the camera
glm::mat4 view(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); //moves camera backwards -3 units in z
//creates perspective projection
glm::mat4 projection(1.0f);
projection = glm::perspective(45.0f, (GLfloat)windowWidth / (GLfloat)windowHeight, 0.1f, 100.0f);
//retrieves and passes transform matrices to shader program
GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
//draws the triangles
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0); //deactive the vertex array object
glutSwapBuffers();
}
void uCreateShader() {
//vertex
GLint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
//fragment
GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
void uCreateBuffers() {
GLfloat vertices[] = {
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, //top right vertex 0
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, //bottom right vertex 1
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, //bottom left vertex 2
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f //top left vertex 3
};
GLuint indices[] = {
0, 1, 3, //triangle 1
1, 2, 3 //triangle 2
};
//gen buffer ids
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
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 attrib pointer 0 to hold pos data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
//set attrib pointer 1 to hold color data
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0); //deactivate VAO
}
The vertex shader and the fragment shader cannot be compiled because a space is missing in the GLSL macro (after #version):
#define GLSL(Version, Source) "#version" #Version "\n" #Source
#define GLSL(Version, Source) "#version " #Version "\n" #Source
I recommend to check if the shader compilation succeeded and if the program object linked successfully.
If the compiling of a shader succeeded can be checked by glGetShaderiv and the parameter GL_COMPILE_STATUS. If the linking of a program was successful can be checked by glGetProgramiv and the parameter GL_LINK_STATUS.
See the answer to OpenGL ignores Quads and makes them Triangles.
The unit of the angle argument of glm::perspective is radian:
projection = glm::perspective(45.0f, (GLfloat)windowWidth / (GLfloat)windowHeight, 0.1f, 100.0f);
projection = glm::perspective(glm::radians(45.0f),
(GLfloat)windowWidth / (GLfloat)windowHeight, 0.1f, 100.0f);
You missed to set the matrix uniforms:
GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));

How to draw multiple objects in OpenGL using multiple VAO and VBO?

I'm trying to render multiple objects in OpenGL using multiple VAO's and VBO's. To render multiple objects using the same vertices I've done it, but what I want to do is to use different vertices for each object, for example to draw a square and a circle. For a square I only need 6 vertices but for circle I need 360.
I have error with reading or creating the shaders.
Here is the Vertex Shader:
#version 330 core
layout (location = 0) in vec4 position;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * position;
}
Fragment Shader:
#version 330 core
layout(location = 0) out vec4 color;
uniform vec4 u_Color;
void main()
{
color = u_Color;
}
The VAO and VBO generating and binding
// Circle
std::vector<VertexFormat> vertices;
for (int i = 0; i < 360; i++)
{
GLfloat angle = i * ((2.0f * 3.14159f) / 360);
vertices.push_back(VertexFormat(glm::vec3(cos(angle) * 100.0f, sin(angle) * 100.0f, 0.0f)));
}
// Pipette object
std::vector<VertexFormat> pipetteVertices;
pipetteVertices.push_back(VertexFormat(glm::vec3(injPipette.x + 500.0f, injPipette.y + 500.0f, 0.0f))); // 0
pipetteVertices.push_back(VertexFormat(glm::vec3(injPipette.x + 700.0f, injPipette.y + 500.0f, 0.0f))); // 1
pipetteVertices.push_back(VertexFormat(glm::vec3(injPipette.x + 700.0f, injPipette.y + 700.0f, 0.0f))); // 2
pipetteVertices.push_back(VertexFormat(glm::vec3(injPipette.x + 500.0f, injPipette.y + 700.0f, 0.0f))); // 3
pipetteVertices.push_back(VertexFormat(glm::vec3(injPipette.x + 500.0f, injPipette.y + 500.0f, 0.0f)));
pipetteVertices.push_back(VertexFormat(glm::vec3(injPipette.x + 700.0f, injPipette.y + 700.0f, 0.0f)));
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexFormat) * 6, &pipetteVertices[0], GL_STATIC_DRAW);
//Position attribute
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexFormat), (GLvoid *)0);
GLuint vao2;
glGenVertexArrays(1, &vao2);
glBindVertexArray(vao2);
GLuint vbo2;
glGenBuffers(1, &vbo2);
glBindBuffer(GL_ARRAY_BUFFER, vbo2);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexFormat) * 360, &vertices[0], GL_STATIC_DRAW);
//Position attribute
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexFormat), (GLvoid *) 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
And the draw call in the rendering loop:
//Get the uniform locations of model, view and projection matrices
modelID = glGetUniformLocation(program, "model");
viewID = glGetUniformLocation(program, "view");
projectionID = glGetUniformLocation(program, "projection");
//View transform
glm::mat4 view = glm::lookAt(glm::vec3(0, 0, 2), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
//Projection transform
//glm::mat4 projection = glm::perspective(45.0f, (GLfloat)screenWidth / (GLfloat)screenHeight, 0.1f, 1000.0f);
glm::mat4 projection = glm::ortho(0.0f, (GLfloat)screenWidth, 0.0f, (GLfloat)screenHeight, 0.1f, 10.0f);
{
glUniformMatrix4fv(viewID, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projectionID, 1, GL_FALSE, glm::value_ptr(projection));
glm::mat4 translate = glm::translate(glm::mat4(1.0), glm::vec3(100.0f + move_x, 100.0f + move_y, 0.0f));
glm::mat4 rotate = glm::rotate(glm::mat4(1.0), 0.0f, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 scale = glm::scale(glm::mat4(1.0), glm::vec3(1.0f, 1.0f, 2.0f));
glm::mat4 model = translate * rotate * scale;
glUniformMatrix4fv(modelID, 1, GL_FALSE, glm::value_ptr(model));
glUniform4f(color, 0.0f, 0.0f, 1.0f, 1.0f);
//Render
glDrawArrays(GL_TRIANGLE_FAN, 0, 360);
}
{
glUniformMatrix4fv(viewID, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projectionID, 1, GL_FALSE, glm::value_ptr(projection));
glm::mat4 translate = glm::translate(glm::mat4(1.0), glm::vec3(300.0f + injPipette.x, 300.0f + injPipette.y, 0.0f));
glm::mat4 rotate = glm::rotate(glm::mat4(1.0), 0.0f, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 scale = glm::scale(glm::mat4(1.0), glm::vec3(1.0f, 1.0f, 2.0f));
glm::mat4 model = translate * rotate * scale;
glUniformMatrix4fv(modelID, 1, GL_FALSE, glm::value_ptr(model));
glUniform4f(color, 1.0f, 0.0f, 0.0f, 0.5f);
//Render
glDrawArrays(GL_TRIANGLES, 0, 6);
}
I repeat, to draw multiple objects using the same vertices I've done it. I need to know how to draw multiple objects using the same vertex and fragment shader but different number of vertices. The project is small, just rendering maximum 10 objects in 2D.
Drawing commands such as glDrawArrays process and draw the arrays of generic vertex attribute data specified in the currently bound Vertex Array Object.
This means that you need to bind the correct vertex array object before executing the Draw call:
// [...]
glBindVertexArray(vao2);
glDrawArrays(GL_TRIANGLE_FAN, 0, 360);
// [...]
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
I found a project with the code:
#pragma once
#include "glm\glm.hpp"
struct VertexFormat
{
glm::vec3 position;
VertexFormat(const glm::vec3 &iPos)
{
position = iPos;
}
VertexFormat()
{
}
};

Modern Opengl: Simultaneous orthographic and perspective projection

I am trying to display two cubes using modern opengl (https://preview.ibb.co/hif8t6/Screenshot_2017_10_31_09_59_27.png). The first cube is displayed by orthographic projection (left) and the second by perspective projection (right). That is working fine but i am unable to get the left cube to go behind the right cube.
Here is the relavant snippet of code
ourShader_ortho.Use();
ourShader_persp.Use();
glm::mat4 model_ortho, model1, model2, model;
glm::mat4 view_ortho, view_persp;
glm::mat4 orthographic;
glm::mat4 perspective;
model1 = glm::rotate(model, (GLfloat)glfwGetTime()*1.0f, glm::vec3(0.5f, 1.0f, 0.0f));
model2 = glm::rotate(model, (GLfloat)glfwGetTime()*1.0f, glm::vec3(0.0f, 1.0f, 0.5f));
model = model1 * model2;
view_ortho = glm::translate(view_ortho, glm::vec3(200.0f, 200.0f, -150.0f));
orthographic = glm::ortho(0.0f, (GLfloat)width, 0.0f, (GLfloat)height, 0.1f, 200.0f);
view_persp = glm::translate(view_persp, glm::vec3(1.0f, 0.0f, -3.0f));
perspective = glm::perspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 200.0f);
GLint modelLoc_ortho = glGetUniformLocation(ourShader_ortho.Program, "model");
GLint viewLoc_ortho = glGetUniformLocation(ourShader_ortho.Program, "view");
GLint projLoc_ortho = glGetUniformLocation(ourShader_ortho.Program, "orthographic");
glUniformMatrix4fv(modelLoc_ortho, 1, GL_FALSE, glm::value_ptr(model_ortho));
glUniformMatrix4fv(viewLoc_ortho, 1, GL_FALSE, glm::value_ptr(view_ortho));
glUniformMatrix4fv(projLoc_ortho, 1, GL_FALSE, glm::value_ptr(orthographic));
glBindVertexArray(VAO_O);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
GLint modelLoc_persp = glGetUniformLocation(ourShader_persp.Program, "model");
GLint viewLoc_persp = glGetUniformLocation(ourShader_persp.Program, "view");
GLint projLoc_persp = glGetUniformLocation(ourShader_persp.Program, "perspective");
glUniformMatrix4fv(modelLoc_persp, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc_persp, 1, GL_FALSE, glm::value_ptr(view_persp));
glUniformMatrix4fv(projLoc_persp, 1, GL_FALSE, glm::value_ptr(perspective));
glBindVertexArray(VAO_P);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
What do i do to get the cube with orthographic projection to go behind the cube with perspective projection?
I'm not a OpenGL programmer but this is what I think.
You are drawing the object twice using the glDrawArrays lines. For the projection matrices they are using your orthographic and perspective matrices.
However these matrices are created using different parameters:
view_ortho = glm::translate(view_ortho, glm::vec3(200.0f, 200.0f, -150.0f));
view_persp = glm::translate(view_persp, glm::vec3(1.0f, 0.0f, -3.0f));
My guess is that you want to unify the translation parameters so that the cubes lie exactly on top of each other.
E.g.
view_ortho = glm::translate(view_ortho, glm::vec3(200.0f, 200.0f, -150.0f));
view_persp = glm::translate(view_persp, glm::vec3(200.0f, 200.0f, -150.0f));

OpenGL Move One Of My Two Shapes

I'm creating a game in SDL where everything has to be drawn in OpenGL. I've drawn two shapes with different textures and added keyboard controls. My problem is only my Player textured shape should be controllable but instead both of my shapes are stuck together and the vector I thought controlled the enemy position doesn't. Is my problem when I draw? How do I make it so I my player controls only move the player shape.
//Declare transform and model matrix.
glm::mat4 Model; glm::mat4 Rotation; glm::mat4 View;
glm::mat4 Projection;
glm::vec3 Player = glm::vec3(2.0f, 0.5f, -1.0f);
GLfloat playermovespeed = 0.1f;
Projection = glm::ortho(0.0f, 4.0f, 0.0f, 3.0f, -1.0f, 100.0f);
Model = glm::translate(Model, Player);
GLint ProjectionLocation = glGetUniformLocation(shaderProgram, "ProjectionMat");
glUniformMatrix4fv(ProjectionLocation, 1, GL_FALSE, glm::value_ptr(Projection));
SDL_GetWindowSize(window, &w, &h); glViewport(0, 0, w, h);
while (WindowOpen)
{
glBindVertexArray(VAO);
glBindTexture(GL_TEXTURE_2D, texture[0]);
Model = glm::mat4();
Model = glm::translate(Model, glm::vec3(0.0f, 15.0f, 5.0f)); //Position of enemy shape
GLint ModelLocation = glGetUniformLocation(shaderProgram, "ModelMat");
GLint ViewLocation = glGetUniformLocation(shaderProgram, "ViewMat");
//glUniformMatrix4fv(ModelLocation, 1, GL_FALSE, glm::value_ptr(Model*Rotation));
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
//Player texture
glBindVertexArray(VAO);
glBindTexture(GL_TEXTURE_2D, textureA);
Model = glm::mat4(); //y
Model = glm::translate(Model, Player); //Position control of ship.
ModelLocation = glGetUniformLocation(shaderProgram, "ModelMat");
glUniformMatrix4fv(ModelLocation, 1, GL_FALSE, glm::value_ptr(Model));
glUniformMatrix4fv(ViewLocation, 1, GL_FALSE, glm::value_ptr(View));
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
SDL_GL_SwapWindow(window);
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
WindowOpen = false;
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_RIGHT:
Player.x += playermovespeed;
break;
case SDLK_LEFT:
Player.x -= playermovespeed;
break;
default:
break;
}
}
}
}
}
But just in case here is also how I set up everything.
GLfloat vertices[] = {
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // Top Right
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // Bottom Right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // Bottom Left
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
};
GLuint indices[] = { start from 0!
0, 1, 3,
1, 2, 3,
};
//Load image Enemy; //Load image Player; //Load shaders;
GLuint VBO;
glGenBuffers(1, &VBO);
GLuint EBO;
glGenBuffers(1, &EBO);
GLuint texture[2];
glGenTextures(2, texture);
glBindTexture(GL_TEXTURE_2D, texture[0]);
//set texture parameters. First texture.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
SDL_FreeSurface(image);
//Second texture same way as first; //Set texture parameters
GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
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);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);

Difficulty with rotation in opengl 3

I have a problem with my code. I am rendering two objects at the moment. They are floor and figure. Floor is in VAO[0] and figure in VAO1. My problem is that I can not see the object floor. I know I have to change the view of the object floor to make it visible. I want to do this with an rotation. I only want to change floor. Here is my hole code. Could someone please help me. I only want to use modern opengl in this content opengl 3.3. I want to have my view like this: My scene The red area is floor and the green is figure.
How can I achieve my objective. For your information. This is a test scene. My distant objective is hole scene like in a 3d game. So this means more polygones for the floor and rest of the scence like walls, ceillings, objects in the room and nps. All made with sprites, as I think this would be the best way if will not use a 3d programm to create 3d models. So if you have tips for my objects, appreciate it.
Thank you for your help.
#define GLFW_NO_GLU
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>
int main()
{
if(glfwInit()==GL_FALSE)
{
std::cerr<<"GLFW FAIL";
return 1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(1024, 748, "My Game", NULL, NULL);
if (window==GL_FALSE)
{
std::cerr<<"Open Window FAIL";
return 1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if(glewInit()!=GLEW_OK)
{
std::cerr<<"GLEW FAIL";
return 1;
}
glViewport(0, 0, 1024, 748);
const GLchar* vertexshadersrc="#version 330 core\n"
"layout (location = 0) in vec4 position;\n"
"uniform mat4 matrix;\n"
"void main()\n"
"{\n"
"gl_Position = gl_Position=matrix * position;\n"
"}\0";
const GLchar* fragmentshadersrc="#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color=vec4(1.0f,0.0f,0.0f,1.0f);\n"
"}\0";
GLuint vertexshader=glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexshader,1,&vertexshadersrc,NULL);
glCompileShader(vertexshader);
GLint compile_ok;
GLint errlength;
GLchar* errmsg;
glGetShaderiv(vertexshader,GL_COMPILE_STATUS, &compile_ok);
if(compile_ok==GL_FALSE)
{
glGetShaderiv(vertexshader, GL_INFO_LOG_LENGTH,&errlength);
errmsg=new GLchar[errlength];
glGetShaderInfoLog(vertexshader,errlength,&errlength,errmsg);
std::cerr<<"Vertexshader";
std::cerr<<errmsg;
return 1;
}
GLuint fragmentshader=glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentshader,1,&fragmentshadersrc,NULL);
glCompileShader(fragmentshader);
glGetShaderiv(fragmentshader,GL_COMPILE_STATUS, &compile_ok);
if(compile_ok==GL_FALSE)
{
glGetShaderiv(fragmentshader, GL_INFO_LOG_LENGTH,&errlength);
errmsg=new GLchar[errlength];
glGetShaderInfoLog(fragmentshader,errlength,&errlength,errmsg);
std::cerr<<"Fragmentshader";
std::cerr<<errmsg;
return 1;
}
GLuint programm=glCreateProgram();
glAttachShader(programm,vertexshader);
glAttachShader(programm,fragmentshader);
glLinkProgram(programm);
glDeleteShader(vertexshader);
glDeleteShader(fragmentshader);
GLfloat floor[]=
{
-1.0f, 0.0f, 1.0f,
-1.0f, 0.0f, -1.0f,
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, -1.0f,
};
GLfloat floorc[]=
{
1.0f,0.0f,0.0f,1.0f
};
GLfloat figure[] = {
-0.2f, 0.4f, -0.1f,
-0.2f, 0.0f, -0.1f,
0.2f, 0.4f, -0.1f,
0.2f, 0.0f, -0.1f
};
GLfloat figurec[]=
{
0.0f,1.0f,0.0f,1.0f
};
GLfloat tree[] = {
-0.7f, 0.4f, -0.1f,
-0.7f, 0.0f, -0.1f,
-0.3f, 0.4f, -0.1f,
-0.3f, 0.0f, -0.1f
};
GLuint VAO[3], VBO[6];
glGenVertexArrays(3, VAO);
glGenBuffers(6, VBO);
glBindVertexArray(VAO[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER,sizeof(floor),floor,GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER,sizeof(floorc),floorc,GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glBindVertexArray(VAO[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(figure), figure, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, VBO[3]);
glBufferData(GL_ARRAY_BUFFER, sizeof(figurec), figurec, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glBindVertexArray(VAO[2]);
glBindBuffer(GL_ARRAY_BUFFER, VBO[4]);
glBufferData(GL_ARRAY_BUFFER, sizeof(tree), tree, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glm::mat4 projection;
projection=glm::perspective(45.0f,4.0f/3.0f,0.1f,100.0f);
glm::mat4 modelview;
modelview=glm::translate(glm::mat4(1.0f),glm::vec3(0.0f, 0.0f, -1.0f));
glm::mat4 mpmatrix=projection*modelview;
//GLuint position=glGetAttribLocation(programm,"position");
GLuint matrixuni=glGetUniformLocation(programm,"matrix");
//glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(GLvoid*)0);
glUseProgram(programm);
glUniformMatrix4fv(matrixuni,1,GL_FALSE,&mpmatrix[0][0]);
while (!glfwWindowShouldClose(window))
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(VAO[0]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(VAO[1]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
/*glBindVertexArray(VAO[2]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);*/
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
Image on the screen generated from 3 matrices: projection matrix, view matrix and model matrix. Seems that you forget about view matrix. I replaced your matrix part by this code:
glm::mat4 projection;
projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
glm::mat4 view;
view = glm::lookAt(glm::vec3(0, 0.25, 0.75), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
glm::mat4 modelview;
modelview = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -1.0f));
glm::mat4 mpmatrix = projection*view*modelview;
And I got this picture:
It's look like your picture but both objects have red color because they use same shaders where color sets as red.
UPD:
Maybe for you will be enough decrease a little floor height:
GLfloat floor[] = {
-1.0f, -0.05f, 1.0f,
-1.0f, -0.05f, -1.0f,
1.0f, -0.05f, 1.0f,
1.0f, -0.05f, -1.0f,
};