LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-18-2011, 08:14 PM   #1
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Rep: Reputation: 62
Red face 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.
 
Old 03-18-2011, 08:46 PM   #2
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
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.
Old 03-18-2011, 08:57 PM   #3
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Original Poster
Rep: Reputation: 62
Quote:
Originally Posted by SigTerm View Post
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.
 
Old 03-18-2011, 09:07 PM   #4
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by arashi256 View Post
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 View Post
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.
Old 03-18-2011, 09:13 PM   #5
arashi256
Member
 
Registered: Jan 2008
Location: Brighton, UK
Distribution: Ubuntu 18.04 / CentOS 7.6
Posts: 397

Original Poster
Rep: Reputation: 62
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C program that displays shapes. Keep getting errors. boilers969 Programming 101 10-12-2010 12:56 AM
C program that displays shapes. Keep getting errors. boilers969 Programming 12 10-10-2010 10:36 PM
[SOLVED] What kind of code has shapes in it? theKbStockpiler Programming 2 09-11-2010 01:33 AM
How to chown -R don.lab * for don.users Hans Zilles Linux - Newbie 3 11-15-2005 07:53 AM
Suse dontīt boot X; donīt ask for root passwd, YAST donīt work... carrie Linux - Newbie 4 10-08-2004 07:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration