Hi,
im studying from the superbible 6th edition, and i want to make a camera to my 3D “game” but, I dont know how. The source is:
#include <sb6.h>
#include <vmath.h>
#include <time.h>
#include <stdio.h>
#include <object.h>
using namespace vmath;
// Remove this to draw only a single cube!
#define MANY_CUBES
/*float r;
float p;*/
class singlepoint_app : public sb6::application
{
void init()
{
static const char title[] = "OpenGL SuperBible - Spinny Cube";
sb6::application::init();
memcpy(info.title, title, sizeof(title));
}
virtual void startup()
{
static const char * vs_source[] =
{
"#version 410 core
"
"
"
"in vec4 position;
"
"
"
"out VS_OUT
"
"{
"
" vec4 color;
"
"} vs_out;
"
"
"
"uniform mat4 mv_matrix;
"
"uniform mat4 proj_matrix;
"
"
"
"void main(void)
"
"{
"
" gl_Position = proj_matrix * mv_matrix * position;
"
" vs_out.color = position * 2.0 + vec4(0.5, 0.5, 0.5, 0.0);
"
"}
"
};
static const char * fs_source[] =
{
"#version 410 core
"
"
"
"out vec4 color;
"
"
"
"in VS_OUT
"
"{
"
" vec4 color;
"
"} fs_in;
"
"
"
"void main(void)
"
"{
"
" color = fs_in.color;
"
"}
"
};
program = glCreateProgram();
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, fs_source, NULL);
glCompileShader(fs);
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, vs_source, NULL);
glCompileShader(vs);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
mv_location = glGetUniformLocation(program, "mv_matrix");
proj_location = glGetUniformLocation(program, "proj_matrix");
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
static const GLfloat data[] =
{
-0.25f, 0.25f, -0.25f,
-0.25f, -0.25f, -0.25f,
0.25f, -0.25f, -0.25f,
0.25f, -0.25f, -0.25f,
0.25f, 0.25f, -0.25f,
-0.25f, 0.25f, -0.25f,
0.25f, -0.25f, -0.25f,
0.25f, -0.25f, 0.25f,
0.25f, 0.25f, -0.25f,
0.25f, -0.25f, 0.25f,
0.25f, 0.25f, 0.25f,
0.25f, 0.25f, -0.25f,
0.25f, -0.25f, 0.25f,
-0.25f, -0.25f, 0.25f,
0.25f, 0.25f, 0.25f,
-0.25f, -0.25f, 0.25f,
-0.25f, 0.25f, 0.25f,
0.25f, 0.25f, 0.25f,
-0.25f, -0.25f, 0.25f,
-0.25f, -0.25f, -0.25f,
-0.25f, 0.25f, 0.25f,
-0.25f, -0.25f, -0.25f,
-0.25f, 0.25f, -0.25f,
-0.25f, 0.25f, 0.25f,
-0.25f, -0.25f, 0.25f,
0.25f, -0.25f, 0.25f,
0.25f, -0.25f, -0.25f,
0.25f, -0.25f, -0.25f,
-0.25f, -0.25f, -0.25f,
-0.25f, -0.25f, 0.25f,
-0.25f, 0.25f, -0.25f,
0.25f, 0.25f, -0.25f,
0.25f, 0.25f, 0.25f,
0.25f, 0.25f, 0.25f,
-0.25f, 0.25f, 0.25f,
-0.25f, 0.25f, -0.25f
};
glGenBuffers(1, &buffer[0]);
glBindBuffer(GL_ARRAY_BUFFER, buffer[0]);
glBufferData(GL_ARRAY_BUFFER,
sizeof(data),
data,
GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
virtual void render(double currentTime)
{
static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
static const GLfloat one = 1.0f;
glViewport(0, 0, info.windowWidth, info.windowHeight);
glClearBufferfv(GL_COLOR, 0, green);
glClearBufferfv(GL_DEPTH, 0, &one);
glUseProgram(program);
glUniformMatrix4fv(proj_location, 1, GL_FALSE, proj_matrix);
#ifdef MANY_CUBES
/*if (GetAsyncKeyState('W'))
{
Sleep (2);
p += 0.01f;
r += 0.01f;
}
else if (GetAsyncKeyState('S'))
{
Sleep (2);
p -= 0.01f;
r -= 0.01f;
}*/
int i;
for (i = 0; i < 24; i++)
{
float f = (float)i + (float)currentTime * 0.3f;
vmath::mat4 mv_matrix =
vmath::translate(0.0f, 0.0f, -6.0f) *
vmath::rotate( 10.0f, 0.0f, 0.0f /*p*/, 0.0f) *
vmath::rotate( 10.0f, 0.0f /*r*/, 0.0f, 0.0f) *
vmath::translate(sinf(0.1f * f) * 2.0f,
cosf(0.1f * f) * 2.0f,
sinf(0.1f * f) * cosf(1.5f * f) * 2.0f);
glUniformMatrix4fv(mv_location, 1, GL_FALSE, mv_matrix);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
#else
float f = (float)currentTime * 0.3f;
vmath::mat4 mv_matrix = vmath::translate(0.0f, 0.0f, -4.0f) *
vmath::translate(sinf(2.1f * f) * 0.5f,
cosf(1.7f * f) * 0.5f,
sinf(1.3f * f) * cosf(1.5f * f) * 2.0f) *
vmath::rotate((float)currentTime * 45.0f, 0.0f, 1.0f, 0.0f) *
vmath::rotate((float)currentTime * 81.0f, 1.0f, 0.0f, 0.0f);
glUniformMatrix4fv(mv_location, 1, GL_FALSE, mv_matrix);
glDrawArrays(GL_TRIANGLES, 0, 36);
#endif
}
virtual void shutdown()
{
glDeleteVertexArrays(1, &vao);
glDeleteProgram(program);
glDeleteBuffers(1, &buffer[0]);
}
void onResize(int w, int h)
{
sb6::application::onResize(w, h);
aspect = (float)w / (float)h;
proj_matrix = vmath::perspective(60.0f, aspect, 0.1f, 2000.0f);
}
private:
GLuint program;
GLuint vao;
GLuint buffer[2];
GLint mv_location;
GLint proj_location;
float aspect;
vmath::mat4 proj_matrix;
};
DECLARE_MAIN(singlepoint_app)
So, iam using the SB6 header file, and vmath, thats the sb6 tool, too. So, Iam thinking about a camera like in a mmorpg games, like wow, savage, fps games… I think, the my way is not good. I have there that my view matrix will view 24 cubes, thats the first problem, that all of them must be rendered alone. i want to make(if it is possible) something like, a world, and the objects will be there, and if i rotate my camera to a any direction, it will render that objects, which are in that direction. Can anybody help me, pls? It is my dream, to make a 3D world, and the first thing what i need, is a camera.