Skip to main content
GameDev.net gamedev.net
🔒 Locked

Sky box texture problem

Started by OsmanOzdemir Dec 9, 2011 at 9:29 PM 1 replies 1.4k views
Original Post
OsmanOzdemir
OsmanOzdemir
Hi all! I am new here and this is my first post.
I have a sky box class in my c++ / opengl game. It renders the sky box but there is no texture. It is just black. I couldn't figure out what the problem is.
Here is my code:
skybox.h
[source lang="cpp"]

#ifndef BOGLGP_SKYBOX_H
#define BOGLGP_SKYBOX_H

#include
#include
#include "geom.h"
#include "targa.h"
#include "glee/GLee.h"

using std::string;

enum SKYBOX_SIDES {
SS_NORTH = 0,
SS_EAST,
SS_SOUTH,
SS_WEST,
SS_TOP,
SS_BOTTOM,
SS_MAX_SIDES
};

class GLSLProgram;

class Skybox
{
public:
Skybox(const string& vertexShader, const string& fragShader);
~Skybox();
bool initialize();
void render(float x, float y, float z);
private:
GLSLProgram* m_basicProgram;
GLSLProgram* m_cubeProgram;

TargaImage m_images[SS_MAX_SIDES];
GLuint m_texID[SS_MAX_SIDES];
string m_texturePath[SS_MAX_SIDES];

GLuint m_vertexBuffers[6];
GLuint m_texCoordBuffers[6];
GLuint indexBuffers[6];
GLuint m_colorBuffer;

std::vector m_vertices[6];
std::vector m_texCoords[6];
std::vector m_indices[6];
std::vector m_colors;
};

#endif
[/source]


skyboc.cpp
[source lang="cpp"]


#include "skybox.h"
#include "glslshader.h"
#include "geom.h"

#include

const std::string SKYBOXBACK = "data/textures/skybox/back2moon.tga";
const std::string SKYBOXRIGHT = "data/textures/skybox/right2moon.tga";
const std::string SKYBOXFRONT = "data/textures/skybox/front2moon.tga";
const std::string SKYBOXLEFT = "data/textures/skybox/left2moon.tga";
const std::string SKYBOXUP = "data/textures/skybox/top2moon.tga";
const std::string SKYBOXDOWN = "data/textures/skybox/bot2moon.tga";

Skybox::Skybox(const string& vertexShader, const string& fragShader)
{
m_basicProgram = new GLSLProgram(vertexShader, fragShader);
}

Skybox::~Skybox()
{
delete m_basicProgram;
}

bool Skybox::initialize()
{
for (int side = 0; side < SS_MAX_SIDES; side++)
{
if(side == 0)
{
if(!m_images[side].load("data/textures/back.tga"))
{
return false;
}
}
if(side == 1)
{
if(!m_images[side].load("data/textures/right.tga"))
{
return false;
}
}
if(side == 2)
{
if(!m_images[side].load("data/textures/front.tga"))
{
return false;
}
}
if(side == 3)
{
if(!m_images[side].load("data/textures/left.tga"))
{
return false;
}
}
if(side == 4)
{
if(!m_images[side].load("data/textures/top.tga"))
{
return false;
}
}
if(side == 5)
{
if(!m_images[side].load("data/textures/bottom.tga"))
{
return false;
}
}
glGenTextures(1, &m_texID[side]);
glActiveTexture(GL_TEXTURE0 + side);
glBindTexture(GL_TEXTURE_2D, m_texID[side]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, m_images[side].getWidth(),
m_images[side].getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,
m_images[side].getImageData());
}

const float SIZE = 32.0;

//Back
m_vertices[0].push_back(Vertex(-SIZE,-SIZE,-SIZE));
m_vertices[0].push_back(Vertex( SIZE,-SIZE,-SIZE));
m_vertices[0].push_back(Vertex( SIZE, SIZE,-SIZE));
m_vertices[0].push_back(Vertex(-SIZE, SIZE,-SIZE));

m_texCoords[0].push_back(TexCoord(0.0f, 0.0f));
m_texCoords[0].push_back(TexCoord(1.0f, 0.0f));
m_texCoords[0].push_back(TexCoord(1.0f, 1.0f));
m_texCoords[0].push_back(TexCoord(0.0f, 1.0f));

m_indices[0].push_back(0);
m_indices[0].push_back(1);
m_indices[0].push_back(2);

m_indices[0].push_back(0);
m_indices[0].push_back(2);
m_indices[0].push_back(3);


//Left
m_vertices[1].push_back(Vertex(-SIZE,-SIZE,-SIZE));
m_vertices[1].push_back(Vertex(-SIZE, SIZE,-SIZE));
m_vertices[1].push_back(Vertex(-SIZE, SIZE, SIZE));
m_vertices[1].push_back(Vertex(-SIZE,-SIZE, SIZE));

m_texCoords[1].push_back(TexCoord(1.0f, 0.0f));
m_texCoords[1].push_back(TexCoord(1.0f, 1.0f));
m_texCoords[1].push_back(TexCoord(0.0f, 1.0f));
m_texCoords[1].push_back(TexCoord(0.0f, 0.0f));

m_indices[1].push_back(3);
m_indices[1].push_back(0);
m_indices[1].push_back(1);

m_indices[1].push_back(3);
m_indices[1].push_back(1);
m_indices[1].push_back(2);

//Front
m_vertices[2].push_back(Vertex( SIZE,-SIZE, SIZE));
m_vertices[2].push_back(Vertex(-SIZE,-SIZE, SIZE));
m_vertices[2].push_back(Vertex(-SIZE, SIZE, SIZE));
m_vertices[2].push_back(Vertex( SIZE, SIZE, SIZE));

m_texCoords[2].push_back(TexCoord(0.0f, 0.0f));
m_texCoords[2].push_back(TexCoord(1.0f, 0.0f));
m_texCoords[2].push_back(TexCoord(1.0f, 1.0f));
m_texCoords[2].push_back(TexCoord(0.0f, 1.0f));

m_indices[2].push_back(0);
m_indices[2].push_back(1);
m_indices[2].push_back(2);

m_indices[2].push_back(0);
m_indices[2].push_back(2);
m_indices[2].push_back(3);

//Right
m_vertices[3].push_back(Vertex( SIZE,-SIZE, SIZE));
m_vertices[3].push_back(Vertex( SIZE, SIZE, SIZE));
m_vertices[3].push_back(Vertex( SIZE, SIZE,-SIZE));
m_vertices[3].push_back(Vertex( SIZE,-SIZE,-SIZE));

m_texCoords[3].push_back(TexCoord(1.0f, 0.0f));
m_texCoords[3].push_back(TexCoord(1.0f, 1.0f));
m_texCoords[3].push_back(TexCoord(0.0f, 1.0f));
m_texCoords[3].push_back(TexCoord(0.0f, 0.0f));

m_indices[3].push_back(0);
m_indices[3].push_back(2);
m_indices[3].push_back(3);

m_indices[3].push_back(0);
m_indices[3].push_back(1);
m_indices[3].push_back(2);

//Top
m_vertices[4].push_back(Vertex(-SIZE, SIZE, -SIZE));
m_vertices[4].push_back(Vertex( SIZE, SIZE, -SIZE));
m_vertices[4].push_back(Vertex( SIZE, SIZE, SIZE));
m_vertices[4].push_back(Vertex(-SIZE, SIZE, SIZE));

m_texCoords[4].push_back(TexCoord(0.0f, 0.0f));
m_texCoords[4].push_back(TexCoord(1.0f, 0.0f));
m_texCoords[4].push_back(TexCoord(1.0f, 1.0f));
m_texCoords[4].push_back(TexCoord(0.0f, 1.0f));

m_indices[4].push_back(3);
m_indices[4].push_back(1);
m_indices[4].push_back(2);

m_indices[4].push_back(3);
m_indices[4].push_back(0);
m_indices[4].push_back(1);

//Bottom
m_vertices[5].push_back(Vertex( SIZE,-SIZE, -SIZE));
m_vertices[5].push_back(Vertex(-SIZE,-SIZE, -SIZE));
m_vertices[5].push_back(Vertex(-SIZE,-SIZE, SIZE));
m_vertices[5].push_back(Vertex( SIZE,-SIZE, SIZE));

m_texCoords[5].push_back(TexCoord(0.0f, 0.0f));
m_texCoords[5].push_back(TexCoord(1.0f, 0.0f));
m_texCoords[5].push_back(TexCoord(1.0f, 1.0f));
m_texCoords[5].push_back(TexCoord(0.0f, 1.0f));

m_indices[5].push_back(3);
m_indices[5].push_back(1);
m_indices[5].push_back(0);

m_indices[5].push_back(3);
m_indices[5].push_back(2);
m_indices[5].push_back(1);

for(int i=0; i<4; i++)
m_colors.push_back(Color(256.0f, 256.0f, 256.0f, 1.0f));


if(!m_basicProgram->initialize())
{
std::cerr << "Could not initialize the tree shaders" << std::endl;
return false;
}


m_basicProgram->bindAttrib(0, "a_Vertex");
m_basicProgram->bindAttrib(1, "a_Color");
m_basicProgram->bindAttrib(2, "a_TexCoord");
m_basicProgram->linkProgram();
m_basicProgram->bindShader();

m_basicProgram->sendUniform("texture0", 0);

glGenBuffers(1, &m_colorBuffer); //Generate a buffer for the colors
glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer); //Bind the color buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(Color) * m_colors.size() , &m_colors[0], GL_STATIC_DRAW); //Send the data to OpenGL


