Home / Expert Answers / Computer Science / i-am-currently-using-opengl-with-blender-to-produce-a-3d-animationright-now-i-have-two-3d-objects-a-pa784

(Solved): I am currently using OpenGL with blender to produce a 3d animationRight now I have two 3d objects a ...



I am currently using OpenGL with blender to produce a 3d animation

Right now I have two 3d objects a cat and a room

student submitted image, transcription available below

student submitted image, transcription available below

How do I show the two 3d objects together in a scene without breaking the code?
Below is the following code I used:

#include <GL/glew.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <gl/GL.h>
#include "main.h"
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
int POS_X, POS_Y;
#define WIDTH 600
#define HEIGHT 600
std::string model_name = "Models/gato.obj";
GLfloat light_pos[] = { -10.0f, 10.0f, 100.00f, 1.0f };
float pos_x, pos_y, pos_z;
float angle_x = 30.0f, angle_y = 0.0f;
int x_old = 0, y_old = 0;
int current_scroll = 5;
float zoom_per_scroll;
bool is_holding_mouse = false;
bool is_updated = false;
Model model;
void init() {
   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);
   glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
   glClearColor(0.4f, 0.4f, 0.4f, 1.0f);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(20.0, 1.0, 1.0, 2000.0);
   glMatrixMode(GL_MODELVIEW);
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glEnable(GL_LINE_SMOOTH);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glEnable(GL_TEXTURE_2D);
   glEnable(GL_DEPTH_TEST);
   model.load(model_name.c_str());
   pos_x = model.pos_x;
   pos_y = model.pos_y;
   pos_z = model.pos_z - 1.0f;
   zoom_per_scroll = -model.pos_z / 10.0f;
}
float someVariable = 0;
void display() {
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glLoadIdentity();
   glTranslatef(pos_x, pos_y, pos_z);
   glRotatef(angle_x, 1.0f, 0.0f, 0.0f);
   glRotatef(angle_y, 0.0f, 1.0f, 0.0f);
   model.draw();
   glutSwapBuffers();
}
void timer(int value) {
   someVariable++;
   glutPostRedisplay();
   glutTimerFunc(15, timer, 0);
}
void mouse(int button, int state, int x, int y) {
   is_updated = true;
   if (button == GLUT_LEFT_BUTTON) {
       if (state == GLUT_DOWN) {
           x_old = x;
           y_old = y;
           is_holding_mouse = true;
       }
       else
           is_holding_mouse = false;
   }
   else if (state == GLUT_UP) {
       switch (button) {
       case 3:
           if (current_scroll > 0) {
               current_scroll--;
               pos_z += zoom_per_scroll;
           }
           break;
       case 4:
           if (current_scroll < 15) {
               current_scroll++;
               pos_z -= zoom_per_scroll;
           }
           break;
       }
   }
}
void motion(int x, int y) {
   if (is_holding_mouse) {
       is_updated = true;
       angle_y += (x - x_old);
       x_old = x;
       if (angle_y > 360.0f)
           angle_y -= 360.0f;
       else if (angle_y < 0.0f)
           angle_y += 360.0f;
       angle_x += (y - y_old);
       y_old = y;
       if (angle_x > 90.0f)
           angle_x = 90.0f;
       else if (angle_x < -90.0f)
           angle_x = -90.0f;
   }
}
int main(int argc, char** argv) {
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
   glEnable(GL_MULTISAMPLE);
   POS_X = (glutGet(GLUT_SCREEN_WIDTH) - WIDTH) >> 1;
   POS_Y = (glutGet(GLUT_SCREEN_HEIGHT) - HEIGHT) >> 1;
   glutInitWindowPosition(POS_X, POS_Y);
   glutInitWindowSize(WIDTH, HEIGHT);
   glutCreateWindow("Load Model");
   init();
   glutDisplayFunc(display);
   glutMouseFunc(mouse);
   glutMotionFunc(motion);
   glutTimerFunc(0, timer, 0);
   glutMainLoop();
   return 0;
}
Load Model Load Model


We have an Answer from Expert

View Expert Answer

Expert Answer



Your current code is loading and displaying a single 3D model (a cat) in an OpenGL window. If you wa...
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe