• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
dreamslayerx

OpenGL
Texture Colors Not exact as Texture

18 posts in this topic

Hi guys. I hope you can help. I am on using Windows 7. For some reason when I use SDL to load a bitmap image, then I display it on a cube the image isn't the same as the texture image. The coloring is off basically. Here is how it looks; its the Color cube attachment. The actual texture is the bricks image. Here is my code below. Can anyone tell me why the texture color isn't being displayed properly?

[code]
[size="2"][color="#008000"][size="2"][color="#008000"]/*Applying Textures to Surfaces: This Program Works Great

Good example video http://www.youtube.com/watch?v=2BsTOgABU1k



Program works, but coloring seems to be off a little. Look into that.

The linux computer at school contains SDL libraries, using -lSDL */

/* Rotating Cube with Textures and Mouse click interactions

Rendered images are stored in OpenGL pictures in the UAB School Folder

This program is a intro to texturing. It use a 3D cube and textures it's sureface with colors defined in the color array RGB.

This program also introcudes a simple way to interact with cube using the mouse buttons.*/

//****Important Note put SDL.dll file in operating directory and link libraries

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]#include[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#a31515"][size="2"][color="#a31515"]<stdlib.h>

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]#include[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#a31515"][size="2"][color="#a31515"]<iostream>

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]#include[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#a31515"][size="2"][color="#a31515"]<GL/glut.h>

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]#include[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#a31515"][size="2"][color="#a31515"]<SDL/SDL.h>

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]#include[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#a31515"][size="2"][color="#a31515"]<SDL_opengl.h>

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]using[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]namespace[/color][/size][/color][/size][size="2"] std;



[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Prototypes

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]unsigned[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] loadTexture([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]const[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]char[/color][/size][/color][/size][size="2"]* filename); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//I had to put this in since the initRendering function was using loadTexture function before it was declared

//note: notice that the range is from -1 to 1, and colors are from 0 to 1, while textures are from 0 to 1

[/color][/size][/color][/size][size="2"]GLfloat vertices[][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}};

GLfloat colors[][3] = {{1.0,0.0,0.0},{0.0,1.0,1.0},

{1.0,1.0,0.0},{0.0,1.0,0.0},

{0.0,0.0,1.0},{1.0,0.0,1.0}};

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]unsigned[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] tex; [/size][size="2"][color="#008000"][size="2"][color="#008000"]//global variable to hold texture

//Added initialization function

//Initializes 3D rendering

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] initRendering() { [/size][size="2"][color="#008000"][size="2"][color="#008000"]//in this function I may need to add in the

[/color][/size][/color][/size][size="2"]glEnable(GL_DEPTH_TEST);

glEnable(GL_COLOR_MATERIAL);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//glEnable(GL_LIGHTING); //enable lighting

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]//glEnable(GL_LIGHT0); //enable light #0

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]//glEnable(GL_LIGHT1); //enable light #1

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]//glEnable(GL_NORMALIZE); //Automatically normalize normals

[/color][/size][/color][/size][size="2"]glShadeModel(GL_SMOOTH); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//enable smooth shading

[/color][/size][/color][/size][size="2"]glEnable(GL_TEXTURE_2D); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//Enables the use of 2D textures

[/color][/size][/color][/size][size="2"]tex = loadTexture([/size][size="2"][color="#a31515"][size="2"][color="#a31515"]"Bricks.bmp"[/color][/size][/color][/size][size="2"]); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//load up file

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]//Adds ambient light

[/color][/size][/color][/size][size="2"]GLfloat ambientColor[] = {0.5, 0.5, 0.5, 1.0};

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Adds positioned light

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]/*GLfloat lightColor0[] = {0.5, 0.5, 0.5, 1.0};

GLfloat lightPos0[] = {4.0, 0.0, 8.0, 1.0};

glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);

glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);*/

//Adds directed light

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]/*GLfloat lightColor1[] = {0.5, 0.2, 0.2, 1.0};

GLfloat lightPos1[] = {-1.0, 0.5, 0.5, 0.0};

glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);

glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);*/

[/color][/size][/color][/size][size="2"]}

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//***********************************Texture Function for Loading*************************************************************

/*Here I need to load a bitmap image, generate textures, bind texture, set glTextImage2D, glTextParameteri

Some ppl use programs such as SDL to read in bitmap images (just not to reinvent the wheel). Depending on the requirements of the Viscube

I can aslo us SDL. In this case I am trying to establish reading using SDL*/

//Reading the texture from an image file: OpenGL has no function to load an image

//Here I am going to use the SDL as my image uploader //SDL uploader test

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]unsigned[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] loadTexture([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]const[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]char[/color][/size][/color][/size][size="2"]* filename)

{

SDL_Surface* img =SDL_LoadBMP(filename); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//figure out why color isn't coming in

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]unsigned[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] id; [/size][size="2"][color="#008000"][size="2"][color="#008000"]//assigning an id for glGenTextures

[/color][/size][/color][/size][size="2"]glGenTextures(1, &id); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//idea assigned

[/color][/size][/color][/size][size="2"]glBindTexture(GL_TEXTURE_2D, id); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//binding texture to the id

[/color][/size][/color][/size][size="2"]glTexImage2D(GL_TEXTURE_2D,

0,

GL_RGB,

img->w, [/size][size="2"][color="#008000"][size="2"][color="#008000"]//width of image

[/color][/size][/color][/size][size="2"]img->h, [/size][size="2"][color="#008000"][size="2"][color="#008000"]//height of image

[/color][/size][/color][/size][size="2"]0,

GL_RGB, [/size][size="2"][color="#008000"][size="2"][color="#008000"]//format

[/color][/size][/color][/size][size="2"]GL_UNSIGNED_BYTE, [/size][size="2"][color="#008000"][size="2"][color="#008000"]//what format is the image stored

[/color][/size][/color][/size][size="2"]img->pixels); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//pixel array, pixels

[/color][/size][/color][/size][size="2"]

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Setting Parameters

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]//Warping Parameters (how texture will be applied to the object)

[/color][/size][/color][/size][size="2"]glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//Many different parameters can be set her to manipulate the textures

[/color][/size][/color][/size][size="2"]glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Filtering Parameters

[/color][/size][/color][/size][size="2"]glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//2d texture with a minimum filter, and linear interpolation (averaging pixlels)

[/color][/size][/color][/size][size="2"]glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

SDL_FreeSurface(img); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//deletes the texture image out of the texture memory

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]return[/color][/size][/color][/size][size="2"] id;

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] polygon([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] a, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] b, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] c, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] d)

{

[/size][size="2"][color="#008000"][size="2"][color="#008000"]/*draw a polygon via list of vertices */

[/color][/size][/color][/size][size="2"]

glBindTexture(GL_TEXTURE_2D, tex); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//This binds the textures to the polygon

[/color][/size][/color][/size][size="2"]glBegin(GL_POLYGON); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//applying the textures are based on a square that is lower left(0,0), Upper left (0,1), lower right (1,0), upper right (1,1)

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]// glColor3fv(colors[a]); //These texture points are placed witht he corresponding vertices, to make sure that the texture is applied correctly

[/color][/size][/color][/size][size="2"]glTexCoord2f(0.0,0.0);

glVertex3fv(vertices[a]);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//glColor3fv(colors[b]);

[/color][/size][/color][/size][size="2"]glTexCoord2f(0.0,1.0);

glVertex3fv(vertices[b]);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//glColor3fv(colors[c]);

[/color][/size][/color][/size][size="2"]glTexCoord2f(1.0,1.0);

glVertex3fv(vertices[c]);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//glColor3fv(colors[d]);

[/color][/size][/color][/size][size="2"]glTexCoord2f(1.0,0.0);

glVertex3fv(vertices[d]);

glEnd();

[/size][size="2"][color="#008000"][size="2"][color="#008000"]/*Here are the vertices that he used

glBegin(GL_QUADS);

glVertex3f(-2.0,2.0,0.0);

glVertex3f(-2.0,-2.0,0.0);

glVertex3f(2.0,-2.0,0.0);

glVertex3f(2.0,2.0,0.0);

glEnd();*/

[/color][/size][/color][/size][size="2"]}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] colorcube([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"])

{ [/size][size="2"][color="#008000"][size="2"][color="#008000"]//function is used to state which sections of the array for colors and vertext to use

/*map vertices to faces */

[/color][/size][/color][/size][size="2"]polygon(0, 3, 2, 1);

polygon(2, 3, 7, 6);

polygon(3, 0, 4, 7);

polygon(1, 2, 6, 5);

polygon(4, 5, 6, 7);

polygon(5, 4, 0, 1);

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]static[/color][/size][/color][/size][size="2"] GLfloat theta[] = {0.0,0.0,0.0}; [/size][size="2"][color="#008000"][size="2"][color="#008000"]//declaring two static variables theta and axis

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]static[/color][/size][/color][/size][size="2"] GLint axis = 2;

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//display function

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] display([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"])

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glRotatef(theta[0], 1.0,0.0,0.0);

glRotatef(theta[1], 0.0,1.0,0.0);

glRotatef(theta[2], 0.0,0.0,1.0);

colorcube(); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//refers to the function that mapped the vertices to faces

[/color][/size][/color][/size][size="2"]glutSwapBuffers();

}

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//The below section contains the interaction component of the program

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] spinCube()

{

theta[axis] += 0.05;

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"](theta[axis] > 360.0)

theta[axis] -= 360.0;

glutPostRedisplay();

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] mouse([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] btn, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] state, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] x, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] y)

{

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Mouse button interaction

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"](btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)

axis = 0;

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"](btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)

axis = 1;

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"](btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)

axis = 2;

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] myReshape([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] w, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] h)

{

glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"](w <= h)

glOrtho(-2.0, 2.0, -2.0* (GLfloat) h/ (GLfloat) w, 2.0 *(GLfloat) h/ (GLfloat) w, -10.0, 10.0);

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]else

[/color][/size][/color][/size][size="2"]glOrtho(-2.0* (GLfloat) h/ (GLfloat) w, 2.0* (GLfloat) h/ (GLfloat) w, -2.0,2.0,-10.0,10.0);

glMatrixMode(GL_MODELVIEW);

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] main([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] argc, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]char[/color][/size][/color][/size][size="2"] ** argv)

{

GLubyte image[64][64][3]; [/size][size="2"][color="#008000"][size="2"][color="#008000"]//seems like 64 by 64 pixels taking a vertice made up of 3 points

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] i, j, r, c;

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]for[/color][/size][/color][/size][size="2"](i =0; i <64; i++)

{

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]for[/color][/size][/color][/size][size="2"](j=0; j<64; j++)

{

c = ((((i & 0x8)== 0)^((j & 0x8)) == 0))*255; [/size][size="2"][color="#008000"][size="2"][color="#008000"]//I think 0x8 has to always be written like this, not 0 x 8 or it will cause problems.

[/color][/size][/color][/size][size="2"]image[i][j][0] = (GLubyte) c;

image[i][j][1] = (GLubyte) c;

image[i][j][2] = (GLubyte) c;

}

}

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(500,500);

glutCreateWindow([/size][size="2"][color="#a31515"][size="2"][color="#a31515"]"colorcube"[/color][/size][/color][/size][size="2"]);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Place initialization function here

[/color][/size][/color][/size][size="2"]initRendering();

glutReshapeFunc(myReshape);

glutDisplayFunc(display);

glutIdleFunc(spinCube);

glutMouseFunc(mouse);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]

[/color][/size][/color][/size][size="2"]glutMainLoop();

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]return[/color][/size][/color][/size][size="2"] 0;

}

[/size]



[/code]
0

Share this post


Link to post
Share on other sites
The red and blue channels are probably swapped, ie. your image data comes in BGR component order.

Try GL_BGR as the format parameter to the glTexImage2D call (instead of GL_RGB).
2

Share this post


Link to post
Share on other sites
[attachment=5226:Fixed Texture Color Scheme GL_BGR.jpg][quote name='Yann L' timestamp='1314935243' post='4856583']
The red and blue channels are probably swapped, ie. your image data comes in BGR component order.

Try GL_BGR as the format parameter to the glTexImage2D call (instead of GL_RGB).
[/quote]


Thanks that was exactly what it was. Do you mind me asking how that happens? Is it just that specific image?
0

Share this post


Link to post
Share on other sites
BMP files are stored in BGR color order. If you read the image data directly from the file, that is the format you get.
1

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1314971628' post='4856737']
BMP files are stored in BGR color order. If you read the image data directly from the file, that is the format you get.
[/quote]


Yeah thats the crazy thing. I used SDL to read in the texture, so I think that its kind of strange that the color format was reversed. Well now I am on to reading in .obj files to display my textures on models using blender and opengl. Thanks guys. Any tips?
0

Share this post


Link to post
Share on other sites
I don't use SDL but I imagine it is because SDL reads the data and doesn't convert the BGR to RGB automatically for you. You are probably suppose to make some function call into the SDL for it to convert for you.

Or, use SDL to know what the storage format is. If it says BGR then you send the data to OpenGL as BGR.
So, you need to know a little of SDL.
0

Share this post


Link to post
Share on other sites

The following code is from freeImage.cpp, it shows how to load an image and swap the RGB channels, I made a copy of the relevant code snippet and posted it first and added some comments to the swapping code. for(int pix=0; pix

1

Share this post


Link to post
Share on other sites
Not even mentioning that the code above invokes undefined behaviour and could possibly lead to memory leaks or even severe heap corruption.

[quote name='marcClintDion' timestamp='1315001078' post='4856946']
BYTE *bits = new BYTE[FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib) * 3];

[..stuff..]

delete bits;
[/quote]
2

Share this post


Link to post
Share on other sites
If someone asks me how to build a house and I suggest to him or her to go cut down trees, gather wood, and use a hammer and nails, yes, I have answered his question.
When someone else comes along and says, "You know, that way is deprecated; we use bricks and mortar now," he or she has given an answer that may be more suitable to the original person seeking advice.

In the pursuit of knowledge one must never be stuck in his or her own ways, and admit one's shortcomings with a smile as new things or ways are learned.

I would not be offended if someone suggested a better way than my own. Why were you?

One of the reasons I post here so often is because I can also learn when I give less-than-the-best advice and someone comes along and corrects it. I think I am pretty useful and help often, but I also say stupid things at times and make poor suggestions.

Your reaction to having been corrected is off-the-wall.

Also, Yann L showed you exactly where the error is.
You [or whomever] used new [] but not delete []. You [or whomever] used plain delete.
Instead of looking with an open mind and saying, "Oh I see, I made a mistake. Thank you for finding that because the quality of my software has increased because of it," you just screamed how correct the code is based on it working "on every computer", which is something you didn't even test, and stomped off with flawed code.


L. Spiro
2

Share this post


Link to post
Share on other sites

Actually L. Spiro. YOU told us exactly where this particular error is. The other person re-wrote the same mistake and said it was wrong without the correction that you presented. Maybe to you it was obvious that [] should have been added, but not to everyone. I do not get upset because I was corrected, I wasn't corrected, I was instead heckled for attempting to help. If I were building a house as you say and someone came by and critisized me for doing it wrong, then turned around and walked away without any clearly stated "help", I would have no respect for this person's opinion. If those people had put things nicely, and had given some useful advice I would have followed it. If that is how the "experts" around here behave then it is clear that the "experts" need to be told how to behave properly. It rubs me the wrong way when no-it-alls use what they "know" to make themselves feel superior. Simply put, I overreacted, but what I reacted to was most certainly something to react to. And by the way, I've tested that code extensively on many different computers with very different hardware and it works beautifully. I re-compile sometimes half-a-dozen times every few minutes. I have also used this code to load 240 pictures at 1024x1024 all at the same time, if there really were memory leaks and corruption it would have been very obvious, these computers would crash. Maybe the Dev-Cpp compiler fixes errors like these automatically, I will however take you advice on this matter and make the appropriate correction:delete[]. I am aware that Nvidia has posted a document called "fast texture transfers" and in this document it is pointed out that BGR should be used but in most cases even they say that it has little or no performance increase. The response I received was snarky and overblown. I escalated things and I'm sorry.

0

Share this post


Link to post
Share on other sites
We;; for my part i wasn't trying to make little of your work; my motive was to point out to the OP that on a more modern GPU using BGR is preferable to swapping the bytes manually. I can see how it could have came across otherwise, and I could have picked my words better.

I have done quite a bit of benchmarking of texture uploads using different format and type parameters, and GL_BGRA is actually the one to use. ;)
0

Share this post


Link to post
Share on other sites
Well if you want any openGL help, then you're kind of pissing of the wrong people. Secondly a fact is not an attack:
[quote]Not even mentioning that the code above invokes undefined behaviour and could possibly lead to memory leaks or even severe heap corruption.[/quote]

The code posted was bad. For instance why make a new array instead of just swapping the r,g,b values in the original array?

[quote]Send your pointless little snippet to the people who built freeImage and tell them that their software isn't written properly.[/quote]
Just because someone gave away free code doesn't mean it is good. It really was bad code, and was a memory leak.

The problem was already solved anyway and then you posted a ton of code and more work than the standard solution of GL_BGR. You can also google things like gluBuild2DMipMaps and find out why he is saying not to use it.
2

Share this post


Link to post
Share on other sites
From the Wiki
http://www.opengl.org/wiki/Common_Mistakes#GL_TEXTURE_MAG_FILTER

and also
http://www.opengl.org/wiki/Common_Mistakes#Automatic_mipmap_generation
2

Share this post


Link to post
Share on other sites

Ok fine I'm sorry for being grouchy. Thank you all for helping people fix their code. How's this look? //------------------------------------------------------------------------------------------------------------------------------------------------. void loadTexture(char *textureFileName, GLuint &textureMapID) { FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(textureFileName, 0); FIBITMAP *dib = FreeImage_Load(fifmt, textureFileName,0); FIBITMAP *temp = dib; dib = FreeImage_ConvertTo32Bits(temp); FreeImage_Unload(temp); if( dib != NULL ) { glGenTextures( 1, &textureMapID ); glBindTexture( GL_TEXTURE_2D, textureMapID ); BYTE *pixels = (BYTE*)FreeImage_GetBits(dib); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels); free(pixels); FreeImage_Unload(dib); } } //------------------------------------------------------------------------------------------------------------------------------------------------. Maybe now we are closer to having a proper image loader for people to make use of. The SGIS mip map generator appears to be hardware accelerated, almost twice as fast on a low end quadro, and about 1/3 faster on a budget intel graphics machine. Is a performance comparison like this inappropriate? Would it be considered bias or favoritism towards one or the other? I'm happy for both running faster now. There do not appear to be memory leaks. The dual core Intel with 2GB RAM and mobile 4 series graphics running on Win7 handled the loading of 100 1024x1024 images, then 200 1024x1024 images, then 300 1024x1024 images.

1

Share this post


Link to post
Share on other sites
Maybe this is better for people who chance upon this topic.. I've chosen to comment out ---> //glGenerateMipmap(GL_TEXTURE_2D); because the documentation mentioned above gives the impression
that --->>>glGenerateMipmap(GL_TEXTURE_2D); is having some sort of version conflict nonsense. It is supported then not supported or some such nonsense. The --->>GL_GENERATE_MIPMAP_SGIS<<----works without adding extensions, seems to have no deprecation or version problems and offers a substantial performance boost on both nVidia as well as mobile Intel graphics(ATI, not tested yet) . Almost half the loading time for a mobile Quadro(NVS 130), and about 25% faster on budget mobile Intel(x4500).

I'm not sure if I've gotten things up to speed so far as all of the hints and tips provided goes, but things are at least, superficially, looking good. Two hundred and forty 1024 x 1024 textures loaded simultaneously without crashing either machine seems workable to me.

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//FreeImage_Save(FIF_JPEG, dib, "mybitmap.jpg", JPEG_QUALITYSUPERB);

void loadTexture(char *textureFileName, GLuint &textureMapID)
{
FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(textureFileName, 0);

FIBITMAP *dib = FreeImage_Load(fifmt, textureFileName,0);

FIBITMAP *temp = dib;
dib = FreeImage_ConvertTo32Bits(temp);
FreeImage_Unload(temp);

if( dib != NULL )
{
glGenTextures( 1, &textureMapID );
glBindTexture( GL_TEXTURE_2D, textureMapID );

BYTE *pixels = (BYTE*)FreeImage_GetBits(dib);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
//glGenerateMipmap(GL_TEXTURE_2D);

free(pixels);
FreeImage_Unload(dib);

}

}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By Solid_Spy
      Hello, I have been working on SH Irradiance map rendering, and I have been using a GLSL pixel shader to render SH irradiance to 2D irradiance maps for my static objects. I already have it working with 9 3D textures so far for the first 9 SH functions.
      In my GLSL shader, I have to send in 9 SH Coefficient 3D Texures that use RGBA8 as a pixel format. RGB being used for the coefficients for red, green, and blue, and the A for checking if the voxel is in use (for the 3D texture solidification shader to prevent bleeding).
      My problem is, I want to knock this number of textures down to something like 4 or 5. Getting even lower would be a godsend. This is because I eventually plan on adding more SH Coefficient 3D Textures for other parts of the game map (such as inside rooms, as opposed to the outside), to circumvent irradiance probe bleeding between rooms separated by walls. I don't want to reach the 32 texture limit too soon. Also, I figure that it would be a LOT faster.
      Is there a way I could, say, store 2 sets of SH Coefficients for 2 SH functions inside a texture with RGBA16 pixels? If so, how would I extract them from inside GLSL? Let me know if you have any suggestions ^^.
    • By DaniDesu
      #include "MyEngine.h" int main() { MyEngine myEngine; myEngine.run(); return 0; } MyEngine.h
      #pragma once #include "MyWindow.h" #include "MyShaders.h" #include "MyShapes.h" class MyEngine { private: GLFWwindow * myWindowHandle; MyWindow * myWindow; public: MyEngine(); ~MyEngine(); void run(); }; MyEngine.cpp
      #include "MyEngine.h" MyEngine::MyEngine() { MyWindow myWindow(800, 600, "My Game Engine"); this->myWindow = &myWindow; myWindow.createWindow(); this->myWindowHandle = myWindow.getWindowHandle(); // Load all OpenGL function pointers for use gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); } MyEngine::~MyEngine() { this->myWindow->destroyWindow(); } void MyEngine::run() { MyShaders myShaders("VertexShader.glsl", "FragmentShader.glsl"); MyShapes myShapes; GLuint vertexArrayObjectHandle; float coordinates[] = { 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, 0.5f, 0.0f }; vertexArrayObjectHandle = myShapes.drawTriangle(coordinates); while (!glfwWindowShouldClose(this->myWindowHandle)) { glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw something glUseProgram(myShaders.getShaderProgram()); glBindVertexArray(vertexArrayObjectHandle); glDrawArrays(GL_TRIANGLES, 0, 3); glfwSwapBuffers(this->myWindowHandle); glfwPollEvents(); } } MyShaders.h
      #pragma once #include <glad\glad.h> #include <GLFW\glfw3.h> #include "MyFileHandler.h" class MyShaders { private: const char * vertexShaderFileName; const char * fragmentShaderFileName; const char * vertexShaderCode; const char * fragmentShaderCode; GLuint vertexShaderHandle; GLuint fragmentShaderHandle; GLuint shaderProgram; void compileShaders(); public: MyShaders(const char * vertexShaderFileName, const char * fragmentShaderFileName); ~MyShaders(); GLuint getShaderProgram(); const char * getVertexShaderCode(); const char * getFragmentShaderCode(); }; MyShaders.cpp
      #include "MyShaders.h" MyShaders::MyShaders(const char * vertexShaderFileName, const char * fragmentShaderFileName) { this->vertexShaderFileName = vertexShaderFileName; this->fragmentShaderFileName = fragmentShaderFileName; // Load shaders from files MyFileHandler myVertexShaderFileHandler(this->vertexShaderFileName); this->vertexShaderCode = myVertexShaderFileHandler.readFile(); MyFileHandler myFragmentShaderFileHandler(this->fragmentShaderFileName); this->fragmentShaderCode = myFragmentShaderFileHandler.readFile(); // Compile shaders this->compileShaders(); } MyShaders::~MyShaders() { } void MyShaders::compileShaders() { this->vertexShaderHandle = glCreateShader(GL_VERTEX_SHADER); this->fragmentShaderHandle = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(this->vertexShaderHandle, 1, &(this->vertexShaderCode), NULL); glShaderSource(this->fragmentShaderHandle, 1, &(this->fragmentShaderCode), NULL); glCompileShader(this->vertexShaderHandle); glCompileShader(this->fragmentShaderHandle); this->shaderProgram = glCreateProgram(); glAttachShader(this->shaderProgram, this->vertexShaderHandle); glAttachShader(this->shaderProgram, this->fragmentShaderHandle); glLinkProgram(this->shaderProgram); return; } GLuint MyShaders::getShaderProgram() { return this->shaderProgram; } const char * MyShaders::getVertexShaderCode() { return this->vertexShaderCode; } const char * MyShaders::getFragmentShaderCode() { return this->fragmentShaderCode; } MyWindow.h
      #pragma once #include <glad\glad.h> #include <GLFW\glfw3.h> class MyWindow { private: GLFWwindow * windowHandle; int windowWidth; int windowHeight; const char * windowTitle; public: MyWindow(int windowWidth, int windowHeight, const char * windowTitle); ~MyWindow(); GLFWwindow * getWindowHandle(); void createWindow(); void MyWindow::destroyWindow(); }; MyWindow.cpp
      #include "MyWindow.h" MyWindow::MyWindow(int windowWidth, int windowHeight, const char * windowTitle) { this->windowHandle = NULL; this->windowWidth = windowWidth; this->windowWidth = windowWidth; this->windowHeight = windowHeight; this->windowTitle = windowTitle; glfwInit(); } MyWindow::~MyWindow() { } GLFWwindow * MyWindow::getWindowHandle() { return this->windowHandle; } void MyWindow::createWindow() { // Use OpenGL 3.3 and GLSL 3.3 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Limit backwards compatibility glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Prevent resizing window glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Create window this->windowHandle = glfwCreateWindow(this->windowWidth, this->windowHeight, this->windowTitle, NULL, NULL); glfwMakeContextCurrent(this->windowHandle); } void MyWindow::destroyWindow() { glfwTerminate(); } MyShapes.h
      #pragma once #include <glad\glad.h> #include <GLFW\glfw3.h> class MyShapes { public: MyShapes(); ~MyShapes(); GLuint & drawTriangle(float coordinates[]); }; MyShapes.cpp
      #include "MyShapes.h" MyShapes::MyShapes() { } MyShapes::~MyShapes() { } GLuint & MyShapes::drawTriangle(float coordinates[]) { GLuint vertexBufferObject{}; GLuint vertexArrayObject{}; // Create a VAO glGenVertexArrays(1, &vertexArrayObject); glBindVertexArray(vertexArrayObject); // Send vertices to the GPU glGenBuffers(1, &vertexBufferObject); glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); glBufferData(GL_ARRAY_BUFFER, sizeof(coordinates), coordinates, GL_STATIC_DRAW); // Dertermine the interpretation of the array buffer glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *)0); glEnableVertexAttribArray(0); // Unbind the buffers glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); return vertexArrayObject; } MyFileHandler.h
      #pragma once #include <cstdio> #include <cstdlib> class MyFileHandler { private: const char * fileName; unsigned long fileSize; void setFileSize(); public: MyFileHandler(const char * fileName); ~MyFileHandler(); unsigned long getFileSize(); const char * readFile(); }; MyFileHandler.cpp
      #include "MyFileHandler.h" MyFileHandler::MyFileHandler(const char * fileName) { this->fileName = fileName; this->setFileSize(); } MyFileHandler::~MyFileHandler() { } void MyFileHandler::setFileSize() { FILE * fileHandle = NULL; fopen_s(&fileHandle, this->fileName, "rb"); fseek(fileHandle, 0L, SEEK_END); this->fileSize = ftell(fileHandle); rewind(fileHandle); fclose(fileHandle); return; } unsigned long MyFileHandler::getFileSize() { return (this->fileSize); } const char * MyFileHandler::readFile() { char * buffer = (char *)malloc((this->fileSize)+1); FILE * fileHandle = NULL; fopen_s(&fileHandle, this->fileName, "rb"); fread(buffer, this->fileSize, sizeof(char), fileHandle); fclose(fileHandle); buffer[this->fileSize] = '\0'; return buffer; } VertexShader.glsl
      #version 330 core layout (location = 0) vec3 VertexPositions; void main() { gl_Position = vec4(VertexPositions, 1.0f); } FragmentShader.glsl
      #version 330 core out vec4 FragmentColor; void main() { FragmentColor = vec4(1.0f, 0.0f, 0.0f, 1.0f); } I am attempting to create a simple engine/graphics utility using some object-oriented paradigms. My first goal is to get some output from my engine, namely, a simple red triangle.
      For this goal, the MyShapes class will be responsible for defining shapes such as triangles, polygons etc. Currently, there is only a drawTriangle() method implemented, because I first wanted to see whether it works or not before attempting to code other shape drawing methods.
      The constructor of the MyEngine class creates a GLFW window (GLAD is also initialized here to load all OpenGL functionality), and the myEngine.run() method in Main.cpp is responsible for firing up the engine. In this run() method, the shaders get loaded from files via the help of my FileHandler class. The vertices for the triangle are processed by the myShapes.drawTriangle() method where a vertex array object, a vertex buffer object and vertrex attributes are set for this purpose.
      The while loop in the run() method should be outputting me the desired red triangle, but all I get is a grey window area. Why?
      Note: The shaders are compiling and linking without any errors.
      (Note: I am aware that this code is not using any good software engineering practices (e.g. exceptions, error handling). I am planning to implement them later, once I get the hang of OpenGL.)

       
    • By KarimIO
      EDIT: I thought this was restricted to Attribute-Created GL contexts, but it isn't, so I rewrote the post.
      Hey guys, whenever I call SwapBuffers(hDC), I get a crash, and I get a "Too many posts were made to a semaphore." from Windows as I call SwapBuffers. What could be the cause of this?
      Update: No crash occurs if I don't draw, just clear and swap.
      static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format 32, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 24, // 24Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; if (!(hDC = GetDC(windowHandle))) return false; unsigned int PixelFormat; if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) return false; if (!SetPixelFormat(hDC, PixelFormat, &pfd)) return false; hRC = wglCreateContext(hDC); if (!hRC) { std::cout << "wglCreateContext Failed!\n"; return false; } if (wglMakeCurrent(hDC, hRC) == NULL) { std::cout << "Make Context Current Second Failed!\n"; return false; } ... // OGL Buffer Initialization glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glBindVertexArray(vao); glUseProgram(myprogram); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, (void *)indexStart); SwapBuffers(GetDC(window_handle));  
    • By Tchom
      Hey devs!
       
      I've been working on a OpenGL ES 2.0 android engine and I have begun implementing some simple (point) lighting. I had something fairly simple working, so I tried to get fancy and added color-tinting light. And it works great... with only one or two lights. Any more than that, the application drops about 15 frames per light added (my ideal is at least 4 or 5). I know implementing lighting is expensive, I just didn't think it was that expensive. I'm fairly new to the world of OpenGL and GLSL, so there is a good chance I've written some crappy shader code. If anyone had any feedback or tips on how I can optimize this code, please let me know.
       
      Vertex Shader
      uniform mat4 u_MVPMatrix; uniform mat4 u_MVMatrix; attribute vec4 a_Position; attribute vec3 a_Normal; attribute vec2 a_TexCoordinate; varying vec3 v_Position; varying vec3 v_Normal; varying vec2 v_TexCoordinate; void main() { v_Position = vec3(u_MVMatrix * a_Position); v_TexCoordinate = a_TexCoordinate; v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0)); gl_Position = u_MVPMatrix * a_Position; } Fragment Shader
      precision mediump float; uniform vec4 u_LightPos["+numLights+"]; uniform vec4 u_LightColours["+numLights+"]; uniform float u_LightPower["+numLights+"]; uniform sampler2D u_Texture; varying vec3 v_Position; varying vec3 v_Normal; varying vec2 v_TexCoordinate; void main() { gl_FragColor = (texture2D(u_Texture, v_TexCoordinate)); float diffuse = 0.0; vec4 colourSum = vec4(1.0); for (int i = 0; i < "+numLights+"; i++) { vec3 toPointLight = vec3(u_LightPos[i]); float distance = length(toPointLight - v_Position); vec3 lightVector = normalize(toPointLight - v_Position); float diffuseDiff = 0.0; // The diffuse difference contributed from current light diffuseDiff = max(dot(v_Normal, lightVector), 0.0); diffuseDiff = diffuseDiff * (1.0 / (1.0 + ((1.0-u_LightPower[i])* distance * distance))); //Determine attenuatio diffuse += diffuseDiff; gl_FragColor.rgb *= vec3(1.0) / ((vec3(1.0) + ((vec3(1.0) - vec3(u_LightColours[i]))*diffuseDiff))); //The expensive part } diffuse += 0.1; //Add ambient light gl_FragColor.rgb *= diffuse; } Am I making any rookie mistakes? Or am I just being unrealistic about what I can do? Thanks in advance
    • By yahiko00
      Hi,
      Not sure to post at the right place, if not, please forgive me...
      For a game project I am working on, I would like to implement a 2D starfield as a background.
      I do not want to deal with static tiles, since I plan to slowly animate the starfield. So, I am trying to figure out how to generate a random starfield for the entire map.
      I feel that using a uniform distribution for the stars will not do the trick. Instead I would like something similar to the screenshot below, taken from the game Star Wars: Empire At War (all credits to Lucasfilm, Disney, and so on...).

      Is there someone who could have an idea of a distribution which could result in such a starfield?
      Any insight would be appreciated
  • Popular Now