for(int i=0; i<6; i++)
{
glGenBuffers(1, &m_vertexBuffers); //Generate a buffer for the vertices
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffers); //Bind the vertex buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * m_vertices.size() * 3, &m_vertices[0], GL_STATIC_DRAW); //Send the data to OpenGL

glGenBuffers(1, &m_texCoordBuffers);
glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffers); //Bind the tex coord buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * m_texCoords.size() * 2, &m_texCoords[0], GL_STATIC_DRAW); //Send the data to OpenGL

glGenBuffers(1, &indexBuffers);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffers); //Bind the vertex buffer
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * m_indices.size(), &m_indices[0], GL_STATIC_DRAW); //Send the data to OpenGL
}
return true;
}

void Skybox::render(float x, float y, float z)
{

glPushMatrix();
glTranslatef(x, y, z);

static float modelviewMatrix[16];
static float projectionMatrix[16];

glGetFloatv(GL_MODELVIEW_MATRIX, modelviewMatrix);
glGetFloatv(GL_PROJECTION_MATRIX, projectionMatrix);
m_basicProgram->bindShader();
m_basicProgram->sendUniform4x4("modelview_matrix", modelviewMatrix);
m_basicProgram->sendUniform4x4("projection_matrix", projectionMatrix);

glClear( GL_DEPTH_BUFFER_BIT );
glDisable(GL_DEPTH_TEST);
//glEnable(GL_CULL_FACE);
//glDisable (GL_LIGHTING);
glDepthMask(GL_FALSE);

//glEnableVertexAttribArray(0);
//glEnableVertexAttribArray(1);

for(int i=0; i<6; i++)
{
glBindTexture(GL_TEXTURE_2D, m_texID);
glActiveTexture(GL_TEXTURE0 + i);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);

glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffers);
glVertexAttribPointer((GLint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
glVertexAttribPointer((GLint)1, 4, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffers);
glVertexAttribPointer((GLint)2, 2, GL_FLOAT, GL_FALSE, 0, 0);


glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffers);
glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, 0);

glDisableVertexAttribArray(2);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
}
//glDisableVertexAttribArray(1);
//glDisableVertexAttribArray(0);

glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
//glEnable(GL_LIGHTING);
glPopMatrix();

}
[/source]


vertex shader:


#version 130

uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;

in vec3 a_Vertex;
in vec4 a_Color;
in vec2 a_TexCoord0;

out vec4 color;
out vec2 texCoord0;

void main(void)
{
texCoord0 = a_TexCoord0;
color = a_Color;
vec4 pos = modelview_matrix * vec4(a_Vertex, 1.0);
gl_Position = projection_matrix * pos;
}




fragment shader:



#version 130

precision highp float;


uniform sampler2D texture0;

in vec4 color;
in vec2 texCoord0;

out vec4 outColor;

void main(void) {
outColor = color * texture(texture0, texCoord0.st);
}




Thx!
tariban
tariban


for(int i=0; i<4; i++)
m_colors.push_back(Color(256.0f, 256.0f, 256.0f, 1.0f));

[/quote]

Should 256.0f be 1.0f? I haven't really done all that much using colour buffers but I know the final value of the components outputted by a GLSL shader should be in the 0.0f - 1.0f range... though in theory this would make it white as opposed to black, so the problem may lie somewhere else.
Kaptein
Kaptein
[color=#000000]outColor [color=#666600]=[color=#000000] color [color=#666600]*[color=#000000] texture[color=#666600]([color=#000000]texture0[color=#666600],[color=#000000] texCoord0[color=#666600].[color=#000000]st[color=#666600]);[color=#000000]

you are reading from a 1D texture if im not mistaken

for 2D textures, its texture2D
for cubemaps its probably textureCube(samplerCube, vec3 coord, [bias])

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.