Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-18-2011, 08:14 PM
|
#1
|
Member
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397
Rep:
|
OpenGL help? My shapes don't know their depth...
HI,
I'm using a mixture of OpenGL and SDL with C++. I've never had much luck with 3D programming despite repeated attempts. Anyway, I've created a couple of C++ objects to hold instances of a Pyramid and (what will be) a Cube. The Cube is just one triangle at the moment, but I noticed that having an instance of my Pyramid and an instance of my Cube, the Cube being positioned 5 units behind the Pyramid - that the objects don't seem to know their depth and are simply rendered in the order that they are drawn in the code.
I *think* I've set up OpenGL and SDL correctly, but could a knowledgeable OpenGL type take a look at my code?
Code:
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <sstream>
#include <math.h>
#include <iostream>
#include "CPyramid.h"
#include "CCube.h"
using namespace std;
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
SDL_Surface *screen = NULL;
CPyramid::CPyramid *myPyramid = NULL;
CCube::CCube *myCube = NULL;
bool isRunning = false;
char *error;
bool init();
char *init_SDL(SDL_Surface **screen, int screenWidth, int screenHeight, int screenBPP);
void init_GL(float screenWidth, float screenHeight);
void process();
void handleKeys(SDL_keysym* keysym, bool state);
void input();
void update();
void render();
float angle = 0.0f;
void shutdown();
int main(int argc, char * argv[]) {
if (init()) {
process();
} else {
return 1;
}
return 0;
}
bool init() {
isRunning = false;
if((error = init_SDL(&screen, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP))) {
printf("OpenGL->ERROR: Could not initialise: %s", error);
} else {
init_GL((float)SCREEN_WIDTH, (float)SCREEN_HEIGHT);
glewInit();
if (!GLEW_ARB_vertex_buffer_object) {
std::cerr << "OpenGL->VBO not supported\n";
isRunning = false;
} else {
std::cout << "OpenGL->VBO supported\n";
myPyramid = new CPyramid(1.0f, 2.0f, 1.0f);
myPyramid->position(0.0f, 0.0f, -5.0f);
myCube = new CCube(2.0f, 2.0f, 2.0f);
myCube->position(0.0f, 0.0f, -10.0f);
isRunning = true;
}
}
return isRunning;
}
char *init_SDL(SDL_Surface **screen, int screenWidth, int screenHeight, int screenBPP) {
if (SDL_Init(SDL_INIT_EVERYTHING)) {
return SDL_GetError();
} else {
*screen = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, SDL_OPENGL | SDL_FULLSCREEN | SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF | SDL_GL_SWAP_CONTROL);
SDL_ShowCursor(SDL_DISABLE);
#if SDL_VERSION_ATLEAST(1,3,0)
SDL_GL_SetSwapInterval(1);
#else /* SDL_VERSION_ATLEAST(1,3,0) */
#ifdef SDL_GL_SWAP_CONTROL
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
#else /* SDL_GL_SWAP_CONTROL */
printf("OpenGL->VSync unsupported on old SDL versions (before 1.2.10).\n");
#endif /* SDL_GL_SWAP_CONTROL */
#endif /* SDL_VERSION_ATLEAST(1,3,0) */
SDL_WM_SetCaption("JonGL", "JonGL");
}
return 0;
}
void init_GL(float screenWidth, float screenHeight) {
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glDepthFunc(GL_LESS);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_BLEND);
glViewport(0, 0, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0f,(double) screenWidth / screenHeight,1.0f, 30.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void process() {
while (isRunning) {
input();
update();
render();
}
shutdown();
}
void handleKeys(SDL_keysym* keysym, bool state) {
switch(keysym->sym) {
case SDLK_ESCAPE:
isRunning = false;
break;
default:
break;
}
}
void input() {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
handleKeys(&event.key.keysym, true);
break;
case SDL_KEYUP:
handleKeys(&event.key.keysym, false);
break;
case SDL_QUIT:
isRunning = false;
break;
default:
break;
}
}
}
void update() {
myCube->rotateY(-2.4f);
myPyramid->rotateY(1.0f);
}
void render() {
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glDisable(GL_TEXTURE_2D);
myCube->draw();
myPyramid->draw();
glFinish();
SDL_GL_SwapBuffers();
}
void shutdown() {
delete myPyramid;
delete myCube;
SDL_ShowCursor(SDL_ENABLE);
SDL_Quit();
}
I've not put the code for the shapes themselves in here as I don't think it's relevant, but shout if you think I'm mistaken I'm using Vertex Buffer Objects for the shapes. Many thanks for any help - this is sort of getting me down.
|
|
|
03-18-2011, 08:46 PM
|
#2
|
Member
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379
|
Normally, for help with graphical OpenGL glitches, you need to post a picture.
SDL initialization:
Code:
int main(int argc, char** argv){
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
/*
*/
if (SDL_SetVideoMode(scrWidth, scrHeight, 32, SDL_OPENGL) == 0){
fprintf(stderr, "couldn't set mode %dx%d!\n", 640, 480);
SDL_Quit();
return -1;
}
/*
*/
SDL_ShowCursor(SDL_DISABLE);
glClearColor(0.2f, 0.2f, 0.2f, 0.2f);
glClearDepth(1.0);
/*glEnable(GL_NORMALIZE);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);*/
glEnable(GL_DEPTH_TEST);
However, the problem is most likely here
Code:
glDepthMask(GL_TRUE);
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_BLEND);
Do you REALLY need transparent shapes with depth buffer enabled? I can't think on any practical application of such setup. Transparency doesn't work well with depth buffering, and in your example shape color adds to whatever is already in color buffer. glBlendFunc(GL_ONE, GL_ONE) is normally used for flame particle systems and multi-pass lighting with depth buffer write disabled (glDepthMask(GL_FALSE)). In combination with z-buffer enabled you'll get all kind of "interesting" effects. I.e. if pyramid is nearer than the cube, but the cube is drawn first, then you will "see" cube through pyramid despite depth buffer working properly. Also if pyramid's color is 0xFF0000(red), and cube's color is 0x0000ff(blue), then intersection would have 0xff00ff (magenta) color.
Anyway, for opengl training start with "red book". Version 1.1 is available from opengl.org.
|
|
1 members found this post helpful.
|
03-18-2011, 08:57 PM
|
#3
|
Member
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397
Original Poster
Rep:
|
Quote:
Originally Posted by SigTerm
Normally, for help with graphical OpenGL glitches, you need to post a picture.
|
Alrighty...
http://joshua14.homelinux.org/images/Screenshot-1.png
You be right about the transparent shapes but the colours don't seem to be mixing. But I have noticed that the pyramid has a strange triangle on one side that seems to suggest a problem like you say...
http://joshua14.homelinux.org/images/Screenshot-2.png
Last edited by arashi256; 03-18-2011 at 09:00 PM.
|
|
|
03-18-2011, 09:07 PM
|
#4
|
Member
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379
|
Quote:
Originally Posted by arashi256
|
Most likely it is as I said - result of blending enabled. Disable either blending (if you wanted depth) or depth buffer write (if you wanted shapes to blend regardless of order/depth).
Quote:
Originally Posted by arashi256
You be right about the transparent shapes but the colours don't seem to be mixing.
|
They ARE mixing, but white + any_other_color = white. (RGB 0xFFFFFF + RGB 0xFF0000 = RGB 0xFFFFFF). Make your triangle gray (glColor3f(0.5f, 0.5f, 0.5f)) or use any other non-white color, and see what happens.
Last edited by SigTerm; 03-18-2011 at 09:10 PM.
|
|
1 members found this post helpful.
|
03-18-2011, 09:13 PM
|
#5
|
Member
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397
Original Poster
Rep:
|
Yup - that totally fixed it. Thank you so much!  I think I need to read a book...thanks for the link.
Last edited by arashi256; 03-18-2011 at 09:14 PM.
|
|
|
All times are GMT -5. The time now is 10:52 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|