In this project, I made a 3d cube and added a camera. We can go forward, backward, right or left with the W, A, S, D keys, but we cannot turn right or left. For example, I want to see the back side of the cube, but I cannot see it.Actually this is a simple problem and its solution is simple but my math level is insufficient, it will be better if someone explain it to me.I'll tell you my code and what I've tried below.
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include <GLFW/glfw3.h>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <math.h>
int width = 1280;
int height = 720;
float camera_z = 5;
float camera_y = 0;
float camera_x = 0;
float fov = 60;
GLFWwindow* window;
float speed = 0.01;
GLfloat vertices[] = {
-1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1,
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1,
-1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1
};
GLfloat colors[] = {
1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0,
1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0,
1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0,
1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0,
1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0,
1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0
};
void keyboard() {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
camera_z = camera_z - speed;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
camera_x = camera_x - speed;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
camera_z = camera_z + speed;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
camera_x = camera_x + speed;
}
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
camera_y = camera_y + speed;
}
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
camera_y = camera_y - speed;
}
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
// need help
}
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
// need help
}
}
void drawCube() {
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);
glDrawArrays(GL_QUADS, 0, 24);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
int main(void)
{
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(width, height, "C++ OpenGL ", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
glEnable(GL_DEPTH_TEST);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
glViewport(0, 0, width, height);
/* Render here */
glClearColor(0.0, 192/256, 1, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = (float)width / (float)height;
float fov = 60;
gluPerspective(fov, aspect, 0.1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
keyboard();
gluLookAt(
camera_x,
camera_y,
camera_z,
camera_x + 1, // need help
camera_y + 1, // need help
camera_z - fov ,
0,
1,
0
);
glTranslatef(0, 0,-3);
drawCube();
glFlush();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
The things I tried were the variables camera_eyex,*_eyey,*_eyez. I added them to the loop to give the camera + 1 value. I used to increase or decrease them when I wanted to go left or right, but there are many errors in this method. For example, even if the camera rotates 45 degrees when I press the W key, it goes straight, so it does not go where I am looking. Also, turning more than 90 degrees to the right or left It's not possible. Also, when I press the left button to go left, turning is getting slower and slower.
You need to build a transform matrix. A transformation usually is a combination of translation, scale and rotation.
Where you first rotate, then scale and then translate (the actual order of calculation - multiplication - is reversed):
translation x scale x rotation
If you want to scale or rotate around a certain point (pivot or center), then you have to translate to the center point and at the end translate back to the origin, like:
translation x center x scale x rotation x -center
The lookAt algorithm sets the rotation and translation based on the parameters (eye, target, up), whereas your goal is to separate the rotation from the translation. Therefore you have to build your own transformation, e.g.:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//set the position of the camera
glTranslate(pos);
//set the scale or rotation pivot
//glTranslate(center);
//scale
//glScale(scale);
//rotate around z-axis first
glRotate(z_angle, 0, 0, 1);
//rotate around y-axis
glRotate(y_angle, 0, 1, 0);
//rotate around x-axis
glRotate(x_angle, 1, 0, 0);
//set the center back, if set before
//glTranslate(-center);
You'll find more info here (OpenGl related, although 'modern' OpenGl): https://learnopengl.com/Getting-started/Transformations
Note: You probably have to adjust the input 'W A S D' to the changed axises. If, for example the 'W' key adjusts the z-component, you will probably not go 'forward' (along the z-axis) with the setup above. In order to move according to the transformed axises, you'll need the transformation matrix and extract them. Better is to use a math library, e.g. glm. How a orbit camera could be implemented, again, have a look at: https://learnopengl.com/Getting-started/Camera
Building a camera system, needs some theoretical background in linear algebra.
Based on the comment section below, i list all relevant links here:
Dot product
Cross product
Translation matrix
Rotation matrix
Matrix multiplication
On top of that, it can be overwhelming for those who never visited a course in linear algebra, therefore i highly recommend The Essence of linear algebra by 3Blue1Brown.
A note on matrix multiplication, what is not adequate emphasized and mostly overlooked: Once one has internalized the geometric meaning of a dot product, and the geometric meaning of the rows and columns of a matrix, one should also note that each component of the resulting matrix is the result of the dot product of a row and a column vector, visualize that and internalize it.
Related
I am practicing DirectX 11 following Frank Luna's book.
I have implemented a demo that renders a cube, but the result is not correct.
https://i.imgur.com/2uSkEiq.gif
As I hope you can see from the image (I apologize for the low quality), it seems like the camera is "trapped" inside the cube even when I move it away. There is also a camera frustum clipping problem.
I think the problem is therefore in the definition of the projection matrix.
Here is the cube vertices definition.
std::vector<Vertex> vertices =
{
{XMFLOAT3(-1, -1, -1), XMFLOAT4(1, 1, 1, 1)},
{XMFLOAT3(-1, +1, -1), XMFLOAT4(0, 0, 0, 1)},
{XMFLOAT3(+1, +1, -1), XMFLOAT4(1, 0, 0, 1)},
{XMFLOAT3(+1, -1, -1), XMFLOAT4(0, 1, 0, 1)},
{XMFLOAT3(-1, -1, +1), XMFLOAT4(0, 0, 1, 1)},
{XMFLOAT3(-1, +1, +1), XMFLOAT4(1, 1, 0, 1)},
{XMFLOAT3(+1, +1, +1), XMFLOAT4(0, 1, 1, 1)},
{XMFLOAT3(+1, -1, +1), XMFLOAT4(1, 0, 1, 1)},
};
Here is how I calculate the view and projection matrices.
void TestApp::OnResize()
{
D3DApp::OnResize();
mProj = XMMatrixPerspectiveFovLH(XM_PIDIV4, AspectRatio(), 1, 1000);
}
void TestApp::UpdateScene(float dt)
{
float x = mRadius * std::sin(mPhi) * std::cos(mTheta);
float y = mRadius * std::cos(mPhi);
float z = mRadius * std::sin(mPhi) * std::sin(mTheta);
XMVECTOR EyePosition = XMVectorSet(x, y, z, 1);
XMVECTOR FocusPosition = XMVectorZero();
XMVECTOR UpDirection = XMVectorSet(0, 1, 0, 0);
mView = XMMatrixLookAtLH(EyePosition, FocusPosition, UpDirection);
}
And here is how I update the camera position on mouse move.
glfwSetCursorPosCallback(mMainWindow, [](GLFWwindow* window, double xpos, double ypos)
{
TestApp* app = reinterpret_cast<TestApp*>(glfwGetWindowUserPointer(window));
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
float dx = 0.25f * XMConvertToRadians(xpos - app->mLastMousePos.x);
float dy = 0.25f * XMConvertToRadians(ypos - app->mLastMousePos.y);
app->mTheta += dx;
app->mPhi += dy;
app->mPhi = std::clamp(app->mPhi, 0.1f, XM_PI - 0.1f);
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
{
float dx = 0.05f * XMConvertToRadians(xpos - app->mLastMousePos.x);
float dy = 0.05f * XMConvertToRadians(ypos - app->mLastMousePos.y);
app->mRadius += (dx - dy);
app->mRadius = std::clamp(app->mRadius, 3.f, 15.f);
}
app->mLastMousePos = XMFLOAT2(xpos, ypos);
});
Thanks.
The root problem here was in the constant buffer vs. CPU update.
HLSL defaults to column-major matrix definitions per Microsoft Docs. DirectXMath uses row-major matrices, so you have to transpose while updating the Constant Buffer.
Alternatively, you can declare the HLSL matrix with the row_major keyword, #pragma pack_matrix, or the /Zpr compiler switch.
I'm trying to look at the square from the other side using the gluLookAt() function.
After using the function, nothing changes, although I expected that the corners of the square will change.
I set the camera point to the rightmost part of the world and look at its center, where the square is located.
He had to stretch out to the sides. Why hasn't anything changed?
Code:
#include "includes.h"
using namespace std;
constexpr auto FPS_RATE = 60;
int windowHeight = 600, windowWidth = 600, windowDepth = 600;
void init();
void idleFunction();
void displayFunction();
double getTime();
double getTime()
{
using Duration = std::chrono::duration<double>;
return std::chrono::duration_cast<Duration>(
std::chrono::high_resolution_clock::now().time_since_epoch()
).count();
}
const double frame_delay = 1.0 / FPS_RATE;
double last_render = 0;
void init()
{
glutDisplayFunc(displayFunction);
glutIdleFunc(idleFunction);
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-windowWidth / 2, windowWidth / 2, -windowHeight / 2, windowHeight / 2, -windowDepth / 2, windowDepth / 2);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
void idleFunction()
{
const double current_time = getTime();
if ((current_time - last_render) > frame_delay)
{
last_render = current_time;
glutPostRedisplay();
}
}
void displayFunction()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
gluLookAt(-300, 0, 0,
0, 0, 0,
0, 1, 0);
glColor3f(1, 1, 1);
glVertex3i(-150, 150, 0);
glVertex3i(150, 150, 0);
glVertex3i(150, -150, 0);
glVertex3i(-150, -150, 0);
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition((GetSystemMetrics(SM_CXSCREEN) - windowWidth) / 2, (GetSystemMetrics(SM_CYSCREEN) - windowHeight) / 2);
glutCreateWindow("Window");
init();
glutMainLoop();
return 0;
}
The issue is caused because gluLookAt() is call with in a glBegin/glEnd sequence. This is not allowed. You've to call gluLookAt before glBegin.
Once drawing of primitives was started by glBegin it is only allowed to specify vertex coordinates (glVertex) and change attributes (e.g. glColor, glTexCoord ...), till the drawn is ended (glEnd).
All other instruction will be ignored and cause a GL_INVALID_OPERATION error (error code 1282).
Further note, that glLookAt doesn't set a the current matrix. It defines a matrix and multiplies the current matrix by the new matrix. Set the matrix mode (glMatrixMode) and set Identity matrix by glLoadIdentity before gluLookAt.
With the view matrix
gluLookAt(-300, 0, 0, 0, 0, 0, 0, 1, 0);
you want "see" anything, because with that matrix the line of sight is set along the x-axis and you look at the 2 dimensional polygon from the side.
Note, the polygon is a 2D object. The size of the object appears different if you look at it from the front, from the side (then it is a line and not visible) or from an direction in between. The first 3 parameters of gluLookAt define the point of view the next 3 parameters define the point you look at. The vector from the point of view to the point you look at is the line of sight.
Probably yo want look along the z-axis:
void displayFunction()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, -300, 0, 0, 0, 0, 1, 0);
glBegin(GL_POLYGON);
glColor3f(1, 1, 1);
glVertex3i(-150, 150, 0);
glVertex3i(150, 150, 0);
glVertex3i(150, -150, 0);
glVertex3i(-150, -150, 0);
glEnd();
glutSwapBuffers();
}
You use Orthographic (parallel) projection. If you would use Perspective projection, then the projected size of the object would decrease, when the distance to the point of view increases. Perspective projection can be set by gluPerspective. e.g.:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (double)windowWidth / windowHeight, 0.1, 600.0);
I have a Hand mesh which I want to animate.
I have the Skeleton which can be hierarchically animated.
My mesh is also weighted in Blender. So each vertex has 4 associated bones to be affected by.
When I apply the Animation of my Skeleton to the mesh, the hierarchy is applied correctly. (so the hierarchy of the mesh, matches the hierarchy of the Skeleton).
So far so good, now question:
the fingers look to be stretched (its like the fingers smashed by a heavy door). Why?
Note: (I didnt apply the bind pose bone Transformation Matrix explicitly, but I read about it and I believe its functionality is there, in the hierarchical Transformation I have for my Skeleton).
If you need more clarification of the steps, please ask.
vector<glm::mat4> Posture1Hand::HierarchyApplied(HandSkltn HNDSKs){
vector <glm::mat4> Matrices;
Matrices.resize(HNDSKs.GetLimbNum());
//non Hierarchical Matrices
for (unsigned int i = 0; i < Matrices.size(); i++){
Matrices[i] = newPose[i].getModelMatSkltn(HNDSKs.GetLimb(i).getLwCenter());
}
for (unsigned int i = 0; i < Matrices.size(); i++){
vector<Limb*>childeren = HNDSKs.GetLimb(i).getChildren();
for (unsigned int j = 0; j < childeren.size(); j++){
Matrices[childeren[j]->getId()] = Matrices[i] * Matrices[childeren[j]->getId()];
}
}
return Matrices;
}
Here is my getModelMatSkltn method.
inline glm::mat4 getModelMatSkltn(const glm::vec3& RotationCentre) const{//to apply the rotation on the whole heirarchy
glm::mat4 posMatrix = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
posMatrix = glm::translate(posMatrix, newPos);
glm::mat4 trMatrix = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
glm::mat4 OriginTranslate = glm::translate(trMatrix, -RotationCentre);
glm::mat4 InverseTranslate = glm::translate(trMatrix, RotationCentre);
glm::mat4 rotXMatrix = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
rotXMatrix = glm::rotate(rotXMatrix, glm::radians(newRot.x), glm::vec3(1, 0, 0));
glm::mat4 rotYMatrix = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
rotYMatrix = glm::rotate(rotYMatrix, glm::radians(newRot.y), glm::vec3(0, 1, 0));
glm::mat4 rotZMatrix = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
rotZMatrix = glm::rotate(rotZMatrix, glm::radians(newRot.z), glm::vec3(0, 0, 1));
glm::mat4 scaleMatric = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
scaleMatric = glm::scale(scaleMatric, newScale);
glm::mat4 rotMatrix = rotZMatrix*rotYMatrix*rotXMatrix;
rotMatrix = InverseTranslate*rotMatrix*OriginTranslate;
return posMatrix*rotMatrix*scaleMatric;
}
and this is how I send 20 transformation Matrix (because of 20 joints in Hand) to GPU:
void GLShader::Update(const vector trMat, const GLCamera& camera){
vector<glm::mat4> MVP; MVP.resize(trMat.size());
for (unsigned int i = 0; i < trMat.size(); i++){
MVP[i] = camera.getViewProjection()* trMat[i];
}
glUniformMatrix4fv(newUniform[TRANSFORM_U], trMat.size(), GL_FALSE, &MVP[0][0][0]);//4 floating value
}
I guess one should be familiar with calculation of vertex position in the shader in order to be able to answer the question, but I send a part of my vertex shader too.
attribute vec3 position;
attribute vec2 texCoord;
attribute vec4 weight;
attribute vec4 weightInd;
uniform mat4 transform[20];//vector of uniform for 20 number of joints in my skleton
void main(){
mat4 WMat;//weighted matrix
float w;
int Index;
for (int i=0; i<4; i++){
Index=int(weightInd[i]);
w=weight[i];
WMat += w*transform[Index];
}
gl_Position= WMat*vec4(position, 1.0);
}
I have been learning VBOs for a couple weeks now, and I have been told here that VBOs can render "~1 million vertices at several hundred fps". However, my current VBO test program only gets around 50 FPS with a little of 1 million vertices to render. Are there ways to optimize VBO efficiency? Or, more likely, am I doing something incorrectly? My test program is here:
EDIT: Improved code based on feedback.
#include <windows.h>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <glew.h>
#include <gl/gl.h>
#include <gl/glu.h>
using namespace std;
float cube_vertices[] = {-1, -1, 1,
1, -1, 1,
1, 1, 1,
-1, 1, 1,
-1, -1, -1,
-1, 1, -1,
1, 1, -1,
1, -1, -1,
-1, 1, -1,
-1, 1, 1,
1, 1, 1,
1, 1, -1,
-1, -1, -1,
1, -1, -1,
1, -1, 1,
-1, -1, 1,
1, -1, -1,
1, 1, -1,
1, 1, 1,
1, -1, 1,
-1, -1, -1,
-1, -1, 1,
-1, 1, 1,
-1, 1, -1};
float cube_normals[] = {0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0};
class Scene {
public:
void setup_projection( int w, int h ) {
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 50, (GLdouble)w/(GLdouble)h, 1, 5000.0 );
glMatrixMode( GL_MODELVIEW );
}
};
int main() {
///Number of models to render
int NumberOfCubes = 0;
cout << "Enter number of cubes to render: ";
cin >> NumberOfCubes;
system("cls");
///Create vectors for mesh data
//6 faces * 4 verts * x, y, z * number of cubes
std::vector<float> vertices; vertices.resize(6*4*3*NumberOfCubes);
std::vector<float> normals; normals.resize(6*4*3*NumberOfCubes);
for(int i = 0; i < NumberOfCubes; i++)
{
for(int j = 0; j < 6*4*3; j++)
{
vertices[(i*6*4*3) + j] = cube_vertices[j] + i;
normals[(i*6*4*3) + j] = cube_normals[j];
}
}
///Store size of the vectors
int SizeOfVertices = vertices.size() * sizeof(float);
int SizeOfNormals = normals.size() * sizeof(float);
///Window setup, lighting setup
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test");
Scene scene;
scene.setup_projection(window.getSize().x,window.getSize().y);
glewInit();
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHT0);
float XL = .5, YL = .1, ZL = 1;
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat lightpos[] = {XL, YL, ZL, 0.};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
///Generate the VBO
GLuint VBOID;
glGenBuffers(1, &VBOID);
glBindBuffer(GL_ARRAY_BUFFER, VBOID);
glBufferData(GL_ARRAY_BUFFER, SizeOfVertices + SizeOfNormals, 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, SizeOfVertices, &vertices[0]);
glBufferSubData(GL_ARRAY_BUFFER, SizeOfVertices, SizeOfNormals + SizeOfVertices, &normals[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
///FPS Stuff
sf::Clock FPS;
sf::Clock ShowFPS;
float fps;
///Start loop
cout << "Rendering " << NumberOfCubes * 8 << " vertices." << endl;
cout << "Using graphics card: " << glGetString(GL_RENDERER) << endl;
while( window.isOpen() ) {
sf::Event event;
while( window.pollEvent( event ) ) {
if( event.type == sf::Event::Closed )
window.close();
}
fps = FPS.getElapsedTime().asSeconds();
fps = 1 / fps;
FPS.restart();
if(ShowFPS.getElapsedTime().asSeconds() > 1)
{
cout << "FPS: " << fps << "\t FrameTime: " << 1000 / fps << endl;
ShowFPS.restart();
}
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
scene.setup_projection(window.getSize().x,window.getSize().y);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-25, -25, 150, 50, 50, 50, 0, 1, 0);
glBindBuffer(GL_ARRAY_BUFFER, VBOID);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColor3f(1, 0, 0);
glNormalPointer(GL_FLOAT, 0, 0);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_QUADS, 0, 6*4*NumberOfCubes);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
window.display();
}
return 1;
}
A few remarks on your code:
void Scene::resize( int w, int h ) {
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 50, (GLdouble)w/(GLdouble)h, 1, 5000.0 );
glMatrixMode( GL_MODELVIEW );
}
Please do understand that setting the viewport and the projection are not some sort "resizing" operation. They're part of the drawing process and hence should be treated like that. The good thing is, that you call this function with every drawing iteration. But I'd not call it resize. A better name was setup_projection or similar, to make it clear, what this function does, and not, what it does react upon. Always call a function by what it does!
This
cout << endl << endl << "Close program when finished." << endl;
bool ProgramRunning(true);
while(ProgramRunning == true) {}
probably does not work as you might expect. What you close is the console window/terminal; this makes your program to loose its standrd input and process leader thereby terminating it. None of the code after the while loop is going to be executed at all. You could install signal handlers that would set the – so far functionless – ProgrammRunning flag to false.
However the canonical way to deal with this is simply waiting for the user to pause until the user hits the enter key:
cout << "Program execution finished, hit the ENTER key to terminate" << endl;
cin.get();
Now about why you get only 50 FPS: The most likely reason is, that you got V-Sync enabled and your display has a refresh frequency of 50Hz. 50Hz is unusual, but not unheared of. Also likely is, that your display is running at 60Hz, but for some reason you're not making the refresh deadline for each retrace, effectively making your code miss in average every 6th frame.
Another reason may be, that you're not running on the GeForce, but maybe on chipset GPU of your laptop. If you have a Hybrid graphics system, make sure you got all the drivers properly installed and that you switch to the GeForce GPU before executing your program.
Print the output of glGetString(GL_RENDERER); to make sure. After opening the window, creating the OpenGL context add a
cout << glGetString(GL_RENDERER) << endl;
Are you using double buffering? Is yes, then you may have sync to vblank enabled in your drivers. This would mean, that EVERY OpengGL application using double buffering will render at most at the refresh rate of your monitor (usually around 50 - 60Hz).
You can try the same code with (significantly) smaller model to see if your FPS ever goes above this value.
After doing further research, I found out about VBO Indexing and was able to use that to get the several hundred FPS with a million vertices.
I have some code which draws a line along the x, y and z axes. My problem is that these lines are being clipped so that they are invisible near the origin:
This sounds like a far clipping plane issue, but I gave zFar=50 to gluPerspective, which should be plenty. Making it even larger doesn't seem to help. What else could be causing the clipping?
Here is my code:
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.*;
import org.lwjgl.util.glu.GLU;
public class Test {
static int width = 300, height = 200;
public static void main(String[] _) throws Exception {
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
glClear(GL_COLOR_BUFFER_BIT);
// projection matrix
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
GLU.gluPerspective(50, width / (float) height, .1f, 50);
// modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLU.gluLookAt(
.8f, .8f, .8f,
0, 0, 0,
0, 1, 0);
// draw a line for each axis
glBegin(GL_LINES);
// x axis in red
glColor3f(1, 0, 0);
glVertex3i(0, 0, 0);
glVertex3i(10, 0, 0);
// y axis in green
glColor3f(0, 1, 0);
glVertex3i(0, 0, 0);
glVertex3i(0, 10, 0);
// z axis in blue
glColor3f(0, 0, 1);
glVertex3i(0, 0, 0);
glVertex3i(0, 0, 10);
glEnd();
Display.update();
// wait for a close event
while (!Display.isCloseRequested()) {
Thread.sleep(20);
Display.processMessages();
}
Display.destroy();
}
}
Update - Removing glLoadIdentity(); after glMatrixMode(GL_MODELVIEW); gives the desired result, but I don't understand why. Isn't the default modelview matrix the identity matrix?
Update - I wrote a C version of the same code and it works as desired. Why the difference?
Indeed, after testing it, it turns out that glMatrixMode(GL_PROJECTION_MATRIX); should be glMatrixMode(GL_PROJECTION); instead.
So it seems that the modelview was active by default and glLoadIdentity() cleared the results of GLU.gluPerspective(50, width / (float) height, .1f, 50);
edit: Btw. in case you wonder what GL_PROJECTION_MATRIX is for, it's to retrieve the current matrix from the top of the matrix stack with glGetFloatv(GL_PROJECTION_MATRIX,output); or glGetDoublev(GL_PROJECTION_MATRIX,output);