Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Textures show up black

  1. #1
    Newbie Newbie
    Join Date
    Dec 2013
    Posts
    2

    Textures show up black

    Hi, I'm new to OpenGL, I have an project due for the 20th and have been racking my brains all night but cant shake why my textures show up black in my project, but they appear fine in the older program I wrote where I am using some of the code from now. Could any body help? What I am trying to achieve is loading more than one texture but I am having issues with just one at the moment.

    Here is the code for the current project:
    Code :
    /*
     * ALEX CLARKE
     * COMPUTER GRAPHICS ASSIGNMENT
     * Copyright {c} 2013 Alex Clarke. All rights reserved.
     */
     
    #include "stdafx.h"
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    #include <GL/glut.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <io.h>
     
    #define ImageHeight 512
    #define ImageWidth 512
    #define ImageColors 3
    GLubyte Image[ImageHeight][ImageWidth][ImageColors];
    static GLuint TextureIndex[2];
     
    //##############################
    //##########Variables###########
    //##############################
     
    void mouseMove(GLint x, GLint y);
    void keyboardDown(unsigned char key, int x, int y);
    void keyboardUp(unsigned char key, int x, int y);
     
    GLfloat cubeVertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
    {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, 
    {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};
     
    GLint sizeX = 30, sizeY = 30;
    GLint TOTAL_SCENE_CUBES = 1500;
    GLint TOTAL_CUBE_COUNT = 0;
    const GLint WINDOW_W = 600;
    const GLint WINDOW_H = 600;
    bool keys[256];
     
    //transformation variables
    GLint MOVE_SPEED = 9.0;
    GLfloat LAST_TIME;
    GLfloat CURRENT_TIME;
    GLfloat DELTA_TIME;
    GLint MOUSE_LAST_X;
    GLint MOUSE_LAST_Y;
    GLint MOUSE_CURRENT_X;
    GLint MOUSE_CURRENT_Y;
    GLint MOUSE_DELTA_X;
    GLint MOUSE_DELTA_Y;
     
    typedef struct Cube
    {
    	GLfloat *cube_position;
    	GLint size;
    	GLint tex_Index;
    	//GLfloat *color;
    }CUBE;
    CUBE *scene;
     
    typedef struct Camera_Translations
    {
    	GLfloat x;
    	GLfloat y;
    	GLfloat z;
    }CAMERA_TRANSLATIONS;
    CAMERA_TRANSLATIONS myCamPos;
    CAMERA_TRANSLATIONS myCamRot;
     
    //##############################
    //##########Methods#############
    //##############################
     
    void readPPMImage(char *name)
    {
    	int i, j, k= 0, id, count = 0;
    	char header[15];
    	GLbyte *im;
     
    	id = open(name, O_RDONLY);
     
    	if(id == -1)
    	{
    		printf("Cannot find file %s\n", name);
    		getchar();
    		exit(0);
    	}
     
    	read(id, header, 15);
    	printf("Header: &s\n",header);
     
    	im = (GLbyte*)malloc(3*512*512*sizeof(GLbyte));
     
    	if(im == NULL)
    	{
    		printf("Memory allocation problem\n");
    		exit(0);
    	}
     
    	read(id, &im[0], ImageColors*ImageHeight*ImageWidth);
     
    	for(int i = 511; i >= 0; i--)
    	{
    		for(int j = 0; j <= 511; j++)
    		{
    			Image[i][j][0] = im[count];
    			Image[i][j][1] = im[count + 1];
    			Image[i][j][2] = im[count + 2];
    			count = count + 3;
    		}
    	}
    	close(id);
    	free(im);
    }
     
    void Polygon(int a, int b, int c , int d, GLuint texIndex)
    {
    /* draw a polygon via list of vertices */
    	glShadeModel(GL_SMOOTH);
    	glPushMatrix();
    	glBindTexture(GL_TEXTURE_2D,texIndex);
     	glBegin(GL_POLYGON);
     
    		glTexCoord2d(0.0,0.0);	glVertex3fv(cubeVertices[a]);
    		glTexCoord2d(0.0,1.0);	glVertex3fv(cubeVertices[b]);
    		glTexCoord2d(1.0,1.0);	glVertex3fv(cubeVertices[c]);
    		glTexCoord2d(1.0,0.0);	glVertex3fv(cubeVertices[d]);
    	glEnd();
    	glPopMatrix();
    }
     
    void PolygonWireframe(int a, int b, int c, int d)
    {
    	/* draw a polygon via list of vertices */
    	//glShadeModel(GL_SMOOTH);
    	glPushMatrix();
    	glBegin(GL_LINES);
    	glLineWidth(4.0);
    		glColor3f(0.0,0.0,0.0);
    		glVertex3fv(cubeVertices[a]);
    		glVertex3fv(cubeVertices[b]);
    		glVertex3fv(cubeVertices[c]);
    		glVertex3fv(cubeVertices[d]);
    	glEnd();
    	glPopMatrix();
    }
     
    void DrawCube(GLint texIndex)
    {
    /* map vertices to faces */
    	Polygon(0,3,2,1, texIndex);
    	Polygon(2,3,7,6, texIndex);
    	Polygon(0,4,7,3, texIndex);
    	Polygon(1,2,6,5, texIndex);
    	Polygon(4,5,6,7, texIndex);
    	Polygon(0,1,5,4, texIndex);
     
    	PolygonWireframe(0,3,2,1);
    	PolygonWireframe(2,3,7,6);
    	PolygonWireframe(0,4,7,3);
    	PolygonWireframe(1,2,6,5);
    	PolygonWireframe(4,5,6,7);
    	PolygonWireframe(0,1,5,4);
    }
     
    void vox_Draw_Terrain(GLint sizeXIn, GLint sizeYIn)
    {
        int cubeNo = 0;
    	for (int x=0; x<sizeXIn; x++)
    	{
    		for(int y=0; y<sizeYIn; y++)
    		{
    			glPushMatrix();
    			glTranslatef(scene [ cubeNo ].cube_position[0], scene [ cubeNo ].cube_position[1], scene [ cubeNo ].cube_position[2]);
    			glScalef( scene [ cubeNo ].size,  scene [ cubeNo ].size, scene [ cubeNo ].size);
    			DrawCube(scene [cubeNo].tex_Index);
    			cubeNo = cubeNo +1;
    			glPopMatrix();
    		}
    	}
    }
     
    void vox_Build_Terrain(void)
    {
    	scene = (CUBE *)malloc(sizeof(CUBE) * TOTAL_SCENE_CUBES);
     
    	for (int x=0; x<sizeX; x++)
    	{
    		for(int y=0; y<sizeY; y++)
    		{
    			scene [ TOTAL_CUBE_COUNT ].cube_position = (GLfloat*)malloc(sizeof(GLfloat) * 3);
    			scene [ TOTAL_CUBE_COUNT ].cube_position[0] = x*2;
    			scene [ TOTAL_CUBE_COUNT ].cube_position[1] = y*2;
    			scene [ TOTAL_CUBE_COUNT ].cube_position[2] = -100;
    			scene [ TOTAL_CUBE_COUNT ].size = 1;
    			scene [ TOTAL_CUBE_COUNT ].tex_Index = 0;
    			TOTAL_CUBE_COUNT = TOTAL_CUBE_COUNT + 1;
    		}
    	}
    }
     
    void BindTextures(GLuint TextureIndex)
    {
    	glBindTexture(GL_TEXTURE_2D, TextureIndex);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ImageWidth,ImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,Image);
    }
     
    void inputHandle()
    {
    	CURRENT_TIME = ((float)glutGet(GLUT_ELAPSED_TIME))/1000;
    	DELTA_TIME = CURRENT_TIME - LAST_TIME;
    	LAST_TIME = CURRENT_TIME;
     
     
    	if(keys['x'])
    		myCamRot.x += MOVE_SPEED*DELTA_TIME;
    	if(keys['y'])
    		myCamRot.y += MOVE_SPEED*DELTA_TIME;
    	if(keys['z'])
    		myCamRot.z += MOVE_SPEED*DELTA_TIME;
    	if(keys[GLUT_LEFT_BUTTON])
    		if(MOVE_SPEED > 0)
    			MOVE_SPEED = MOVE_SPEED - 1;
    	if(keys[GLUT_RIGHT_BUTTON])
    		if(MOVE_SPEED <= 50)
    			MOVE_SPEED = MOVE_SPEED + 1;
     
     
    	MOUSE_DELTA_X = MOUSE_CURRENT_X - MOUSE_LAST_X;
    	MOUSE_DELTA_Y = MOUSE_CURRENT_Y - MOUSE_LAST_Y;
     
    	glutPostRedisplay();
    }
    void keyboardDown(unsigned char key, int x, int y)
    {
    	keys[key] = true;
    }
    void keyboardUp(unsigned char key, int x, int y)
    {
    	keys[key] = false;
    }
    void mouseMove(GLint x, GLint y)
    {
    	MOUSE_CURRENT_X = x;
    	MOUSE_CURRENT_Y = y;
    }
     
    void display(void)
    {
    	//Set screen & misc
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glClearColor(0.0,0.0,200.0,0.0);
    	inputHandle();
        glLoadIdentity();
     
    	//Position Camera
    	glTranslatef(myCamPos.x,myCamPos.y,myCamPos.z);
    	glRotatef(myCamRot.x,1,0,0);
    	glRotatef(myCamRot.y,0,1,0);
    	glRotatef(myCamRot.z,0,0,1);
     
    	//Render 
    	vox_Draw_Terrain(sizeX,sizeY);
     
    	glFlush();
        glutSwapBuffers();
    }
     
    void vox_init(void)
    {
        glMatrixMode(GL_PROJECTION);
    	glClearColor(0.0,0.0,0.0,0.0);
    	glEnable(GL_DEPTH_TEST);
     
    	//set camera position/rotation
    	myCamRot.x = 0; myCamRot.y = 0; myCamRot.z = 0;
    	myCamPos.x = -30; myCamPos.y = -30; myCamPos.z = 0;
     
     
    	readPPMImage("I:\\texGround.ppm");
    	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    	glLoadIdentity();
     
    	glMatrixMode(GL_MODELVIEW);
    	glGenTextures(2, TextureIndex);
    	glBindTexture(GL_TEXTURE_2D, TextureIndex[0]);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
    					GL_NEAREST);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
    					GL_NEAREST);
    	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ImageWidth,
    				ImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
    				Image);
     
    	glEnable(GL_TEXTURE_2D);
     
    	vox_Build_Terrain();
    }
     
    void reshape(int w, int h)
    {
    	if(h == 0)
    		h = 1;
     
    	float ratio = (float)w/h;
    	glViewport(0,0,w,h);
     
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective(45,ratio,0.01,2500);
    	glMatrixMode(GL_MODELVIEW);
    }
     
    void main(int argc, char **argv)
    {
    	//Initialize GLUT & OpenGL
        glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    	glutInitWindowPosition(100,100);
    	glutInitWindowSize(WINDOW_W, WINDOW_H);
     
    	//Create Window
    	glutCreateWindow("Assignment");
     
    	//Initialisation & GLUT callback functions
    	vox_init();
    	glutReshapeFunc(reshape);
        glutDisplayFunc(display);
    	//glutIdleFunc(idle);
    	glutKeyboardFunc(keyboardDown);
    	glutKeyboardUpFunc(keyboardUp);
    	glutMotionFunc(mouseMove);
    	glutPassiveMotionFunc(mouseMove);
     
    	//Enter Main Loop
    	glEnable(GL_DEPTH_TEST);
        glutMainLoop();
    	free(scene);
    }

  2. #2
    Intern Contributor uwi2k2's Avatar
    Join Date
    Mar 2011
    Posts
    98
    hi,

    when you create the texture you bind
    Code :
    glBindTexture(GL_TEXTURE_2D, TextureIndex[0]);
    but everytime you draw you just bind:
    Code :
    glBindTexture(GL_TEXTURE_2D,texIndex);
    as you are passing something to the funcion.

    as far as i see it, try:
    Code :
    Polygon(0,3,2,1, TextureIndex[0]);

    i am sure you are just not binding the propper texture id.

    cu
    uwi
    uwi2k2 - OpenGL Trainer: www.opengl-trainer.com
    ---------------------------------------------------------
    Free Programming Helpline: www.programming-hotline.com
    ---------------------------------------------------------
    A Friday Night Programmer: www.uwiworld.net

  3. #3
    Newbie Newbie
    Join Date
    Dec 2013
    Posts
    2
    Thanks for the help, your answer was correct and I have fixed it now, the reason it was black was something to do with where I was drawing the wireframe, it didn't like it so I removed it for now, after that my textures were white and after passing in the right parameter it works now. Thank you (:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •