PDA

View Full Version : Vertice Order matters for Skybox?



ParagonArcade
12-13-2014, 03:37 AM
I've looked through the internet and the forums on cubemaps. Didn't find anything that helped. I am getting no output. All black I guess. When I switch the frag_color output to vec4(1.0f), nothing happens still. So that leads me to believe that the vertices are not working proper, wrong order maybe.... what am I doing wrong? Trying to use my initial mesh class which works fine. I used this as a reference: http://antongerdelan.net/opengl/cubemaps.html




CubeMap.h

#ifndef CUBEMAP_H
#define CUBEMAP_H

#include <iostream>

#include "Helper.h"

//Forward Declarations
class Mesh;
class Texture;

class CubeMap
{
private:
float size;
GLuint cube;
Mesh* mesh;

void Generator();

void loadCubeFace(GLenum face, char* filename);
public:
CubeMap();
CubeMap(char* front, char* back, char* right, char* left, char* top, char* bottom);

void Render();
};

#endif

...

CubeMap.cpp

#include "CubeMap.h"

#include "Mesh.h"
#include "Texture.h"

void CubeMap::Generator()
{
glActiveTexture(GL_TEXTURE0);

glGenTextures(1, &cube);
}

CubeMap::CubeMap() :
size(0),
cube(0),
mesh(NULL)
{
}
CubeMap::CubeMap(char* front, char* back, char* right, char* left, char* top, char* bottom) :
cube(0),
size(1.0f),
mesh(new Mesh())
{
//. Generate Main Texture
Generator();

//. Generate Mesh
generateCubeMap(1.0f, mesh);

//.
cube = SOIL_load_OGL_cubemap(right, left, top, bottom, back, front, SOIL_LOAD_RGB, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);

//. Texture Faces
glBindTexture(GL_TEXTURE_CUBE_MAP, cube);

//. Format Cube
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

//. Unbind Texture
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}

void CubeMap::Render()
{
glActiveTexture(GL_TEXTURE0);

glBindTexture(GL_TEXTURE_CUBE_MAP, cube);

mesh->Render();

glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}



Shader compiles with no errors.



#version 440

layout (location = 0) in vec3 position;

//.

uniform mat4 projection;

//.

out vec3 vPosition;

//.

void main()
{
vPosition = position;

gl_Position = (projection * vec4(position, 1.0f));
}





#version 440

in vec3 vPosition;

//.

uniform samplerCube sampler;

//.

out vec4 frag_color;

//.

void main()
{
vec4 result = texture(sampler, vPosition);

//.

frag_color = result;
}

ParagonArcade
12-13-2014, 07:01 AM
I've been looking at this more and I copied my PhongShader to the CubeShader and bound a 2D texture. All the faces are drawing correctly around the origin. Its a box. So the vertices/indices are fine. I've noticed something really strange, when I use glUseProgram(0) after I draw something to the screen, to unbind the shader after I'm done and ready for new shader, it turns my screen black. Is this suppose to happen... what is going on?



EDIT: I figured out the root cause. If you screen is clearing when you are calling different shaders, the uniforms are not been bound properly. It works for me when I had one. But with additional shader steps, you have to be certain that the shader is bound WHEN you update the uniforms. See: http://stackoverflow.com/questions/24706464/how-to-use-more-than-one-shader-program

ParagonArcade
12-13-2014, 08:13 PM
So I got my cube map to finally start working. I am loading in the 6 textures files into a GL_TEXTURE_CUBE_MAP using SOIL. No problems so far. I sent the projection matrix as a matrix4f uniform without the camera translation included. Getting some weird behavior if I use the follow shader code:



#version 440

in vec3 vPosition;
in vec3 vColor;

uniform mat4 projection;
uniform samplerCube sampler;

out vec4 frag_color;

void main()
{
vec4 tColor = texture(sampler, vPosition);

frag_color = tColor;
}


1545

It looks okay from that static view. But the cube map is never rotating with the scene. Its static. I am always looking at the same face of the cube. Notice the "bend" in the top face, this move with rotation of the camera/world. See below. If I change the shader code to this:



#version 440

in vec3 vPosition;
in vec3 vColor;

uniform mat4 projection;

uniform samplerCube sampler;

//.

out vec4 frag_color;

//.

void main()
{

vec4 tColor = texture(sampler, vPosition);

vec4 result = vec4(vColor, 1.0f) * tColor;

frag_color = result;
}


With the vertex color being either red, green, blue depend on the face normal, then I get this. So I have two cubes???

Rotating around Y, looking at corner?

1546

Rotating around X, looking up?

1547